blob: 858c34dd032d27d31cadb2112e7bc36ec36fe8de [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>
43#include <linux/ioctl.h>
44#include <linux/module.h>
45#include <linux/init.h>
46#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/timer.h>
48#include <linux/pci.h>
49#include <linux/slab.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];
143static struct block_device_operations mm_fops;
144static struct timer_list battery_timer;
145
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100146static int num_cards;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148static struct gendisk *mm_gendisk[MM_MAXCARDS];
149
150static void check_batteries(struct cardinfo *card);
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static int get_userbit(struct cardinfo *card, int bit)
153{
154 unsigned char led;
155
156 led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
157 return led & bit;
158}
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static int set_userbit(struct cardinfo *card, int bit, unsigned char state)
161{
162 unsigned char led;
163
164 led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
165 if (state)
166 led |= bit;
167 else
168 led &= ~bit;
169 writeb(led, card->csr_remap + MEMCTRLCMD_LEDCTRL);
170
171 return 0;
172}
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/*
175 * NOTE: For the power LED, use the LED_POWER_* macros since they differ
176 */
177static void set_led(struct cardinfo *card, int shift, unsigned char state)
178{
179 unsigned char led;
180
181 led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
182 if (state == LED_FLIP)
183 led ^= (1<<shift);
184 else {
185 led &= ~(0x03 << shift);
186 led |= (state << shift);
187 }
188 writeb(led, card->csr_remap + MEMCTRLCMD_LEDCTRL);
189
190}
191
192#ifdef MM_DIAG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static void dump_regs(struct cardinfo *card)
194{
195 unsigned char *p;
196 int i, i1;
197
198 p = card->csr_remap;
199 for (i = 0; i < 8; i++) {
200 printk(KERN_DEBUG "%p ", p);
201
202 for (i1 = 0; i1 < 16; i1++)
203 printk("%02x ", *p++);
204
205 printk("\n");
206 }
207}
208#endif
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210static void dump_dmastat(struct cardinfo *card, unsigned int dmastat)
211{
Jeff Garzik4e0af882007-09-27 06:41:25 -0400212 dev_printk(KERN_DEBUG, &card->dev->dev, "DMAstat - ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (dmastat & DMASCR_ANY_ERR)
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100214 printk(KERN_CONT "ANY_ERR ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (dmastat & DMASCR_MBE_ERR)
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100216 printk(KERN_CONT "MBE_ERR ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (dmastat & DMASCR_PARITY_ERR_REP)
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100218 printk(KERN_CONT "PARITY_ERR_REP ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (dmastat & DMASCR_PARITY_ERR_DET)
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100220 printk(KERN_CONT "PARITY_ERR_DET ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (dmastat & DMASCR_SYSTEM_ERR_SIG)
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100222 printk(KERN_CONT "SYSTEM_ERR_SIG ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (dmastat & DMASCR_TARGET_ABT)
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100224 printk(KERN_CONT "TARGET_ABT ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (dmastat & DMASCR_MASTER_ABT)
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100226 printk(KERN_CONT "MASTER_ABT ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (dmastat & DMASCR_CHAIN_COMPLETE)
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100228 printk(KERN_CONT "CHAIN_COMPLETE ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (dmastat & DMASCR_DMA_COMPLETE)
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100230 printk(KERN_CONT "DMA_COMPLETE ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 printk("\n");
232}
233
234/*
235 * Theory of request handling
236 *
237 * Each bio is assigned to one mm_dma_desc - which may not be enough FIXME
238 * We have two pages of mm_dma_desc, holding about 64 descriptors
239 * each. These are allocated at init time.
240 * One page is "Ready" and is either full, or can have request added.
241 * The other page might be "Active", which DMA is happening on it.
242 *
243 * Whenever IO on the active page completes, the Ready page is activated
244 * and the ex-Active page is clean out and made Ready.
245 * Otherwise the Ready page is only activated when it becomes full, or
246 * when mm_unplug_device is called via the unplug_io_fn.
247 *
248 * If a request arrives while both pages a full, it is queued, and b_rdev is
249 * overloaded to record whether it was a read or a write.
250 *
251 * The interrupt handler only polls the device to clear the interrupt.
252 * The processing of the result is done in a tasklet.
253 */
254
255static void mm_start_io(struct cardinfo *card)
256{
257 /* we have the lock, we know there is
258 * no IO active, and we know that card->Active
259 * is set
260 */
261 struct mm_dma_desc *desc;
262 struct mm_page *page;
263 int offset;
264
265 /* make the last descriptor end the chain */
266 page = &card->mm_pages[card->Active];
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100267 pr_debug("start_io: %d %d->%d\n",
268 card->Active, page->headcnt, page->cnt - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 desc = &page->desc[page->cnt-1];
270
271 desc->control_bits |= cpu_to_le32(DMASCR_CHAIN_COMP_EN);
272 desc->control_bits &= ~cpu_to_le32(DMASCR_CHAIN_EN);
273 desc->sem_control_bits = desc->control_bits;
274
Jeff Garzik4e953a22007-09-27 07:41:50 -0400275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (debug & DEBUG_LED_ON_TRANSFER)
277 set_led(card, LED_REMOVE, LED_ON);
278
279 desc = &page->desc[page->headcnt];
280 writel(0, card->csr_remap + DMA_PCI_ADDR);
281 writel(0, card->csr_remap + DMA_PCI_ADDR + 4);
282
283 writel(0, card->csr_remap + DMA_LOCAL_ADDR);
284 writel(0, card->csr_remap + DMA_LOCAL_ADDR + 4);
285
286 writel(0, card->csr_remap + DMA_TRANSFER_SIZE);
287 writel(0, card->csr_remap + DMA_TRANSFER_SIZE + 4);
288
289 writel(0, card->csr_remap + DMA_SEMAPHORE_ADDR);
290 writel(0, card->csr_remap + DMA_SEMAPHORE_ADDR + 4);
291
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100292 offset = ((char *)desc) - ((char *)page->desc);
293 writel(cpu_to_le32((page->page_dma+offset) & 0xffffffff),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 card->csr_remap + DMA_DESCRIPTOR_ADDR);
295 /* Force the value to u64 before shifting otherwise >> 32 is undefined C
296 * and on some ports will do nothing ! */
297 writel(cpu_to_le32(((u64)page->page_dma)>>32),
298 card->csr_remap + DMA_DESCRIPTOR_ADDR + 4);
299
300 /* Go, go, go */
301 writel(cpu_to_le32(DMASCR_GO | DMASCR_CHAIN_EN | pci_cmds),
302 card->csr_remap + DMA_STATUS_CTRL);
303}
304
305static int add_bio(struct cardinfo *card);
306
307static void activate(struct cardinfo *card)
308{
Jeff Garzik4e953a22007-09-27 07:41:50 -0400309 /* if No page is Active, and Ready is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * not empty, then switch Ready page
311 * to active and start IO.
312 * Then add any bh's that are available to Ready
313 */
314
315 do {
316 while (add_bio(card))
317 ;
318
319 if (card->Active == -1 &&
320 card->mm_pages[card->Ready].cnt > 0) {
321 card->Active = card->Ready;
322 card->Ready = 1-card->Ready;
323 mm_start_io(card);
324 }
325
326 } while (card->Active == -1 && add_bio(card));
327}
328
329static inline void reset_page(struct mm_page *page)
330{
331 page->cnt = 0;
332 page->headcnt = 0;
333 page->bio = NULL;
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100334 page->biotail = &page->bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Jens Axboe165125e2007-07-24 09:28:11 +0200337static void mm_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 struct cardinfo *card = q->queuedata;
340 unsigned long flags;
341
342 spin_lock_irqsave(&card->lock, flags);
343 if (blk_remove_plug(q))
344 activate(card);
345 spin_unlock_irqrestore(&card->lock, flags);
346}
347
Jeff Garzik4e953a22007-09-27 07:41:50 -0400348/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 * If there is room on Ready page, take
350 * one bh off list and add it.
351 * return 1 if there was room, else 0.
352 */
353static int add_bio(struct cardinfo *card)
354{
355 struct mm_page *p;
356 struct mm_dma_desc *desc;
357 dma_addr_t dma_handle;
358 int offset;
359 struct bio *bio;
NeilBrowneea9bef2007-08-16 13:31:26 +0200360 struct bio_vec *vec;
361 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 int rw;
363 int len;
364
365 bio = card->currentbio;
366 if (!bio && card->bio) {
367 card->currentbio = card->bio;
NeilBrowneea9bef2007-08-16 13:31:26 +0200368 card->current_idx = card->bio->bi_idx;
369 card->current_sector = card->bio->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 card->bio = card->bio->bi_next;
371 if (card->bio == NULL)
372 card->biotail = &card->bio;
373 card->currentbio->bi_next = NULL;
374 return 1;
375 }
376 if (!bio)
377 return 0;
NeilBrowneea9bef2007-08-16 13:31:26 +0200378 idx = card->current_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 rw = bio_rw(bio);
381 if (card->mm_pages[card->Ready].cnt >= DESC_PER_PAGE)
382 return 0;
383
NeilBrowneea9bef2007-08-16 13:31:26 +0200384 vec = bio_iovec_idx(bio, idx);
385 len = vec->bv_len;
386 dma_handle = pci_map_page(card->dev,
387 vec->bv_page,
388 vec->bv_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 len,
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100390 (rw == READ) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);
392
393 p = &card->mm_pages[card->Ready];
394 desc = &p->desc[p->cnt];
395 p->cnt++;
NeilBrowneea9bef2007-08-16 13:31:26 +0200396 if (p->bio == NULL)
397 p->idx = idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if ((p->biotail) != &bio->bi_next) {
399 *(p->biotail) = bio;
400 p->biotail = &(bio->bi_next);
401 bio->bi_next = NULL;
402 }
403
404 desc->data_dma_handle = dma_handle;
405
406 desc->pci_addr = cpu_to_le64((u64)desc->data_dma_handle);
NeilBrowneea9bef2007-08-16 13:31:26 +0200407 desc->local_addr = cpu_to_le64(card->current_sector << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 desc->transfer_size = cpu_to_le32(len);
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100409 offset = (((char *)&desc->sem_control_bits) - ((char *)p->desc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 desc->sem_addr = cpu_to_le64((u64)(p->page_dma+offset));
411 desc->zero1 = desc->zero2 = 0;
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100412 offset = (((char *)(desc+1)) - ((char *)p->desc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 desc->next_desc_addr = cpu_to_le64(p->page_dma+offset);
414 desc->control_bits = cpu_to_le32(DMASCR_GO|DMASCR_ERR_INT_EN|
415 DMASCR_PARITY_INT_EN|
416 DMASCR_CHAIN_EN |
417 DMASCR_SEM_EN |
418 pci_cmds);
419 if (rw == WRITE)
420 desc->control_bits |= cpu_to_le32(DMASCR_TRANSFER_READ);
421 desc->sem_control_bits = desc->control_bits;
422
NeilBrowneea9bef2007-08-16 13:31:26 +0200423 card->current_sector += (len >> 9);
424 idx++;
425 card->current_idx = idx;
426 if (idx >= bio->bi_vcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 card->currentbio = NULL;
428
429 return 1;
430}
431
432static void process_page(unsigned long data)
433{
434 /* check if any of the requests in the page are DMA_COMPLETE,
435 * and deal with them appropriately.
436 * If we find a descriptor without DMA_COMPLETE in the semaphore, then
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100437 * dma must have hit an error on that descriptor, so use dma_status
438 * instead and assume that all following descriptors must be re-tried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 */
440 struct mm_page *page;
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100441 struct bio *return_bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 struct cardinfo *card = (struct cardinfo *)data;
443 unsigned int dma_status = card->dma_status;
444
445 spin_lock_bh(&card->lock);
446 if (card->Active < 0)
447 goto out_unlock;
448 page = &card->mm_pages[card->Active];
Jeff Garzik4e953a22007-09-27 07:41:50 -0400449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 while (page->headcnt < page->cnt) {
451 struct bio *bio = page->bio;
452 struct mm_dma_desc *desc = &page->desc[page->headcnt];
453 int control = le32_to_cpu(desc->sem_control_bits);
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100454 int last = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 int idx;
456
457 if (!(control & DMASCR_DMA_COMPLETE)) {
458 control = dma_status;
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100459 last = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461 page->headcnt++;
NeilBrowneea9bef2007-08-16 13:31:26 +0200462 idx = page->idx;
463 page->idx++;
464 if (page->idx >= bio->bi_vcnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 page->bio = bio->bi_next;
Neil Brown794e64d2007-12-10 15:49:30 -0800466 if (page->bio)
467 page->idx = page->bio->bi_idx;
NeilBrowneea9bef2007-08-16 13:31:26 +0200468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Jeff Garzik4e953a22007-09-27 07:41:50 -0400470 pci_unmap_page(card->dev, desc->data_dma_handle,
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100471 bio_iovec_idx(bio, idx)->bv_len,
472 (control & DMASCR_TRANSFER_READ) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
474 if (control & DMASCR_HARD_ERROR) {
475 /* error */
476 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Jeff Garzik4e0af882007-09-27 06:41:25 -0400477 dev_printk(KERN_WARNING, &card->dev->dev,
478 "I/O error on sector %d/%d\n",
479 le32_to_cpu(desc->local_addr)>>9,
480 le32_to_cpu(desc->transfer_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 dump_dmastat(card, control);
482 } else if (test_bit(BIO_RW, &bio->bi_rw) &&
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100483 le32_to_cpu(desc->local_addr) >> 9 ==
484 card->init_size) {
485 card->init_size += le32_to_cpu(desc->transfer_size) >> 9;
486 if (card->init_size >> 1 >= card->mm_size) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400487 dev_printk(KERN_INFO, &card->dev->dev,
488 "memory now initialised\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 set_userbit(card, MEMORY_INITIALIZED, 1);
490 }
491 }
492 if (bio != page->bio) {
493 bio->bi_next = return_bio;
494 return_bio = bio;
495 }
496
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100497 if (last)
498 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500
501 if (debug & DEBUG_LED_ON_TRANSFER)
502 set_led(card, LED_REMOVE, LED_OFF);
503
504 if (card->check_batteries) {
505 card->check_batteries = 0;
506 check_batteries(card);
507 }
508 if (page->headcnt >= page->cnt) {
509 reset_page(page);
510 card->Active = -1;
511 activate(card);
512 } else {
513 /* haven't finished with this one yet */
Domen Puncer46308c02005-09-10 00:27:09 -0700514 pr_debug("do some more\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 mm_start_io(card);
516 }
517 out_unlock:
518 spin_unlock_bh(&card->lock);
519
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100520 while (return_bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 struct bio *bio = return_bio;
522
523 return_bio = bio->bi_next;
524 bio->bi_next = NULL;
NeilBrown6712ecf2007-09-27 12:47:43 +0200525 bio_endio(bio, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527}
528
Jens Axboe165125e2007-07-24 09:28:11 +0200529static int mm_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 struct cardinfo *card = q->queuedata;
Zach Brownf2b9ecc2006-10-03 01:16:07 -0700532 pr_debug("mm_make_request %llu %u\n",
533 (unsigned long long)bio->bi_sector, bio->bi_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 spin_lock_irq(&card->lock);
536 *card->biotail = bio;
537 bio->bi_next = NULL;
538 card->biotail = &bio->bi_next;
539 blk_plug_device(q);
540 spin_unlock_irq(&card->lock);
541
542 return 0;
543}
544
David Howells7d12e782006-10-05 14:55:46 +0100545static irqreturn_t mm_interrupt(int irq, void *__card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 struct cardinfo *card = (struct cardinfo *) __card;
548 unsigned int dma_status;
549 unsigned short cfg_status;
550
551HW_TRACE(0x30);
552
553 dma_status = le32_to_cpu(readl(card->csr_remap + DMA_STATUS_CTRL));
554
555 if (!(dma_status & (DMASCR_ERROR_MASK | DMASCR_CHAIN_COMPLETE))) {
556 /* interrupt wasn't for me ... */
557 return IRQ_NONE;
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 /* clear COMPLETION interrupts */
561 if (card->flags & UM_FLAG_NO_BYTE_STATUS)
562 writel(cpu_to_le32(DMASCR_DMA_COMPLETE|DMASCR_CHAIN_COMPLETE),
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100563 card->csr_remap + DMA_STATUS_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 else
565 writeb((DMASCR_DMA_COMPLETE|DMASCR_CHAIN_COMPLETE) >> 16,
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100566 card->csr_remap + DMA_STATUS_CTRL + 2);
Jeff Garzik4e953a22007-09-27 07:41:50 -0400567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* log errors and clear interrupt status */
569 if (dma_status & DMASCR_ANY_ERR) {
570 unsigned int data_log1, data_log2;
571 unsigned int addr_log1, addr_log2;
572 unsigned char stat, count, syndrome, check;
573
574 stat = readb(card->csr_remap + MEMCTRLCMD_ERRSTATUS);
575
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100576 data_log1 = le32_to_cpu(readl(card->csr_remap +
577 ERROR_DATA_LOG));
578 data_log2 = le32_to_cpu(readl(card->csr_remap +
579 ERROR_DATA_LOG + 4));
580 addr_log1 = le32_to_cpu(readl(card->csr_remap +
581 ERROR_ADDR_LOG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 addr_log2 = readb(card->csr_remap + ERROR_ADDR_LOG + 4);
583
584 count = readb(card->csr_remap + ERROR_COUNT);
585 syndrome = readb(card->csr_remap + ERROR_SYNDROME);
586 check = readb(card->csr_remap + ERROR_CHECK);
587
588 dump_dmastat(card, dma_status);
589
590 if (stat & 0x01)
Jeff Garzik4e0af882007-09-27 06:41:25 -0400591 dev_printk(KERN_ERR, &card->dev->dev,
592 "Memory access error detected (err count %d)\n",
593 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (stat & 0x02)
Jeff Garzik4e0af882007-09-27 06:41:25 -0400595 dev_printk(KERN_ERR, &card->dev->dev,
596 "Multi-bit EDC error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Jeff Garzik4e0af882007-09-27 06:41:25 -0400598 dev_printk(KERN_ERR, &card->dev->dev,
599 "Fault Address 0x%02x%08x, Fault Data 0x%08x%08x\n",
600 addr_log2, addr_log1, data_log2, data_log1);
601 dev_printk(KERN_ERR, &card->dev->dev,
602 "Fault Check 0x%02x, Fault Syndrome 0x%02x\n",
603 check, syndrome);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 writeb(0, card->csr_remap + ERROR_COUNT);
606 }
607
608 if (dma_status & DMASCR_PARITY_ERR_REP) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400609 dev_printk(KERN_ERR, &card->dev->dev,
610 "PARITY ERROR REPORTED\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
612 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
613 }
614
615 if (dma_status & DMASCR_PARITY_ERR_DET) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400616 dev_printk(KERN_ERR, &card->dev->dev,
617 "PARITY ERROR DETECTED\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
619 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
620 }
621
622 if (dma_status & DMASCR_SYSTEM_ERR_SIG) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400623 dev_printk(KERN_ERR, &card->dev->dev, "SYSTEM ERROR\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
625 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
626 }
627
628 if (dma_status & DMASCR_TARGET_ABT) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400629 dev_printk(KERN_ERR, &card->dev->dev, "TARGET ABORT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
631 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
632 }
633
634 if (dma_status & DMASCR_MASTER_ABT) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400635 dev_printk(KERN_ERR, &card->dev->dev, "MASTER ABORT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
637 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
638 }
639
640 /* and process the DMA descriptors */
641 card->dma_status = dma_status;
642 tasklet_schedule(&card->tasklet);
643
644HW_TRACE(0x36);
645
Jeff Garzik4e953a22007-09-27 07:41:50 -0400646 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649/*
650 * If both batteries are good, no LED
651 * If either battery has been warned, solid LED
652 * If both batteries are bad, flash the LED quickly
653 * If either battery is bad, flash the LED semi quickly
654 */
655static void set_fault_to_battery_status(struct cardinfo *card)
656{
657 if (card->battery[0].good && card->battery[1].good)
658 set_led(card, LED_FAULT, LED_OFF);
659 else if (card->battery[0].warned || card->battery[1].warned)
660 set_led(card, LED_FAULT, LED_ON);
661 else if (!card->battery[0].good && !card->battery[1].good)
662 set_led(card, LED_FAULT, LED_FLASH_7_0);
663 else
664 set_led(card, LED_FAULT, LED_FLASH_3_5);
665}
666
667static void init_battery_timer(void);
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669static int check_battery(struct cardinfo *card, int battery, int status)
670{
671 if (status != card->battery[battery].good) {
672 card->battery[battery].good = !card->battery[battery].good;
673 card->battery[battery].last_change = jiffies;
674
675 if (card->battery[battery].good) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400676 dev_printk(KERN_ERR, &card->dev->dev,
677 "Battery %d now good\n", battery + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 card->battery[battery].warned = 0;
679 } else
Jeff Garzik4e0af882007-09-27 06:41:25 -0400680 dev_printk(KERN_ERR, &card->dev->dev,
681 "Battery %d now FAILED\n", battery + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 return 1;
684 } else if (!card->battery[battery].good &&
685 !card->battery[battery].warned &&
686 time_after_eq(jiffies, card->battery[battery].last_change +
687 (HZ * 60 * 60 * 5))) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400688 dev_printk(KERN_ERR, &card->dev->dev,
689 "Battery %d still FAILED after 5 hours\n", battery + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 card->battery[battery].warned = 1;
691
692 return 1;
693 }
694
695 return 0;
696}
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698static void check_batteries(struct cardinfo *card)
699{
700 /* NOTE: this must *never* be called while the card
701 * is doing (bus-to-card) DMA, or you will need the
702 * reset switch
703 */
704 unsigned char status;
705 int ret1, ret2;
706
707 status = readb(card->csr_remap + MEMCTRLSTATUS_BATTERY);
708 if (debug & DEBUG_BATTERY_POLLING)
Jeff Garzik4e0af882007-09-27 06:41:25 -0400709 dev_printk(KERN_DEBUG, &card->dev->dev,
710 "checking battery status, 1 = %s, 2 = %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 (status & BATTERY_1_FAILURE) ? "FAILURE" : "OK",
712 (status & BATTERY_2_FAILURE) ? "FAILURE" : "OK");
713
714 ret1 = check_battery(card, 0, !(status & BATTERY_1_FAILURE));
715 ret2 = check_battery(card, 1, !(status & BATTERY_2_FAILURE));
716
717 if (ret1 || ret2)
718 set_fault_to_battery_status(card);
719}
720
721static void check_all_batteries(unsigned long ptr)
722{
723 int i;
724
Jeff Garzik4e953a22007-09-27 07:41:50 -0400725 for (i = 0; i < num_cards; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (!(cards[i].flags & UM_FLAG_NO_BATT)) {
727 struct cardinfo *card = &cards[i];
728 spin_lock_bh(&card->lock);
729 if (card->Active >= 0)
730 card->check_batteries = 1;
731 else
732 check_batteries(card);
733 spin_unlock_bh(&card->lock);
734 }
735
736 init_battery_timer();
737}
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739static void init_battery_timer(void)
740{
741 init_timer(&battery_timer);
742 battery_timer.function = check_all_batteries;
743 battery_timer.expires = jiffies + (HZ * 60);
744 add_timer(&battery_timer);
745}
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747static void del_battery_timer(void)
748{
749 del_timer(&battery_timer);
750}
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752/*
753 * Note no locks taken out here. In a worst case scenario, we could drop
754 * a chunk of system memory. But that should never happen, since validation
755 * happens at open or mount time, when locks are held.
756 *
757 * That's crap, since doing that while some partitions are opened
758 * or mounted will give you really nasty results.
759 */
760static int mm_revalidate(struct gendisk *disk)
761{
762 struct cardinfo *card = disk->private_data;
763 set_capacity(disk, card->mm_size << 1);
764 return 0;
765}
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800766
767static int mm_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800769 struct cardinfo *card = bdev->bd_disk->private_data;
770 int size = card->mm_size * (1024 / MM_HARDSECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800772 /*
773 * get geometry: we have to fake one... trim the size to a
774 * multiple of 2048 (1M): tell we have 32 sectors, 64 heads,
775 * whatever cylinders.
776 */
777 geo->heads = 64;
778 geo->sectors = 32;
779 geo->cylinders = size / (geo->heads * geo->sectors);
780 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783/*
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100784 * Future support for removable devices
785 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786static int mm_check_change(struct gendisk *disk)
787{
788/* struct cardinfo *dev = disk->private_data; */
789 return 0;
790}
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792static struct block_device_operations mm_fops = {
793 .owner = THIS_MODULE,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800794 .getgeo = mm_getgeo,
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100795 .revalidate_disk = mm_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 .media_changed = mm_check_change,
797};
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100798
799static int __devinit mm_pci_probe(struct pci_dev *dev,
800 const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 int ret = -ENODEV;
803 struct cardinfo *card = &cards[num_cards];
804 unsigned char mem_present;
805 unsigned char batt_status;
806 unsigned int saved_bar, data;
Jeff Garzikee4a7b62007-09-27 07:40:33 -0400807 unsigned long csr_base;
808 unsigned long csr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 int magic_number;
Jeff Garzik4e0af882007-09-27 06:41:25 -0400810 static int printed_version;
811
812 if (!printed_version++)
813 printk(KERN_INFO DRIVER_VERSION " : " DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Jeff Garzikee4a7b62007-09-27 07:40:33 -0400815 ret = pci_enable_device(dev);
816 if (ret)
817 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0xF8);
820 pci_set_master(dev);
821
822 card->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Jeff Garzikee4a7b62007-09-27 07:40:33 -0400824 csr_base = pci_resource_start(dev, 0);
825 csr_len = pci_resource_len(dev, 0);
826 if (!csr_base || !csr_len)
827 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Jeff Garzik4e0af882007-09-27 06:41:25 -0400829 dev_printk(KERN_INFO, &dev->dev,
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100830 "Micro Memory(tm) controller found (PCI Mem Module (Battery Backup))\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Yang Hongyang6a355282009-04-06 19:01:13 -0700832 if (pci_set_dma_mask(dev, DMA_BIT_MASK(64)) &&
Yang Hongyang284901a2009-04-06 19:01:15 -0700833 pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400834 dev_printk(KERN_WARNING, &dev->dev, "NO suitable DMA found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return -ENOMEM;
836 }
Jeff Garzikee4a7b62007-09-27 07:40:33 -0400837
838 ret = pci_request_regions(dev, DRIVER_NAME);
839 if (ret) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400840 dev_printk(KERN_ERR, &card->dev->dev,
841 "Unable to request memory region\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 goto failed_req_csr;
843 }
844
Jeff Garzikee4a7b62007-09-27 07:40:33 -0400845 card->csr_remap = ioremap_nocache(csr_base, csr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (!card->csr_remap) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400847 dev_printk(KERN_ERR, &card->dev->dev,
848 "Unable to remap memory region\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 ret = -ENOMEM;
850
851 goto failed_remap_csr;
852 }
853
Jeff Garzik4e0af882007-09-27 06:41:25 -0400854 dev_printk(KERN_INFO, &card->dev->dev,
855 "CSR 0x%08lx -> 0x%p (0x%lx)\n",
Jeff Garzikee4a7b62007-09-27 07:40:33 -0400856 csr_base, card->csr_remap, csr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100858 switch (card->dev->device) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 case 0x5415:
860 card->flags |= UM_FLAG_NO_BYTE_STATUS | UM_FLAG_NO_BATTREG;
861 magic_number = 0x59;
862 break;
863
864 case 0x5425:
865 card->flags |= UM_FLAG_NO_BYTE_STATUS;
866 magic_number = 0x5C;
867 break;
868
869 case 0x6155:
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100870 card->flags |= UM_FLAG_NO_BYTE_STATUS |
871 UM_FLAG_NO_BATTREG | UM_FLAG_NO_BATT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 magic_number = 0x99;
873 break;
874
875 default:
876 magic_number = 0x100;
877 break;
878 }
879
880 if (readb(card->csr_remap + MEMCTRLSTATUS_MAGIC) != magic_number) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400881 dev_printk(KERN_ERR, &card->dev->dev, "Magic number invalid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 ret = -ENOMEM;
883 goto failed_magic;
884 }
885
886 card->mm_pages[0].desc = pci_alloc_consistent(card->dev,
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100887 PAGE_SIZE * 2,
888 &card->mm_pages[0].page_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 card->mm_pages[1].desc = pci_alloc_consistent(card->dev,
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100890 PAGE_SIZE * 2,
891 &card->mm_pages[1].page_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (card->mm_pages[0].desc == NULL ||
893 card->mm_pages[1].desc == NULL) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400894 dev_printk(KERN_ERR, &card->dev->dev, "alloc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 goto failed_alloc;
896 }
897 reset_page(&card->mm_pages[0]);
898 reset_page(&card->mm_pages[1]);
899 card->Ready = 0; /* page 0 is ready */
900 card->Active = -1; /* no page is active */
901 card->bio = NULL;
902 card->biotail = &card->bio;
903
904 card->queue = blk_alloc_queue(GFP_KERNEL);
905 if (!card->queue)
906 goto failed_alloc;
907
908 blk_queue_make_request(card->queue, mm_make_request);
Sage Weilf3c737d2009-04-23 08:37:58 +0200909 card->queue->queue_lock = &card->lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 card->queue->queuedata = card;
911 card->queue->unplug_fn = mm_unplug_device;
912
913 tasklet_init(&card->tasklet, process_page, (unsigned long)card);
914
915 card->check_batteries = 0;
Jeff Garzik4e953a22007-09-27 07:41:50 -0400916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 mem_present = readb(card->csr_remap + MEMCTRLSTATUS_MEMORY);
918 switch (mem_present) {
919 case MEM_128_MB:
920 card->mm_size = 1024 * 128;
921 break;
922 case MEM_256_MB:
923 card->mm_size = 1024 * 256;
924 break;
925 case MEM_512_MB:
926 card->mm_size = 1024 * 512;
927 break;
928 case MEM_1_GB:
929 card->mm_size = 1024 * 1024;
930 break;
931 case MEM_2_GB:
932 card->mm_size = 1024 * 2048;
933 break;
934 default:
935 card->mm_size = 0;
936 break;
937 }
938
939 /* Clear the LED's we control */
940 set_led(card, LED_REMOVE, LED_OFF);
941 set_led(card, LED_FAULT, LED_OFF);
942
943 batt_status = readb(card->csr_remap + MEMCTRLSTATUS_BATTERY);
944
945 card->battery[0].good = !(batt_status & BATTERY_1_FAILURE);
946 card->battery[1].good = !(batt_status & BATTERY_2_FAILURE);
947 card->battery[0].last_change = card->battery[1].last_change = jiffies;
948
Jeff Garzik4e953a22007-09-27 07:41:50 -0400949 if (card->flags & UM_FLAG_NO_BATT)
Jeff Garzik4e0af882007-09-27 06:41:25 -0400950 dev_printk(KERN_INFO, &card->dev->dev,
951 "Size %d KB\n", card->mm_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 else {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400953 dev_printk(KERN_INFO, &card->dev->dev,
954 "Size %d KB, Battery 1 %s (%s), Battery 2 %s (%s)\n",
955 card->mm_size,
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100956 batt_status & BATTERY_1_DISABLED ? "Disabled" : "Enabled",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 card->battery[0].good ? "OK" : "FAILURE",
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100958 batt_status & BATTERY_2_DISABLED ? "Disabled" : "Enabled",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 card->battery[1].good ? "OK" : "FAILURE");
960
961 set_fault_to_battery_status(card);
962 }
963
964 pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &saved_bar);
965 data = 0xffffffff;
966 pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, data);
967 pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &data);
968 pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, saved_bar);
969 data &= 0xfffffff0;
970 data = ~data;
971 data += 1;
972
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100973 if (request_irq(dev->irq, mm_interrupt, IRQF_SHARED, DRIVER_NAME,
974 card)) {
Jeff Garzik4e0af882007-09-27 06:41:25 -0400975 dev_printk(KERN_ERR, &card->dev->dev,
976 "Unable to allocate IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 goto failed_req_irq;
979 }
980
Jeff Garzik4e0af882007-09-27 06:41:25 -0400981 dev_printk(KERN_INFO, &card->dev->dev,
Jeff Garzikee4a7b62007-09-27 07:40:33 -0400982 "Window size %d bytes, IRQ %d\n", data, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Randy Dunlap458cf5e2007-12-17 20:24:20 +0100984 spin_lock_init(&card->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 pci_set_drvdata(dev, card);
987
988 if (pci_write_cmd != 0x0F) /* If not Memory Write & Invalidate */
989 pci_write_cmd = 0x07; /* then Memory Write command */
990
991 if (pci_write_cmd & 0x08) { /* use Memory Write and Invalidate */
992 unsigned short cfg_command;
993 pci_read_config_word(dev, PCI_COMMAND, &cfg_command);
994 cfg_command |= 0x10; /* Memory Write & Invalidate Enable */
995 pci_write_config_word(dev, PCI_COMMAND, cfg_command);
996 }
997 pci_cmds = (pci_read_cmd << 28) | (pci_write_cmd << 24);
998
999 num_cards++;
1000
1001 if (!get_userbit(card, MEMORY_INITIALIZED)) {
Jeff Garzik4e0af882007-09-27 06:41:25 -04001002 dev_printk(KERN_INFO, &card->dev->dev,
Randy Dunlap458cf5e2007-12-17 20:24:20 +01001003 "memory NOT initialized. Consider over-writing whole device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 card->init_size = 0;
1005 } else {
Jeff Garzik4e0af882007-09-27 06:41:25 -04001006 dev_printk(KERN_INFO, &card->dev->dev,
1007 "memory already initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 card->init_size = card->mm_size;
1009 }
1010
1011 /* Enable ECC */
1012 writeb(EDC_STORE_CORRECT, card->csr_remap + MEMCTRLCMD_ERRCTRL);
1013
1014 return 0;
1015
1016 failed_req_irq:
1017 failed_alloc:
1018 if (card->mm_pages[0].desc)
1019 pci_free_consistent(card->dev, PAGE_SIZE*2,
1020 card->mm_pages[0].desc,
1021 card->mm_pages[0].page_dma);
1022 if (card->mm_pages[1].desc)
1023 pci_free_consistent(card->dev, PAGE_SIZE*2,
1024 card->mm_pages[1].desc,
1025 card->mm_pages[1].page_dma);
1026 failed_magic:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 iounmap(card->csr_remap);
1028 failed_remap_csr:
Jeff Garzikee4a7b62007-09-27 07:40:33 -04001029 pci_release_regions(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 failed_req_csr:
1031
1032 return ret;
1033}
Randy Dunlap458cf5e2007-12-17 20:24:20 +01001034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035static void mm_pci_remove(struct pci_dev *dev)
1036{
1037 struct cardinfo *card = pci_get_drvdata(dev);
1038
1039 tasklet_kill(&card->tasklet);
Jeff Garzikee4a7b62007-09-27 07:40:33 -04001040 free_irq(dev->irq, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 iounmap(card->csr_remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 if (card->mm_pages[0].desc)
1044 pci_free_consistent(card->dev, PAGE_SIZE*2,
1045 card->mm_pages[0].desc,
1046 card->mm_pages[0].page_dma);
1047 if (card->mm_pages[1].desc)
1048 pci_free_consistent(card->dev, PAGE_SIZE*2,
1049 card->mm_pages[1].desc,
1050 card->mm_pages[1].page_dma);
Al Viro1312f402006-03-12 11:02:03 -05001051 blk_cleanup_queue(card->queue);
Jeff Garzikee4a7b62007-09-27 07:40:33 -04001052
1053 pci_release_regions(dev);
1054 pci_disable_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
Neil Brown5874c182007-07-13 07:39:46 +02001057static const struct pci_device_id mm_pci_ids[] = {
Randy Dunlap458cf5e2007-12-17 20:24:20 +01001058 {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY, PCI_DEVICE_ID_MICRO_MEMORY_5415CN)},
1059 {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY, PCI_DEVICE_ID_MICRO_MEMORY_5425CN)},
1060 {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY, PCI_DEVICE_ID_MICRO_MEMORY_6155)},
Neil Brown5874c182007-07-13 07:39:46 +02001061 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 .vendor = 0x8086,
1063 .device = 0xB555,
Randy Dunlap458cf5e2007-12-17 20:24:20 +01001064 .subvendor = 0x1332,
1065 .subdevice = 0x5460,
1066 .class = 0x050000,
1067 .class_mask = 0,
Neil Brown5874c182007-07-13 07:39:46 +02001068 }, { /* end: all zeroes */ }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069};
1070
1071MODULE_DEVICE_TABLE(pci, mm_pci_ids);
1072
1073static struct pci_driver mm_pci_driver = {
Jeff Garzikee4a7b62007-09-27 07:40:33 -04001074 .name = DRIVER_NAME,
1075 .id_table = mm_pci_ids,
1076 .probe = mm_pci_probe,
1077 .remove = mm_pci_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078};
Jeff Garzikee4a7b62007-09-27 07:40:33 -04001079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080static int __init mm_init(void)
1081{
1082 int retval, i;
1083 int err;
1084
Richard Knutsson9bfab8c2005-11-30 00:59:34 +01001085 retval = pci_register_driver(&mm_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (retval)
1087 return -ENOMEM;
1088
Jeff Garzikcb3503c2007-09-27 07:49:39 -04001089 err = major_nr = register_blkdev(0, DRIVER_NAME);
NeilBrown5a243e02007-02-28 20:11:12 -08001090 if (err < 0) {
1091 pci_unregister_driver(&mm_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return -EIO;
NeilBrown5a243e02007-02-28 20:11:12 -08001093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 for (i = 0; i < num_cards; i++) {
1096 mm_gendisk[i] = alloc_disk(1 << MM_SHIFT);
1097 if (!mm_gendisk[i])
1098 goto out;
1099 }
1100
1101 for (i = 0; i < num_cards; i++) {
1102 struct gendisk *disk = mm_gendisk[i];
1103 sprintf(disk->disk_name, "umem%c", 'a'+i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 spin_lock_init(&cards[i].lock);
1105 disk->major = major_nr;
1106 disk->first_minor = i << MM_SHIFT;
1107 disk->fops = &mm_fops;
1108 disk->private_data = &cards[i];
1109 disk->queue = cards[i].queue;
1110 set_capacity(disk, cards[i].mm_size << 1);
1111 add_disk(disk);
1112 }
1113
1114 init_battery_timer();
Jeff Garzik4e0af882007-09-27 06:41:25 -04001115 printk(KERN_INFO "MM: desc_per_page = %ld\n", DESC_PER_PAGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116/* printk("mm_init: Done. 10-19-01 9:00\n"); */
1117 return 0;
1118
1119out:
NeilBrown5a243e02007-02-28 20:11:12 -08001120 pci_unregister_driver(&mm_pci_driver);
Jeff Garzikcb3503c2007-09-27 07:49:39 -04001121 unregister_blkdev(major_nr, DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 while (i--)
1123 put_disk(mm_gendisk[i]);
1124 return -ENOMEM;
1125}
Randy Dunlap458cf5e2007-12-17 20:24:20 +01001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127static void __exit mm_cleanup(void)
1128{
1129 int i;
1130
1131 del_battery_timer();
1132
Randy Dunlap458cf5e2007-12-17 20:24:20 +01001133 for (i = 0; i < num_cards ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 del_gendisk(mm_gendisk[i]);
1135 put_disk(mm_gendisk[i]);
1136 }
1137
1138 pci_unregister_driver(&mm_pci_driver);
1139
Jeff Garzikcb3503c2007-09-27 07:49:39 -04001140 unregister_blkdev(major_nr, DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
1143module_init(mm_init);
1144module_exit(mm_cleanup);
1145
1146MODULE_AUTHOR(DRIVER_AUTHOR);
1147MODULE_DESCRIPTION(DRIVER_DESC);
1148MODULE_LICENSE("GPL");