aboutsummaryrefslogtreecommitdiff
path: root/drivers/staging/westbridge/astoria/block/cyasblkdev_queue.c
blob: 0bbb8a3e191df18963e98bafeaef480da125a3a2 (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
/* cyanblkdev_queue.h - Antioch Linux Block Driver queue source file
## ===========================
## Copyright (C) 2010  Cypress Semiconductor
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## 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.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin Street, Fifth Floor,
## Boston, MA  02110-1301, USA.
## ===========================
*/

/*
 * Request queue handling for Antioch block device driver.
 * Based on the mmc queue handling code by Russell King in the
 * linux 2.6.10 kernel.
 */

/*
 *  linux/drivers/mmc/mmc_queue.c
 *
 *  Copyright (C) 2003 Russell King, All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

#include <linux/module.h>
#include <linux/blkdev.h>

#include "cyasblkdev_queue.h"

#define CYASBLKDEV_QUEUE_EXIT		(1 << 0)
#define CYASBLKDEV_QUEUE_SUSPENDED	(1 << 1)
#define CY_AS_USE_ASYNC_API



/* print flags by name */
const char *rq_flag_bit_names[] = {
	"REQ_RW",			/* not set, read. set, write */
	"REQ_FAILFAST",		/* no low level driver retries */
	"REQ_SORTED",		/* elevator knows about this request */
	"REQ_SOFTBARRIER",	/* may not be passed by ioscheduler */
	"REQ_HARDBARRIER",	/* may not be passed by drive either */
	"REQ_FUA",			/* forced unit access */
	"REQ_NOMERGE",		/* don't touch this for merging */
	"REQ_STARTED",		/* drive already may have started this one */
	"REQ_DONTPREP",		/* don't call prep for this one */
	"REQ_QUEUED",		/* uses queueing */
	"REQ_ELVPRIV",		/* elevator private data attached */
	"REQ_FAILED",		/* set if the request failed */
	"REQ_QUIET",		/* don't worry about errors */
	"REQ_PREEMPT",		/* set for "ide_preempt" requests */
	"REQ_ORDERED_COLOR",/* is before or after barrier */
	"REQ_RW_SYNC",		/* request is sync (O_DIRECT) */
	"REQ_ALLOCED",		/* request came from our alloc pool */
	"REQ_RW_META",		/* metadata io request */
	"REQ_COPY_USER",	/* contains copies of user pages */
	"REQ_NR_BITS",		/* stops here */
};

void verbose_rq_flags(int flags)
{
	int i;
	uint32_t j;
	j = 1;
	for (i = 0; i < 32; i++) {
		if (flags & j)
			DBGPRN("<1>%s", rq_flag_bit_names[i]);
		j = j << 1;
	}
}


/*
 * Prepare a -BLK_DEV  request.  Essentially, this means passing the
 * preparation off to the media driver.  The media driver will
 * create request to CyAsDev.
 */
static int cyasblkdev_prep_request(
	struct request_queue *q, struct request *req)
{
	DBGPRN_FUNC_NAME;

	/* we only like normal block requests.*/
	if (req->cmd_type != REQ_TYPE_FS && !(req->cmd_flags & REQ_DISCARD)) {
		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message("%s:%x bad request received\n",
			__func__, current->pid);
		#endif

		blk_dump_rq_flags(req, "cyasblkdev bad request");
		return BLKPREP_KILL;
	}

	req->cmd_flags |= REQ_DONTPREP;

	return BLKPREP_OK;
}

/* queue worker thread */
static int cyasblkdev_queue_thread(void *d)
{
	DECLARE_WAITQUEUE(wait, current);
	struct cyasblkdev_queue *bq = d;
	struct request_queue *q = bq->queue;
	u32 qth_pid;

	DBGPRN_FUNC_NAME;

	/*
	 * set iothread to ensure that we aren't put to sleep by
	 * the process freezing.  we handle suspension ourselves.
	 */
	daemonize("cyasblkdev_queue_thread");

	/* signal to queue_init() so it could contnue */
	complete(&bq->thread_complete);

	down(&bq->thread_sem);
	add_wait_queue(&bq->thread_wq, &wait);

	qth_pid = current->pid;

	#ifndef WESTBRIDGE_NDEBUG
	cy_as_hal_print_message(
		"%s:%x started, bq:%p, q:%p\n", __func__, qth_pid, bq, q);
	#endif

	do {
		struct request *req = NULL;

		/* the thread wants to be woken up by signals as well */
		set_current_state(TASK_INTERRUPTIBLE);

		spin_lock_irq(q->queue_lock);

		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message(
			"%s: for bq->queue is null\n", __func__);
		#endif

		if (!bq->req) {
			/* chk if queue is plugged */
			if (!blk_queue_plugged(q)) {
				bq->req = req = blk_fetch_request(q);
				#ifndef WESTBRIDGE_NDEBUG
				cy_as_hal_print_message(
					"%s: blk_fetch_request:%x\n",
					__func__, (uint32_t)req);
				#endif
			} else {
				#ifndef WESTBRIDGE_NDEBUG
				cy_as_hal_print_message(
					"%s: queue plugged, "
					"skip blk_fetch()\n", __func__);
				#endif
			}
		}
		spin_unlock_irq(q->queue_lock);

		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message(
			"%s: checking if request queue is null\n", __func__);
		#endif

		if (!req) {
			if (bq->flags & CYASBLKDEV_QUEUE_EXIT) {
				#ifndef WESTBRIDGE_NDEBUG
				cy_as_hal_print_message(
					"%s:got QUEUE_EXIT flag\n", __func__);
				#endif

				break;
			}

			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message(
				"%s: request queue is null, goto sleep, "
				"thread_sem->count=%d\n",
				__func__, bq->thread_sem.count);
			if (spin_is_locked(q->queue_lock)) {
				cy_as_hal_print_message("%s: queue_lock "
				"is locked, need to release\n", __func__);
				spin_unlock(q->queue_lock);

				if (spin_is_locked(q->queue_lock))
					cy_as_hal_print_message(
						"%s: unlock did not work\n",
						__func__);
			} else {
				cy_as_hal_print_message(
					"%s: checked lock, is not locked\n",
					__func__);
			}
			#endif

			up(&bq->thread_sem);

			/* yields to the next rdytorun proc,
			 * then goes back to sleep*/
			schedule();
			down(&bq->thread_sem);

			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message(
				"%s: wake_up,continue\n",
				__func__);
			#endif
			continue;
		}

		/* new req recieved, issue it to the driver  */
		set_current_state(TASK_RUNNING);

		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message(
			"%s: issued a RQ:%x\n",
			__func__, (uint32_t)req);
		#endif

		bq->issue_fn(bq, req);

		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message(
			"%s: bq->issue_fn() returned\n",
			__func__);
		#endif


	} while (1);

	set_current_state(TASK_RUNNING);
	remove_wait_queue(&bq->thread_wq, &wait);
	up(&bq->thread_sem);

	complete_and_exit(&bq->thread_complete, 0);

	#ifndef WESTBRIDGE_NDEBUG
	cy_as_hal_print_message("%s: is finished\n", __func__);
	#endif

	return 0;
}

/*
 * Generic request handler. it is called for any queue on a
 * particular host.  When the host is not busy, we look for a request
 * on any queue on this host, and attempt to issue it.  This may
 * not be the queue we were asked to process.
 */
static void cyasblkdev_request(struct request_queue *q)
{
	struct cyasblkdev_queue *bq = q->queuedata;
	DBGPRN_FUNC_NAME;

	#ifndef WESTBRIDGE_NDEBUG
	cy_as_hal_print_message(
		"%s new request on cyasblkdev_queue_t bq:=%x\n",
		__func__, (uint32_t)bq);
	#endif

	if (!bq->req) {
		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message("%s wake_up(&bq->thread_wq)\n",
			__func__);
		#endif

		/* wake up cyasblkdev_queue worker thread*/
		wake_up(&bq->thread_wq);
	} else {
		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message("%s: don't wake Q_thr, bq->req:%x\n",
			__func__, (uint32_t)bq->req);
		#endif
	}
}

/*
 * cyasblkdev_init_queue - initialise a queue structure.
 * @bq: cyasblkdev queue
 * @dev:  CyAsDeviceHandle to attach this queue
 * @lock: queue lock
 *
 * Initialise a cyasblkdev_request queue.
 */

/* MAX NUMBER OF SECTORS PER REQUEST **/
#define Q_MAX_SECTORS 128

/* MAX NUMBER OF PHYS SEGMENTS (entries in the SG list)*/
#define Q_MAX_SGS   16

int cyasblkdev_init_queue(struct cyasblkdev_queue *bq, spinlock_t *lock)
{
	int ret;

	DBGPRN_FUNC_NAME;

	/* 1st param is a function that wakes up the queue thread */
	bq->queue = blk_init_queue(cyasblkdev_request, lock);
	if (!bq->queue)
		return -ENOMEM;

	blk_queue_prep_rq(bq->queue, cyasblkdev_prep_request);

	blk_queue_bounce_limit(bq->queue, BLK_BOUNCE_ANY);
	blk_queue_max_hw_sectors(bq->queue, Q_MAX_SECTORS);

	/* As of now, we have the HAL/driver support to
	 * merge scattered segments and handle them simultaneously.
	 * so, setting the max_phys_segments to 8. */
	/*blk_queue_max_phys_segments(bq->queue, Q_MAX_SGS);
	blk_queue_max_hw_segments(bq->queue, Q_MAX_SGS);*/
	blk_queue_max_segments(bq->queue, Q_MAX_SGS);

	/* should be < then HAL can handle */
	blk_queue_max_segment_size(bq->queue, 512*Q_MAX_SECTORS);

	bq->queue->queuedata = bq;
	bq->req = NULL;

	init_completion(&bq->thread_complete);
	init_waitqueue_head(&bq->thread_wq);
	sema_init(&bq->thread_sem, 1);

	ret = kernel_thread(cyasblkdev_queue_thread, bq, CLONE_KERNEL);
	if (ret >= 0) {
		/* wait until the thread is spawned */
		wait_for_completion(&bq->thread_complete);

		/* reinitialize the completion */
		init_completion(&bq->thread_complete);
		ret = 0;
		goto out;
	}

out:
	return ret;
}
EXPORT_SYMBOL(cyasblkdev_init_queue);

/*called from blk_put()  */
void cyasblkdev_cleanup_queue(struct cyasblkdev_queue *bq)
{
	DBGPRN_FUNC_NAME;

	bq->flags |= CYASBLKDEV_QUEUE_EXIT;
	wake_up(&bq->thread_wq);
	wait_for_completion(&bq->thread_complete);

	blk_cleanup_queue(bq->queue);
}
EXPORT_SYMBOL(cyasblkdev_cleanup_queue);


/**
 * cyasblkdev_queue_suspend - suspend a CyAsBlkDev request queue
 * @bq: CyAsBlkDev queue to suspend
 *
 * Stop the block request queue, and wait for our thread to
 * complete any outstanding requests.  This ensures that we
 * won't suspend while a request is being processed.
 */
void cyasblkdev_queue_suspend(struct cyasblkdev_queue *bq)
{
	struct request_queue *q = bq->queue;
	unsigned long flags;

	DBGPRN_FUNC_NAME;

	if (!(bq->flags & CYASBLKDEV_QUEUE_SUSPENDED)) {
		bq->flags |= CYASBLKDEV_QUEUE_SUSPENDED;

		spin_lock_irqsave(q->queue_lock, flags);
		blk_stop_queue(q);
		spin_unlock_irqrestore(q->queue_lock, flags);

		down(&bq->thread_sem);
	}
}
EXPORT_SYMBOL(cyasblkdev_queue_suspend);

/*cyasblkdev_queue_resume - resume a previously suspended
 * CyAsBlkDev request queue @bq: CyAsBlkDev queue to resume */
void cyasblkdev_queue_resume(struct cyasblkdev_queue *bq)
{
	struct request_queue *q = bq->queue;
	unsigned long flags;

	DBGPRN_FUNC_NAME;

	if (bq->flags & CYASBLKDEV_QUEUE_SUSPENDED)  {
		bq->flags &= ~CYASBLKDEV_QUEUE_SUSPENDED;

		up(&bq->thread_sem);

		spin_lock_irqsave(q->queue_lock, flags);
		blk_start_queue(q);
		spin_unlock_irqrestore(q->queue_lock, flags);
	}
}
EXPORT_SYMBOL(cyasblkdev_queue_resume);

/*[]*/