block: replace bi_bdev with a gendisk pointer and partitions index
[linux-block.git] / arch / powerpc / sysdev / axonram.c
CommitLineData
dbdf04c4
MS
1/*
2 * (C) Copyright IBM Deutschland Entwicklung GmbH 2006
3 *
4 * Author: Maxim Shchetynin <maxim@de.ibm.com>
5 *
6 * Axon DDR2 device driver.
7 * It registers one block device per Axon's DDR2 memory bank found on a system.
8 * Block devices are called axonram?, their major and minor numbers are
9 * available in /proc/devices, /proc/partitions or in /sys/block/axonram?/dev.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26#include <linux/bio.h>
27#include <linux/blkdev.h>
60fcd55c 28#include <linux/dax.h>
dbdf04c4
MS
29#include <linux/device.h>
30#include <linux/errno.h>
31#include <linux/fs.h>
32#include <linux/genhd.h>
33#include <linux/interrupt.h>
34#include <linux/io.h>
35#include <linux/ioport.h>
36#include <linux/irq.h>
37#include <linux/irqreturn.h>
38#include <linux/kernel.h>
39#include <linux/mm.h>
40#include <linux/mod_devicetable.h>
41#include <linux/module.h>
42#include <linux/slab.h>
43#include <linux/string.h>
44#include <linux/types.h>
eb8f2763
JL
45#include <linux/of_device.h>
46#include <linux/of_platform.h>
34c0fd54 47#include <linux/pfn_t.h>
5d61e43b 48#include <linux/uio.h>
eb8f2763 49
dbdf04c4
MS
50#include <asm/page.h>
51#include <asm/prom.h>
52
53#define AXON_RAM_MODULE_NAME "axonram"
54#define AXON_RAM_DEVICE_NAME "axonram"
55#define AXON_RAM_MINORS_PER_DISK 16
56#define AXON_RAM_BLOCK_SHIFT PAGE_SHIFT
57#define AXON_RAM_BLOCK_SIZE 1 << AXON_RAM_BLOCK_SHIFT
58#define AXON_RAM_SECTOR_SHIFT 9
59#define AXON_RAM_SECTOR_SIZE 1 << AXON_RAM_SECTOR_SHIFT
60#define AXON_RAM_IRQ_FLAGS IRQF_SHARED | IRQF_TRIGGER_RISING
61
9a23409b
MS
62static int azfs_major, azfs_minor;
63
dbdf04c4 64struct axon_ram_bank {
a454dc50 65 struct platform_device *device;
dbdf04c4 66 struct gendisk *disk;
60fcd55c 67 struct dax_device *dax_dev;
fedcd2c5 68 unsigned int irq_id;
dbdf04c4
MS
69 unsigned long ph_addr;
70 unsigned long io_addr;
71 unsigned long size;
72 unsigned long ecc_counter;
73};
74
75static ssize_t
76axon_ram_sysfs_ecc(struct device *dev, struct device_attribute *attr, char *buf)
77{
a454dc50 78 struct platform_device *device = to_platform_device(dev);
dbdf04c4
MS
79 struct axon_ram_bank *bank = device->dev.platform_data;
80
81 BUG_ON(!bank);
82
83 return sprintf(buf, "%ld\n", bank->ecc_counter);
84}
85
86static DEVICE_ATTR(ecc, S_IRUGO, axon_ram_sysfs_ecc, NULL);
87
88/**
89 * axon_ram_irq_handler - interrupt handler for Axon RAM ECC
90 * @irq: interrupt ID
91 * @dev: pointer to of_device
92 */
93static irqreturn_t
94axon_ram_irq_handler(int irq, void *dev)
95{
a454dc50 96 struct platform_device *device = dev;
dbdf04c4
MS
97 struct axon_ram_bank *bank = device->dev.platform_data;
98
99 BUG_ON(!bank);
100
25985edc 101 dev_err(&device->dev, "Correctable memory error occurred\n");
fedcd2c5
MS
102 bank->ecc_counter++;
103 return IRQ_HANDLED;
dbdf04c4
MS
104}
105
106/**
107 * axon_ram_make_request - make_request() method for block device
108 * @queue, @bio: see blk_queue_make_request()
109 */
dece1635 110static blk_qc_t
dbdf04c4
MS
111axon_ram_make_request(struct request_queue *queue, struct bio *bio)
112{
74d46992 113 struct axon_ram_bank *bank = bio->bi_disk->private_data;
dbdf04c4
MS
114 unsigned long phys_mem, phys_end;
115 void *user_mem;
7988613b 116 struct bio_vec vec;
dbdf04c4 117 unsigned int transfered;
7988613b 118 struct bvec_iter iter;
dbdf04c4 119
4f024f37
KO
120 phys_mem = bank->io_addr + (bio->bi_iter.bi_sector <<
121 AXON_RAM_SECTOR_SHIFT);
dbdf04c4
MS
122 phys_end = bank->io_addr + bank->size;
123 transfered = 0;
7988613b
KO
124 bio_for_each_segment(vec, bio, iter) {
125 if (unlikely(phys_mem + vec.bv_len > phys_end)) {
d81fec0f 126 bio_io_error(bio);
dece1635 127 return BLK_QC_T_NONE;
dbdf04c4
MS
128 }
129
7988613b 130 user_mem = page_address(vec.bv_page) + vec.bv_offset;
dbdf04c4 131 if (bio_data_dir(bio) == READ)
7988613b 132 memcpy(user_mem, (void *) phys_mem, vec.bv_len);
dbdf04c4 133 else
7988613b 134 memcpy((void *) phys_mem, user_mem, vec.bv_len);
dbdf04c4 135
7988613b
KO
136 phys_mem += vec.bv_len;
137 transfered += vec.bv_len;
dbdf04c4 138 }
4246a0b6 139 bio_endio(bio);
dece1635 140 return BLK_QC_T_NONE;
dbdf04c4
MS
141}
142
d4b29fd7
DW
143static const struct block_device_operations axon_ram_devops = {
144 .owner = THIS_MODULE,
145};
146
60fcd55c
DW
147static long
148__axon_ram_direct_access(struct axon_ram_bank *bank, pgoff_t pgoff, long nr_pages,
149 void **kaddr, pfn_t *pfn)
150{
151 resource_size_t offset = pgoff * PAGE_SIZE;
152
153 *kaddr = (void *) bank->io_addr + offset;
154 *pfn = phys_to_pfn_t(bank->ph_addr + offset, PFN_DEV);
155 return (bank->size - offset) / PAGE_SIZE;
156}
157
60fcd55c
DW
158static long
159axon_ram_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
160 void **kaddr, pfn_t *pfn)
161{
162 struct axon_ram_bank *bank = dax_get_private(dax_dev);
163
164 return __axon_ram_direct_access(bank, pgoff, nr_pages, kaddr, pfn);
165}
166
5d61e43b
DW
167static size_t axon_ram_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
168 void *addr, size_t bytes, struct iov_iter *i)
169{
170 return copy_from_iter(addr, bytes, i);
171}
172
60fcd55c
DW
173static const struct dax_operations axon_ram_dax_ops = {
174 .direct_access = axon_ram_dax_direct_access,
5d61e43b 175 .copy_from_iter = axon_ram_copy_from_iter,
dbdf04c4
MS
176};
177
178/**
179 * axon_ram_probe - probe() method for platform driver
00006124 180 * @device: see platform_driver method
dbdf04c4 181 */
00006124 182static int axon_ram_probe(struct platform_device *device)
dbdf04c4
MS
183{
184 static int axon_ram_bank_id = -1;
185 struct axon_ram_bank *bank;
186 struct resource resource;
187 int rc = 0;
188
189 axon_ram_bank_id++;
190
191 dev_info(&device->dev, "Found memory controller on %s\n",
61c7a080 192 device->dev.of_node->full_name);
dbdf04c4
MS
193
194 bank = kzalloc(sizeof(struct axon_ram_bank), GFP_KERNEL);
195 if (bank == NULL) {
196 dev_err(&device->dev, "Out of memory\n");
197 rc = -ENOMEM;
198 goto failed;
199 }
200
201 device->dev.platform_data = bank;
202
203 bank->device = device;
204
61c7a080 205 if (of_address_to_resource(device->dev.of_node, 0, &resource) != 0) {
dbdf04c4
MS
206 dev_err(&device->dev, "Cannot access device tree\n");
207 rc = -EFAULT;
208 goto failed;
209 }
210
28f65c11 211 bank->size = resource_size(&resource);
dbdf04c4
MS
212
213 if (bank->size == 0) {
214 dev_err(&device->dev, "No DDR2 memory found for %s%d\n",
215 AXON_RAM_DEVICE_NAME, axon_ram_bank_id);
216 rc = -ENODEV;
217 goto failed;
218 }
219
220 dev_info(&device->dev, "Register DDR2 memory device %s%d with %luMB\n",
221 AXON_RAM_DEVICE_NAME, axon_ram_bank_id, bank->size >> 20);
222
223 bank->ph_addr = resource.start;
40f1ce7f 224 bank->io_addr = (unsigned long) ioremap_prot(
dbdf04c4
MS
225 bank->ph_addr, bank->size, _PAGE_NO_CACHE);
226 if (bank->io_addr == 0) {
227 dev_err(&device->dev, "ioremap() failed\n");
228 rc = -EFAULT;
229 goto failed;
230 }
231
232 bank->disk = alloc_disk(AXON_RAM_MINORS_PER_DISK);
233 if (bank->disk == NULL) {
234 dev_err(&device->dev, "Cannot register disk\n");
235 rc = -EFAULT;
236 goto failed;
237 }
238
60fcd55c 239
9a23409b
MS
240 bank->disk->major = azfs_major;
241 bank->disk->first_minor = azfs_minor;
dbdf04c4
MS
242 bank->disk->fops = &axon_ram_devops;
243 bank->disk->private_data = bank;
dbdf04c4
MS
244
245 sprintf(bank->disk->disk_name, "%s%d",
246 AXON_RAM_DEVICE_NAME, axon_ram_bank_id);
dbdf04c4 247
60fcd55c
DW
248 bank->dax_dev = alloc_dax(bank, bank->disk->disk_name,
249 &axon_ram_dax_ops);
250 if (!bank->dax_dev) {
251 rc = -ENOMEM;
252 goto failed;
253 }
254
dbdf04c4
MS
255 bank->disk->queue = blk_alloc_queue(GFP_KERNEL);
256 if (bank->disk->queue == NULL) {
257 dev_err(&device->dev, "Cannot register disk queue\n");
258 rc = -EFAULT;
259 goto failed;
260 }
261
262 set_capacity(bank->disk, bank->size >> AXON_RAM_SECTOR_SHIFT);
263 blk_queue_make_request(bank->disk->queue, axon_ram_make_request);
e1defc4f 264 blk_queue_logical_block_size(bank->disk->queue, AXON_RAM_SECTOR_SIZE);
0d52c756 265 device_add_disk(&device->dev, bank->disk);
dbdf04c4 266
61c7a080 267 bank->irq_id = irq_of_parse_and_map(device->dev.of_node, 0);
ef24ba70 268 if (!bank->irq_id) {
dbdf04c4
MS
269 dev_err(&device->dev, "Cannot access ECC interrupt ID\n");
270 rc = -EFAULT;
271 goto failed;
272 }
273
fedcd2c5 274 rc = request_irq(bank->irq_id, axon_ram_irq_handler,
dbdf04c4
MS
275 AXON_RAM_IRQ_FLAGS, bank->disk->disk_name, device);
276 if (rc != 0) {
277 dev_err(&device->dev, "Cannot register ECC interrupt handler\n");
ef24ba70 278 bank->irq_id = 0;
dbdf04c4
MS
279 rc = -EFAULT;
280 goto failed;
281 }
282
283 rc = device_create_file(&device->dev, &dev_attr_ecc);
284 if (rc != 0) {
285 dev_err(&device->dev, "Cannot create sysfs file\n");
286 rc = -EFAULT;
287 goto failed;
288 }
289
9a23409b
MS
290 azfs_minor += bank->disk->minors;
291
dbdf04c4
MS
292 return 0;
293
294failed:
295 if (bank != NULL) {
ef24ba70 296 if (bank->irq_id)
fedcd2c5 297 free_irq(bank->irq_id, device);
dbdf04c4 298 if (bank->disk != NULL) {
dbdf04c4
MS
299 if (bank->disk->major > 0)
300 unregister_blkdev(bank->disk->major,
301 bank->disk->disk_name);
672a2c87
JK
302 if (bank->disk->flags & GENHD_FL_UP)
303 del_gendisk(bank->disk);
304 put_disk(bank->disk);
dbdf04c4 305 }
60fcd55c
DW
306 kill_dax(bank->dax_dev);
307 put_dax(bank->dax_dev);
dbdf04c4
MS
308 device->dev.platform_data = NULL;
309 if (bank->io_addr != 0)
310 iounmap((void __iomem *) bank->io_addr);
311 kfree(bank);
312 }
313
314 return rc;
315}
316
317/**
318 * axon_ram_remove - remove() method for platform driver
319 * @device: see of_platform_driver method
320 */
321static int
a454dc50 322axon_ram_remove(struct platform_device *device)
dbdf04c4
MS
323{
324 struct axon_ram_bank *bank = device->dev.platform_data;
325
326 BUG_ON(!bank || !bank->disk);
327
328 device_remove_file(&device->dev, &dev_attr_ecc);
fedcd2c5 329 free_irq(bank->irq_id, device);
60fcd55c
DW
330 kill_dax(bank->dax_dev);
331 put_dax(bank->dax_dev);
dbdf04c4 332 del_gendisk(bank->disk);
672a2c87 333 put_disk(bank->disk);
dbdf04c4
MS
334 iounmap((void __iomem *) bank->io_addr);
335 kfree(bank);
336
337 return 0;
338}
339
ce6d73c9 340static const struct of_device_id axon_ram_device_id[] = {
dbdf04c4
MS
341 {
342 .type = "dma-memory"
343 },
344 {}
345};
b4f81445 346MODULE_DEVICE_TABLE(of, axon_ram_device_id);
dbdf04c4 347
00006124 348static struct platform_driver axon_ram_driver = {
dbdf04c4 349 .probe = axon_ram_probe,
84dd4676 350 .remove = axon_ram_remove,
4018294b
GL
351 .driver = {
352 .name = AXON_RAM_MODULE_NAME,
4018294b 353 .of_match_table = axon_ram_device_id,
84dd4676 354 },
dbdf04c4
MS
355};
356
357/**
358 * axon_ram_init
359 */
360static int __init
361axon_ram_init(void)
362{
9a23409b
MS
363 azfs_major = register_blkdev(azfs_major, AXON_RAM_DEVICE_NAME);
364 if (azfs_major < 0) {
365 printk(KERN_ERR "%s cannot become block device major number\n",
366 AXON_RAM_MODULE_NAME);
367 return -EFAULT;
368 }
369 azfs_minor = 0;
370
00006124 371 return platform_driver_register(&axon_ram_driver);
dbdf04c4
MS
372}
373
374/**
375 * axon_ram_exit
376 */
377static void __exit
378axon_ram_exit(void)
379{
00006124 380 platform_driver_unregister(&axon_ram_driver);
9a23409b 381 unregister_blkdev(azfs_major, AXON_RAM_DEVICE_NAME);
dbdf04c4
MS
382}
383
384module_init(axon_ram_init);
385module_exit(axon_ram_exit);
386
387MODULE_LICENSE("GPL");
388MODULE_AUTHOR("Maxim Shchetynin <maxim@de.ibm.com>");
389MODULE_DESCRIPTION("Axon DDR2 RAM device driver for IBM Cell BE");