blob: dab4f1afeae9cb09c2b8dae9131fc8aee01a49c7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mm.c - Micro Memory(tm) PCI memory board block device driver - v2.3
3 *
4 * (C) 2001 San Mehat <nettwerk@valinux.com>
5 * (C) 2001 Johannes Erdfelt <jerdfelt@valinux.com>
6 * (C) 2001 NeilBrown <neilb@cse.unsw.edu.au>
7 *
8 * This driver for the Micro Memory PCI Memory Module with Battery Backup
9 * is Copyright Micro Memory Inc 2001-2002. All rights reserved.
10 *
11 * This driver is released to the public under the terms of the
12 * GNU GENERAL PUBLIC LICENSE version 2
13 * See the file COPYING for details.
14 *
15 * This driver provides a standard block device interface for Micro Memory(tm)
16 * PCI based RAM boards.
17 * 10/05/01: Phap Nguyen - Rebuilt the driver
18 * 10/22/01: Phap Nguyen - v2.1 Added disk partitioning
19 * 29oct2001:NeilBrown - Use make_request_fn instead of request_fn
20 * - use stand disk partitioning (so fdisk works).
21 * 08nov2001:NeilBrown - change driver name from "mm" to "umem"
22 * - incorporate into main kernel
23 * 08apr2002:NeilBrown - Move some of interrupt handle to tasklet
24 * - use spin_lock_bh instead of _irq
25 * - Never block on make_request. queue
26 * bh's instead.
27 * - unregister umem from devfs at mod unload
28 * - Change version to 2.3
29 * 07Nov2001:Phap Nguyen - Select pci read command: 06, 12, 15 (Decimal)
30 * 07Jan2002: P. Nguyen - Used PCI Memory Write & Invalidate for DMA
31 * 15May2002:NeilBrown - convert to bio for 2.5
32 * 17May2002:NeilBrown - remove init_mem initialisation. Instead detect
33 * - a sequence of writes that cover the card, and
34 * - set initialised bit then.
35 */
36
Randy Dunlap458cf5e2007-12-17 20:24:20 +010037#undef DEBUG /* #define DEBUG if you want debugging info (pr_debug) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/fs.h>
39#include <linux/bio.h>
40#include <linux/kernel.h>
41#include <linux/mm.h>
42#include <linux/mman.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/ioctl.h>
45#include <linux/module.h>
46#include <linux/init.h>
47#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/timer.h>
49#include <linux/pci.h>
Matthias Gehre910638a2006-03-28 01:56:48 -080050#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#include <linux/fcntl.h> /* O_ACCMODE */
53#include <linux/hdreg.h> /* HDIO_GETGEO */
54
Jeff Garzik3084f0c2007-09-27 06:25:06 -040055#include "umem.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#include <asm/uaccess.h>
58#include <asm/io.h>
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define MM_MAXCARDS 4
61#define MM_RAHEAD 2 /* two sectors */
62#define MM_BLKSIZE 1024 /* 1k blocks */
63#define MM_HARDSECT 512 /* 512-byte hardware sectors */
64#define MM_SHIFT 6 /* max 64 partitions on 4 cards */
65
66/*
67 * Version Information
68 */
69
Jeff Garzikee4a7b62007-09-27 07:40:33 -040070#define DRIVER_NAME "umem"
71#define DRIVER_VERSION "v2.3"
72#define DRIVER_AUTHOR "San Mehat, Johannes Erdfelt, NeilBrown"
73#define DRIVER_DESC "Micro Memory(tm) PCI memory board block driver"
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75static int debug;
76/* #define HW_TRACE(x) writeb(x,cards[0].csr_remap + MEMCTRLSTATUS_MAGIC) */
77#define HW_TRACE(x)
78
79#define DEBUG_LED_ON_TRANSFER 0x01
80#define DEBUG_BATTERY_POLLING 0x02
81
82module_param(debug, int, 0644);
83MODULE_PARM_DESC(debug, "Debug bitmask");
84
85static int pci_read_cmd = 0x0C; /* Read Multiple */
86module_param(pci_read_cmd, int, 0);
87MODULE_PARM_DESC(pci_read_cmd, "PCI read command");
88
89static int pci_write_cmd = 0x0F; /* Write and Invalidate */
90module_param(pci_write_cmd, int, 0);
91MODULE_PARM_DESC(pci_write_cmd, "PCI write command");
92
93static int pci_cmds;
94
95static int major_nr;
96
97#include <linux/blkdev.h>
98#include <linux/blkpg.h>
99
100struct cardinfo {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 struct pci_dev *dev;
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unsigned char __iomem *csr_remap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 unsigned int mm_size; /* size in kbytes */
105
106 unsigned int init_size; /* initial segment, in sectors,
107 * that we know to
108 * have been written
109 */
110 struct bio *bio, *currentbio, **biotail;
NeilBrowneea9bef2007-08-16 13:31:26 +0200111 int current_idx;
112 sector_t current_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Jens Axboe165125e2007-07-24 09:28:11 +0200114 struct request_queue *queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 struct mm_page {
117 dma_addr_t page_dma;
118 struct mm_dma_desc *desc;
119 int cnt, headcnt;
120 struct bio *bio, **biotail;
NeilBrowneea9bef2007-08-16 13:31:26 +0200121 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 } mm_pages[2];
123#define DESC_PER_PAGE ((PAGE_SIZE*2)/sizeof(struct mm_dma_desc))
124
125 int Active, Ready;
126
127 struct tasklet_struct tasklet;
128 unsigned int dma_status;
129
130 struct {
131 int good;
132 int warned;
133 unsigned long last_change;
134 } battery[2];
135
136 spinlock_t lock;
137 int check_batteries;
138
139 int flags;
140};
141
142static struct cardinfo cards[MM_MAXCARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static struct timer_list battery_timer;
144
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100145static int num_cards;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147static struct gendisk *mm_gendisk[MM_MAXCARDS];
148
149static void check_batteries(struct cardinfo *card);
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static int get_userbit(struct cardinfo *card, int bit)
152{
153 unsigned char led;
154
155 led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
156 return led & bit;
157}
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static int set_userbit(struct cardinfo *card, int bit, unsigned char state)
160{
161 unsigned char led;
162
163 led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
164 if (state)
165 led |= bit;
166 else
167 led &= ~bit;
168 writeb(led, card->csr_remap + MEMCTRLCMD_LEDCTRL);
169
170 return 0;
171}
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/*
174 * NOTE: For the power LED, use the LED_POWER_* macros since they differ
175 */
176static void set_led(struct cardinfo *card, int shift, unsigned char state)
177{
178 unsigned char led;
179
180 led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
181 if (state == LED_FLIP)
182 led ^= (1<<shift);
183 else {
184 led &= ~(0x03 << shift);
185 led |= (state << shift);
186 }
187 writeb(led, card->csr_remap + MEMCTRLCMD_LEDCTRL);
188
189}
190
191#ifdef MM_DIAG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192static void dump_regs(struct cardinfo *card)
193{
194 unsigned char *p;
195 int i, i1;
196
197 p = card->csr_remap;
198 for (i = 0; i < 8; i++) {
199 printk(KERN_DEBUG "%p ", p);
200
201 for (i1 = 0; i1 < 16; i1++)
202 printk("%02x ", *p++);
203
204 printk("\n");
205 }
206}
207#endif
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209static void dump_dmastat(struct cardinfo *card, unsigned int dmastat)
210{
Jeff Garzik4e0af882007-09-27 06:41:25 -0400211 dev_printk(KERN_DEBUG, &card->dev->dev, "DMAstat - ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (dmastat & DMASCR_ANY_ERR)
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100213 printk(KERN_CONT "ANY_ERR ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (dmastat & DMASCR_MBE_ERR)
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100215 printk(KERN_CONT "MBE_ERR ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (dmastat & DMASCR_PARITY_ERR_REP)
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100217 printk(KERN_CONT "PARITY_ERR_REP ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (dmastat & DMASCR_PARITY_ERR_DET)
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100219 printk(KERN_CONT "PARITY_ERR_DET ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (dmastat & DMASCR_SYSTEM_ERR_SIG)
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100221 printk(KERN_CONT "SYSTEM_ERR_SIG ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (dmastat & DMASCR_TARGET_ABT)
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100223 printk(KERN_CONT "TARGET_ABT ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (dmastat & DMASCR_MASTER_ABT)
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100225 printk(KERN_CONT "MASTER_ABT ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (dmastat & DMASCR_CHAIN_COMPLETE)
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100227 printk(KERN_CONT "CHAIN_COMPLETE ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (dmastat & DMASCR_DMA_COMPLETE)
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100229 printk(KERN_CONT "DMA_COMPLETE ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 printk("\n");
231}
232
233/*
234 * Theory of request handling
235 *
236 * Each bio is assigned to one mm_dma_desc - which may not be enough FIXME
237 * We have two pages of mm_dma_desc, holding about 64 descriptors
238 * each. These are allocated at init time.
239 * One page is "Ready" and is either full, or can have request added.
240 * The other page might be "Active", which DMA is happening on it.
241 *
242 * Whenever IO on the active page completes, the Ready page is activated
243 * and the ex-Active page is clean out and made Ready.
Jens Axboe7eaceac2011-03-10 08:52:07 +0100244 * Otherwise the Ready page is only activated when it becomes full.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 *
246 * If a request arrives while both pages a full, it is queued, and b_rdev is
247 * overloaded to record whether it was a read or a write.
248 *
249 * The interrupt handler only polls the device to clear the interrupt.
250 * The processing of the result is done in a tasklet.
251 */
252
253static void mm_start_io(struct cardinfo *card)
254{
255 /* we have the lock, we know there is
256 * no IO active, and we know that card->Active
257 * is set
258 */
259 struct mm_dma_desc *desc;
260 struct mm_page *page;
261 int offset;
262
263 /* make the last descriptor end the chain */
264 page = &card->mm_pages[card->Active];
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100265 pr_debug("start_io: %d %d->%d\n",
266 card->Active, page->headcnt, page->cnt - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 desc = &page->desc[page->cnt-1];
268
269 desc->control_bits |= cpu_to_le32(DMASCR_CHAIN_COMP_EN);
270 desc->control_bits &= ~cpu_to_le32(DMASCR_CHAIN_EN);
271 desc->sem_control_bits = desc->control_bits;
272
Jeff Garzik4e953a22007-09-27 07:41:50 -0400273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (debug & DEBUG_LED_ON_TRANSFER)
275 set_led(card, LED_REMOVE, LED_ON);
276
277 desc = &page->desc[page->headcnt];
278 writel(0, card->csr_remap + DMA_PCI_ADDR);
279 writel(0, card->csr_remap + DMA_PCI_ADDR + 4);
280
281 writel(0, card->csr_remap + DMA_LOCAL_ADDR);
282 writel(0, card->csr_remap + DMA_LOCAL_ADDR + 4);
283
284 writel(0, card->csr_remap + DMA_TRANSFER_SIZE);
285 writel(0, card->csr_remap + DMA_TRANSFER_SIZE + 4);
286
287 writel(0, card->csr_remap + DMA_SEMAPHORE_ADDR);
288 writel(0, card->csr_remap + DMA_SEMAPHORE_ADDR + 4);
289
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100290 offset = ((char *)desc) - ((char *)page->desc);
291 writel(cpu_to_le32((page->page_dma+offset) & 0xffffffff),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 card->csr_remap + DMA_DESCRIPTOR_ADDR);
293 /* Force the value to u64 before shifting otherwise >> 32 is undefined C
294 * and on some ports will do nothing ! */
295 writel(cpu_to_le32(((u64)page->page_dma)>>32),
296 card->csr_remap + DMA_DESCRIPTOR_ADDR + 4);
297
298 /* Go, go, go */
299 writel(cpu_to_le32(DMASCR_GO | DMASCR_CHAIN_EN | pci_cmds),
300 card->csr_remap + DMA_STATUS_CTRL);
301}
302
303static int add_bio(struct cardinfo *card);
304
305static void activate(struct cardinfo *card)
306{
Jeff Garzik4e953a22007-09-27 07:41:50 -0400307 /* if No page is Active, and Ready is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 * not empty, then switch Ready page
309 * to active and start IO.
310 * Then add any bh's that are available to Ready
311 */
312
313 do {
314 while (add_bio(card))
315 ;
316
317 if (card->Active == -1 &&
318 card->mm_pages[card->Ready].cnt > 0) {
319 card->Active = card->Ready;
320 card->Ready = 1-card->Ready;
321 mm_start_io(card);
322 }
323
324 } while (card->Active == -1 && add_bio(card));
325}
326
327static inline void reset_page(struct mm_page *page)
328{
329 page->cnt = 0;
330 page->headcnt = 0;
331 page->bio = NULL;
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100332 page->biotail = &page->bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Jeff Garzik4e953a22007-09-27 07:41:50 -0400335/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * If there is room on Ready page, take
337 * one bh off list and add it.
338 * return 1 if there was room, else 0.
339 */
340static int add_bio(struct cardinfo *card)
341{
342 struct mm_page *p;
343 struct mm_dma_desc *desc;
344 dma_addr_t dma_handle;
345 int offset;
346 struct bio *bio;
NeilBrowneea9bef2007-08-16 13:31:26 +0200347 struct bio_vec *vec;
348 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 int rw;
350 int len;
351
352 bio = card->currentbio;
353 if (!bio && card->bio) {
354 card->currentbio = card->bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700355 card->current_idx = card->bio->bi_iter.bi_idx;
356 card->current_sector = card->bio->bi_iter.bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 card->bio = card->bio->bi_next;
358 if (card->bio == NULL)
359 card->biotail = &card->bio;
360 card->currentbio->bi_next = NULL;
361 return 1;
362 }
363 if (!bio)
364 return 0;
NeilBrowneea9bef2007-08-16 13:31:26 +0200365 idx = card->current_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 rw = bio_rw(bio);
368 if (card->mm_pages[card->Ready].cnt >= DESC_PER_PAGE)
369 return 0;
370
NeilBrowneea9bef2007-08-16 13:31:26 +0200371 vec = bio_iovec_idx(bio, idx);
372 len = vec->bv_len;
373 dma_handle = pci_map_page(card->dev,
374 vec->bv_page,
375 vec->bv_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 len,
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100377 (rw == READ) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);
379
380 p = &card->mm_pages[card->Ready];
381 desc = &p->desc[p->cnt];
382 p->cnt++;
NeilBrowneea9bef2007-08-16 13:31:26 +0200383 if (p->bio == NULL)
384 p->idx = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if ((p->biotail) != &bio->bi_next) {
386 *(p->biotail) = bio;
387 p->biotail = &(bio->bi_next);
388 bio->bi_next = NULL;
389 }
390
391 desc->data_dma_handle = dma_handle;
392
393 desc->pci_addr = cpu_to_le64((u64)desc->data_dma_handle);
NeilBrowneea9bef2007-08-16 13:31:26 +0200394 desc->local_addr = cpu_to_le64(card->current_sector << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 desc->transfer_size = cpu_to_le32(len);
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100396 offset = (((char *)&desc->sem_control_bits) - ((char *)p->desc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 desc->sem_addr = cpu_to_le64((u64)(p->page_dma+offset));
398 desc->zero1 = desc->zero2 = 0;
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100399 offset = (((char *)(desc+1)) - ((char *)p->desc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 desc->next_desc_addr = cpu_to_le64(p->page_dma+offset);
401 desc->control_bits = cpu_to_le32(DMASCR_GO|DMASCR_ERR_INT_EN|
402 DMASCR_PARITY_INT_EN|
403 DMASCR_CHAIN_EN |
404 DMASCR_SEM_EN |
405 pci_cmds);
406 if (rw == WRITE)
407 desc->control_bits |= cpu_to_le32(DMASCR_TRANSFER_READ);
408 desc->sem_control_bits = desc->control_bits;
409
NeilBrowneea9bef2007-08-16 13:31:26 +0200410 card->current_sector += (len >> 9);
411 idx++;
412 card->current_idx = idx;
413 if (idx >= bio->bi_vcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 card->currentbio = NULL;
415
416 return 1;
417}
418
419static void process_page(unsigned long data)
420{
421 /* check if any of the requests in the page are DMA_COMPLETE,
422 * and deal with them appropriately.
423 * If we find a descriptor without DMA_COMPLETE in the semaphore, then
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100424 * dma must have hit an error on that descriptor, so use dma_status
425 * instead and assume that all following descriptors must be re-tried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 */
427 struct mm_page *page;
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100428 struct bio *return_bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 struct cardinfo *card = (struct cardinfo *)data;
430 unsigned int dma_status = card->dma_status;
431
432 spin_lock_bh(&card->lock);
433 if (card->Active < 0)
434 goto out_unlock;
435 page = &card->mm_pages[card->Active];
Jeff Garzik4e953a22007-09-27 07:41:50 -0400436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 while (page->headcnt < page->cnt) {
438 struct bio *bio = page->bio;
439 struct mm_dma_desc *desc = &page->desc[page->headcnt];
440 int control = le32_to_cpu(desc->sem_control_bits);
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100441 int last = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 int idx;
443
444 if (!(control & DMASCR_DMA_COMPLETE)) {
445 control = dma_status;
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100446 last = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448 page->headcnt++;
NeilBrowneea9bef2007-08-16 13:31:26 +0200449 idx = page->idx;
450 page->idx++;
451 if (page->idx >= bio->bi_vcnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 page->bio = bio->bi_next;
Neil Brown794e64d2007-12-10 15:49:30 -0800453 if (page->bio)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700454 page->idx = page->bio->bi_iter.bi_idx;
NeilBrowneea9bef2007-08-16 13:31:26 +0200455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Jeff Garzik4e953a22007-09-27 07:41:50 -0400457 pci_unmap_page(card->dev, desc->data_dma_handle,
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100458 bio_iovec_idx(bio, idx)->bv_len,
459 (control & DMASCR_TRANSFER_READ) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
461 if (control & DMASCR_HARD_ERROR) {
462 /* error */
463 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Jeff Garzik4e0af882007-09-27 06:41:25 -0400464 dev_printk(KERN_WARNING, &card->dev->dev,
465 "I/O error on sector %d/%d\n",
466 le32_to_cpu(desc->local_addr)>>9,
467 le32_to_cpu(desc->transfer_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 dump_dmastat(card, control);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200469 } else if ((bio->bi_rw & REQ_WRITE) &&
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100470 le32_to_cpu(desc->local_addr) >> 9 ==
471 card->init_size) {
472 card->init_size += le32_to_cpu(desc->transfer_size) >> 9;
473 if (card->init_size >> 1 >= card->mm_size) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400474 dev_printk(KERN_INFO, &card->dev->dev,
475 "memory now initialised\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 set_userbit(card, MEMORY_INITIALIZED, 1);
477 }
478 }
479 if (bio != page->bio) {
480 bio->bi_next = return_bio;
481 return_bio = bio;
482 }
483
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100484 if (last)
485 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487
488 if (debug & DEBUG_LED_ON_TRANSFER)
489 set_led(card, LED_REMOVE, LED_OFF);
490
491 if (card->check_batteries) {
492 card->check_batteries = 0;
493 check_batteries(card);
494 }
495 if (page->headcnt >= page->cnt) {
496 reset_page(page);
497 card->Active = -1;
498 activate(card);
499 } else {
500 /* haven't finished with this one yet */
Domen Puncer46308c02005-09-10 00:27:09 -0700501 pr_debug("do some more\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 mm_start_io(card);
503 }
504 out_unlock:
505 spin_unlock_bh(&card->lock);
506
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100507 while (return_bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 struct bio *bio = return_bio;
509
510 return_bio = bio->bi_next;
511 bio->bi_next = NULL;
NeilBrown6712ecf2007-09-27 12:47:43 +0200512 bio_endio(bio, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514}
515
NeilBrown74018dc2012-07-31 09:08:15 +0200516static void mm_unplug(struct blk_plug_cb *cb, bool from_schedule)
Tao Guo32587372012-06-13 21:17:21 +0200517{
NeilBrown9cbb1752012-07-31 09:08:14 +0200518 struct cardinfo *card = cb->data;
Tao Guo32587372012-06-13 21:17:21 +0200519
NeilBrown9cbb1752012-07-31 09:08:14 +0200520 spin_lock_irq(&card->lock);
521 activate(card);
522 spin_unlock_irq(&card->lock);
523 kfree(cb);
Tao Guo32587372012-06-13 21:17:21 +0200524}
525
526static int mm_check_plugged(struct cardinfo *card)
527{
NeilBrown9cbb1752012-07-31 09:08:14 +0200528 return !!blk_check_plugged(mm_unplug, card, sizeof(struct blk_plug_cb));
Tao Guo32587372012-06-13 21:17:21 +0200529}
530
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200531static void mm_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 struct cardinfo *card = q->queuedata;
Zach Brownf2b9ecc2006-10-03 01:16:07 -0700534 pr_debug("mm_make_request %llu %u\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -0700535 (unsigned long long)bio->bi_iter.bi_sector,
536 bio->bi_iter.bi_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 spin_lock_irq(&card->lock);
539 *card->biotail = bio;
540 bio->bi_next = NULL;
541 card->biotail = &bio->bi_next;
Tao Guo32587372012-06-13 21:17:21 +0200542 if (bio->bi_rw & REQ_SYNC || !mm_check_plugged(card))
543 activate(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 spin_unlock_irq(&card->lock);
545
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200546 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
David Howells7d12e782006-10-05 14:55:46 +0100549static irqreturn_t mm_interrupt(int irq, void *__card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 struct cardinfo *card = (struct cardinfo *) __card;
552 unsigned int dma_status;
553 unsigned short cfg_status;
554
555HW_TRACE(0x30);
556
557 dma_status = le32_to_cpu(readl(card->csr_remap + DMA_STATUS_CTRL));
558
559 if (!(dma_status & (DMASCR_ERROR_MASK | DMASCR_CHAIN_COMPLETE))) {
560 /* interrupt wasn't for me ... */
561 return IRQ_NONE;
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /* clear COMPLETION interrupts */
565 if (card->flags & UM_FLAG_NO_BYTE_STATUS)
566 writel(cpu_to_le32(DMASCR_DMA_COMPLETE|DMASCR_CHAIN_COMPLETE),
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100567 card->csr_remap + DMA_STATUS_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 else
569 writeb((DMASCR_DMA_COMPLETE|DMASCR_CHAIN_COMPLETE) >> 16,
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100570 card->csr_remap + DMA_STATUS_CTRL + 2);
Jeff Garzik4e953a22007-09-27 07:41:50 -0400571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /* log errors and clear interrupt status */
573 if (dma_status & DMASCR_ANY_ERR) {
574 unsigned int data_log1, data_log2;
575 unsigned int addr_log1, addr_log2;
576 unsigned char stat, count, syndrome, check;
577
578 stat = readb(card->csr_remap + MEMCTRLCMD_ERRSTATUS);
579
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100580 data_log1 = le32_to_cpu(readl(card->csr_remap +
581 ERROR_DATA_LOG));
582 data_log2 = le32_to_cpu(readl(card->csr_remap +
583 ERROR_DATA_LOG + 4));
584 addr_log1 = le32_to_cpu(readl(card->csr_remap +
585 ERROR_ADDR_LOG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 addr_log2 = readb(card->csr_remap + ERROR_ADDR_LOG + 4);
587
588 count = readb(card->csr_remap + ERROR_COUNT);
589 syndrome = readb(card->csr_remap + ERROR_SYNDROME);
590 check = readb(card->csr_remap + ERROR_CHECK);
591
592 dump_dmastat(card, dma_status);
593
594 if (stat & 0x01)
Jeff Garzik4e0af882007-09-27 06:41:25 -0400595 dev_printk(KERN_ERR, &card->dev->dev,
596 "Memory access error detected (err count %d)\n",
597 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (stat & 0x02)
Jeff Garzik4e0af882007-09-27 06:41:25 -0400599 dev_printk(KERN_ERR, &card->dev->dev,
600 "Multi-bit EDC error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Jeff Garzik4e0af882007-09-27 06:41:25 -0400602 dev_printk(KERN_ERR, &card->dev->dev,
603 "Fault Address 0x%02x%08x, Fault Data 0x%08x%08x\n",
604 addr_log2, addr_log1, data_log2, data_log1);
605 dev_printk(KERN_ERR, &card->dev->dev,
606 "Fault Check 0x%02x, Fault Syndrome 0x%02x\n",
607 check, syndrome);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 writeb(0, card->csr_remap + ERROR_COUNT);
610 }
611
612 if (dma_status & DMASCR_PARITY_ERR_REP) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400613 dev_printk(KERN_ERR, &card->dev->dev,
614 "PARITY ERROR REPORTED\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
616 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
617 }
618
619 if (dma_status & DMASCR_PARITY_ERR_DET) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400620 dev_printk(KERN_ERR, &card->dev->dev,
621 "PARITY ERROR DETECTED\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
623 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
624 }
625
626 if (dma_status & DMASCR_SYSTEM_ERR_SIG) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400627 dev_printk(KERN_ERR, &card->dev->dev, "SYSTEM ERROR\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
629 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
630 }
631
632 if (dma_status & DMASCR_TARGET_ABT) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400633 dev_printk(KERN_ERR, &card->dev->dev, "TARGET ABORT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
635 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
636 }
637
638 if (dma_status & DMASCR_MASTER_ABT) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400639 dev_printk(KERN_ERR, &card->dev->dev, "MASTER ABORT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
641 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
642 }
643
644 /* and process the DMA descriptors */
645 card->dma_status = dma_status;
646 tasklet_schedule(&card->tasklet);
647
648HW_TRACE(0x36);
649
Jeff Garzik4e953a22007-09-27 07:41:50 -0400650 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653/*
654 * If both batteries are good, no LED
655 * If either battery has been warned, solid LED
656 * If both batteries are bad, flash the LED quickly
657 * If either battery is bad, flash the LED semi quickly
658 */
659static void set_fault_to_battery_status(struct cardinfo *card)
660{
661 if (card->battery[0].good && card->battery[1].good)
662 set_led(card, LED_FAULT, LED_OFF);
663 else if (card->battery[0].warned || card->battery[1].warned)
664 set_led(card, LED_FAULT, LED_ON);
665 else if (!card->battery[0].good && !card->battery[1].good)
666 set_led(card, LED_FAULT, LED_FLASH_7_0);
667 else
668 set_led(card, LED_FAULT, LED_FLASH_3_5);
669}
670
671static void init_battery_timer(void);
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673static int check_battery(struct cardinfo *card, int battery, int status)
674{
675 if (status != card->battery[battery].good) {
676 card->battery[battery].good = !card->battery[battery].good;
677 card->battery[battery].last_change = jiffies;
678
679 if (card->battery[battery].good) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400680 dev_printk(KERN_ERR, &card->dev->dev,
681 "Battery %d now good\n", battery + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 card->battery[battery].warned = 0;
683 } else
Jeff Garzik4e0af882007-09-27 06:41:25 -0400684 dev_printk(KERN_ERR, &card->dev->dev,
685 "Battery %d now FAILED\n", battery + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 return 1;
688 } else if (!card->battery[battery].good &&
689 !card->battery[battery].warned &&
690 time_after_eq(jiffies, card->battery[battery].last_change +
691 (HZ * 60 * 60 * 5))) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400692 dev_printk(KERN_ERR, &card->dev->dev,
693 "Battery %d still FAILED after 5 hours\n", battery + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 card->battery[battery].warned = 1;
695
696 return 1;
697 }
698
699 return 0;
700}
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702static void check_batteries(struct cardinfo *card)
703{
704 /* NOTE: this must *never* be called while the card
705 * is doing (bus-to-card) DMA, or you will need the
706 * reset switch
707 */
708 unsigned char status;
709 int ret1, ret2;
710
711 status = readb(card->csr_remap + MEMCTRLSTATUS_BATTERY);
712 if (debug & DEBUG_BATTERY_POLLING)
Jeff Garzik4e0af882007-09-27 06:41:25 -0400713 dev_printk(KERN_DEBUG, &card->dev->dev,
714 "checking battery status, 1 = %s, 2 = %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 (status & BATTERY_1_FAILURE) ? "FAILURE" : "OK",
716 (status & BATTERY_2_FAILURE) ? "FAILURE" : "OK");
717
718 ret1 = check_battery(card, 0, !(status & BATTERY_1_FAILURE));
719 ret2 = check_battery(card, 1, !(status & BATTERY_2_FAILURE));
720
721 if (ret1 || ret2)
722 set_fault_to_battery_status(card);
723}
724
725static void check_all_batteries(unsigned long ptr)
726{
727 int i;
728
Jeff Garzik4e953a22007-09-27 07:41:50 -0400729 for (i = 0; i < num_cards; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (!(cards[i].flags & UM_FLAG_NO_BATT)) {
731 struct cardinfo *card = &cards[i];
732 spin_lock_bh(&card->lock);
733 if (card->Active >= 0)
734 card->check_batteries = 1;
735 else
736 check_batteries(card);
737 spin_unlock_bh(&card->lock);
738 }
739
740 init_battery_timer();
741}
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743static void init_battery_timer(void)
744{
745 init_timer(&battery_timer);
746 battery_timer.function = check_all_batteries;
747 battery_timer.expires = jiffies + (HZ * 60);
748 add_timer(&battery_timer);
749}
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751static void del_battery_timer(void)
752{
753 del_timer(&battery_timer);
754}
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756/*
757 * Note no locks taken out here. In a worst case scenario, we could drop
758 * a chunk of system memory. But that should never happen, since validation
759 * happens at open or mount time, when locks are held.
760 *
761 * That's crap, since doing that while some partitions are opened
762 * or mounted will give you really nasty results.
763 */
764static int mm_revalidate(struct gendisk *disk)
765{
766 struct cardinfo *card = disk->private_data;
767 set_capacity(disk, card->mm_size << 1);
768 return 0;
769}
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800770
771static int mm_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800773 struct cardinfo *card = bdev->bd_disk->private_data;
774 int size = card->mm_size * (1024 / MM_HARDSECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800776 /*
777 * get geometry: we have to fake one... trim the size to a
778 * multiple of 2048 (1M): tell we have 32 sectors, 64 heads,
779 * whatever cylinders.
780 */
781 geo->heads = 64;
782 geo->sectors = 32;
783 geo->cylinders = size / (geo->heads * geo->sectors);
784 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800786
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700787static const struct block_device_operations mm_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 .owner = THIS_MODULE,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800789 .getgeo = mm_getgeo,
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100790 .revalidate_disk = mm_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791};
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100792
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800793static int mm_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 int ret = -ENODEV;
796 struct cardinfo *card = &cards[num_cards];
797 unsigned char mem_present;
798 unsigned char batt_status;
799 unsigned int saved_bar, data;
Jeff Garzikee4a7b62007-09-27 07:40:33 -0400800 unsigned long csr_base;
801 unsigned long csr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 int magic_number;
Jeff Garzik4e0af882007-09-27 06:41:25 -0400803 static int printed_version;
804
805 if (!printed_version++)
806 printk(KERN_INFO DRIVER_VERSION " : " DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Jeff Garzikee4a7b62007-09-27 07:40:33 -0400808 ret = pci_enable_device(dev);
809 if (ret)
810 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0xF8);
813 pci_set_master(dev);
814
815 card->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Jeff Garzikee4a7b62007-09-27 07:40:33 -0400817 csr_base = pci_resource_start(dev, 0);
818 csr_len = pci_resource_len(dev, 0);
819 if (!csr_base || !csr_len)
820 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Jeff Garzik4e0af882007-09-27 06:41:25 -0400822 dev_printk(KERN_INFO, &dev->dev,
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100823 "Micro Memory(tm) controller found (PCI Mem Module (Battery Backup))\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Yang Hongyang6a355282009-04-06 19:01:13 -0700825 if (pci_set_dma_mask(dev, DMA_BIT_MASK(64)) &&
Yang Hongyang284901a2009-04-06 19:01:15 -0700826 pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400827 dev_printk(KERN_WARNING, &dev->dev, "NO suitable DMA found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return -ENOMEM;
829 }
Jeff Garzikee4a7b62007-09-27 07:40:33 -0400830
831 ret = pci_request_regions(dev, DRIVER_NAME);
832 if (ret) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400833 dev_printk(KERN_ERR, &card->dev->dev,
834 "Unable to request memory region\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 goto failed_req_csr;
836 }
837
Jeff Garzikee4a7b62007-09-27 07:40:33 -0400838 card->csr_remap = ioremap_nocache(csr_base, csr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (!card->csr_remap) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400840 dev_printk(KERN_ERR, &card->dev->dev,
841 "Unable to remap memory region\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 ret = -ENOMEM;
843
844 goto failed_remap_csr;
845 }
846
Jeff Garzik4e0af882007-09-27 06:41:25 -0400847 dev_printk(KERN_INFO, &card->dev->dev,
848 "CSR 0x%08lx -> 0x%p (0x%lx)\n",
Jeff Garzikee4a7b62007-09-27 07:40:33 -0400849 csr_base, card->csr_remap, csr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100851 switch (card->dev->device) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 case 0x5415:
853 card->flags |= UM_FLAG_NO_BYTE_STATUS | UM_FLAG_NO_BATTREG;
854 magic_number = 0x59;
855 break;
856
857 case 0x5425:
858 card->flags |= UM_FLAG_NO_BYTE_STATUS;
859 magic_number = 0x5C;
860 break;
861
862 case 0x6155:
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100863 card->flags |= UM_FLAG_NO_BYTE_STATUS |
864 UM_FLAG_NO_BATTREG | UM_FLAG_NO_BATT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 magic_number = 0x99;
866 break;
867
868 default:
869 magic_number = 0x100;
870 break;
871 }
872
873 if (readb(card->csr_remap + MEMCTRLSTATUS_MAGIC) != magic_number) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400874 dev_printk(KERN_ERR, &card->dev->dev, "Magic number invalid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 ret = -ENOMEM;
876 goto failed_magic;
877 }
878
879 card->mm_pages[0].desc = pci_alloc_consistent(card->dev,
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100880 PAGE_SIZE * 2,
881 &card->mm_pages[0].page_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 card->mm_pages[1].desc = pci_alloc_consistent(card->dev,
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100883 PAGE_SIZE * 2,
884 &card->mm_pages[1].page_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (card->mm_pages[0].desc == NULL ||
886 card->mm_pages[1].desc == NULL) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400887 dev_printk(KERN_ERR, &card->dev->dev, "alloc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 goto failed_alloc;
889 }
890 reset_page(&card->mm_pages[0]);
891 reset_page(&card->mm_pages[1]);
892 card->Ready = 0; /* page 0 is ready */
893 card->Active = -1; /* no page is active */
894 card->bio = NULL;
895 card->biotail = &card->bio;
896
897 card->queue = blk_alloc_queue(GFP_KERNEL);
898 if (!card->queue)
899 goto failed_alloc;
900
901 blk_queue_make_request(card->queue, mm_make_request);
Sage Weilf3c737d2009-04-23 08:37:58 +0200902 card->queue->queue_lock = &card->lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 card->queue->queuedata = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 tasklet_init(&card->tasklet, process_page, (unsigned long)card);
906
907 card->check_batteries = 0;
Jeff Garzik4e953a22007-09-27 07:41:50 -0400908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 mem_present = readb(card->csr_remap + MEMCTRLSTATUS_MEMORY);
910 switch (mem_present) {
911 case MEM_128_MB:
912 card->mm_size = 1024 * 128;
913 break;
914 case MEM_256_MB:
915 card->mm_size = 1024 * 256;
916 break;
917 case MEM_512_MB:
918 card->mm_size = 1024 * 512;
919 break;
920 case MEM_1_GB:
921 card->mm_size = 1024 * 1024;
922 break;
923 case MEM_2_GB:
924 card->mm_size = 1024 * 2048;
925 break;
926 default:
927 card->mm_size = 0;
928 break;
929 }
930
931 /* Clear the LED's we control */
932 set_led(card, LED_REMOVE, LED_OFF);
933 set_led(card, LED_FAULT, LED_OFF);
934
935 batt_status = readb(card->csr_remap + MEMCTRLSTATUS_BATTERY);
936
937 card->battery[0].good = !(batt_status & BATTERY_1_FAILURE);
938 card->battery[1].good = !(batt_status & BATTERY_2_FAILURE);
939 card->battery[0].last_change = card->battery[1].last_change = jiffies;
940
Jeff Garzik4e953a22007-09-27 07:41:50 -0400941 if (card->flags & UM_FLAG_NO_BATT)
Jeff Garzik4e0af882007-09-27 06:41:25 -0400942 dev_printk(KERN_INFO, &card->dev->dev,
943 "Size %d KB\n", card->mm_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 else {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400945 dev_printk(KERN_INFO, &card->dev->dev,
946 "Size %d KB, Battery 1 %s (%s), Battery 2 %s (%s)\n",
947 card->mm_size,
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100948 batt_status & BATTERY_1_DISABLED ? "Disabled" : "Enabled",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 card->battery[0].good ? "OK" : "FAILURE",
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100950 batt_status & BATTERY_2_DISABLED ? "Disabled" : "Enabled",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 card->battery[1].good ? "OK" : "FAILURE");
952
953 set_fault_to_battery_status(card);
954 }
955
956 pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &saved_bar);
957 data = 0xffffffff;
958 pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, data);
959 pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &data);
960 pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, saved_bar);
961 data &= 0xfffffff0;
962 data = ~data;
963 data += 1;
964
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100965 if (request_irq(dev->irq, mm_interrupt, IRQF_SHARED, DRIVER_NAME,
966 card)) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400967 dev_printk(KERN_ERR, &card->dev->dev,
968 "Unable to allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 goto failed_req_irq;
971 }
972
Jeff Garzik4e0af882007-09-27 06:41:25 -0400973 dev_printk(KERN_INFO, &card->dev->dev,
Jeff Garzikee4a7b62007-09-27 07:40:33 -0400974 "Window size %d bytes, IRQ %d\n", data, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100976 spin_lock_init(&card->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 pci_set_drvdata(dev, card);
979
980 if (pci_write_cmd != 0x0F) /* If not Memory Write & Invalidate */
981 pci_write_cmd = 0x07; /* then Memory Write command */
982
983 if (pci_write_cmd & 0x08) { /* use Memory Write and Invalidate */
984 unsigned short cfg_command;
985 pci_read_config_word(dev, PCI_COMMAND, &cfg_command);
986 cfg_command |= 0x10; /* Memory Write & Invalidate Enable */
987 pci_write_config_word(dev, PCI_COMMAND, cfg_command);
988 }
989 pci_cmds = (pci_read_cmd << 28) | (pci_write_cmd << 24);
990
991 num_cards++;
992
993 if (!get_userbit(card, MEMORY_INITIALIZED)) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400994 dev_printk(KERN_INFO, &card->dev->dev,
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100995 "memory NOT initialized. Consider over-writing whole device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 card->init_size = 0;
997 } else {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400998 dev_printk(KERN_INFO, &card->dev->dev,
999 "memory already initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 card->init_size = card->mm_size;
1001 }
1002
1003 /* Enable ECC */
1004 writeb(EDC_STORE_CORRECT, card->csr_remap + MEMCTRLCMD_ERRCTRL);
1005
1006 return 0;
1007
1008 failed_req_irq:
1009 failed_alloc:
1010 if (card->mm_pages[0].desc)
1011 pci_free_consistent(card->dev, PAGE_SIZE*2,
1012 card->mm_pages[0].desc,
1013 card->mm_pages[0].page_dma);
1014 if (card->mm_pages[1].desc)
1015 pci_free_consistent(card->dev, PAGE_SIZE*2,
1016 card->mm_pages[1].desc,
1017 card->mm_pages[1].page_dma);
1018 failed_magic:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 iounmap(card->csr_remap);
1020 failed_remap_csr:
Jeff Garzikee4a7b62007-09-27 07:40:33 -04001021 pci_release_regions(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 failed_req_csr:
1023
1024 return ret;
1025}
Randy Dunlap458cf5e2007-12-17 20:24:20 +01001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027static void mm_pci_remove(struct pci_dev *dev)
1028{
1029 struct cardinfo *card = pci_get_drvdata(dev);
1030
1031 tasklet_kill(&card->tasklet);
Jeff Garzikee4a7b62007-09-27 07:40:33 -04001032 free_irq(dev->irq, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 iounmap(card->csr_remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 if (card->mm_pages[0].desc)
1036 pci_free_consistent(card->dev, PAGE_SIZE*2,
1037 card->mm_pages[0].desc,
1038 card->mm_pages[0].page_dma);
1039 if (card->mm_pages[1].desc)
1040 pci_free_consistent(card->dev, PAGE_SIZE*2,
1041 card->mm_pages[1].desc,
1042 card->mm_pages[1].page_dma);
Al Viro1312f402006-03-12 11:02:03 -05001043 blk_cleanup_queue(card->queue);
Jeff Garzikee4a7b62007-09-27 07:40:33 -04001044
1045 pci_release_regions(dev);
1046 pci_disable_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
Neil Brown5874c182007-07-13 07:39:46 +02001049static const struct pci_device_id mm_pci_ids[] = {
Randy Dunlap458cf5e2007-12-17 20:24:20 +01001050 {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY, PCI_DEVICE_ID_MICRO_MEMORY_5415CN)},
1051 {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY, PCI_DEVICE_ID_MICRO_MEMORY_5425CN)},
1052 {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY, PCI_DEVICE_ID_MICRO_MEMORY_6155)},
Neil Brown5874c182007-07-13 07:39:46 +02001053 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 .vendor = 0x8086,
1055 .device = 0xB555,
Randy Dunlap458cf5e2007-12-17 20:24:20 +01001056 .subvendor = 0x1332,
1057 .subdevice = 0x5460,
1058 .class = 0x050000,
1059 .class_mask = 0,
Neil Brown5874c182007-07-13 07:39:46 +02001060 }, { /* end: all zeroes */ }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061};
1062
1063MODULE_DEVICE_TABLE(pci, mm_pci_ids);
1064
1065static struct pci_driver mm_pci_driver = {
Jeff Garzikee4a7b62007-09-27 07:40:33 -04001066 .name = DRIVER_NAME,
1067 .id_table = mm_pci_ids,
1068 .probe = mm_pci_probe,
1069 .remove = mm_pci_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070};
Jeff Garzikee4a7b62007-09-27 07:40:33 -04001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072static int __init mm_init(void)
1073{
1074 int retval, i;
1075 int err;
1076
Richard Knutsson9bfab8c2005-11-30 00:59:34 +01001077 retval = pci_register_driver(&mm_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (retval)
1079 return -ENOMEM;
1080
Jeff Garzikcb3503c2007-09-27 07:49:39 -04001081 err = major_nr = register_blkdev(0, DRIVER_NAME);
NeilBrown5a243e02007-02-28 20:11:12 -08001082 if (err < 0) {
1083 pci_unregister_driver(&mm_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return -EIO;
NeilBrown5a243e02007-02-28 20:11:12 -08001085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 for (i = 0; i < num_cards; i++) {
1088 mm_gendisk[i] = alloc_disk(1 << MM_SHIFT);
1089 if (!mm_gendisk[i])
1090 goto out;
1091 }
1092
1093 for (i = 0; i < num_cards; i++) {
1094 struct gendisk *disk = mm_gendisk[i];
1095 sprintf(disk->disk_name, "umem%c", 'a'+i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 spin_lock_init(&cards[i].lock);
1097 disk->major = major_nr;
1098 disk->first_minor = i << MM_SHIFT;
1099 disk->fops = &mm_fops;
1100 disk->private_data = &cards[i];
1101 disk->queue = cards[i].queue;
1102 set_capacity(disk, cards[i].mm_size << 1);
1103 add_disk(disk);
1104 }
1105
1106 init_battery_timer();
Jeff Garzik4e0af882007-09-27 06:41:25 -04001107 printk(KERN_INFO "MM: desc_per_page = %ld\n", DESC_PER_PAGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108/* printk("mm_init: Done. 10-19-01 9:00\n"); */
1109 return 0;
1110
1111out:
NeilBrown5a243e02007-02-28 20:11:12 -08001112 pci_unregister_driver(&mm_pci_driver);
Jeff Garzikcb3503c2007-09-27 07:49:39 -04001113 unregister_blkdev(major_nr, DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 while (i--)
1115 put_disk(mm_gendisk[i]);
1116 return -ENOMEM;
1117}
Randy Dunlap458cf5e2007-12-17 20:24:20 +01001118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119static void __exit mm_cleanup(void)
1120{
1121 int i;
1122
1123 del_battery_timer();
1124
Randy Dunlap458cf5e2007-12-17 20:24:20 +01001125 for (i = 0; i < num_cards ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 del_gendisk(mm_gendisk[i]);
1127 put_disk(mm_gendisk[i]);
1128 }
1129
1130 pci_unregister_driver(&mm_pci_driver);
1131
Jeff Garzikcb3503c2007-09-27 07:49:39 -04001132 unregister_blkdev(major_nr, DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
1134
1135module_init(mm_init);
1136module_exit(mm_cleanup);
1137
1138MODULE_AUTHOR(DRIVER_AUTHOR);
1139MODULE_DESCRIPTION(DRIVER_DESC);
1140MODULE_LICENSE("GPL");