blob: a910eb562a5ca815c219740419d30e5232a03c76 [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();
Brent Hronik96630422013-05-01 16:38:43 -0600915 if (unlikely(in_global_reset == 1)) {
916 kfree(hdr);
Jeff Hugo4838f412012-01-20 11:19:37 -0700917 return -EFAULT;
Brent Hronik96630422013-05-01 16:38:43 -0600918 }
Jeff Hugo061ce672011-10-21 17:15:32 -0600919 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600920 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600921 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600922
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700923 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
924 hdr->cmd = BAM_MUX_HDR_CMD_OPEN;
925 hdr->reserved = 0;
926 hdr->ch_id = id;
927 hdr->pkt_len = 0;
928 hdr->pad_len = 0;
929
930 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600931 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700932
933open_done:
934 DBG("%s: opened ch %d\n", __func__, id);
935 return rc;
936}
937
938int msm_bam_dmux_close(uint32_t id)
939{
940 struct bam_mux_hdr *hdr;
941 unsigned long flags;
942 int rc;
943
944 if (id >= BAM_DMUX_NUM_CHANNELS)
945 return -EINVAL;
946 DBG("%s: closing ch %d\n", __func__, id);
947 if (!bam_mux_initialized)
948 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700949
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600950 read_lock(&ul_wakeup_lock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600951 if (!bam_is_connected && !bam_ch_is_in_reset(id)) {
Jeff Hugo061ce672011-10-21 17:15:32 -0600952 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600953 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -0700954 if (unlikely(in_global_reset == 1))
955 return -EFAULT;
Jeff Hugo061ce672011-10-21 17:15:32 -0600956 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600957 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600958 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600959
Jeff Hugo061ce672011-10-21 17:15:32 -0600960 spin_lock_irqsave(&bam_ch[id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600961 bam_ch[id].notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700962 bam_ch[id].priv = NULL;
963 bam_ch[id].status &= ~BAM_CH_LOCAL_OPEN;
964 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
965
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600966 if (bam_ch_is_in_reset(id)) {
967 read_unlock(&ul_wakeup_lock);
968 bam_ch[id].status &= ~BAM_CH_IN_RESET;
969 return 0;
970 }
971
Jeff Hugobb5802f2011-11-02 17:10:29 -0600972 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700973 if (hdr == NULL) {
974 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600975 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700976 return -ENOMEM;
977 }
978 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
979 hdr->cmd = BAM_MUX_HDR_CMD_CLOSE;
980 hdr->reserved = 0;
981 hdr->ch_id = id;
982 hdr->pkt_len = 0;
983 hdr->pad_len = 0;
984
985 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600986 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700987
988 DBG("%s: closed ch %d\n", __func__, id);
989 return rc;
990}
991
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700992int msm_bam_dmux_is_ch_full(uint32_t id)
993{
994 unsigned long flags;
995 int ret;
996
997 if (id >= BAM_DMUX_NUM_CHANNELS)
998 return -EINVAL;
999
1000 spin_lock_irqsave(&bam_ch[id].lock, flags);
1001 bam_ch[id].use_wm = 1;
1002 ret = bam_ch[id].num_tx_pkts >= HIGH_WATERMARK;
1003 DBG("%s: ch %d num tx pkts=%d, HWM=%d\n", __func__,
1004 id, bam_ch[id].num_tx_pkts, ret);
1005 if (!bam_ch_is_local_open(id)) {
1006 ret = -ENODEV;
1007 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
1008 }
1009 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
1010
1011 return ret;
1012}
1013
1014int msm_bam_dmux_is_ch_low(uint32_t id)
1015{
Eric Holmberged3ca0a2012-04-09 15:44:58 -06001016 unsigned long flags;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001017 int ret;
1018
1019 if (id >= BAM_DMUX_NUM_CHANNELS)
1020 return -EINVAL;
1021
Eric Holmberged3ca0a2012-04-09 15:44:58 -06001022 spin_lock_irqsave(&bam_ch[id].lock, flags);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001023 bam_ch[id].use_wm = 1;
1024 ret = bam_ch[id].num_tx_pkts <= LOW_WATERMARK;
1025 DBG("%s: ch %d num tx pkts=%d, LWM=%d\n", __func__,
1026 id, bam_ch[id].num_tx_pkts, ret);
1027 if (!bam_ch_is_local_open(id)) {
1028 ret = -ENODEV;
1029 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
1030 }
Eric Holmberged3ca0a2012-04-09 15:44:58 -06001031 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001032
1033 return ret;
1034}
1035
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001036static void rx_switch_to_interrupt_mode(void)
1037{
1038 struct sps_connect cur_rx_conn;
1039 struct sps_iovec iov;
1040 struct rx_pkt_info *info;
1041 int ret;
1042
1043 /*
1044 * Attempt to enable interrupts - if this fails,
1045 * continue polling and we will retry later.
1046 */
1047 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
1048 if (ret) {
1049 pr_err("%s: sps_get_config() failed %d\n", __func__, ret);
1050 goto fail;
1051 }
1052
1053 rx_register_event.options = SPS_O_EOT;
1054 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
1055 if (ret) {
1056 pr_err("%s: sps_register_event() failed %d\n", __func__, ret);
1057 goto fail;
1058 }
1059
1060 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
1061 SPS_O_EOT | SPS_O_ACK_TRANSFERS;
1062 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
1063 if (ret) {
1064 pr_err("%s: sps_set_config() failed %d\n", __func__, ret);
1065 goto fail;
1066 }
1067 polling_mode = 0;
Eric Holmberg006057d2012-01-11 10:10:42 -07001068 release_wakelock();
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001069
1070 /* handle any rx packets before interrupt was enabled */
1071 while (bam_connection_is_active && !polling_mode) {
1072 ret = sps_get_iovec(bam_rx_pipe, &iov);
1073 if (ret) {
1074 pr_err("%s: sps_get_iovec failed %d\n",
1075 __func__, ret);
1076 break;
1077 }
1078 if (iov.addr == 0)
1079 break;
1080
1081 mutex_lock(&bam_rx_pool_mutexlock);
1082 if (unlikely(list_empty(&bam_rx_pool))) {
Eric Holmberg00cf8692012-07-16 14:21:19 -06001083 DMUX_LOG_KERR("%s: have iovec %p but rx pool empty\n",
1084 __func__, (void *)iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001085 mutex_unlock(&bam_rx_pool_mutexlock);
1086 continue;
1087 }
1088 info = list_first_entry(&bam_rx_pool, struct rx_pkt_info,
1089 list_node);
Eric Holmberg00cf8692012-07-16 14:21:19 -06001090 if (info->dma_address != iov.addr) {
1091 DMUX_LOG_KERR("%s: iovec %p != dma %p\n",
1092 __func__,
1093 (void *)iov.addr,
1094 (void *)info->dma_address);
1095 list_for_each_entry(info, &bam_rx_pool, list_node) {
1096 DMUX_LOG_KERR("%s: dma %p\n", __func__,
1097 (void *)info->dma_address);
1098 if (iov.addr == info->dma_address)
1099 break;
1100 }
1101 }
1102 BUG_ON(info->dma_address != iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001103 list_del(&info->list_node);
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001104 --bam_rx_pool_len;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001105 mutex_unlock(&bam_rx_pool_mutexlock);
1106 handle_bam_mux_cmd(&info->work);
1107 }
1108 return;
1109
1110fail:
1111 pr_err("%s: reverting to polling\n", __func__);
Jeff Hugofff43af92012-03-29 17:54:52 -06001112 queue_work_on(0, bam_mux_rx_workqueue, &rx_timer_work);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001113}
1114
Jeff Hugo949080a2011-08-30 11:58:56 -06001115static void rx_timer_work_func(struct work_struct *work)
1116{
1117 struct sps_iovec iov;
Jeff Hugo949080a2011-08-30 11:58:56 -06001118 struct rx_pkt_info *info;
1119 int inactive_cycles = 0;
1120 int ret;
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07001121 u32 buffs_unused, buffs_used;
Jeff Hugo949080a2011-08-30 11:58:56 -06001122
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001123 while (bam_connection_is_active) { /* timer loop */
Jeff Hugo949080a2011-08-30 11:58:56 -06001124 ++inactive_cycles;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001125 while (bam_connection_is_active) { /* deplete queue loop */
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001126 if (in_global_reset)
1127 return;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001128
1129 ret = sps_get_iovec(bam_rx_pipe, &iov);
1130 if (ret) {
1131 pr_err("%s: sps_get_iovec failed %d\n",
1132 __func__, ret);
1133 break;
1134 }
Jeff Hugo949080a2011-08-30 11:58:56 -06001135 if (iov.addr == 0)
1136 break;
1137 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -06001138 mutex_lock(&bam_rx_pool_mutexlock);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001139 if (unlikely(list_empty(&bam_rx_pool))) {
Eric Holmberg00cf8692012-07-16 14:21:19 -06001140 DMUX_LOG_KERR(
1141 "%s: have iovec %p but rx pool empty\n",
1142 __func__, (void *)iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001143 mutex_unlock(&bam_rx_pool_mutexlock);
1144 continue;
1145 }
1146 info = list_first_entry(&bam_rx_pool,
1147 struct rx_pkt_info, list_node);
Eric Holmberg00cf8692012-07-16 14:21:19 -06001148 if (info->dma_address != iov.addr) {
1149 DMUX_LOG_KERR("%s: iovec %p != dma %p\n",
1150 __func__,
1151 (void *)iov.addr,
1152 (void *)info->dma_address);
1153 list_for_each_entry(info, &bam_rx_pool,
1154 list_node) {
1155 DMUX_LOG_KERR("%s: dma %p\n", __func__,
1156 (void *)info->dma_address);
1157 if (iov.addr == info->dma_address)
1158 break;
1159 }
1160 }
1161 BUG_ON(info->dma_address != iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001162 list_del(&info->list_node);
Eric Holmberg00cf8692012-07-16 14:21:19 -06001163 --bam_rx_pool_len;
Jeff Hugoc9749932011-11-02 17:50:40 -06001164 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -06001165 handle_bam_mux_cmd(&info->work);
1166 }
1167
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07001168 if (inactive_cycles >= POLLING_INACTIVITY) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001169 rx_switch_to_interrupt_mode();
1170 break;
Jeff Hugo949080a2011-08-30 11:58:56 -06001171 }
1172
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07001173 if (bam_adaptive_timer_enabled) {
1174 usleep_range(rx_timer_interval, rx_timer_interval + 50);
1175
1176 ret = sps_get_unused_desc_num(bam_rx_pipe,
1177 &buffs_unused);
1178
1179 if (ret) {
1180 pr_err("%s: error getting num buffers unused after sleep\n",
1181 __func__);
1182
1183 break;
1184 }
1185
1186 buffs_used = NUM_BUFFERS - buffs_unused;
1187
1188 if (buffs_unused == 0) {
1189 rx_timer_interval = MIN_POLLING_SLEEP;
1190 } else {
1191 if (buffs_used > 0) {
1192 rx_timer_interval =
1193 (2 * NUM_BUFFERS *
1194 rx_timer_interval)/
1195 (3 * buffs_used);
1196 } else {
1197 rx_timer_interval =
1198 MAX_POLLING_SLEEP;
1199 }
1200 }
1201
1202 if (rx_timer_interval > MAX_POLLING_SLEEP)
1203 rx_timer_interval = MAX_POLLING_SLEEP;
1204 else if (rx_timer_interval < MIN_POLLING_SLEEP)
1205 rx_timer_interval = MIN_POLLING_SLEEP;
1206 } else {
1207 usleep_range(POLLING_MIN_SLEEP, POLLING_MAX_SLEEP);
1208 }
Jeff Hugo949080a2011-08-30 11:58:56 -06001209 }
1210}
1211
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001212static void bam_mux_tx_notify(struct sps_event_notify *notify)
1213{
1214 struct tx_pkt_info *pkt;
1215
1216 DBG("%s: event %d notified\n", __func__, notify->event_id);
1217
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001218 if (in_global_reset)
1219 return;
1220
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001221 switch (notify->event_id) {
1222 case SPS_EVENT_EOT:
1223 pkt = notify->data.transfer.user;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001224 if (!pkt->is_cmd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001225 dma_unmap_single(NULL, pkt->dma_address,
1226 pkt->skb->len,
1227 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001228 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001229 dma_unmap_single(NULL, pkt->dma_address,
1230 pkt->len,
1231 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001232 queue_work(bam_mux_tx_workqueue, &pkt->work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001233 break;
1234 default:
1235 pr_err("%s: recieved unexpected event id %d\n", __func__,
1236 notify->event_id);
1237 }
1238}
1239
Jeff Hugo33dbc002011-08-25 15:52:53 -06001240static void bam_mux_rx_notify(struct sps_event_notify *notify)
1241{
Jeff Hugo949080a2011-08-30 11:58:56 -06001242 int ret;
1243 struct sps_connect cur_rx_conn;
Jeff Hugo33dbc002011-08-25 15:52:53 -06001244
1245 DBG("%s: event %d notified\n", __func__, notify->event_id);
1246
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001247 if (in_global_reset)
1248 return;
1249
Jeff Hugo33dbc002011-08-25 15:52:53 -06001250 switch (notify->event_id) {
1251 case SPS_EVENT_EOT:
Jeff Hugo949080a2011-08-30 11:58:56 -06001252 /* attempt to disable interrupts in this pipe */
1253 if (!polling_mode) {
1254 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
1255 if (ret) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001256 pr_err("%s: sps_get_config() failed %d, interrupts"
1257 " not disabled\n", __func__, ret);
Jeff Hugo949080a2011-08-30 11:58:56 -06001258 break;
1259 }
Jeff Hugoa9d32ba2011-11-21 14:59:48 -07001260 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
Jeff Hugo949080a2011-08-30 11:58:56 -06001261 SPS_O_ACK_TRANSFERS | SPS_O_POLL;
1262 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
1263 if (ret) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001264 pr_err("%s: sps_set_config() failed %d, interrupts"
1265 " not disabled\n", __func__, ret);
Jeff Hugo949080a2011-08-30 11:58:56 -06001266 break;
1267 }
Eric Holmberg006057d2012-01-11 10:10:42 -07001268 grab_wakelock();
Jeff Hugo949080a2011-08-30 11:58:56 -06001269 polling_mode = 1;
Jeff Hugofff43af92012-03-29 17:54:52 -06001270 /*
1271 * run on core 0 so that netif_rx() in rmnet uses only
1272 * one queue
1273 */
1274 queue_work_on(0, bam_mux_rx_workqueue, &rx_timer_work);
Jeff Hugo949080a2011-08-30 11:58:56 -06001275 }
Jeff Hugo33dbc002011-08-25 15:52:53 -06001276 break;
1277 default:
1278 pr_err("%s: recieved unexpected event id %d\n", __func__,
1279 notify->event_id);
1280 }
1281}
1282
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001283#ifdef CONFIG_DEBUG_FS
1284
1285static int debug_tbl(char *buf, int max)
1286{
1287 int i = 0;
1288 int j;
1289
1290 for (j = 0; j < BAM_DMUX_NUM_CHANNELS; ++j) {
1291 i += scnprintf(buf + i, max - i,
1292 "ch%02d local open=%s remote open=%s\n",
1293 j, bam_ch_is_local_open(j) ? "Y" : "N",
1294 bam_ch_is_remote_open(j) ? "Y" : "N");
1295 }
1296
1297 return i;
1298}
1299
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001300static int debug_ul_pkt_cnt(char *buf, int max)
1301{
1302 struct list_head *p;
1303 unsigned long flags;
1304 int n = 0;
1305
1306 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
1307 __list_for_each(p, &bam_tx_pool) {
1308 ++n;
1309 }
1310 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
1311
1312 return scnprintf(buf, max, "Number of UL packets in flight: %d\n", n);
1313}
1314
1315static int debug_stats(char *buf, int max)
1316{
1317 int i = 0;
1318
1319 i += scnprintf(buf + i, max - i,
Eric Holmberg9fdef262012-02-14 11:46:05 -07001320 "skb read cnt: %u\n"
1321 "skb write cnt: %u\n"
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001322 "skb copy cnt: %u\n"
1323 "skb copy bytes: %u\n"
Eric Holmberg6074aba2012-01-18 17:59:44 -07001324 "sps tx failures: %u\n"
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001325 "sps tx stalls: %u\n"
Eric Holmberg1f1255d2012-02-22 13:37:21 -07001326 "rx queue len: %d\n"
1327 "a2 ack out cnt: %d\n"
1328 "a2 ack in cnt: %d\n"
1329 "a2 pwr cntl in: %d\n",
Eric Holmberg9fdef262012-02-14 11:46:05 -07001330 bam_dmux_read_cnt,
1331 bam_dmux_write_cnt,
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001332 bam_dmux_write_cpy_cnt,
1333 bam_dmux_write_cpy_bytes,
Eric Holmberg6074aba2012-01-18 17:59:44 -07001334 bam_dmux_tx_sps_failure_cnt,
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001335 bam_dmux_tx_stall_cnt,
Eric Holmberg1f1255d2012-02-22 13:37:21 -07001336 bam_rx_pool_len,
1337 atomic_read(&bam_dmux_ack_out_cnt),
1338 atomic_read(&bam_dmux_ack_in_cnt),
1339 atomic_read(&bam_dmux_a2_pwr_cntl_in_cnt)
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001340 );
1341
1342 return i;
1343}
1344
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001345#define DEBUG_BUFMAX 4096
1346static char debug_buffer[DEBUG_BUFMAX];
1347
1348static ssize_t debug_read(struct file *file, char __user *buf,
1349 size_t count, loff_t *ppos)
1350{
1351 int (*fill)(char *buf, int max) = file->private_data;
1352 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
1353 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
1354}
1355
1356static int debug_open(struct inode *inode, struct file *file)
1357{
1358 file->private_data = inode->i_private;
1359 return 0;
1360}
1361
1362
1363static const struct file_operations debug_ops = {
1364 .read = debug_read,
1365 .open = debug_open,
1366};
1367
1368static void debug_create(const char *name, mode_t mode,
1369 struct dentry *dent,
1370 int (*fill)(char *buf, int max))
1371{
Eric Holmberge4ac80b2012-01-12 09:21:59 -07001372 struct dentry *file;
1373
1374 file = debugfs_create_file(name, mode, dent, fill, &debug_ops);
1375 if (IS_ERR(file))
1376 pr_err("%s: debugfs create failed %d\n", __func__,
1377 (int)PTR_ERR(file));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001378}
1379
1380#endif
1381
Jeff Hugod98b1082011-10-24 10:30:23 -06001382static void notify_all(int event, unsigned long data)
1383{
1384 int i;
Jeff Hugocb798022012-04-09 14:55:40 -06001385 struct list_head *temp;
1386 struct outside_notify_func *func;
Jeff Hugod98b1082011-10-24 10:30:23 -06001387
1388 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
Eric Holmberg454d9da2012-01-12 09:37:14 -07001389 if (bam_ch_is_open(i)) {
Jeff Hugod98b1082011-10-24 10:30:23 -06001390 bam_ch[i].notify(bam_ch[i].priv, event, data);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301391 BAM_DMUX_LOG("%s: cid=%d, event=%d, data=%lu\n",
Eric Holmberg454d9da2012-01-12 09:37:14 -07001392 __func__, i, event, data);
1393 }
Jeff Hugod98b1082011-10-24 10:30:23 -06001394 }
Jeff Hugocb798022012-04-09 14:55:40 -06001395
1396 __list_for_each(temp, &bam_other_notify_funcs) {
1397 func = container_of(temp, struct outside_notify_func,
1398 list_node);
1399 func->notify(func->priv, event, data);
1400 }
Jeff Hugod98b1082011-10-24 10:30:23 -06001401}
1402
1403static void kickoff_ul_wakeup_func(struct work_struct *work)
1404{
1405 read_lock(&ul_wakeup_lock);
1406 if (!bam_is_connected) {
1407 read_unlock(&ul_wakeup_lock);
1408 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -07001409 if (unlikely(in_global_reset == 1))
1410 return;
Jeff Hugod98b1082011-10-24 10:30:23 -06001411 read_lock(&ul_wakeup_lock);
1412 ul_packet_written = 1;
1413 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
1414 }
1415 read_unlock(&ul_wakeup_lock);
1416}
1417
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001418int msm_bam_dmux_kickoff_ul_wakeup(void)
Jeff Hugod98b1082011-10-24 10:30:23 -06001419{
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001420 int is_connected;
1421
1422 read_lock(&ul_wakeup_lock);
1423 ul_packet_written = 1;
1424 is_connected = bam_is_connected;
1425 if (!is_connected)
1426 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
1427 read_unlock(&ul_wakeup_lock);
1428
1429 return is_connected;
Jeff Hugod98b1082011-10-24 10:30:23 -06001430}
1431
Eric Holmberg878923a2012-01-10 14:28:19 -07001432static void power_vote(int vote)
1433{
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301434 BAM_DMUX_LOG("%s: curr=%d, vote=%d\n", __func__,
Eric Holmberg878923a2012-01-10 14:28:19 -07001435 bam_dmux_uplink_vote, vote);
1436
1437 if (bam_dmux_uplink_vote == vote)
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301438 BAM_DMUX_LOG("%s: warning - duplicate power vote\n", __func__);
Eric Holmberg878923a2012-01-10 14:28:19 -07001439
1440 bam_dmux_uplink_vote = vote;
1441 if (vote)
1442 smsm_change_state(SMSM_APPS_STATE, 0, SMSM_A2_POWER_CONTROL);
1443 else
1444 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1445}
1446
Eric Holmberg454d9da2012-01-12 09:37:14 -07001447/*
1448 * @note: Must be called with ul_wakeup_lock locked.
1449 */
1450static inline void ul_powerdown(void)
1451{
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301452 BAM_DMUX_LOG("%s: powerdown\n", __func__);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001453 verify_tx_queue_is_empty(__func__);
1454
1455 if (a2_pc_disabled) {
1456 wait_for_dfab = 1;
1457 INIT_COMPLETION(dfab_unvote_completion);
1458 release_wakelock();
1459 } else {
1460 wait_for_ack = 1;
1461 INIT_COMPLETION(ul_wakeup_ack_completion);
1462 power_vote(0);
1463 }
1464 bam_is_connected = 0;
1465 notify_all(BAM_DMUX_UL_DISCONNECTED, (unsigned long)(NULL));
1466}
1467
1468static inline void ul_powerdown_finish(void)
1469{
1470 if (a2_pc_disabled && wait_for_dfab) {
1471 unvote_dfab();
1472 complete_all(&dfab_unvote_completion);
1473 wait_for_dfab = 0;
1474 }
1475}
1476
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001477/*
1478 * Votes for UL power and returns current power state.
1479 *
1480 * @returns true if currently connected
1481 */
1482int msm_bam_dmux_ul_power_vote(void)
1483{
1484 int is_connected;
1485
1486 read_lock(&ul_wakeup_lock);
1487 atomic_inc(&ul_ondemand_vote);
1488 is_connected = bam_is_connected;
1489 if (!is_connected)
1490 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
1491 read_unlock(&ul_wakeup_lock);
1492
1493 return is_connected;
1494}
1495
1496/*
1497 * Unvotes for UL power.
1498 *
1499 * @returns true if vote count is 0 (UL shutdown possible)
1500 */
1501int msm_bam_dmux_ul_power_unvote(void)
1502{
1503 int vote;
1504
1505 read_lock(&ul_wakeup_lock);
1506 vote = atomic_dec_return(&ul_ondemand_vote);
1507 if (unlikely(vote) < 0)
1508 DMUX_LOG_KERR("%s: invalid power vote %d\n", __func__, vote);
1509 read_unlock(&ul_wakeup_lock);
1510
1511 return vote == 0;
1512}
1513
Jeff Hugocb798022012-04-09 14:55:40 -06001514int msm_bam_dmux_reg_notify(void *priv,
1515 void (*notify)(void *priv, int event_type,
1516 unsigned long data))
1517{
1518 struct outside_notify_func *func;
1519
1520 if (!notify)
1521 return -EINVAL;
1522
1523 func = kmalloc(sizeof(struct outside_notify_func), GFP_KERNEL);
1524 if (!func)
1525 return -ENOMEM;
1526
1527 func->notify = notify;
1528 func->priv = priv;
1529 list_add(&func->list_node, &bam_other_notify_funcs);
1530
1531 return 0;
1532}
1533
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001534static void ul_timeout(struct work_struct *work)
1535{
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001536 unsigned long flags;
1537 int ret;
1538
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001539 if (in_global_reset)
1540 return;
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001541 ret = write_trylock_irqsave(&ul_wakeup_lock, flags);
1542 if (!ret) { /* failed to grab lock, reschedule and bail */
1543 schedule_delayed_work(&ul_timeout_work,
1544 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1545 return;
1546 }
Eric Holmberg454d9da2012-01-12 09:37:14 -07001547 if (bam_is_connected) {
Eric Holmberg6074aba2012-01-18 17:59:44 -07001548 if (!ul_packet_written) {
1549 spin_lock(&bam_tx_pool_spinlock);
1550 if (!list_empty(&bam_tx_pool)) {
1551 struct tx_pkt_info *info;
1552
1553 info = list_first_entry(&bam_tx_pool,
1554 struct tx_pkt_info, list_node);
1555 DMUX_LOG_KERR("%s: UL delayed ts=%u.%09lu\n",
1556 __func__, info->ts_sec, info->ts_nsec);
1557 DBG_INC_TX_STALL_CNT();
1558 ul_packet_written = 1;
1559 }
1560 spin_unlock(&bam_tx_pool_spinlock);
1561 }
1562
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001563 if (ul_packet_written || atomic_read(&ul_ondemand_vote)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301564 BAM_DMUX_LOG("%s: pkt written %d\n",
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001565 __func__, ul_packet_written);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001566 ul_packet_written = 0;
1567 schedule_delayed_work(&ul_timeout_work,
1568 msecs_to_jiffies(UL_TIMEOUT_DELAY));
Eric Holmberg006057d2012-01-11 10:10:42 -07001569 } else {
Eric Holmberg454d9da2012-01-12 09:37:14 -07001570 ul_powerdown();
Eric Holmberg006057d2012-01-11 10:10:42 -07001571 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001572 }
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001573 write_unlock_irqrestore(&ul_wakeup_lock, flags);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001574 ul_powerdown_finish();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001575}
Jeff Hugo4838f412012-01-20 11:19:37 -07001576
1577static int ssrestart_check(void)
1578{
Jeff Hugob8156d72013-06-04 12:51:10 -06001579 int ret = 0;
1580
1581 DMUX_LOG_KERR("%s: modem timeout: BAM DMUX disabled for SSR\n",
1582 __func__);
Eric Holmberg90285e22012-02-22 12:33:05 -07001583 in_global_reset = 1;
Jeff Hugob8156d72013-06-04 12:51:10 -06001584 ret = subsystem_restart("modem");
1585 if (ret == -ENODEV)
1586 panic("modem subsystem restart failed\n");
Eric Holmberg90285e22012-02-22 12:33:05 -07001587 return 1;
Jeff Hugo4838f412012-01-20 11:19:37 -07001588}
1589
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001590static void ul_wakeup(void)
1591{
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001592 int ret;
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001593 int do_vote_dfab = 0;
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001594
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001595 mutex_lock(&wakeup_lock);
1596 if (bam_is_connected) { /* bam got connected before lock grabbed */
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301597 BAM_DMUX_LOG("%s Already awake\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001598 mutex_unlock(&wakeup_lock);
1599 return;
1600 }
Eric Holmberg878923a2012-01-10 14:28:19 -07001601
Jeff Hugoc2696142012-05-03 11:42:13 -06001602 /*
Jeff Hugof5001732012-08-27 13:19:09 -06001603 * if this gets hit, that means restart_notifier_cb() has started
1604 * but probably not finished, thus we know SSR has happened, but
1605 * haven't been able to send that info to our clients yet.
1606 * in that case, abort the ul_wakeup() so that we don't undo any
1607 * work restart_notifier_cb() has done. The clients will be notified
1608 * shortly. No cleanup necessary (reschedule the wakeup) as our and
1609 * their SSR handling will cover it
1610 */
1611 if (unlikely(in_global_reset == 1)) {
1612 mutex_unlock(&wakeup_lock);
1613 return;
1614 }
1615
1616 /*
Jeff Hugoc2696142012-05-03 11:42:13 -06001617 * if someone is voting for UL before bam is inited (modem up first
1618 * time), set flag for init to kickoff ul wakeup once bam is inited
1619 */
1620 mutex_lock(&delayed_ul_vote_lock);
1621 if (unlikely(!bam_mux_initialized)) {
1622 need_delayed_ul_vote = 1;
1623 mutex_unlock(&delayed_ul_vote_lock);
1624 mutex_unlock(&wakeup_lock);
1625 return;
1626 }
1627 mutex_unlock(&delayed_ul_vote_lock);
1628
Eric Holmberg006057d2012-01-11 10:10:42 -07001629 if (a2_pc_disabled) {
1630 /*
1631 * don't grab the wakelock the first time because it is
1632 * already grabbed when a2 powers on
1633 */
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001634 if (likely(a2_pc_disabled_wakelock_skipped)) {
Eric Holmberg006057d2012-01-11 10:10:42 -07001635 grab_wakelock();
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001636 do_vote_dfab = 1; /* vote must occur after wait */
1637 } else {
Jeff Hugo583a6da2012-02-03 11:37:30 -07001638 a2_pc_disabled_wakelock_skipped = 1;
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001639 }
Eric Holmberg006057d2012-01-11 10:10:42 -07001640 if (wait_for_dfab) {
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001641 ret = wait_for_completion_timeout(
Eric Holmberg006057d2012-01-11 10:10:42 -07001642 &dfab_unvote_completion, HZ);
1643 BUG_ON(ret == 0);
1644 }
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001645 if (likely(do_vote_dfab))
1646 vote_dfab();
Eric Holmberg006057d2012-01-11 10:10:42 -07001647 schedule_delayed_work(&ul_timeout_work,
1648 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1649 bam_is_connected = 1;
1650 mutex_unlock(&wakeup_lock);
1651 return;
1652 }
1653
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001654 /*
1655 * must wait for the previous power down request to have been acked
1656 * chances are it already came in and this will just fall through
1657 * instead of waiting
1658 */
1659 if (wait_for_ack) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301660 BAM_DMUX_LOG("%s waiting for previous ack\n", __func__);
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001661 ret = wait_for_completion_timeout(
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001662 &ul_wakeup_ack_completion, HZ);
Eric Holmberg006057d2012-01-11 10:10:42 -07001663 wait_for_ack = 0;
Jeff Hugo4838f412012-01-20 11:19:37 -07001664 if (unlikely(ret == 0) && ssrestart_check()) {
1665 mutex_unlock(&wakeup_lock);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301666 BAM_DMUX_LOG("%s timeout previous ack\n", __func__);
Jeff Hugo4838f412012-01-20 11:19:37 -07001667 return;
1668 }
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001669 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001670 INIT_COMPLETION(ul_wakeup_ack_completion);
Eric Holmberg878923a2012-01-10 14:28:19 -07001671 power_vote(1);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301672 BAM_DMUX_LOG("%s waiting for wakeup ack\n", __func__);
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001673 ret = wait_for_completion_timeout(&ul_wakeup_ack_completion, HZ);
Jeff Hugo4838f412012-01-20 11:19:37 -07001674 if (unlikely(ret == 0) && ssrestart_check()) {
1675 mutex_unlock(&wakeup_lock);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301676 BAM_DMUX_LOG("%s timeout wakeup ack\n", __func__);
Jeff Hugo4838f412012-01-20 11:19:37 -07001677 return;
1678 }
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301679 BAM_DMUX_LOG("%s waiting completion\n", __func__);
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001680 ret = wait_for_completion_timeout(&bam_connection_completion, HZ);
Jeff Hugo4838f412012-01-20 11:19:37 -07001681 if (unlikely(ret == 0) && ssrestart_check()) {
1682 mutex_unlock(&wakeup_lock);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301683 BAM_DMUX_LOG("%s timeout power on\n", __func__);
Jeff Hugo4838f412012-01-20 11:19:37 -07001684 return;
1685 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001686
1687 bam_is_connected = 1;
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301688 BAM_DMUX_LOG("%s complete\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001689 schedule_delayed_work(&ul_timeout_work,
1690 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1691 mutex_unlock(&wakeup_lock);
1692}
1693
1694static void reconnect_to_bam(void)
1695{
1696 int i;
1697
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001698 in_global_reset = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001699 vote_dfab();
Jeff Hugo18792a32012-06-20 15:25:55 -06001700 if (!power_management_only_mode) {
1701 i = sps_device_reset(a2_device_handle);
1702 if (i)
1703 pr_err("%s: device reset failed rc = %d\n", __func__,
1704 i);
1705 i = sps_connect(bam_tx_pipe, &tx_connection);
1706 if (i)
1707 pr_err("%s: tx connection failed rc = %d\n", __func__,
1708 i);
1709 i = sps_connect(bam_rx_pipe, &rx_connection);
1710 if (i)
1711 pr_err("%s: rx connection failed rc = %d\n", __func__,
1712 i);
1713 i = sps_register_event(bam_tx_pipe, &tx_register_event);
1714 if (i)
1715 pr_err("%s: tx event reg failed rc = %d\n", __func__,
1716 i);
1717 i = sps_register_event(bam_rx_pipe, &rx_register_event);
1718 if (i)
1719 pr_err("%s: rx event reg failed rc = %d\n", __func__,
1720 i);
1721 }
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001722
1723 bam_connection_is_active = 1;
1724
1725 if (polling_mode)
1726 rx_switch_to_interrupt_mode();
1727
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001728 toggle_apps_ack();
1729 complete_all(&bam_connection_completion);
Jeff Hugo18792a32012-06-20 15:25:55 -06001730 if (!power_management_only_mode)
1731 queue_rx();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001732}
1733
1734static void disconnect_to_bam(void)
1735{
1736 struct list_head *node;
1737 struct rx_pkt_info *info;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001738 unsigned long flags;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001739
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001740 bam_connection_is_active = 0;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001741
1742 /* handle disconnect during active UL */
1743 write_lock_irqsave(&ul_wakeup_lock, flags);
1744 if (bam_is_connected) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301745 BAM_DMUX_LOG("%s: UL active - forcing powerdown\n", __func__);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001746 ul_powerdown();
1747 }
1748 write_unlock_irqrestore(&ul_wakeup_lock, flags);
1749 ul_powerdown_finish();
1750
1751 /* tear down BAM connection */
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001752 INIT_COMPLETION(bam_connection_completion);
Jeff Hugo18792a32012-06-20 15:25:55 -06001753 if (!power_management_only_mode) {
1754 sps_disconnect(bam_tx_pipe);
1755 sps_disconnect(bam_rx_pipe);
1756 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
1757 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
1758 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001759 unvote_dfab();
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001760
1761 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001762 while (!list_empty(&bam_rx_pool)) {
1763 node = bam_rx_pool.next;
1764 list_del(node);
1765 info = container_of(node, struct rx_pkt_info, list_node);
1766 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
1767 DMA_FROM_DEVICE);
1768 dev_kfree_skb_any(info->skb);
1769 kfree(info);
1770 }
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001771 bam_rx_pool_len = 0;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001772 mutex_unlock(&bam_rx_pool_mutexlock);
Eric Holmberg878923a2012-01-10 14:28:19 -07001773
Jeff Hugo0b13a352012-03-17 23:18:30 -06001774 if (disconnect_ack)
1775 toggle_apps_ack();
1776
Eric Holmberg878923a2012-01-10 14:28:19 -07001777 verify_tx_queue_is_empty(__func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001778}
1779
1780static void vote_dfab(void)
1781{
Jeff Hugoca0caa82011-12-05 16:05:23 -07001782 int rc;
1783
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301784 BAM_DMUX_LOG("%s\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07001785 mutex_lock(&dfab_status_lock);
1786 if (dfab_is_on) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301787 BAM_DMUX_LOG("%s: dfab is already on\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07001788 mutex_unlock(&dfab_status_lock);
1789 return;
1790 }
Jeff Hugod0befde2012-08-09 15:32:49 -06001791 if (dfab_clk) {
1792 rc = clk_prepare_enable(dfab_clk);
1793 if (rc)
1794 DMUX_LOG_KERR("bam_dmux vote for dfab failed rc = %d\n",
1795 rc);
1796 }
1797 if (xo_clk) {
1798 rc = clk_prepare_enable(xo_clk);
1799 if (rc)
1800 DMUX_LOG_KERR("bam_dmux vote for xo failed rc = %d\n",
1801 rc);
1802 }
Eric Holmberg006057d2012-01-11 10:10:42 -07001803 dfab_is_on = 1;
1804 mutex_unlock(&dfab_status_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001805}
1806
1807static void unvote_dfab(void)
1808{
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301809 BAM_DMUX_LOG("%s\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07001810 mutex_lock(&dfab_status_lock);
1811 if (!dfab_is_on) {
1812 DMUX_LOG_KERR("%s: dfab is already off\n", __func__);
1813 dump_stack();
1814 mutex_unlock(&dfab_status_lock);
1815 return;
1816 }
Jeff Hugod0befde2012-08-09 15:32:49 -06001817 if (dfab_clk)
1818 clk_disable_unprepare(dfab_clk);
1819 if (xo_clk)
1820 clk_disable_unprepare(xo_clk);
Eric Holmberg006057d2012-01-11 10:10:42 -07001821 dfab_is_on = 0;
1822 mutex_unlock(&dfab_status_lock);
1823}
1824
1825/* reference counting wrapper around wakelock */
1826static void grab_wakelock(void)
1827{
1828 unsigned long flags;
1829
1830 spin_lock_irqsave(&wakelock_reference_lock, flags);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301831 BAM_DMUX_LOG("%s: ref count = %d\n", __func__,
Eric Holmberg006057d2012-01-11 10:10:42 -07001832 wakelock_reference_count);
1833 if (wakelock_reference_count == 0)
1834 wake_lock(&bam_wakelock);
1835 ++wakelock_reference_count;
1836 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
1837}
1838
1839static void release_wakelock(void)
1840{
1841 unsigned long flags;
1842
1843 spin_lock_irqsave(&wakelock_reference_lock, flags);
1844 if (wakelock_reference_count == 0) {
1845 DMUX_LOG_KERR("%s: bam_dmux wakelock not locked\n", __func__);
1846 dump_stack();
1847 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
1848 return;
1849 }
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301850 BAM_DMUX_LOG("%s: ref count = %d\n", __func__,
Eric Holmberg006057d2012-01-11 10:10:42 -07001851 wakelock_reference_count);
1852 --wakelock_reference_count;
1853 if (wakelock_reference_count == 0)
1854 wake_unlock(&bam_wakelock);
1855 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001856}
1857
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001858static int restart_notifier_cb(struct notifier_block *this,
1859 unsigned long code,
1860 void *data)
1861{
1862 int i;
1863 struct list_head *node;
1864 struct tx_pkt_info *info;
1865 int temp_remote_status;
Jeff Hugo626303bf2011-11-21 11:43:28 -07001866 unsigned long flags;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001867
1868 if (code != SUBSYS_AFTER_SHUTDOWN)
1869 return NOTIFY_DONE;
1870
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301871 BAM_DMUX_LOG("%s: begin\n", __func__);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001872 in_global_reset = 1;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001873
1874 /* Handle uplink Powerdown */
1875 write_lock_irqsave(&ul_wakeup_lock, flags);
1876 if (bam_is_connected) {
1877 ul_powerdown();
1878 wait_for_ack = 0;
1879 }
Jeff Hugo4838f412012-01-20 11:19:37 -07001880 /*
1881 * if modem crash during ul_wakeup(), power_vote is 1, needs to be
1882 * reset to 0. harmless if bam_is_connected check above passes
1883 */
1884 power_vote(0);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001885 write_unlock_irqrestore(&ul_wakeup_lock, flags);
1886 ul_powerdown_finish();
Eric Holmberg006057d2012-01-11 10:10:42 -07001887 a2_pc_disabled = 0;
Jeff Hugo583a6da2012-02-03 11:37:30 -07001888 a2_pc_disabled_wakelock_skipped = 0;
Jeff Hugof62029d2012-07-17 13:39:53 -06001889 disconnect_ack = 1;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001890
1891 /* Cleanup Channel States */
Eric Holmberga623da82012-07-12 09:37:09 -06001892 mutex_lock(&bam_pdev_mutexlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001893 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
1894 temp_remote_status = bam_ch_is_remote_open(i);
1895 bam_ch[i].status &= ~BAM_CH_REMOTE_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001896 bam_ch[i].num_tx_pkts = 0;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001897 if (bam_ch_is_local_open(i))
1898 bam_ch[i].status |= BAM_CH_IN_RESET;
1899 if (temp_remote_status) {
1900 platform_device_unregister(bam_ch[i].pdev);
1901 bam_ch[i].pdev = platform_device_alloc(
1902 bam_ch[i].name, 2);
1903 }
1904 }
Eric Holmberga623da82012-07-12 09:37:09 -06001905 mutex_unlock(&bam_pdev_mutexlock);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001906
1907 /* Cleanup pending UL data */
Jeff Hugo626303bf2011-11-21 11:43:28 -07001908 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001909 while (!list_empty(&bam_tx_pool)) {
1910 node = bam_tx_pool.next;
1911 list_del(node);
1912 info = container_of(node, struct tx_pkt_info,
1913 list_node);
1914 if (!info->is_cmd) {
1915 dma_unmap_single(NULL, info->dma_address,
1916 info->skb->len,
1917 DMA_TO_DEVICE);
1918 dev_kfree_skb_any(info->skb);
1919 } else {
1920 dma_unmap_single(NULL, info->dma_address,
1921 info->len,
1922 DMA_TO_DEVICE);
1923 kfree(info->skb);
1924 }
1925 kfree(info);
1926 }
Jeff Hugo626303bf2011-11-21 11:43:28 -07001927 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001928
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301929 BAM_DMUX_LOG("%s: complete\n", __func__);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001930 return NOTIFY_DONE;
1931}
1932
Jeff Hugo9dea05c2011-12-21 12:23:05 -07001933static int bam_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001934{
1935 u32 h;
1936 dma_addr_t dma_addr;
1937 int ret;
1938 void *a2_virt_addr;
Jeff Hugo4b2890d2012-01-16 16:14:21 -07001939 int skip_iounmap = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001940
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001941 vote_dfab();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001942 /* init BAM */
Jeff Hugo3910ee12012-08-21 14:08:20 -06001943 a2_virt_addr = ioremap_nocache((unsigned long)(a2_phys_base),
1944 a2_phys_size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001945 if (!a2_virt_addr) {
1946 pr_err("%s: ioremap failed\n", __func__);
1947 ret = -ENOMEM;
Jeff Hugo994a92d2012-01-05 13:25:21 -07001948 goto ioremap_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001949 }
Jeff Hugo3910ee12012-08-21 14:08:20 -06001950 a2_props.phys_addr = (u32)(a2_phys_base);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001951 a2_props.virt_addr = a2_virt_addr;
Jeff Hugo3910ee12012-08-21 14:08:20 -06001952 a2_props.virt_size = a2_phys_size;
1953 a2_props.irq = a2_bam_irq;
Jeff Hugo927cba62011-11-11 11:49:52 -07001954 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001955 a2_props.num_pipes = A2_NUM_PIPES;
1956 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
Jeff Hugo75913c82011-12-05 15:59:01 -07001957 if (cpu_is_msm9615())
1958 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001959 /* need to free on tear down */
1960 ret = sps_register_bam_device(&a2_props, &h);
1961 if (ret < 0) {
1962 pr_err("%s: register bam error %d\n", __func__, ret);
1963 goto register_bam_failed;
1964 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001965 a2_device_handle = h;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001966
1967 bam_tx_pipe = sps_alloc_endpoint();
1968 if (bam_tx_pipe == NULL) {
1969 pr_err("%s: tx alloc endpoint failed\n", __func__);
1970 ret = -ENOMEM;
Jeff Hugo8ff4a812012-01-17 11:03:13 -07001971 goto tx_alloc_endpoint_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001972 }
1973 ret = sps_get_config(bam_tx_pipe, &tx_connection);
1974 if (ret) {
1975 pr_err("%s: tx get config failed %d\n", __func__, ret);
1976 goto tx_get_config_failed;
1977 }
1978
1979 tx_connection.source = SPS_DEV_HANDLE_MEM;
1980 tx_connection.src_pipe_index = 0;
1981 tx_connection.destination = h;
1982 tx_connection.dest_pipe_index = 4;
1983 tx_connection.mode = SPS_MODE_DEST;
1984 tx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT;
1985 tx_desc_mem_buf.size = 0x800; /* 2k */
1986 tx_desc_mem_buf.base = dma_alloc_coherent(NULL, tx_desc_mem_buf.size,
1987 &dma_addr, 0);
1988 if (tx_desc_mem_buf.base == NULL) {
1989 pr_err("%s: tx memory alloc failed\n", __func__);
1990 ret = -ENOMEM;
Jeff Hugo8ff4a812012-01-17 11:03:13 -07001991 goto tx_get_config_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001992 }
1993 tx_desc_mem_buf.phys_base = dma_addr;
1994 memset(tx_desc_mem_buf.base, 0x0, tx_desc_mem_buf.size);
1995 tx_connection.desc = tx_desc_mem_buf;
1996 tx_connection.event_thresh = 0x10;
1997
1998 ret = sps_connect(bam_tx_pipe, &tx_connection);
1999 if (ret < 0) {
2000 pr_err("%s: tx connect error %d\n", __func__, ret);
2001 goto tx_connect_failed;
2002 }
2003
2004 bam_rx_pipe = sps_alloc_endpoint();
2005 if (bam_rx_pipe == NULL) {
2006 pr_err("%s: rx alloc endpoint failed\n", __func__);
2007 ret = -ENOMEM;
Jeff Hugo8ff4a812012-01-17 11:03:13 -07002008 goto rx_alloc_endpoint_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002009 }
2010 ret = sps_get_config(bam_rx_pipe, &rx_connection);
2011 if (ret) {
2012 pr_err("%s: rx get config failed %d\n", __func__, ret);
2013 goto rx_get_config_failed;
2014 }
2015
2016 rx_connection.source = h;
2017 rx_connection.src_pipe_index = 5;
2018 rx_connection.destination = SPS_DEV_HANDLE_MEM;
2019 rx_connection.dest_pipe_index = 1;
2020 rx_connection.mode = SPS_MODE_SRC;
Jeff Hugo949080a2011-08-30 11:58:56 -06002021 rx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
2022 SPS_O_ACK_TRANSFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002023 rx_desc_mem_buf.size = 0x800; /* 2k */
2024 rx_desc_mem_buf.base = dma_alloc_coherent(NULL, rx_desc_mem_buf.size,
2025 &dma_addr, 0);
2026 if (rx_desc_mem_buf.base == NULL) {
2027 pr_err("%s: rx memory alloc failed\n", __func__);
2028 ret = -ENOMEM;
2029 goto rx_mem_failed;
2030 }
2031 rx_desc_mem_buf.phys_base = dma_addr;
2032 memset(rx_desc_mem_buf.base, 0x0, rx_desc_mem_buf.size);
2033 rx_connection.desc = rx_desc_mem_buf;
2034 rx_connection.event_thresh = 0x10;
2035
2036 ret = sps_connect(bam_rx_pipe, &rx_connection);
2037 if (ret < 0) {
2038 pr_err("%s: rx connect error %d\n", __func__, ret);
2039 goto rx_connect_failed;
2040 }
2041
2042 tx_register_event.options = SPS_O_EOT;
2043 tx_register_event.mode = SPS_TRIGGER_CALLBACK;
2044 tx_register_event.xfer_done = NULL;
2045 tx_register_event.callback = bam_mux_tx_notify;
2046 tx_register_event.user = NULL;
2047 ret = sps_register_event(bam_tx_pipe, &tx_register_event);
2048 if (ret < 0) {
2049 pr_err("%s: tx register event error %d\n", __func__, ret);
2050 goto rx_event_reg_failed;
2051 }
2052
Jeff Hugo33dbc002011-08-25 15:52:53 -06002053 rx_register_event.options = SPS_O_EOT;
2054 rx_register_event.mode = SPS_TRIGGER_CALLBACK;
2055 rx_register_event.xfer_done = NULL;
2056 rx_register_event.callback = bam_mux_rx_notify;
2057 rx_register_event.user = NULL;
2058 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
2059 if (ret < 0) {
2060 pr_err("%s: tx register event error %d\n", __func__, ret);
2061 goto rx_event_reg_failed;
2062 }
2063
Jeff Hugoc2696142012-05-03 11:42:13 -06002064 mutex_lock(&delayed_ul_vote_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002065 bam_mux_initialized = 1;
Jeff Hugoc2696142012-05-03 11:42:13 -06002066 if (need_delayed_ul_vote) {
2067 need_delayed_ul_vote = 0;
2068 msm_bam_dmux_kickoff_ul_wakeup();
2069 }
2070 mutex_unlock(&delayed_ul_vote_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002071 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06002072 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002073 complete_all(&bam_connection_completion);
Jeff Hugo2fb555e2012-03-14 16:33:47 -06002074 queue_rx();
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002075 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002076
2077rx_event_reg_failed:
2078 sps_disconnect(bam_rx_pipe);
2079rx_connect_failed:
2080 dma_free_coherent(NULL, rx_desc_mem_buf.size, rx_desc_mem_buf.base,
2081 rx_desc_mem_buf.phys_base);
2082rx_mem_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002083rx_get_config_failed:
2084 sps_free_endpoint(bam_rx_pipe);
Jeff Hugo8ff4a812012-01-17 11:03:13 -07002085rx_alloc_endpoint_failed:
2086 sps_disconnect(bam_tx_pipe);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002087tx_connect_failed:
2088 dma_free_coherent(NULL, tx_desc_mem_buf.size, tx_desc_mem_buf.base,
2089 tx_desc_mem_buf.phys_base);
2090tx_get_config_failed:
2091 sps_free_endpoint(bam_tx_pipe);
Jeff Hugo8ff4a812012-01-17 11:03:13 -07002092tx_alloc_endpoint_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002093 sps_deregister_bam_device(h);
Jeff Hugo4b2890d2012-01-16 16:14:21 -07002094 /*
2095 * sps_deregister_bam_device() calls iounmap. calling iounmap on the
2096 * same handle below will cause a crash, so skip it if we've freed
2097 * the handle here.
2098 */
2099 skip_iounmap = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002100register_bam_failed:
Jeff Hugo4b2890d2012-01-16 16:14:21 -07002101 if (!skip_iounmap)
2102 iounmap(a2_virt_addr);
Jeff Hugo994a92d2012-01-05 13:25:21 -07002103ioremap_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002104 /*destroy_workqueue(bam_mux_workqueue);*/
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002105 return ret;
2106}
2107
2108static int bam_init_fallback(void)
2109{
2110 u32 h;
2111 int ret;
2112 void *a2_virt_addr;
2113
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002114 /* init BAM */
Jeff Hugo3910ee12012-08-21 14:08:20 -06002115 a2_virt_addr = ioremap_nocache((unsigned long)(a2_phys_base),
2116 a2_phys_size);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002117 if (!a2_virt_addr) {
2118 pr_err("%s: ioremap failed\n", __func__);
2119 ret = -ENOMEM;
2120 goto ioremap_failed;
2121 }
Jeff Hugo3910ee12012-08-21 14:08:20 -06002122 a2_props.phys_addr = (u32)(a2_phys_base);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002123 a2_props.virt_addr = a2_virt_addr;
Jeff Hugo3910ee12012-08-21 14:08:20 -06002124 a2_props.virt_size = a2_phys_size;
2125 a2_props.irq = a2_bam_irq;
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002126 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
2127 a2_props.num_pipes = A2_NUM_PIPES;
2128 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
2129 if (cpu_is_msm9615())
2130 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
2131 ret = sps_register_bam_device(&a2_props, &h);
2132 if (ret < 0) {
2133 pr_err("%s: register bam error %d\n", __func__, ret);
2134 goto register_bam_failed;
2135 }
2136 a2_device_handle = h;
Jeff Hugoc2696142012-05-03 11:42:13 -06002137
2138 mutex_lock(&delayed_ul_vote_lock);
2139 bam_mux_initialized = 1;
2140 if (need_delayed_ul_vote) {
2141 need_delayed_ul_vote = 0;
2142 msm_bam_dmux_kickoff_ul_wakeup();
2143 }
2144 mutex_unlock(&delayed_ul_vote_lock);
Jeff Hugo2bec9772012-04-05 12:25:16 -06002145 toggle_apps_ack();
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002146
Jeff Hugo18792a32012-06-20 15:25:55 -06002147 power_management_only_mode = 1;
2148 bam_connection_is_active = 1;
2149 complete_all(&bam_connection_completion);
2150
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002151 return 0;
2152
2153register_bam_failed:
Jeff Hugo4b2890d2012-01-16 16:14:21 -07002154 iounmap(a2_virt_addr);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002155ioremap_failed:
2156 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002157}
Jeff Hugoade1f842011-08-03 15:53:59 -06002158
Jeff Hugoa670b762012-03-15 15:58:28 -06002159static void msm9615_bam_init(void)
Eric Holmberg604ab252012-01-15 00:01:18 -07002160{
2161 int ret = 0;
2162
2163 ret = bam_init();
2164 if (ret) {
2165 ret = bam_init_fallback();
2166 if (ret)
2167 pr_err("%s: bam init fallback failed: %d",
2168 __func__, ret);
2169 }
2170}
2171
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002172static void toggle_apps_ack(void)
2173{
2174 static unsigned int clear_bit; /* 0 = set the bit, else clear bit */
Eric Holmberg878923a2012-01-10 14:28:19 -07002175
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302176 BAM_DMUX_LOG("%s: apps ack %d->%d\n", __func__,
Eric Holmberg878923a2012-01-10 14:28:19 -07002177 clear_bit & 0x1, ~clear_bit & 0x1);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002178 smsm_change_state(SMSM_APPS_STATE,
2179 clear_bit & SMSM_A2_POWER_CONTROL_ACK,
2180 ~clear_bit & SMSM_A2_POWER_CONTROL_ACK);
2181 clear_bit = ~clear_bit;
Eric Holmberg1f1255d2012-02-22 13:37:21 -07002182 DBG_INC_ACK_OUT_CNT();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002183}
2184
Jeff Hugoade1f842011-08-03 15:53:59 -06002185static void bam_dmux_smsm_cb(void *priv, uint32_t old_state, uint32_t new_state)
2186{
Jeff Hugo4b7c7b32012-04-18 16:25:14 -06002187 static int last_processed_state;
2188
2189 mutex_lock(&smsm_cb_lock);
Eric Holmberg878923a2012-01-10 14:28:19 -07002190 bam_dmux_power_state = new_state & SMSM_A2_POWER_CONTROL ? 1 : 0;
Eric Holmberg1f1255d2012-02-22 13:37:21 -07002191 DBG_INC_A2_POWER_CONTROL_IN_CNT();
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302192 BAM_DMUX_LOG("%s: 0x%08x -> 0x%08x\n", __func__, old_state,
Eric Holmberg878923a2012-01-10 14:28:19 -07002193 new_state);
Jeff Hugo4b7c7b32012-04-18 16:25:14 -06002194 if (last_processed_state == (new_state & SMSM_A2_POWER_CONTROL)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302195 BAM_DMUX_LOG("%s: already processed this state\n", __func__);
Jeff Hugo4b7c7b32012-04-18 16:25:14 -06002196 mutex_unlock(&smsm_cb_lock);
2197 return;
2198 }
2199
2200 last_processed_state = new_state & SMSM_A2_POWER_CONTROL;
Eric Holmberg878923a2012-01-10 14:28:19 -07002201
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002202 if (bam_mux_initialized && new_state & SMSM_A2_POWER_CONTROL) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302203 BAM_DMUX_LOG("%s: reconnect\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07002204 grab_wakelock();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002205 reconnect_to_bam();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002206 } else if (bam_mux_initialized &&
2207 !(new_state & SMSM_A2_POWER_CONTROL)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302208 BAM_DMUX_LOG("%s: disconnect\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002209 disconnect_to_bam();
Eric Holmberg006057d2012-01-11 10:10:42 -07002210 release_wakelock();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002211 } else if (new_state & SMSM_A2_POWER_CONTROL) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302212 BAM_DMUX_LOG("%s: init\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07002213 grab_wakelock();
Jeff Hugoa670b762012-03-15 15:58:28 -06002214 if (cpu_is_msm9615())
2215 msm9615_bam_init();
2216 else
Eric Holmberg604ab252012-01-15 00:01:18 -07002217 bam_init();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002218 } else {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302219 BAM_DMUX_LOG("%s: bad state change\n", __func__);
Jeff Hugoade1f842011-08-03 15:53:59 -06002220 pr_err("%s: unsupported state change\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002221 }
Jeff Hugo4b7c7b32012-04-18 16:25:14 -06002222 mutex_unlock(&smsm_cb_lock);
Jeff Hugoade1f842011-08-03 15:53:59 -06002223
2224}
2225
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002226static void bam_dmux_smsm_ack_cb(void *priv, uint32_t old_state,
2227 uint32_t new_state)
2228{
Eric Holmberg1f1255d2012-02-22 13:37:21 -07002229 DBG_INC_ACK_IN_CNT();
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302230 BAM_DMUX_LOG("%s: 0x%08x -> 0x%08x\n", __func__, old_state,
Eric Holmberg878923a2012-01-10 14:28:19 -07002231 new_state);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002232 complete_all(&ul_wakeup_ack_completion);
2233}
2234
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002235static int bam_dmux_probe(struct platform_device *pdev)
2236{
2237 int rc;
Jeff Hugo3910ee12012-08-21 14:08:20 -06002238 struct resource *r;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002239
2240 DBG("%s probe called\n", __func__);
2241 if (bam_mux_initialized)
2242 return 0;
2243
Jeff Hugo3910ee12012-08-21 14:08:20 -06002244 if (pdev->dev.of_node) {
2245 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2246 if (!r) {
2247 pr_err("%s: reg field missing\n", __func__);
2248 return -ENODEV;
2249 }
2250 a2_phys_base = (void *)(r->start);
2251 a2_phys_size = (uint32_t)(resource_size(r));
2252 a2_bam_irq = platform_get_irq(pdev, 0);
2253 if (a2_bam_irq == -ENXIO) {
2254 pr_err("%s: irq field missing\n", __func__);
2255 return -ENODEV;
2256 }
2257 DBG("%s: base:%p size:%x irq:%d\n", __func__,
2258 a2_phys_base,
2259 a2_phys_size,
2260 a2_bam_irq);
2261 } else { /* fallback to default init data */
2262 a2_phys_base = (void *)(A2_PHYS_BASE);
2263 a2_phys_size = A2_PHYS_SIZE;
2264 a2_bam_irq = A2_BAM_IRQ;
2265 }
2266
Stephen Boyd69d35e32012-02-14 15:33:30 -08002267 xo_clk = clk_get(&pdev->dev, "xo");
2268 if (IS_ERR(xo_clk)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302269 BAM_DMUX_LOG("%s: did not get xo clock\n", __func__);
Jeff Hugod0befde2012-08-09 15:32:49 -06002270 xo_clk = NULL;
Stephen Boyd69d35e32012-02-14 15:33:30 -08002271 }
Stephen Boyd1c51a492011-10-26 12:11:47 -07002272 dfab_clk = clk_get(&pdev->dev, "bus_clk");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002273 if (IS_ERR(dfab_clk)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302274 BAM_DMUX_LOG("%s: did not get dfab clock\n", __func__);
Jeff Hugod0befde2012-08-09 15:32:49 -06002275 dfab_clk = NULL;
2276 } else {
2277 rc = clk_set_rate(dfab_clk, 64000000);
2278 if (rc)
2279 pr_err("%s: unable to set dfab clock rate\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002280 }
2281
Jeff Hugofff43af92012-03-29 17:54:52 -06002282 /*
2283 * setup the workqueue so that it can be pinned to core 0 and not
2284 * block the watchdog pet function, so that netif_rx() in rmnet
2285 * only uses one queue.
2286 */
2287 bam_mux_rx_workqueue = alloc_workqueue("bam_dmux_rx",
2288 WQ_MEM_RECLAIM | WQ_CPU_INTENSIVE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002289 if (!bam_mux_rx_workqueue)
2290 return -ENOMEM;
2291
2292 bam_mux_tx_workqueue = create_singlethread_workqueue("bam_dmux_tx");
2293 if (!bam_mux_tx_workqueue) {
2294 destroy_workqueue(bam_mux_rx_workqueue);
2295 return -ENOMEM;
2296 }
2297
Jeff Hugo7960abd2011-08-02 15:39:38 -06002298 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002299 spin_lock_init(&bam_ch[rc].lock);
Jeff Hugo7960abd2011-08-02 15:39:38 -06002300 scnprintf(bam_ch[rc].name, BAM_DMUX_CH_NAME_MAX_LEN,
2301 "bam_dmux_ch_%d", rc);
2302 /* bus 2, ie a2 stream 2 */
2303 bam_ch[rc].pdev = platform_device_alloc(bam_ch[rc].name, 2);
2304 if (!bam_ch[rc].pdev) {
2305 pr_err("%s: platform device alloc failed\n", __func__);
2306 destroy_workqueue(bam_mux_rx_workqueue);
2307 destroy_workqueue(bam_mux_tx_workqueue);
2308 return -ENOMEM;
2309 }
2310 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002311
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002312 init_completion(&ul_wakeup_ack_completion);
2313 init_completion(&bam_connection_completion);
Eric Holmberg006057d2012-01-11 10:10:42 -07002314 init_completion(&dfab_unvote_completion);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002315 INIT_DELAYED_WORK(&ul_timeout_work, ul_timeout);
Jeff Hugo988e7ba2012-10-03 15:53:54 -06002316 INIT_DELAYED_WORK(&queue_rx_work, queue_rx_work_func);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002317 wake_lock_init(&bam_wakelock, WAKE_LOCK_SUSPEND, "bam_dmux_wakelock");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002318
Jeff Hugoade1f842011-08-03 15:53:59 -06002319 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL,
2320 bam_dmux_smsm_cb, NULL);
2321
2322 if (rc) {
2323 destroy_workqueue(bam_mux_rx_workqueue);
2324 destroy_workqueue(bam_mux_tx_workqueue);
2325 pr_err("%s: smsm cb register failed, rc: %d\n", __func__, rc);
2326 return -ENOMEM;
2327 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002328
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002329 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL_ACK,
2330 bam_dmux_smsm_ack_cb, NULL);
2331
2332 if (rc) {
2333 destroy_workqueue(bam_mux_rx_workqueue);
2334 destroy_workqueue(bam_mux_tx_workqueue);
2335 smsm_state_cb_deregister(SMSM_MODEM_STATE,
2336 SMSM_A2_POWER_CONTROL,
2337 bam_dmux_smsm_cb, NULL);
2338 pr_err("%s: smsm ack cb register failed, rc: %d\n", __func__,
2339 rc);
2340 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc)
2341 platform_device_put(bam_ch[rc].pdev);
2342 return -ENOMEM;
2343 }
2344
Eric Holmbergfd1e2ae2011-11-15 18:28:17 -07002345 if (smsm_get_state(SMSM_MODEM_STATE) & SMSM_A2_POWER_CONTROL)
2346 bam_dmux_smsm_cb(NULL, 0, smsm_get_state(SMSM_MODEM_STATE));
2347
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002348 return 0;
2349}
2350
Jeff Hugo3910ee12012-08-21 14:08:20 -06002351static struct of_device_id msm_match_table[] = {
2352 {.compatible = "qcom,bam_dmux"},
2353 {},
2354};
2355
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002356static struct platform_driver bam_dmux_driver = {
2357 .probe = bam_dmux_probe,
2358 .driver = {
2359 .name = "BAM_RMNT",
2360 .owner = THIS_MODULE,
Jeff Hugo3910ee12012-08-21 14:08:20 -06002361 .of_match_table = msm_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002362 },
2363};
2364
2365static int __init bam_dmux_init(void)
2366{
2367#ifdef CONFIG_DEBUG_FS
2368 struct dentry *dent;
2369
2370 dent = debugfs_create_dir("bam_dmux", 0);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07002371 if (!IS_ERR(dent)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002372 debug_create("tbl", 0444, dent, debug_tbl);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07002373 debug_create("ul_pkt_cnt", 0444, dent, debug_ul_pkt_cnt);
2374 debug_create("stats", 0444, dent, debug_stats);
2375 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002376#endif
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302377
2378 bam_ipc_log_txt = ipc_log_context_create(BAM_IPC_LOG_PAGES, "bam_dmux");
2379 if (!bam_ipc_log_txt) {
2380 pr_err("%s : unable to create IPC Logging Context", __func__);
Eric Holmberg878923a2012-01-10 14:28:19 -07002381 }
2382
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07002383 rx_timer_interval = DEFAULT_POLLING_MIN_SLEEP;
2384
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06002385 subsys_notif_register_notifier("modem", &restart_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002386 return platform_driver_register(&bam_dmux_driver);
2387}
2388
Jeff Hugoade1f842011-08-03 15:53:59 -06002389late_initcall(bam_dmux_init); /* needs to init after SMD */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002390MODULE_DESCRIPTION("MSM BAM DMUX");
2391MODULE_LICENSE("GPL v2");