summaryrefstree
path: root/kernel/drivers/misc/modem_v1/link_ctrlmsg_iosm.c
blob: 8ee5f73eb671701f793cf1318f67ebbe82402f83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
 * Copyright (C) 2014 Samsung Electronics.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include "modem_utils.h"
#include "link_device_memory.h"
#include "link_device_memory_ctrlmsg_iosm.h"

#ifdef GROUP_MEM_LINK_IOSM_MESSAGE

#define pr_circ_idx(hdr) \
	mif_info("circ: in=%d, out=%d\n", hdr->w_idx, hdr->r_idx);

struct iosm_msg_area_head {
	u32	w_idx;		/* Write index */
	u32	r_idx;		/* Read index */
	u32	reserved[2];
	u32	num_msg;	/* Is this actually required? */
} __packed;

struct iosm_msg {
	u8	msg_id;		/* message id */
	u8	trs_id;		/* transaction id */
	union {
		struct __packed {
			u8	reserved[2];
			u32	addr;
		} ap_ready;
		struct __packed {
			u8	ch_id;
			u8	cfg;
		} open_ch;
		struct __packed {
			u8	ch_id;
			u8	cfg;
		} close_ch;
		struct __packed {
			u8	ch_id;
			u8	cfg;
		} conf_ch_req;
		struct __packed {
			u8	ch_id;
			u8	cfg;
		} conf_ch_rsp;
		struct __packed {
			u8	ch_id;
		} stop_tx_ch;
		struct __packed {
			u8	ch_id;
		} start_tx_ch;
		struct __packed {
			u8	ch_id;
			u8	msg_id;
		} ack;
		struct __packed {
			u8	ch_id;
			u8	msg_id;
			u8	err_class;
			u8	err_subclass;
		} nack;
		u8	reserved[10];
	};
} __packed;

#define IOSM_NUM_ELEMENTS ( \
	(IOSM_MSG_AREA_SIZE - sizeof(struct iosm_msg_area_head)) \
		/ sizeof(struct iosm_msg))

/* Message Area definition */
struct iosm_msg_area {
	struct iosm_msg_area_head hdr;
	struct iosm_msg	elements[IOSM_NUM_ELEMENTS];
} __packed;

static struct workqueue_struct *iosm_wq;
static struct mutex iosm_mtx;
static atomic_t mdm_ready;

static const char * const tx_iosm_str[] = {
	[IOSM_A2C_AP_READY] = "AP_READY",
	[IOSM_A2C_CONF_CH_REQ] = "CONF_CH_REQ",
	[IOSM_A2C_OPEN_CH] = "OPEN_CH",
	[IOSM_A2C_CLOSE_CH] = "CLOSE_CH",
	[IOSM_A2C_STOP_TX_CH] = "STOP_TX_CH",
	[IOSM_A2C_START_TX_CH] = "START_TX_CH",
	[IOSM_A2C_ACK] = "ACK",
	[IOSM_A2C_NACK] = "NACK",
};

/* 8-bit transaction ID: holds unique id value (1 ~ 255)
 * different number than the last message and incremented by one
 * also to be echoed back in the response of received message
 */
static atomic_t tid = ATOMIC_INIT(-1);

static inline int get_transaction_id(void)
{
	return atomic_inc_return(&tid) % IOSM_TRANS_ID_MAX + 1;
}

static inline int check_ul_space(u32 qlen, u32 in, u32 out)
{
	u32 usage, space;

	if (!circ_valid(qlen, in, out)) {
		mif_err("ERR! TXQ DIRTY (qlen:%d in:%d out:%d)\n",
				qlen, in, out);
		return -EIO;
	}

	usage = circ_get_usage(qlen, in, out);
	if (unlikely(usage > SBD_UL_LIMIT)) {
		mif_err("TXQ BUSY (qlen:%d in:%d out:%d usage:%d)\n",
			qlen, in, out, usage);
		return -EBUSY;
	}

	space = circ_get_space(qlen, in, out);
	if (unlikely(space < 1)) {
		mif_err("TXQ NOSPC (qlen:%d in:%d out:%d)\n", qlen, in, out);
		return -ENOSPC;
	}

	return space;
}

inline void create_iosm_message(struct iosm_msg *txmsg, u8 mid, u32 *args)
{
	struct iosm_msg *msg;

	txmsg->msg_id = mid;

	switch (mid) {
	case IOSM_A2C_AP_READY:
		/* set global descriptor address */
		txmsg->ap_ready.addr = IOSM_MSG_DESC_OFFSET;
		break;
	case IOSM_A2C_CONF_CH_REQ:
		txmsg->conf_ch_req.ch_id = *((u32 *)args);
		txmsg->conf_ch_req.cfg = 0x1;
		break;
	case IOSM_A2C_OPEN_CH:
	case IOSM_A2C_CLOSE_CH:
		txmsg->close_ch.ch_id = *((u32 *)args);
		txmsg->close_ch.cfg = 0x7;
		break;
	case IOSM_A2C_ACK:
	case IOSM_A2C_NACK:
		msg = (struct iosm_msg *) args;
		txmsg->ack.msg_id = msg->msg_id;
		if (msg->msg_id == IOSM_C2A_CONF_CH_RSP)
			txmsg->ack.ch_id = msg->conf_ch_rsp.ch_id;
		if (msg->msg_id == IOSM_C2A_STOP_TX_CH)
			txmsg->ack.ch_id = msg->stop_tx_ch.ch_id;
		if (msg->msg_id == IOSM_C2A_START_TX_CH)
			txmsg->ack.ch_id = msg->start_tx_ch.ch_id;
		/* trans_id shouldn't be modified from end of this function */
		txmsg->trs_id = msg->trs_id;
		return;
	default:
		mif_err("0x%x message is not supported.\n", mid);
	}

	txmsg->trs_id = get_transaction_id();
}

