Automatic merge of rsync://rsync.kernel.org/pub/scm/linux/kernel/git/gregkh/driver...
[linux-2.6/verdex.git] / drivers / block / umem.c
blob0c4c121d2e7962bf798958ad2cee2df0e907527c
1 /*
2 * mm.c - Micro Memory(tm) PCI memory board block device driver - v2.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>
8 * This driver for the Micro Memory PCI Memory Module with Battery Backup
9 * is Copyright Micro Memory Inc 2001-2002. All rights reserved.
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.
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.
37 #include <linux/config.h>
38 #include <linux/sched.h>
39 #include <linux/fs.h>
40 #include <linux/bio.h>
41 #include <linux/kernel.h>
42 #include <linux/mm.h>
43 #include <linux/mman.h>
44 #include <linux/ioctl.h>
45 #include <linux/module.h>
46 #include <linux/init.h>
47 #include <linux/interrupt.h>
48 #include <linux/smp_lock.h>
49 #include <linux/timer.h>
50 #include <linux/pci.h>
51 #include <linux/slab.h>
53 #include <linux/fcntl.h> /* O_ACCMODE */
54 #include <linux/hdreg.h> /* HDIO_GETGEO */
56 #include <linux/umem.h>
58 #include <asm/uaccess.h>
59 #include <asm/io.h>
61 #define PRINTK(x...) do {} while (0)
62 #define dprintk(x...) do {} while (0)
63 /*#define dprintk(x...) printk(x) */
65 #define MM_MAXCARDS 4
66 #define MM_RAHEAD 2 /* two sectors */
67 #define MM_BLKSIZE 1024 /* 1k blocks */
68 #define MM_HARDSECT 512 /* 512-byte hardware sectors */
69 #define MM_SHIFT 6 /* max 64 partitions on 4 cards */
72 * Version Information
75 #define DRIVER_VERSION "v2.3"
76 #define DRIVER_AUTHOR "San Mehat, Johannes Erdfelt, NeilBrown"
77 #define DRIVER_DESC "Micro Memory(tm) PCI memory board block driver"
79 static int debug;
80 /* #define HW_TRACE(x) writeb(x,cards[0].csr_remap + MEMCTRLSTATUS_MAGIC) */
81 #define HW_TRACE(x)
83 #define DEBUG_LED_ON_TRANSFER 0x01
84 #define DEBUG_BATTERY_POLLING 0x02
86 module_param(debug, int, 0644);
87 MODULE_PARM_DESC(debug, "Debug bitmask");
89 static int pci_read_cmd = 0x0C; /* Read Multiple */
90 module_param(pci_read_cmd, int, 0);
91 MODULE_PARM_DESC(pci_read_cmd, "PCI read command");
93 static int pci_write_cmd = 0x0F; /* Write and Invalidate */
94 module_param(pci_write_cmd, int, 0);
95 MODULE_PARM_DESC(pci_write_cmd, "PCI write command");
97 static int pci_cmds;
99 static int major_nr;
101 #include <linux/blkdev.h>
102 #include <linux/blkpg.h>
104 struct cardinfo {
105 int card_number;
106 struct pci_dev *dev;
108 int irq;
110 unsigned long csr_base;
111 unsigned char __iomem *csr_remap;
112 unsigned long csr_len;
113 #ifdef CONFIG_MM_MAP_MEMORY
114 unsigned long mem_base;
115 unsigned char __iomem *mem_remap;
116 unsigned long mem_len;
117 #endif
119 unsigned int win_size; /* PCI window size */
120 unsigned int mm_size; /* size in kbytes */
122 unsigned int init_size; /* initial segment, in sectors,
123 * that we know to
124 * have been written
126 struct bio *bio, *currentbio, **biotail;
128 request_queue_t *queue;
130 struct mm_page {
131 dma_addr_t page_dma;
132 struct mm_dma_desc *desc;
133 int cnt, headcnt;
134 struct bio *bio, **biotail;
135 } mm_pages[2];
136 #define DESC_PER_PAGE ((PAGE_SIZE*2)/sizeof(struct mm_dma_desc))
138 int Active, Ready;
140 struct tasklet_struct tasklet;
141 unsigned int dma_status;
143 struct {
144 int good;
145 int warned;
146 unsigned long last_change;
147 } battery[2];
149 spinlock_t lock;
150 int check_batteries;
152 int flags;
155 static struct cardinfo cards[MM_MAXCARDS];
156 static struct block_device_operations mm_fops;
157 static struct timer_list battery_timer;
159 static int num_cards = 0;
161 static struct gendisk *mm_gendisk[MM_MAXCARDS];
163 static void check_batteries(struct cardinfo *card);
166 -----------------------------------------------------------------------------------
167 -- get_userbit
168 -----------------------------------------------------------------------------------
170 static int get_userbit(struct cardinfo *card, int bit)
172 unsigned char led;
174 led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
175 return led & bit;
178 -----------------------------------------------------------------------------------
179 -- set_userbit
180 -----------------------------------------------------------------------------------
182 static int set_userbit(struct cardinfo *card, int bit, unsigned char state)
184 unsigned char led;
186 led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
187 if (state)
188 led |= bit;
189 else
190 led &= ~bit;
191 writeb(led, card->csr_remap + MEMCTRLCMD_LEDCTRL);
193 return 0;
196 -----------------------------------------------------------------------------------
197 -- set_led
198 -----------------------------------------------------------------------------------
201 * NOTE: For the power LED, use the LED_POWER_* macros since they differ
203 static void set_led(struct cardinfo *card, int shift, unsigned char state)
205 unsigned char led;
207 led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
208 if (state == LED_FLIP)
209 led ^= (1<<shift);
210 else {
211 led &= ~(0x03 << shift);
212 led |= (state << shift);
214 writeb(led, card->csr_remap + MEMCTRLCMD_LEDCTRL);
218 #ifdef MM_DIAG
220 -----------------------------------------------------------------------------------
221 -- dump_regs
222 -----------------------------------------------------------------------------------
224 static void dump_regs(struct cardinfo *card)
226 unsigned char *p;
227 int i, i1;
229 p = card->csr_remap;
230 for (i = 0; i < 8; i++) {
231 printk(KERN_DEBUG "%p ", p);
233 for (i1 = 0; i1 < 16; i1++)
234 printk("%02x ", *p++);
236 printk("\n");
239 #endif
241 -----------------------------------------------------------------------------------
242 -- dump_dmastat
243 -----------------------------------------------------------------------------------
245 static void dump_dmastat(struct cardinfo *card, unsigned int dmastat)
247 printk(KERN_DEBUG "MM%d*: DMAstat - ", card->card_number);
248 if (dmastat & DMASCR_ANY_ERR)
249 printk("ANY_ERR ");
250 if (dmastat & DMASCR_MBE_ERR)
251 printk("MBE_ERR ");
252 if (dmastat & DMASCR_PARITY_ERR_REP)
253 printk("PARITY_ERR_REP ");
254 if (dmastat & DMASCR_PARITY_ERR_DET)
255 printk("PARITY_ERR_DET ");
256 if (dmastat & DMASCR_SYSTEM_ERR_SIG)
257 printk("SYSTEM_ERR_SIG ");
258 if (dmastat & DMASCR_TARGET_ABT)
259 printk("TARGET_ABT ");
260 if (dmastat & DMASCR_MASTER_ABT)
261 printk("MASTER_ABT ");
262 if (dmastat & DMASCR_CHAIN_COMPLETE)
263 printk("CHAIN_COMPLETE ");
264 if (dmastat & DMASCR_DMA_COMPLETE)
265 printk("DMA_COMPLETE ");
266 printk("\n");
270 * Theory of request handling
272 * Each bio is assigned to one mm_dma_desc - which may not be enough FIXME
273 * We have two pages of mm_dma_desc, holding about 64 descriptors
274 * each. These are allocated at init time.
275 * One page is "Ready" and is either full, or can have request added.
276 * The other page might be "Active", which DMA is happening on it.
278 * Whenever IO on the active page completes, the Ready page is activated
279 * and the ex-Active page is clean out and made Ready.
280 * Otherwise the Ready page is only activated when it becomes full, or
281 * when mm_unplug_device is called via the unplug_io_fn.
283 * If a request arrives while both pages a full, it is queued, and b_rdev is
284 * overloaded to record whether it was a read or a write.
286 * The interrupt handler only polls the device to clear the interrupt.
287 * The processing of the result is done in a tasklet.
290 static void mm_start_io(struct cardinfo *card)
292 /* we have the lock, we know there is
293 * no IO active, and we know that card->Active
294 * is set
296 struct mm_dma_desc *desc;
297 struct mm_page *page;
298 int offset;
300 /* make the last descriptor end the chain */
301 page = &card->mm_pages[card->Active];
302 PRINTK("start_io: %d %d->%d\n", card->Active, page->headcnt, page->cnt-1);
303 desc = &page->desc[page->cnt-1];
305 desc->control_bits |= cpu_to_le32(DMASCR_CHAIN_COMP_EN);
306 desc->control_bits &= ~cpu_to_le32(DMASCR_CHAIN_EN);
307 desc->sem_control_bits = desc->control_bits;
310 if (debug & DEBUG_LED_ON_TRANSFER)
311 set_led(card, LED_REMOVE, LED_ON);
313 desc = &page->desc[page->headcnt];
314 writel(0, card->csr_remap + DMA_PCI_ADDR);
315 writel(0, card->csr_remap + DMA_PCI_ADDR + 4);
317 writel(0, card->csr_remap + DMA_LOCAL_ADDR);
318 writel(0, card->csr_remap + DMA_LOCAL_ADDR + 4);
320 writel(0, card->csr_remap + DMA_TRANSFER_SIZE);
321 writel(0, card->csr_remap + DMA_TRANSFER_SIZE + 4);
323 writel(0, card->csr_remap + DMA_SEMAPHORE_ADDR);
324 writel(0, card->csr_remap + DMA_SEMAPHORE_ADDR + 4);
326 offset = ((char*)desc) - ((char*)page->desc);
327 writel(cpu_to_le32((page->page_dma+offset)&0xffffffff),
328 card->csr_remap + DMA_DESCRIPTOR_ADDR);
329 /* Force the value to u64 before shifting otherwise >> 32 is undefined C
330 * and on some ports will do nothing ! */
331 writel(cpu_to_le32(((u64)page->page_dma)>>32),
332 card->csr_remap + DMA_DESCRIPTOR_ADDR + 4);
334 /* Go, go, go */
335 writel(cpu_to_le32(DMASCR_GO | DMASCR_CHAIN_EN | pci_cmds),
336 card->csr_remap + DMA_STATUS_CTRL);
339 static int add_bio(struct cardinfo *card);
341 static void activate(struct cardinfo *card)
343 /* if No page is Active, and Ready is
344 * not empty, then switch Ready page
345 * to active and start IO.
346 * Then add any bh's that are available to Ready
349 do {
350 while (add_bio(card))
353 if (card->Active == -1 &&
354 card->mm_pages[card->Ready].cnt > 0) {
355 card->Active = card->Ready;
356 card->Ready = 1-card->Ready;
357 mm_start_io(card);
360 } while (card->Active == -1 && add_bio(card));
363 static inline void reset_page(struct mm_page *page)
365 page->cnt = 0;
366 page->headcnt = 0;
367 page->bio = NULL;
368 page->biotail = & page->bio;
371 static void mm_unplug_device(request_queue_t *q)
373 struct cardinfo *card = q->queuedata;
374 unsigned long flags;
376 spin_lock_irqsave(&card->lock, flags);
377 if (blk_remove_plug(q))
378 activate(card);
379 spin_unlock_irqrestore(&card->lock, flags);
383 * If there is room on Ready page, take
384 * one bh off list and add it.
385 * return 1 if there was room, else 0.
387 static int add_bio(struct cardinfo *card)
389 struct mm_page *p;
390 struct mm_dma_desc *desc;
391 dma_addr_t dma_handle;
392 int offset;
393 struct bio *bio;
394 int rw;
395 int len;
397 bio = card->currentbio;
398 if (!bio && card->bio) {
399 card->currentbio = card->bio;
400 card->bio = card->bio->bi_next;
401 if (card->bio == NULL)
402 card->biotail = &card->bio;
403 card->currentbio->bi_next = NULL;
404 return 1;
406 if (!bio)
407 return 0;
409 rw = bio_rw(bio);
410 if (card->mm_pages[card->Ready].cnt >= DESC_PER_PAGE)
411 return 0;
413 len = bio_iovec(bio)->bv_len;
414 dma_handle = pci_map_page(card->dev,
415 bio_page(bio),
416 bio_offset(bio),
417 len,
418 (rw==READ) ?
419 PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);
421 p = &card->mm_pages[card->Ready];
422 desc = &p->desc[p->cnt];
423 p->cnt++;
424 if ((p->biotail) != &bio->bi_next) {
425 *(p->biotail) = bio;
426 p->biotail = &(bio->bi_next);
427 bio->bi_next = NULL;
430 desc->data_dma_handle = dma_handle;
432 desc->pci_addr = cpu_to_le64((u64)desc->data_dma_handle);
433 desc->local_addr= cpu_to_le64(bio->bi_sector << 9);
434 desc->transfer_size = cpu_to_le32(len);
435 offset = ( ((char*)&desc->sem_control_bits) - ((char*)p->desc));
436 desc->sem_addr = cpu_to_le64((u64)(p->page_dma+offset));
437 desc->zero1 = desc->zero2 = 0;
438 offset = ( ((char*)(desc+1)) - ((char*)p->desc));
439 desc->next_desc_addr = cpu_to_le64(p->page_dma+offset);
440 desc->control_bits = cpu_to_le32(DMASCR_GO|DMASCR_ERR_INT_EN|
441 DMASCR_PARITY_INT_EN|
442 DMASCR_CHAIN_EN |
443 DMASCR_SEM_EN |
444 pci_cmds);
445 if (rw == WRITE)
446 desc->control_bits |= cpu_to_le32(DMASCR_TRANSFER_READ);
447 desc->sem_control_bits = desc->control_bits;
449 bio->bi_sector += (len>>9);
450 bio->bi_size -= len;
451 bio->bi_idx++;
452 if (bio->bi_idx >= bio->bi_vcnt)
453 card->currentbio = NULL;
455 return 1;
458 static void process_page(unsigned long data)
460 /* check if any of the requests in the page are DMA_COMPLETE,
461 * and deal with them appropriately.
462 * If we find a descriptor without DMA_COMPLETE in the semaphore, then
463 * dma must have hit an error on that descriptor, so use dma_status instead
464 * and assume that all following descriptors must be re-tried.
466 struct mm_page *page;
467 struct bio *return_bio=NULL;
468 struct cardinfo *card = (struct cardinfo *)data;
469 unsigned int dma_status = card->dma_status;
471 spin_lock_bh(&card->lock);
472 if (card->Active < 0)
473 goto out_unlock;
474 page = &card->mm_pages[card->Active];
476 while (page->headcnt < page->cnt) {
477 struct bio *bio = page->bio;
478 struct mm_dma_desc *desc = &page->desc[page->headcnt];
479 int control = le32_to_cpu(desc->sem_control_bits);
480 int last=0;
481 int idx;
483 if (!(control & DMASCR_DMA_COMPLETE)) {
484 control = dma_status;
485 last=1;
487 page->headcnt++;
488 idx = bio->bi_phys_segments;
489 bio->bi_phys_segments++;
490 if (bio->bi_phys_segments >= bio->bi_vcnt)
491 page->bio = bio->bi_next;
493 pci_unmap_page(card->dev, desc->data_dma_handle,
494 bio_iovec_idx(bio,idx)->bv_len,
495 (control& DMASCR_TRANSFER_READ) ?
496 PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
497 if (control & DMASCR_HARD_ERROR) {
498 /* error */
499 clear_bit(BIO_UPTODATE, &bio->bi_flags);
500 printk(KERN_WARNING "MM%d: I/O error on sector %d/%d\n",
501 card->card_number,
502 le32_to_cpu(desc->local_addr)>>9,
503 le32_to_cpu(desc->transfer_size));
504 dump_dmastat(card, control);
505 } else if (test_bit(BIO_RW, &bio->bi_rw) &&
506 le32_to_cpu(desc->local_addr)>>9 == card->init_size) {
507 card->init_size += le32_to_cpu(desc->transfer_size)>>9;
508 if (card->init_size>>1 >= card->mm_size) {
509 printk(KERN_INFO "MM%d: memory now initialised\n",
510 card->card_number);
511 set_userbit(card, MEMORY_INITIALIZED, 1);
514 if (bio != page->bio) {
515 bio->bi_next = return_bio;
516 return_bio = bio;
519 if (last) break;
522 if (debug & DEBUG_LED_ON_TRANSFER)
523 set_led(card, LED_REMOVE, LED_OFF);
525 if (card->check_batteries) {
526 card->check_batteries = 0;
527 check_batteries(card);
529 if (page->headcnt >= page->cnt) {
530 reset_page(page);
531 card->Active = -1;
532 activate(card);
533 } else {
534 /* haven't finished with this one yet */
535 PRINTK("do some more\n");
536 mm_start_io(card);
538 out_unlock:
539 spin_unlock_bh(&card->lock);
541 while(return_bio) {
542 struct bio *bio = return_bio;
544 return_bio = bio->bi_next;
545 bio->bi_next = NULL;
546 bio_endio(bio, bio->bi_size, 0);
551 -----------------------------------------------------------------------------------
552 -- mm_make_request
553 -----------------------------------------------------------------------------------
555 static int mm_make_request(request_queue_t *q, struct bio *bio)
557 struct cardinfo *card = q->queuedata;
558 PRINTK("mm_make_request %ld %d\n", bh->b_rsector, bh->b_size);
560 bio->bi_phys_segments = bio->bi_idx; /* count of completed segments*/
561 spin_lock_irq(&card->lock);
562 *card->biotail = bio;
563 bio->bi_next = NULL;
564 card->biotail = &bio->bi_next;
565 blk_plug_device(q);
566 spin_unlock_irq(&card->lock);
568 return 0;
572 -----------------------------------------------------------------------------------
573 -- mm_interrupt
574 -----------------------------------------------------------------------------------
576 static irqreturn_t mm_interrupt(int irq, void *__card, struct pt_regs *regs)
578 struct cardinfo *card = (struct cardinfo *) __card;
579 unsigned int dma_status;
580 unsigned short cfg_status;
582 HW_TRACE(0x30);
584 dma_status = le32_to_cpu(readl(card->csr_remap + DMA_STATUS_CTRL));
586 if (!(dma_status & (DMASCR_ERROR_MASK | DMASCR_CHAIN_COMPLETE))) {
587 /* interrupt wasn't for me ... */
588 return IRQ_NONE;
591 /* clear COMPLETION interrupts */
592 if (card->flags & UM_FLAG_NO_BYTE_STATUS)
593 writel(cpu_to_le32(DMASCR_DMA_COMPLETE|DMASCR_CHAIN_COMPLETE),
594 card->csr_remap+ DMA_STATUS_CTRL);
595 else
596 writeb((DMASCR_DMA_COMPLETE|DMASCR_CHAIN_COMPLETE) >> 16,
597 card->csr_remap+ DMA_STATUS_CTRL + 2);
599 /* log errors and clear interrupt status */
600 if (dma_status & DMASCR_ANY_ERR) {
601 unsigned int data_log1, data_log2;
602 unsigned int addr_log1, addr_log2;
603 unsigned char stat, count, syndrome, check;
605 stat = readb(card->csr_remap + MEMCTRLCMD_ERRSTATUS);
607 data_log1 = le32_to_cpu(readl(card->csr_remap + ERROR_DATA_LOG));
608 data_log2 = le32_to_cpu(readl(card->csr_remap + ERROR_DATA_LOG + 4));
609 addr_log1 = le32_to_cpu(readl(card->csr_remap + ERROR_ADDR_LOG));
610 addr_log2 = readb(card->csr_remap + ERROR_ADDR_LOG + 4);
612 count = readb(card->csr_remap + ERROR_COUNT);
613 syndrome = readb(card->csr_remap + ERROR_SYNDROME);
614 check = readb(card->csr_remap + ERROR_CHECK);
616 dump_dmastat(card, dma_status);
618 if (stat & 0x01)
619 printk(KERN_ERR "MM%d*: Memory access error detected (err count %d)\n",
620 card->card_number, count);
621 if (stat & 0x02)
622 printk(KERN_ERR "MM%d*: Multi-bit EDC error\n",
623 card->card_number);
625 printk(KERN_ERR "MM%d*: Fault Address 0x%02x%08x, Fault Data 0x%08x%08x\n",
626 card->card_number, addr_log2, addr_log1, data_log2, data_log1);
627 printk(KERN_ERR "MM%d*: Fault Check 0x%02x, Fault Syndrome 0x%02x\n",
628 card->card_number, check, syndrome);
630 writeb(0, card->csr_remap + ERROR_COUNT);
633 if (dma_status & DMASCR_PARITY_ERR_REP) {
634 printk(KERN_ERR "MM%d*: PARITY ERROR REPORTED\n", card->card_number);
635 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
636 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
639 if (dma_status & DMASCR_PARITY_ERR_DET) {
640 printk(KERN_ERR "MM%d*: PARITY ERROR DETECTED\n", card->card_number);
641 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
642 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
645 if (dma_status & DMASCR_SYSTEM_ERR_SIG) {
646 printk(KERN_ERR "MM%d*: SYSTEM ERROR\n", card->card_number);
647 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
648 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
651 if (dma_status & DMASCR_TARGET_ABT) {
652 printk(KERN_ERR "MM%d*: TARGET ABORT\n", card->card_number);
653 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
654 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
657 if (dma_status & DMASCR_MASTER_ABT) {
658 printk(KERN_ERR "MM%d*: MASTER ABORT\n", card->card_number);
659 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
660 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
663 /* and process the DMA descriptors */
664 card->dma_status = dma_status;
665 tasklet_schedule(&card->tasklet);
667 HW_TRACE(0x36);
669 return IRQ_HANDLED;
672 -----------------------------------------------------------------------------------
673 -- set_fault_to_battery_status
674 -----------------------------------------------------------------------------------
677 * If both batteries are good, no LED
678 * If either battery has been warned, solid LED
679 * If both batteries are bad, flash the LED quickly
680 * If either battery is bad, flash the LED semi quickly
682 static void set_fault_to_battery_status(struct cardinfo *card)
684 if (card->battery[0].good && card->battery[1].good)
685 set_led(card, LED_FAULT, LED_OFF);
686 else if (card->battery[0].warned || card->battery[1].warned)
687 set_led(card, LED_FAULT, LED_ON);
688 else if (!card->battery[0].good && !card->battery[1].good)
689 set_led(card, LED_FAULT, LED_FLASH_7_0);
690 else
691 set_led(card, LED_FAULT, LED_FLASH_3_5);
694 static void init_battery_timer(void);
698 -----------------------------------------------------------------------------------
699 -- check_battery
700 -----------------------------------------------------------------------------------
702 static int check_battery(struct cardinfo *card, int battery, int status)
704 if (status != card->battery[battery].good) {
705 card->battery[battery].good = !card->battery[battery].good;
706 card->battery[battery].last_change = jiffies;
708 if (card->battery[battery].good) {
709 printk(KERN_ERR "MM%d: Battery %d now good\n",
710 card->card_number, battery + 1);
711 card->battery[battery].warned = 0;
712 } else
713 printk(KERN_ERR "MM%d: Battery %d now FAILED\n",
714 card->card_number, battery + 1);
716 return 1;
717 } else if (!card->battery[battery].good &&
718 !card->battery[battery].warned &&
719 time_after_eq(jiffies, card->battery[battery].last_change +
720 (HZ * 60 * 60 * 5))) {
721 printk(KERN_ERR "MM%d: Battery %d still FAILED after 5 hours\n",
722 card->card_number, battery + 1);
723 card->battery[battery].warned = 1;
725 return 1;
728 return 0;
731 -----------------------------------------------------------------------------------
732 -- check_batteries
733 -----------------------------------------------------------------------------------
735 static void check_batteries(struct cardinfo *card)
737 /* NOTE: this must *never* be called while the card
738 * is doing (bus-to-card) DMA, or you will need the
739 * reset switch
741 unsigned char status;
742 int ret1, ret2;
744 status = readb(card->csr_remap + MEMCTRLSTATUS_BATTERY);
745 if (debug & DEBUG_BATTERY_POLLING)
746 printk(KERN_DEBUG "MM%d: checking battery status, 1 = %s, 2 = %s\n",
747 card->card_number,
748 (status & BATTERY_1_FAILURE) ? "FAILURE" : "OK",
749 (status & BATTERY_2_FAILURE) ? "FAILURE" : "OK");
751 ret1 = check_battery(card, 0, !(status & BATTERY_1_FAILURE));
752 ret2 = check_battery(card, 1, !(status & BATTERY_2_FAILURE));
754 if (ret1 || ret2)
755 set_fault_to_battery_status(card);
758 static void check_all_batteries(unsigned long ptr)
760 int i;
762 for (i = 0; i < num_cards; i++)
763 if (!(cards[i].flags & UM_FLAG_NO_BATT)) {
764 struct cardinfo *card = &cards[i];
765 spin_lock_bh(&card->lock);
766 if (card->Active >= 0)
767 card->check_batteries = 1;
768 else
769 check_batteries(card);
770 spin_unlock_bh(&card->lock);
773 init_battery_timer();
776 -----------------------------------------------------------------------------------
777 -- init_battery_timer
778 -----------------------------------------------------------------------------------
780 static void init_battery_timer(void)
782 init_timer(&battery_timer);
783 battery_timer.function = check_all_batteries;
784 battery_timer.expires = jiffies + (HZ * 60);
785 add_timer(&battery_timer);
788 -----------------------------------------------------------------------------------
789 -- del_battery_timer
790 -----------------------------------------------------------------------------------
792 static void del_battery_timer(void)
794 del_timer(&battery_timer);
797 -----------------------------------------------------------------------------------
798 -- mm_revalidate
799 -----------------------------------------------------------------------------------
802 * Note no locks taken out here. In a worst case scenario, we could drop
803 * a chunk of system memory. But that should never happen, since validation
804 * happens at open or mount time, when locks are held.
806 * That's crap, since doing that while some partitions are opened
807 * or mounted will give you really nasty results.
809 static int mm_revalidate(struct gendisk *disk)
811 struct cardinfo *card = disk->private_data;
812 set_capacity(disk, card->mm_size << 1);
813 return 0;
816 -----------------------------------------------------------------------------------
817 -- mm_ioctl
818 -----------------------------------------------------------------------------------
820 static int mm_ioctl(struct inode *i, struct file *f, unsigned int cmd, unsigned long arg)
822 if (cmd == HDIO_GETGEO) {
823 struct cardinfo *card = i->i_bdev->bd_disk->private_data;
824 int size = card->mm_size * (1024 / MM_HARDSECT);
825 struct hd_geometry geo;
827 * get geometry: we have to fake one... trim the size to a
828 * multiple of 2048 (1M): tell we have 32 sectors, 64 heads,
829 * whatever cylinders.
831 geo.heads = 64;
832 geo.sectors = 32;
833 geo.start = get_start_sect(i->i_bdev);
834 geo.cylinders = size / (geo.heads * geo.sectors);
836 if (copy_to_user((void __user *) arg, &geo, sizeof(geo)))
837 return -EFAULT;
838 return 0;
841 return -EINVAL;
844 -----------------------------------------------------------------------------------
845 -- mm_check_change
846 -----------------------------------------------------------------------------------
847 Future support for removable devices
849 static int mm_check_change(struct gendisk *disk)
851 /* struct cardinfo *dev = disk->private_data; */
852 return 0;
855 -----------------------------------------------------------------------------------
856 -- mm_fops
857 -----------------------------------------------------------------------------------
859 static struct block_device_operations mm_fops = {
860 .owner = THIS_MODULE,
861 .ioctl = mm_ioctl,
862 .revalidate_disk= mm_revalidate,
863 .media_changed = mm_check_change,
866 -----------------------------------------------------------------------------------
867 -- mm_pci_probe
868 -----------------------------------------------------------------------------------
870 static int __devinit mm_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
872 int ret = -ENODEV;
873 struct cardinfo *card = &cards[num_cards];
874 unsigned char mem_present;
875 unsigned char batt_status;
876 unsigned int saved_bar, data;
877 int magic_number;
879 if (pci_enable_device(dev) < 0)
880 return -ENODEV;
882 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0xF8);
883 pci_set_master(dev);
885 card->dev = dev;
886 card->card_number = num_cards;
888 card->csr_base = pci_resource_start(dev, 0);
889 card->csr_len = pci_resource_len(dev, 0);
890 #ifdef CONFIG_MM_MAP_MEMORY
891 card->mem_base = pci_resource_start(dev, 1);
892 card->mem_len = pci_resource_len(dev, 1);
893 #endif
895 printk(KERN_INFO "Micro Memory(tm) controller #%d found at %02x:%02x (PCI Mem Module (Battery Backup))\n",
896 card->card_number, dev->bus->number, dev->devfn);
898 if (pci_set_dma_mask(dev, 0xffffffffffffffffLL) &&
899 !pci_set_dma_mask(dev, 0xffffffffLL)) {
900 printk(KERN_WARNING "MM%d: NO suitable DMA found\n",num_cards);
901 return -ENOMEM;
903 if (!request_mem_region(card->csr_base, card->csr_len, "Micro Memory")) {
904 printk(KERN_ERR "MM%d: Unable to request memory region\n", card->card_number);
905 ret = -ENOMEM;
907 goto failed_req_csr;
910 card->csr_remap = ioremap_nocache(card->csr_base, card->csr_len);
911 if (!card->csr_remap) {
912 printk(KERN_ERR "MM%d: Unable to remap memory region\n", card->card_number);
913 ret = -ENOMEM;
915 goto failed_remap_csr;
918 printk(KERN_INFO "MM%d: CSR 0x%08lx -> 0x%p (0x%lx)\n", card->card_number,
919 card->csr_base, card->csr_remap, card->csr_len);
921 #ifdef CONFIG_MM_MAP_MEMORY
922 if (!request_mem_region(card->mem_base, card->mem_len, "Micro Memory")) {
923 printk(KERN_ERR "MM%d: Unable to request memory region\n", card->card_number);
924 ret = -ENOMEM;
926 goto failed_req_mem;
929 if (!(card->mem_remap = ioremap(card->mem_base, cards->mem_len))) {
930 printk(KERN_ERR "MM%d: Unable to remap memory region\n", card->card_number);
931 ret = -ENOMEM;
933 goto failed_remap_mem;
936 printk(KERN_INFO "MM%d: MEM 0x%8lx -> 0x%8lx (0x%lx)\n", card->card_number,
937 card->mem_base, card->mem_remap, card->mem_len);
938 #else
939 printk(KERN_INFO "MM%d: MEM area not remapped (CONFIG_MM_MAP_MEMORY not set)\n",
940 card->card_number);
941 #endif
942 switch(card->dev->device) {
943 case 0x5415:
944 card->flags |= UM_FLAG_NO_BYTE_STATUS | UM_FLAG_NO_BATTREG;
945 magic_number = 0x59;
946 break;
948 case 0x5425:
949 card->flags |= UM_FLAG_NO_BYTE_STATUS;
950 magic_number = 0x5C;
951 break;
953 case 0x6155:
954 card->flags |= UM_FLAG_NO_BYTE_STATUS | UM_FLAG_NO_BATTREG | UM_FLAG_NO_BATT;
955 magic_number = 0x99;
956 break;
958 default:
959 magic_number = 0x100;
960 break;
963 if (readb(card->csr_remap + MEMCTRLSTATUS_MAGIC) != magic_number) {
964 printk(KERN_ERR "MM%d: Magic number invalid\n", card->card_number);
965 ret = -ENOMEM;
966 goto failed_magic;
969 card->mm_pages[0].desc = pci_alloc_consistent(card->dev,
970 PAGE_SIZE*2,
971 &card->mm_pages[0].page_dma);
972 card->mm_pages[1].desc = pci_alloc_consistent(card->dev,
973 PAGE_SIZE*2,
974 &card->mm_pages[1].page_dma);
975 if (card->mm_pages[0].desc == NULL ||
976 card->mm_pages[1].desc == NULL) {
977 printk(KERN_ERR "MM%d: alloc failed\n", card->card_number);
978 goto failed_alloc;
980 reset_page(&card->mm_pages[0]);
981 reset_page(&card->mm_pages[1]);
982 card->Ready = 0; /* page 0 is ready */
983 card->Active = -1; /* no page is active */
984 card->bio = NULL;
985 card->biotail = &card->bio;
987 card->queue = blk_alloc_queue(GFP_KERNEL);
988 if (!card->queue)
989 goto failed_alloc;
991 blk_queue_make_request(card->queue, mm_make_request);
992 card->queue->queuedata = card;
993 card->queue->unplug_fn = mm_unplug_device;
995 tasklet_init(&card->tasklet, process_page, (unsigned long)card);
997 card->check_batteries = 0;
999 mem_present = readb(card->csr_remap + MEMCTRLSTATUS_MEMORY);
1000 switch (mem_present) {
1001 case MEM_128_MB:
1002 card->mm_size = 1024 * 128;
1003 break;
1004 case MEM_256_MB:
1005 card->mm_size = 1024 * 256;
1006 break;
1007 case MEM_512_MB:
1008 card->mm_size = 1024 * 512;
1009 break;
1010 case MEM_1_GB:
1011 card->mm_size = 1024 * 1024;
1012 break;
1013 case MEM_2_GB:
1014 card->mm_size = 1024 * 2048;
1015 break;
1016 default:
1017 card->mm_size = 0;
1018 break;
1021 /* Clear the LED's we control */
1022 set_led(card, LED_REMOVE, LED_OFF);
1023 set_led(card, LED_FAULT, LED_OFF);
1025 batt_status = readb(card->csr_remap + MEMCTRLSTATUS_BATTERY);
1027 card->battery[0].good = !(batt_status & BATTERY_1_FAILURE);
1028 card->battery[1].good = !(batt_status & BATTERY_2_FAILURE);
1029 card->battery[0].last_change = card->battery[1].last_change = jiffies;
1031 if (card->flags & UM_FLAG_NO_BATT)
1032 printk(KERN_INFO "MM%d: Size %d KB\n",
1033 card->card_number, card->mm_size);
1034 else {
1035 printk(KERN_INFO "MM%d: Size %d KB, Battery 1 %s (%s), Battery 2 %s (%s)\n",
1036 card->card_number, card->mm_size,
1037 (batt_status & BATTERY_1_DISABLED ? "Disabled" : "Enabled"),
1038 card->battery[0].good ? "OK" : "FAILURE",
1039 (batt_status & BATTERY_2_DISABLED ? "Disabled" : "Enabled"),
1040 card->battery[1].good ? "OK" : "FAILURE");
1042 set_fault_to_battery_status(card);
1045 pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &saved_bar);
1046 data = 0xffffffff;
1047 pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, data);
1048 pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &data);
1049 pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, saved_bar);
1050 data &= 0xfffffff0;
1051 data = ~data;
1052 data += 1;
1054 card->win_size = data;
1057 if (request_irq(dev->irq, mm_interrupt, SA_SHIRQ, "pci-umem", card)) {
1058 printk(KERN_ERR "MM%d: Unable to allocate IRQ\n", card->card_number);
1059 ret = -ENODEV;
1061 goto failed_req_irq;
1064 card->irq = dev->irq;
1065 printk(KERN_INFO "MM%d: Window size %d bytes, IRQ %d\n", card->card_number,
1066 card->win_size, card->irq);
1068 spin_lock_init(&card->lock);
1070 pci_set_drvdata(dev, card);
1072 if (pci_write_cmd != 0x0F) /* If not Memory Write & Invalidate */
1073 pci_write_cmd = 0x07; /* then Memory Write command */
1075 if (pci_write_cmd & 0x08) { /* use Memory Write and Invalidate */
1076 unsigned short cfg_command;
1077 pci_read_config_word(dev, PCI_COMMAND, &cfg_command);
1078 cfg_command |= 0x10; /* Memory Write & Invalidate Enable */
1079 pci_write_config_word(dev, PCI_COMMAND, cfg_command);
1081 pci_cmds = (pci_read_cmd << 28) | (pci_write_cmd << 24);
1083 num_cards++;
1085 if (!get_userbit(card, MEMORY_INITIALIZED)) {
1086 printk(KERN_INFO "MM%d: memory NOT initialized. Consider over-writing whole device.\n", card->card_number);
1087 card->init_size = 0;
1088 } else {
1089 printk(KERN_INFO "MM%d: memory already initialized\n", card->card_number);
1090 card->init_size = card->mm_size;
1093 /* Enable ECC */
1094 writeb(EDC_STORE_CORRECT, card->csr_remap + MEMCTRLCMD_ERRCTRL);
1096 return 0;
1098 failed_req_irq:
1099 failed_alloc:
1100 if (card->mm_pages[0].desc)
1101 pci_free_consistent(card->dev, PAGE_SIZE*2,
1102 card->mm_pages[0].desc,
1103 card->mm_pages[0].page_dma);
1104 if (card->mm_pages[1].desc)
1105 pci_free_consistent(card->dev, PAGE_SIZE*2,
1106 card->mm_pages[1].desc,
1107 card->mm_pages[1].page_dma);
1108 failed_magic:
1109 #ifdef CONFIG_MM_MAP_MEMORY
1110 iounmap(card->mem_remap);
1111 failed_remap_mem:
1112 release_mem_region(card->mem_base, card->mem_len);
1113 failed_req_mem:
1114 #endif
1115 iounmap(card->csr_remap);
1116 failed_remap_csr:
1117 release_mem_region(card->csr_base, card->csr_len);
1118 failed_req_csr:
1120 return ret;
1123 -----------------------------------------------------------------------------------
1124 -- mm_pci_remove
1125 -----------------------------------------------------------------------------------
1127 static void mm_pci_remove(struct pci_dev *dev)
1129 struct cardinfo *card = pci_get_drvdata(dev);
1131 tasklet_kill(&card->tasklet);
1132 iounmap(card->csr_remap);
1133 release_mem_region(card->csr_base, card->csr_len);
1134 #ifdef CONFIG_MM_MAP_MEMORY
1135 iounmap(card->mem_remap);
1136 release_mem_region(card->mem_base, card->mem_len);
1137 #endif
1138 free_irq(card->irq, card);
1140 if (card->mm_pages[0].desc)
1141 pci_free_consistent(card->dev, PAGE_SIZE*2,
1142 card->mm_pages[0].desc,
1143 card->mm_pages[0].page_dma);
1144 if (card->mm_pages[1].desc)
1145 pci_free_consistent(card->dev, PAGE_SIZE*2,
1146 card->mm_pages[1].desc,
1147 card->mm_pages[1].page_dma);
1148 blk_put_queue(card->queue);
1151 static const struct pci_device_id mm_pci_ids[] = { {
1152 .vendor = PCI_VENDOR_ID_MICRO_MEMORY,
1153 .device = PCI_DEVICE_ID_MICRO_MEMORY_5415CN,
1154 }, {
1155 .vendor = PCI_VENDOR_ID_MICRO_MEMORY,
1156 .device = PCI_DEVICE_ID_MICRO_MEMORY_5425CN,
1157 }, {
1158 .vendor = PCI_VENDOR_ID_MICRO_MEMORY,
1159 .device = PCI_DEVICE_ID_MICRO_MEMORY_6155,
1160 }, {
1161 .vendor = 0x8086,
1162 .device = 0xB555,
1163 .subvendor= 0x1332,
1164 .subdevice= 0x5460,
1165 .class = 0x050000,
1166 .class_mask= 0,
1167 }, { /* end: all zeroes */ }
1170 MODULE_DEVICE_TABLE(pci, mm_pci_ids);
1172 static struct pci_driver mm_pci_driver = {
1173 .name = "umem",
1174 .id_table = mm_pci_ids,
1175 .probe = mm_pci_probe,
1176 .remove = mm_pci_remove,
1179 -----------------------------------------------------------------------------------
1180 -- mm_init
1181 -----------------------------------------------------------------------------------
1184 static int __init mm_init(void)
1186 int retval, i;
1187 int err;
1189 printk(KERN_INFO DRIVER_VERSION " : " DRIVER_DESC "\n");
1191 retval = pci_module_init(&mm_pci_driver);
1192 if (retval)
1193 return -ENOMEM;
1195 err = major_nr = register_blkdev(0, "umem");
1196 if (err < 0)
1197 return -EIO;
1199 for (i = 0; i < num_cards; i++) {
1200 mm_gendisk[i] = alloc_disk(1 << MM_SHIFT);
1201 if (!mm_gendisk[i])
1202 goto out;
1205 for (i = 0; i < num_cards; i++) {
1206 struct gendisk *disk = mm_gendisk[i];
1207 sprintf(disk->disk_name, "umem%c", 'a'+i);
1208 sprintf(disk->devfs_name, "umem/card%d", i);
1209 spin_lock_init(&cards[i].lock);
1210 disk->major = major_nr;
1211 disk->first_minor = i << MM_SHIFT;
1212 disk->fops = &mm_fops;
1213 disk->private_data = &cards[i];
1214 disk->queue = cards[i].queue;
1215 set_capacity(disk, cards[i].mm_size << 1);
1216 add_disk(disk);
1219 init_battery_timer();
1220 printk("MM: desc_per_page = %ld\n", DESC_PER_PAGE);
1221 /* printk("mm_init: Done. 10-19-01 9:00\n"); */
1222 return 0;
1224 out:
1225 unregister_blkdev(major_nr, "umem");
1226 while (i--)
1227 put_disk(mm_gendisk[i]);
1228 return -ENOMEM;
1231 -----------------------------------------------------------------------------------
1232 -- mm_cleanup
1233 -----------------------------------------------------------------------------------
1235 static void __exit mm_cleanup(void)
1237 int i;
1239 del_battery_timer();
1241 for (i=0; i < num_cards ; i++) {
1242 del_gendisk(mm_gendisk[i]);
1243 put_disk(mm_gendisk[i]);
1246 pci_unregister_driver(&mm_pci_driver);
1248 unregister_blkdev(major_nr, "umem");
1251 module_init(mm_init);
1252 module_exit(mm_cleanup);
1254 MODULE_AUTHOR(DRIVER_AUTHOR);
1255 MODULE_DESCRIPTION(DRIVER_DESC);
1256 MODULE_LICENSE("GPL");