aboutsummaryrefslogtreecommitdiff
path: root/drivers/mtd/nand/lba/lba-blk.c
blob: 228aa9d27760eb2f338d60b44663ffc0ff36b233 (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
/*
 * Freescale STMP37XX/STMP378X LBA/block driver
 *
 * Author: Dmitrij Frasenyak <sed@embeddedalley.com>
 *
 * Copyright 2009 Freescale Semiconductor, Inc. All Rights Reserved.
 * Copyright 2009 Embedded Alley Solutions, Inc All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>

#include <linux/sched.h>
#include <linux/kernel.h>	/* printk() */
#include <linux/slab.h>		/* kmalloc() */
#include <linux/fs.h>		/* everything... */
#include <linux/errno.h>	/* error codes */
#include <linux/kthread.h>
#include <linux/timer.h>
#include <linux/types.h>	/* size_t */
#include <linux/fcntl.h>	/* O_ACCMODE */
#include <linux/hdreg.h>	/* HDIO_GETGEO */
#include <linux/kdev_t.h>
#include <linux/vmalloc.h>
#include <linux/genhd.h>
#include <linux/blkdev.h>
#include <linux/buffer_head.h>	/* invalidate_bdev */
#include <linux/bio.h>
#include <linux/dma-mapping.h>
#include <linux/hdreg.h>
#include <linux/blkdev.h>
#include "lba.h"

static int lba_major;

#define LBA_NAME "lba"

#if 0
#define TAG() printk(KERNE_ERR "%s: %d\n", __func__, __LINE__)
#else
#define TAG()
#endif

/*
 * The internal representation of our device.
 */
struct lba_blk_dev {
	int size;                       /* Device size in sectors */
	spinlock_t lock;                /* For mutual exclusion */
	int users;
	struct request_queue *queue;    /* The device request queue */
	struct gendisk *gd;             /* The gendisk structure */
	struct lba_data *data;          /* pointer from lba core */

	struct task_struct	*thread;
	struct bio 		*bio_head;
	struct bio		*bio_tail;
	wait_queue_head_t	wait_q;
	struct semaphore        busy;

};

static struct lba_blk_dev *g_lba_blk;

static void blk_add_bio(struct lba_blk_dev *dev, struct bio *bio);


/*
 * Transfer a single BIO.
 */
static int lba_blk_xfer_bio(struct lba_blk_dev *dev, struct bio *bio)
{
	int i;
	struct bio_vec *bvec;
	sector_t sector = bio->bi_sector;
	enum dma_data_direction dir;
	int status = 0;
	int (*lba_xfer)(void *priv,
			unsigned int sector,
			unsigned int count,
			void *buffer,
			dma_addr_t handle);

	if  (bio_data_dir(bio) == WRITE) {
		lba_xfer = lba_write_sectors;
		dir = DMA_TO_DEVICE;
	} else {
		lba_xfer = lba_read_sectors;
		dir = DMA_FROM_DEVICE;
	}

	/* Fixme: merge segments */
	bio_for_each_segment(bvec, bio, i) {
		void *buffer = page_address(bvec->bv_page);
		dma_addr_t handle ;
		if (!buffer)
			BUG();
		buffer += bvec->bv_offset;
		handle = dma_map_single(&dev->data->nand->dev->dev,
					    buffer,
					    bvec->bv_len,
					    dir);
		status = lba_xfer(dev->data->nand, sector,
				  bvec->bv_len >> 9,
				  buffer,
				  handle);

		dma_unmap_single(&dev->data->nand->dev->dev,
				 handle,
				 bvec->bv_len,
				 dir);
		if (status)
			break;

		sector += bio_cur_bytes(bio) >> 9;
	}

	return status;
}


/*
 * The direct make request version.
 */
static int lba_make_request(struct request_queue *q, struct bio *bio)
{
	struct lba_blk_dev *dev = q->queuedata;

	blk_add_bio(dev, bio);
	return 0;
}

/*
 * Open and close.
 */

static int lba_blk_open(struct block_device *bdev, fmode_t mode)
{
	struct lba_blk_dev *dev = bdev->bd_disk->private_data;

	TAG();

	spin_lock_irq(&dev->lock);
	dev->users++;
	spin_unlock_irq(&dev->lock);
	return 0;
}


static int lba_blk_release(struct gendisk *gd, fmode_t mode)
{
	struct lba_blk_dev *dev = gd->private_data;

	spin_lock(&dev->lock);
	dev->users--;
	spin_unlock(&dev->lock);

	return 0;
}

static int lba_getgeo(struct block_device *bdev, struct hd_geometry *geo)
{
	/*
	 * get geometry: we have to fake one...  trim the size to a
	 * multiple of 2048 (1M): tell we have 32 sectors, 64 heads,
	 * whatever cylinders.
	 */
	geo->heads = 1 << 6;
	geo->sectors = 1 << 5;
	geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
	return 0;
}

/*
 * Add bio to back of pending list
 */
static void blk_add_bio(struct lba_blk_dev *dev, struct bio *bio)
{
	unsigned long flags;
	spin_lock_irqsave(&dev->lock, flags);
	if (dev->bio_tail) {
		dev->bio_tail->bi_next = bio;
		dev->bio_tail = bio;
	} else
		dev->bio_head = dev->bio_tail = bio;
	wake_up(&dev->wait_q);
	spin_unlock_irqrestore(&dev->lock, flags);
}

/*
 * Grab first pending buffer
 */
static struct bio *blk_get_bio(struct lba_blk_dev *dev)
{
	struct bio *bio;
	unsigned long flags;

	spin_lock_irqsave(&dev->lock, flags);
	bio = dev->bio_head;
	if (bio) {
		if (bio == dev->bio_tail) {
			dev->bio_tail = NULL;
			dev->bio_head = NULL;
		}
		dev->bio_head = bio->bi_next;
		bio->bi_next = NULL;
	}
	spin_unlock_irqrestore(&dev->lock, flags);

	return bio;
}

static int lba_thread(void *data)
{
	struct lba_blk_dev *dev = data;
	struct bio *bio;
	int status;

	set_user_nice(current, -20);

	while (!kthread_should_stop() || dev->bio_head) {

		wait_event_interruptible(dev->wait_q,
				dev->bio_head || kthread_should_stop());

		if (!dev->bio_head)
			continue;

		if (lba_core_lock_mode(dev->data, LBA_MODE_MDP))
			continue;

		bio = blk_get_bio(dev);
		status = lba_blk_xfer_bio(dev, bio);
		bio_endio(bio,  status);

		lba_core_unlock_mode(dev->data);
	}

	return 0;
}



/*
 * The device operations structure.
 */
static struct block_device_operations lba_blk_ops = {
	.owner           = THIS_MODULE,
	.open 	         = lba_blk_open,
	.release 	 = lba_blk_release,
	.getgeo		=  lba_getgeo,
};


int lba_blk_init(struct lba_data *data)
{

	struct lba_blk_dev *dev;
	int err;
	if (!data)
		BUG();

	printk(KERN_INFO "LBA block driver v0.1\n");
	lba_major = LBA_MAJOR;
	dev = g_lba_blk =  kzalloc(sizeof(struct lba_blk_dev), GFP_KERNEL);
	if (!dev)
		return -ENOMEM;

	dev->data = data;
	register_blkdev(lba_major, "lba");

	spin_lock_init(&dev->lock);
	init_waitqueue_head(&dev->wait_q);
	sema_init(&dev->busy, 1);

	dev->queue = blk_alloc_queue(GFP_KERNEL);
	if (!dev->queue)
		goto out2;
	blk_queue_make_request(dev->queue, lba_make_request);
	/*dev->queue->unplug_fn = lba_unplug_device;*/
	blk_queue_logical_block_size(dev->queue, 512);

	dev->queue->queuedata = dev;
	dev->gd = alloc_disk(32);
	if (!dev->gd) {
		printk(KERN_ERR "failed to alloc disk\n");
		goto out3;
	}
	dev->size = data->mdp_size  ;
	printk(KERN_INFO "%s: set capacity of the device to 0x%x\n",
	       __func__, dev->size);
	dev->gd->major = lba_major;
	dev->gd->first_minor = 0;
	dev->gd->fops = &lba_blk_ops;
	dev->gd->queue = dev->queue;
	dev->gd->private_data = dev;
	snprintf(dev->gd->disk_name, 8, LBA_NAME);
	set_capacity(dev->gd, dev->size);

	dev->thread = kthread_create(lba_thread, dev, "lba-%d", 1);
	if (IS_ERR(dev->thread)) {
		err = PTR_ERR(dev->thread);
		goto out3;
	}
	wake_up_process(dev->thread);

	add_disk(dev->gd);


	TAG();

	return 0;
out3:
out2:
	unregister_blkdev(lba_major, "lba");
	return -ENOMEM;
}

int lba_blk_remove(struct lba_data *data)
{

	struct lba_blk_dev *dev = g_lba_blk;

	del_gendisk(dev->gd);
	kthread_stop(dev->thread);
	blk_cleanup_queue(dev->queue);
	put_disk(dev->gd);

	unregister_blkdev(lba_major, LBA_NAME);
	kfree(dev);
	return 0;
}

MODULE_LICENSE("GPL");
MODULE_ALIAS_BLOCKDEV_MAJOR(254);