void tx_iosm_message(struct mem_link_device *mld, u8 id, u32 *args)
{
	struct iosm_msg_area *base;
	struct iosm_msg_area_head *hdr;
	struct iosm_msg *msg;
	struct link_device *ld = &mld->link_dev;
	struct modem_ctl *mc = ld->mc;
	int space, retry_cnt = 500;

	if (!cp_online(mc))
		return;

	mutex_lock(&iosm_mtx);

	base = (struct iosm_msg_area *) (mld->base + IOSM_MSG_TX_OFFSET);
	hdr = &base->hdr;

	/* A message sender reads the read and write index and
	 * determines whether there are free elements.
	 */
	space = check_ul_space(IOSM_NUM_ELEMENTS, hdr->w_idx, hdr->r_idx);
	if (space <= 0) {
		mutex_unlock(&iosm_mtx);
		return;
	}

	msg = &base->elements[hdr->w_idx];
	create_iosm_message(msg, id, args);

	/* The write index is incremented and interrupt is triggered
	 * to the message receiver.
	 */
	hdr->w_idx = circ_new_ptr(IOSM_NUM_ELEMENTS, hdr->w_idx, 1);
	pr_circ_idx(hdr);

	mutex_unlock(&iosm_mtx);

	if (cp_online(mc) && mld->forbid_cp_sleep)
		mld->forbid_cp_sleep(mld);

	/* As of now, tx path of iosm message should always guarantee
	 * process context. We don't have to care rx path because cp
	 * might try to mount lli i/f before sending data. */
	while (!mld->link_active(mld)) {
		if (--retry_cnt == 0) {
			modemctl_notify_event(MDM_EVENT_CP_FORCE_CRASH);
			return;
		}
		usleep_range(10000, 11000);
	}

	send_ipc_irq(mld, mask2int(MASK_CMD_VALID));
	mif_info("sent msg %s\n", tx_iosm_str[msg->msg_id]);

	if (cp_online(mc) && mld->permit_cp_sleep)
		mld->permit_cp_sleep(mld);
}

void mdm_ready_handler(struct mem_link_device *mld, struct iosm_msg *msg)
{
	int err;
	struct link_device *ld = &mld->link_dev;
	struct modem_ctl *mc = ld->mc;

	mif_err("%s: %s.state:%s cp_boot_done:%d\n", ld->name,
		mc->name, mc_state(mc), atomic_read(&mld->cp_boot_done));

	if (!ld->sbd_ipc) {
		mif_err("%s: LINK_ATTR_SBD_IPC is NOT set\n", ld->name);
		return;
	}

	ld->netif_stop_mask = 0;
	atomic_set(&ld->netif_stopped, 0);
	atomic_set(&mc->forced_cp_crash, 0);

	mc->iod->modem_state_changed(mc->iod, STATE_ONLINE);

#ifdef CONFIG_LINK_POWER_MANAGEMENT
	if (mld->start_pm) {
		mld->start_pm(mld);
		gpio_set_value(mld->gpio_cp_wakeup, 0);
		gpio_set_value(mld->gpio_ap_status, 0);
	}

	if (cp_online(mc) && mld->forbid_cp_sleep)
		mld->forbid_cp_sleep(mld);
#endif

	tx_iosm_message(mld, IOSM_A2C_ACK, (u32 *)msg);

	err = init_sbd_link(&mld->sbd_link_dev);
	if (err < 0) {
		mif_err("%s: init_sbd_link fail(%d)\n", ld->name, err);
		return;
	}

	if (mld->attrs & LINK_ATTR(LINK_ATTR_IPC_ALIGNED))
		ld->aligned = true;
	else
		ld->aligned = false;

	sbd_activate(&mld->sbd_link_dev);

	tx_iosm_message(mld, IOSM_A2C_AP_READY, 0);

	mif_info("%s: %s mdm_ready done\n", ld->name, mc->name);
}

void conf_ch_rsp_handler(struct mem_link_device *mld, struct iosm_msg *msg)
{
	struct sbd_link_device *sl = &mld->sbd_link_dev;
	int dev_id = sbd_ch2id(sl, msg->ack.ch_id);
	struct sbd_ipc_device *sid = sbd_id2dev(sl, dev_id);

	atomic_set(&sid->config_done, 1);
	tx_iosm_message(mld, IOSM_A2C_ACK, (u32 *)msg);

	mif_info("ch_id : %d, dev_id : %d\n", sid->ch, dev_id);
}

void stop_tx_ch_handler(struct mem_link_device *mld, struct iosm_msg *msg)
{
	struct link_device *ld = &mld->link_dev;

	stop_net_iface(ld, msg->stop_tx_ch.ch_id);
	tx_iosm_message(mld, IOSM_A2C_ACK, (u32 *)msg);
}

void start_tx_ch_handler(struct mem_link_device *mld, struct iosm_msg *msg)
{
	struct link_device *ld = &mld->link_dev;

	resume_net_iface(ld, msg->start_tx_ch.ch_id);
	tx_iosm_message(mld, IOSM_A2C_ACK, (u32 *)msg);
}

static void action(struct io_device *iod, void *args)
{
	struct mem_link_device *mld = (struct mem_link_device *) args;

	tx_iosm_message(mld, IOSM_A2C_CONF_CH_REQ, (u32 *)&iod->id);
}

void ack_handler(struct mem_link_device *mld, struct iosm_msg *msg)
{
	struct link_device *ld = &mld->link_dev;
	struct modem_ctl *mc = ld->mc;

	mif_err("got ack for msg id = 0x%x\n", msg->ack.msg_id);

	switch (msg->ack.msg_id) {
	case IOSM_A2C_AP_READY:
		atomic_set(&mdm_ready, 1);
		iodevs_for_each(ld->msd, action, mld);
		break;
	case IOSM_A2C_OPEN_CH:
#ifdef CONFIG_LINK_POWER_MANAGEMENT
	if (msg->ack.ch_id == SIPC5_CH_ID_FMT_0) {
		if (cp_online(mc) && mld->permit_cp_sleep)
			mld->permit_cp_sleep(mld);
	}
#endif
		break;
	case IOSM_A2C_CLOSE_CH:
	default:
		break;
	}
}

void nack_handler(struct mem_link_device *mld, struct iosm_msg *msg)
{
	mif_err("got nack for msg id = 0x%x\n", msg->nack.msg_id);
}

static struct {
	u16	cmd;
	char	*name;
	void (*handler)(struct mem_link_device *mld, struct iosm_msg *msg);
} iosm_handler[] = {
	{ IOSM_C2A_MDM_READY, "MDM_READY", mdm_ready_handler },
	{ IOSM_C2A_CONF_CH_RSP, "CONFG_CH_RSP", conf_ch_rsp_handler },
	{ IOSM_C2A_STOP_TX_CH, "STOP_TX_CH", stop_tx_ch_handler },
	{ IOSM_C2A_START_TX_CH, "START_TX_CH", start_tx_ch_handler },
	{ IOSM_C2A_ACK, "ACK", ack_handler },
	{ IOSM_C2A_NACK, "NACK", nack_handler },
};

void iosm_event_work(struct work_struct *work)
{
	struct iosm_msg_area *base;
	struct iosm_msg_area_head *hdr;
	struct iosm_msg *msg;
	u32 i, size;
	struct mem_link_device *mld =
		container_of(work, struct mem_link_device, iosm_w);

	base = (struct iosm_msg_area *) (mld->base + IOSM_MSG_RX_OFFSET);
	hdr = &base->hdr;

	if (unlikely(circ_empty(hdr->w_idx, hdr->r_idx))) {
		mif_info("iosm message area is full\n");
		return;
	}

	/* The message receiver determines the number of available messages
	 * based on the read and write index.
	 */
	size = circ_get_usage(IOSM_NUM_ELEMENTS, hdr->w_idx, hdr->r_idx);
	mif_debug("number of available messages = %d\n", size);

	while (size--) {
		msg = &base->elements[hdr->r_idx];
		for (i = 0; i < ARRAY_SIZE(iosm_handler); i++)
			if (iosm_handler[i].cmd == msg->msg_id) {
				mif_info("got msg %s\n", iosm_handler[i].name);
				(*iosm_handler[i].handler)(mld, msg);
				break;
			}
		if (i >= ARRAY_SIZE(iosm_handler))
			mif_err("0x%x message is not supported\n", msg->msg_id);
		/* read index is increamented by the number of read messages */
		hdr->r_idx = circ_new_ptr(IOSM_NUM_ELEMENTS, hdr->r_idx, 1);
	}
	pr_circ_idx(hdr);
}

void iosm_event_bh(struct mem_link_device *mld, u16 cmd)
{
	queue_work(iosm_wq, &mld->iosm_w);
}

static int __init iosm_init(void)
{
	iosm_wq = create_singlethread_workqueue("iosm_wq");
	if (!iosm_wq) {
		mif_err("ERR! fail to create tx_wq\n");
		return -ENOMEM;
	}
	mutex_init(&iosm_mtx);
	atomic_set(&mdm_ready, 0);
	mif_info("iosm_msg size = %ld, num of iosm elements = %ld\n",
			(long)sizeof(struct iosm_msg), (long)IOSM_NUM_ELEMENTS);
	return 0;
}
module_init(iosm_init);

static void __exit iosm_exit(void)
{
	destroy_workqueue(iosm_wq);
}
module_exit(iosm_exit);

#endif