Drop 'size' argument from bio_endio and bi_end_io
[linux-2.6/mini2440.git] / drivers / block / umem.c
blobbe7fac86725e21c27957ea9f5737fa68b87a59fd
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 //#define DEBUG /* uncomment if you want debugging info (pr_debug) */
38 #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>
47 #include <linux/timer.h>
48 #include <linux/pci.h>
49 #include <linux/slab.h>
50 #include <linux/dma-mapping.h>
52 #include <linux/fcntl.h> /* O_ACCMODE */
53 #include <linux/hdreg.h> /* HDIO_GETGEO */
55 #include <linux/umem.h>
57 #include <asm/uaccess.h>
58 #include <asm/io.h>
60 #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 */
67 * Version Information
70 #define DRIVER_VERSION "v2.3"
71 #define DRIVER_AUTHOR "San Mehat, Johannes Erdfelt, NeilBrown"
72 #define DRIVER_DESC "Micro Memory(tm) PCI memory board block driver"
74 static int debug;
75 /* #define HW_TRACE(x) writeb(x,cards[0].csr_remap + MEMCTRLSTATUS_MAGIC) */
76 #define HW_TRACE(x)
78 #define DEBUG_LED_ON_TRANSFER 0x01
79 #define DEBUG_BATTERY_POLLING 0x02
81 module_param(debug, int, 0644);
82 MODULE_PARM_DESC(debug, "Debug bitmask");
84 static int pci_read_cmd = 0x0C; /* Read Multiple */
85 module_param(pci_read_cmd, int, 0);
86 MODULE_PARM_DESC(pci_read_cmd, "PCI read command");
88 static int pci_write_cmd = 0x0F; /* Write and Invalidate */
89 module_param(pci_write_cmd, int, 0);
90 MODULE_PARM_DESC(pci_write_cmd, "PCI write command");
92 static int pci_cmds;
94 static int major_nr;
96 #include <linux/blkdev.h>
97 #include <linux/blkpg.h>
99 struct cardinfo {
100 int card_number;
101 struct pci_dev *dev;
103 int irq;
105 unsigned long csr_base;
106 unsigned char __iomem *csr_remap;
107 unsigned long csr_len;
108 unsigned int win_size; /* PCI window size */
109 unsigned int mm_size; /* size in kbytes */
111 unsigned int init_size; /* initial segment, in sectors,
112 * that we know to
113 * have been written
115 struct bio *bio, *currentbio, **biotail;
116 int current_idx;
117 sector_t current_sector;
119 struct request_queue *queue;
121 struct mm_page {
122 dma_addr_t page_dma;
123 struct mm_dma_desc *desc;
124 int cnt, headcnt;
125 struct bio *bio, **biotail;
126 int idx;
127 } mm_pages[2];
128 #define DESC_PER_PAGE ((PAGE_SIZE*2)/sizeof(struct mm_dma_desc))
130 int Active, Ready;
132 struct tasklet_struct tasklet;
133 unsigned int dma_status;
135 struct {
136 int good;
137 int warned;
138 unsigned long last_change;
139 } battery[2];
141 spinlock_t lock;
142 int check_batteries;
144 int flags;
147 static struct cardinfo cards[MM_MAXCARDS];
148 static struct block_device_operations mm_fops;
149 static struct timer_list battery_timer;
151 static int num_cards = 0;
153 static struct gendisk *mm_gendisk[MM_MAXCARDS];
155 static void check_batteries(struct cardinfo *card);
158 -----------------------------------------------------------------------------------
159 -- get_userbit
160 -----------------------------------------------------------------------------------
162 static int get_userbit(struct cardinfo *card, int bit)
164 unsigned char led;
166 led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
167 return led & bit;
170 -----------------------------------------------------------------------------------
171 -- set_userbit
172 -----------------------------------------------------------------------------------
174 static int set_userbit(struct cardinfo *card, int bit, unsigned char state)
176 unsigned char led;
178 led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
179 if (state)
180 led |= bit;
181 else
182 led &= ~bit;
183 writeb(led, card->csr_remap + MEMCTRLCMD_LEDCTRL);
185 return 0;
188 -----------------------------------------------------------------------------------
189 -- set_led
190 -----------------------------------------------------------------------------------
193 * NOTE: For the power LED, use the LED_POWER_* macros since they differ
195 static void set_led(struct cardinfo *card, int shift, unsigned char state)
197 unsigned char led;
199 led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
200 if (state == LED_FLIP)
201 led ^= (1<<shift);
202 else {
203 led &= ~(0x03 << shift);
204 led |= (state << shift);
206 writeb(led, card->csr_remap + MEMCTRLCMD_LEDCTRL);
210 #ifdef MM_DIAG
212 -----------------------------------------------------------------------------------
213 -- dump_regs
214 -----------------------------------------------------------------------------------
216 static void dump_regs(struct cardinfo *card)
218 unsigned char *p;
219 int i, i1;
221 p = card->csr_remap;
222 for (i = 0; i < 8; i++) {
223 printk(KERN_DEBUG "%p ", p);
225 for (i1 = 0; i1 < 16; i1++)
226 printk("%02x ", *p++);
228 printk("\n");
231 #endif
233 -----------------------------------------------------------------------------------
234 -- dump_dmastat
235 -----------------------------------------------------------------------------------
237 static void dump_dmastat(struct cardinfo *card, unsigned int dmastat)
239 printk(KERN_DEBUG "MM%d*: DMAstat - ", card->card_number);
240 if (dmastat & DMASCR_ANY_ERR)
241 printk("ANY_ERR ");
242 if (dmastat & DMASCR_MBE_ERR)
243 printk("MBE_ERR ");
244 if (dmastat & DMASCR_PARITY_ERR_REP)
245 printk("PARITY_ERR_REP ");
246 if (dmastat & DMASCR_PARITY_ERR_DET)
247 printk("PARITY_ERR_DET ");
248 if (dmastat & DMASCR_SYSTEM_ERR_SIG)
249 printk("SYSTEM_ERR_SIG ");
250 if (dmastat & DMASCR_TARGET_ABT)
251 printk("TARGET_ABT ");
252 if (dmastat & DMASCR_MASTER_ABT)
253 printk("MASTER_ABT ");
254 if (dmastat & DMASCR_CHAIN_COMPLETE)
255 printk("CHAIN_COMPLETE ");
256 if (dmastat & DMASCR_DMA_COMPLETE)
257 printk("DMA_COMPLETE ");
258 printk("\n");
262 * Theory of request handling
264 * Each bio is assigned to one mm_dma_desc - which may not be enough FIXME
265 * We have two pages of mm_dma_desc, holding about 64 descriptors
266 * each. These are allocated at init time.
267 * One page is "Ready" and is either full, or can have request added.
268 * The other page might be "Active", which DMA is happening on it.
270 * Whenever IO on the active page completes, the Ready page is activated
271 * and the ex-Active page is clean out and made Ready.
272 * Otherwise the Ready page is only activated when it becomes full, or
273 * when mm_unplug_device is called via the unplug_io_fn.
275 * If a request arrives while both pages a full, it is queued, and b_rdev is
276 * overloaded to record whether it was a read or a write.
278 * The interrupt handler only polls the device to clear the interrupt.
279 * The processing of the result is done in a tasklet.
282 static void mm_start_io(struct cardinfo *card)
284 /* we have the lock, we know there is
285 * no IO active, and we know that card->Active
286 * is set
288 struct mm_dma_desc *desc;
289 struct mm_page *page;
290 int offset;
292 /* make the last descriptor end the chain */
293 page = &card->mm_pages[card->Active];
294 pr_debug("start_io: %d %d->%d\n", card->Active, page->headcnt, page->cnt-1);
295 desc = &page->desc[page->cnt-1];
297 desc->control_bits |= cpu_to_le32(DMASCR_CHAIN_COMP_EN);
298 desc->control_bits &= ~cpu_to_le32(DMASCR_CHAIN_EN);
299 desc->sem_control_bits = desc->control_bits;
302 if (debug & DEBUG_LED_ON_TRANSFER)
303 set_led(card, LED_REMOVE, LED_ON);
305 desc = &page->desc[page->headcnt];
306 writel(0, card->csr_remap + DMA_PCI_ADDR);
307 writel(0, card->csr_remap + DMA_PCI_ADDR + 4);
309 writel(0, card->csr_remap + DMA_LOCAL_ADDR);
310 writel(0, card->csr_remap + DMA_LOCAL_ADDR + 4);
312 writel(0, card->csr_remap + DMA_TRANSFER_SIZE);
313 writel(0, card->csr_remap + DMA_TRANSFER_SIZE + 4);
315 writel(0, card->csr_remap + DMA_SEMAPHORE_ADDR);
316 writel(0, card->csr_remap + DMA_SEMAPHORE_ADDR + 4);
318 offset = ((char*)desc) - ((char*)page->desc);
319 writel(cpu_to_le32((page->page_dma+offset)&0xffffffff),
320 card->csr_remap + DMA_DESCRIPTOR_ADDR);
321 /* Force the value to u64 before shifting otherwise >> 32 is undefined C
322 * and on some ports will do nothing ! */
323 writel(cpu_to_le32(((u64)page->page_dma)>>32),
324 card->csr_remap + DMA_DESCRIPTOR_ADDR + 4);
326 /* Go, go, go */
327 writel(cpu_to_le32(DMASCR_GO | DMASCR_CHAIN_EN | pci_cmds),
328 card->csr_remap + DMA_STATUS_CTRL);
331 static int add_bio(struct cardinfo *card);
333 static void activate(struct cardinfo *card)
335 /* if No page is Active, and Ready is
336 * not empty, then switch Ready page
337 * to active and start IO.
338 * Then add any bh's that are available to Ready
341 do {
342 while (add_bio(card))
345 if (card->Active == -1 &&
346 card->mm_pages[card->Ready].cnt > 0) {
347 card->Active = card->Ready;
348 card->Ready = 1-card->Ready;
349 mm_start_io(card);
352 } while (card->Active == -1 && add_bio(card));
355 static inline void reset_page(struct mm_page *page)
357 page->cnt = 0;
358 page->headcnt = 0;
359 page->bio = NULL;
360 page->biotail = & page->bio;
363 static void mm_unplug_device(struct request_queue *q)
365 struct cardinfo *card = q->queuedata;
366 unsigned long flags;
368 spin_lock_irqsave(&card->lock, flags);
369 if (blk_remove_plug(q))
370 activate(card);
371 spin_unlock_irqrestore(&card->lock, flags);
375 * If there is room on Ready page, take
376 * one bh off list and add it.
377 * return 1 if there was room, else 0.
379 static int add_bio(struct cardinfo *card)
381 struct mm_page *p;
382 struct mm_dma_desc *desc;
383 dma_addr_t dma_handle;
384 int offset;
385 struct bio *bio;
386 struct bio_vec *vec;
387 int idx;
388 int rw;
389 int len;
391 bio = card->currentbio;
392 if (!bio && card->bio) {
393 card->currentbio = card->bio;
394 card->current_idx = card->bio->bi_idx;
395 card->current_sector = card->bio->bi_sector;
396 card->bio = card->bio->bi_next;
397 if (card->bio == NULL)
398 card->biotail = &card->bio;
399 card->currentbio->bi_next = NULL;
400 return 1;
402 if (!bio)
403 return 0;
404 idx = card->current_idx;
406 rw = bio_rw(bio);
407 if (card->mm_pages[card->Ready].cnt >= DESC_PER_PAGE)
408 return 0;
410 vec = bio_iovec_idx(bio, idx);
411 len = vec->bv_len;
412 dma_handle = pci_map_page(card->dev,
413 vec->bv_page,
414 vec->bv_offset,
415 len,
416 (rw==READ) ?
417 PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);
419 p = &card->mm_pages[card->Ready];
420 desc = &p->desc[p->cnt];
421 p->cnt++;
422 if (p->bio == NULL)
423 p->idx = idx;
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(card->current_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 card->current_sector += (len >> 9);
450 idx++;
451 card->current_idx = idx;
452 if (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 = page->idx;
489 page->idx++;
490 if (page->idx >= bio->bi_vcnt) {
491 page->bio = bio->bi_next;
492 page->idx = page->bio->bi_idx;
495 pci_unmap_page(card->dev, desc->data_dma_handle,
496 bio_iovec_idx(bio,idx)->bv_len,
497 (control& DMASCR_TRANSFER_READ) ?
498 PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
499 if (control & DMASCR_HARD_ERROR) {
500 /* error */
501 clear_bit(BIO_UPTODATE, &bio->bi_flags);
502 printk(KERN_WARNING "MM%d: I/O error on sector %d/%d\n",
503 card->card_number,
504 le32_to_cpu(desc->local_addr)>>9,
505 le32_to_cpu(desc->transfer_size));
506 dump_dmastat(card, control);
507 } else if (test_bit(BIO_RW, &bio->bi_rw) &&
508 le32_to_cpu(desc->local_addr)>>9 == card->init_size) {
509 card->init_size += le32_to_cpu(desc->transfer_size)>>9;
510 if (card->init_size>>1 >= card->mm_size) {
511 printk(KERN_INFO "MM%d: memory now initialised\n",
512 card->card_number);
513 set_userbit(card, MEMORY_INITIALIZED, 1);
516 if (bio != page->bio) {
517 bio->bi_next = return_bio;
518 return_bio = bio;
521 if (last) break;
524 if (debug & DEBUG_LED_ON_TRANSFER)
525 set_led(card, LED_REMOVE, LED_OFF);
527 if (card->check_batteries) {
528 card->check_batteries = 0;
529 check_batteries(card);
531 if (page->headcnt >= page->cnt) {
532 reset_page(page);
533 card->Active = -1;
534 activate(card);
535 } else {
536 /* haven't finished with this one yet */
537 pr_debug("do some more\n");
538 mm_start_io(card);
540 out_unlock:
541 spin_unlock_bh(&card->lock);
543 while(return_bio) {
544 struct bio *bio = return_bio;
546 return_bio = bio->bi_next;
547 bio->bi_next = NULL;
548 bio_endio(bio, 0);
553 -----------------------------------------------------------------------------------
554 -- mm_make_request
555 -----------------------------------------------------------------------------------
557 static int mm_make_request(struct request_queue *q, struct bio *bio)
559 struct cardinfo *card = q->queuedata;
560 pr_debug("mm_make_request %llu %u\n",
561 (unsigned long long)bio->bi_sector, bio->bi_size);
563 spin_lock_irq(&card->lock);
564 *card->biotail = bio;
565 bio->bi_next = NULL;
566 card->biotail = &bio->bi_next;
567 blk_plug_device(q);
568 spin_unlock_irq(&card->lock);
570 return 0;
574 -----------------------------------------------------------------------------------
575 -- mm_interrupt
576 -----------------------------------------------------------------------------------
578 static irqreturn_t mm_interrupt(int irq, void *__card)
580 struct cardinfo *card = (struct cardinfo *) __card;
581 unsigned int dma_status;
582 unsigned short cfg_status;
584 HW_TRACE(0x30);
586 dma_status = le32_to_cpu(readl(card->csr_remap + DMA_STATUS_CTRL));
588 if (!(dma_status & (DMASCR_ERROR_MASK | DMASCR_CHAIN_COMPLETE))) {
589 /* interrupt wasn't for me ... */
590 return IRQ_NONE;
593 /* clear COMPLETION interrupts */
594 if (card->flags & UM_FLAG_NO_BYTE_STATUS)
595 writel(cpu_to_le32(DMASCR_DMA_COMPLETE|DMASCR_CHAIN_COMPLETE),
596 card->csr_remap+ DMA_STATUS_CTRL);
597 else
598 writeb((DMASCR_DMA_COMPLETE|DMASCR_CHAIN_COMPLETE) >> 16,
599 card->csr_remap+ DMA_STATUS_CTRL + 2);
601 /* log errors and clear interrupt status */
602 if (dma_status & DMASCR_ANY_ERR) {
603 unsigned int data_log1, data_log2;
604 unsigned int addr_log1, addr_log2;
605 unsigned char stat, count, syndrome, check;
607 stat = readb(card->csr_remap + MEMCTRLCMD_ERRSTATUS);
609 data_log1 = le32_to_cpu(readl(card->csr_remap + ERROR_DATA_LOG));
610 data_log2 = le32_to_cpu(readl(card->csr_remap + ERROR_DATA_LOG + 4));
611 addr_log1 = le32_to_cpu(readl(card->csr_remap + ERROR_ADDR_LOG));
612 addr_log2 = readb(card->csr_remap + ERROR_ADDR_LOG + 4);
614 count = readb(card->csr_remap + ERROR_COUNT);
615 syndrome = readb(card->csr_remap + ERROR_SYNDROME);
616 check = readb(card->csr_remap + ERROR_CHECK);
618 dump_dmastat(card, dma_status);
620 if (stat & 0x01)
621 printk(KERN_ERR "MM%d*: Memory access error detected (err count %d)\n",
622 card->card_number, count);
623 if (stat & 0x02)
624 printk(KERN_ERR "MM%d*: Multi-bit EDC error\n",
625 card->card_number);
627 printk(KERN_ERR "MM%d*: Fault Address 0x%02x%08x, Fault Data 0x%08x%08x\n",
628 card->card_number, addr_log2, addr_log1, data_log2, data_log1);
629 printk(KERN_ERR "MM%d*: Fault Check 0x%02x, Fault Syndrome 0x%02x\n",
630 card->card_number, check, syndrome);
632 writeb(0, card->csr_remap + ERROR_COUNT);
635 if (dma_status & DMASCR_PARITY_ERR_REP) {
636 printk(KERN_ERR "MM%d*: PARITY ERROR REPORTED\n", card->card_number);
637 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
638 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
641 if (dma_status & DMASCR_PARITY_ERR_DET) {
642 printk(KERN_ERR "MM%d*: PARITY ERROR DETECTED\n", card->card_number);
643 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
644 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
647 if (dma_status & DMASCR_SYSTEM_ERR_SIG) {
648 printk(KERN_ERR "MM%d*: SYSTEM ERROR\n", card->card_number);
649 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
650 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
653 if (dma_status & DMASCR_TARGET_ABT) {
654 printk(KERN_ERR "MM%d*: TARGET ABORT\n", card->card_number);
655 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
656 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
659 if (dma_status & DMASCR_MASTER_ABT) {
660 printk(KERN_ERR "MM%d*: MASTER ABORT\n", card->card_number);
661 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
662 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
665 /* and process the DMA descriptors */
666 card->dma_status = dma_status;
667 tasklet_schedule(&card->tasklet);
669 HW_TRACE(0x36);
671 return IRQ_HANDLED;
674 -----------------------------------------------------------------------------------
675 -- set_fault_to_battery_status
676 -----------------------------------------------------------------------------------
679 * If both batteries are good, no LED
680 * If either battery has been warned, solid LED
681 * If both batteries are bad, flash the LED quickly
682 * If either battery is bad, flash the LED semi quickly
684 static void set_fault_to_battery_status(struct cardinfo *card)
686 if (card->battery[0].good && card->battery[1].good)
687 set_led(card, LED_FAULT, LED_OFF);
688 else if (card->battery[0].warned || card->battery[1].warned)
689 set_led(card, LED_FAULT, LED_ON);
690 else if (!card->battery[0].good && !card->battery[1].good)
691 set_led(card, LED_FAULT, LED_FLASH_7_0);
692 else
693 set_led(card, LED_FAULT, LED_FLASH_3_5);
696 static void init_battery_timer(void);
700 -----------------------------------------------------------------------------------
701 -- check_battery
702 -----------------------------------------------------------------------------------
704 static int check_battery(struct cardinfo *card, int battery, int status)
706 if (status != card->battery[battery].good) {
707 card->battery[battery].good = !card->battery[battery].good;
708 card->battery[battery].last_change = jiffies;
710 if (card->battery[battery].good) {
711 printk(KERN_ERR "MM%d: Battery %d now good\n",
712 card->card_number, battery + 1);
713 card->battery[battery].warned = 0;
714 } else
715 printk(KERN_ERR "MM%d: Battery %d now FAILED\n",
716 card->card_number, battery + 1);
718 return 1;
719 } else if (!card->battery[battery].good &&
720 !card->battery[battery].warned &&
721 time_after_eq(jiffies, card->battery[battery].last_change +
722 (HZ * 60 * 60 * 5))) {
723 printk(KERN_ERR "MM%d: Battery %d still FAILED after 5 hours\n",
724 card->card_number, battery + 1);
725 card->battery[battery].warned = 1;
727 return 1;
730 return 0;
733 -----------------------------------------------------------------------------------
734 -- check_batteries
735 -----------------------------------------------------------------------------------
737 static void check_batteries(struct cardinfo *card)
739 /* NOTE: this must *never* be called while the card
740 * is doing (bus-to-card) DMA, or you will need the
741 * reset switch
743 unsigned char status;
744 int ret1, ret2;
746 status = readb(card->csr_remap + MEMCTRLSTATUS_BATTERY);
747 if (debug & DEBUG_BATTERY_POLLING)
748 printk(KERN_DEBUG "MM%d: checking battery status, 1 = %s, 2 = %s\n",
749 card->card_number,
750 (status & BATTERY_1_FAILURE) ? "FAILURE" : "OK",
751 (status & BATTERY_2_FAILURE) ? "FAILURE" : "OK");
753 ret1 = check_battery(card, 0, !(status & BATTERY_1_FAILURE));
754 ret2 = check_battery(card, 1, !(status & BATTERY_2_FAILURE));
756 if (ret1 || ret2)
757 set_fault_to_battery_status(card);
760 static void check_all_batteries(unsigned long ptr)
762 int i;
764 for (i = 0; i < num_cards; i++)
765 if (!(cards[i].flags & UM_FLAG_NO_BATT)) {
766 struct cardinfo *card = &cards[i];
767 spin_lock_bh(&card->lock);
768 if (card->Active >= 0)
769 card->check_batteries = 1;
770 else
771 check_batteries(card);
772 spin_unlock_bh(&card->lock);
775 init_battery_timer();
778 -----------------------------------------------------------------------------------
779 -- init_battery_timer
780 -----------------------------------------------------------------------------------
782 static void init_battery_timer(void)
784 init_timer(&battery_timer);
785 battery_timer.function = check_all_batteries;
786 battery_timer.expires = jiffies + (HZ * 60);
787 add_timer(&battery_timer);
790 -----------------------------------------------------------------------------------
791 -- del_battery_timer
792 -----------------------------------------------------------------------------------
794 static void del_battery_timer(void)
796 del_timer(&battery_timer);
799 -----------------------------------------------------------------------------------
800 -- mm_revalidate
801 -----------------------------------------------------------------------------------
804 * Note no locks taken out here. In a worst case scenario, we could drop
805 * a chunk of system memory. But that should never happen, since validation
806 * happens at open or mount time, when locks are held.
808 * That's crap, since doing that while some partitions are opened
809 * or mounted will give you really nasty results.
811 static int mm_revalidate(struct gendisk *disk)
813 struct cardinfo *card = disk->private_data;
814 set_capacity(disk, card->mm_size << 1);
815 return 0;
818 static int mm_getgeo(struct block_device *bdev, struct hd_geometry *geo)
820 struct cardinfo *card = bdev->bd_disk->private_data;
821 int size = card->mm_size * (1024 / MM_HARDSECT);
824 * get geometry: we have to fake one... trim the size to a
825 * multiple of 2048 (1M): tell we have 32 sectors, 64 heads,
826 * whatever cylinders.
828 geo->heads = 64;
829 geo->sectors = 32;
830 geo->cylinders = size / (geo->heads * geo->sectors);
831 return 0;
835 -----------------------------------------------------------------------------------
836 -- mm_check_change
837 -----------------------------------------------------------------------------------
838 Future support for removable devices
840 static int mm_check_change(struct gendisk *disk)
842 /* struct cardinfo *dev = disk->private_data; */
843 return 0;
846 -----------------------------------------------------------------------------------
847 -- mm_fops
848 -----------------------------------------------------------------------------------
850 static struct block_device_operations mm_fops = {
851 .owner = THIS_MODULE,
852 .getgeo = mm_getgeo,
853 .revalidate_disk= mm_revalidate,
854 .media_changed = mm_check_change,
857 -----------------------------------------------------------------------------------
858 -- mm_pci_probe
859 -----------------------------------------------------------------------------------
861 static int __devinit mm_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
863 int ret = -ENODEV;
864 struct cardinfo *card = &cards[num_cards];
865 unsigned char mem_present;
866 unsigned char batt_status;
867 unsigned int saved_bar, data;
868 int magic_number;
870 if (pci_enable_device(dev) < 0)
871 return -ENODEV;
873 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0xF8);
874 pci_set_master(dev);
876 card->dev = dev;
877 card->card_number = num_cards;
879 card->csr_base = pci_resource_start(dev, 0);
880 card->csr_len = pci_resource_len(dev, 0);
882 printk(KERN_INFO "Micro Memory(tm) controller #%d found at %02x:%02x (PCI Mem Module (Battery Backup))\n",
883 card->card_number, dev->bus->number, dev->devfn);
885 if (pci_set_dma_mask(dev, DMA_64BIT_MASK) &&
886 pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
887 printk(KERN_WARNING "MM%d: NO suitable DMA found\n",num_cards);
888 return -ENOMEM;
890 if (!request_mem_region(card->csr_base, card->csr_len, "Micro Memory")) {
891 printk(KERN_ERR "MM%d: Unable to request memory region\n", card->card_number);
892 ret = -ENOMEM;
894 goto failed_req_csr;
897 card->csr_remap = ioremap_nocache(card->csr_base, card->csr_len);
898 if (!card->csr_remap) {
899 printk(KERN_ERR "MM%d: Unable to remap memory region\n", card->card_number);
900 ret = -ENOMEM;
902 goto failed_remap_csr;
905 printk(KERN_INFO "MM%d: CSR 0x%08lx -> 0x%p (0x%lx)\n", card->card_number,
906 card->csr_base, card->csr_remap, card->csr_len);
908 switch(card->dev->device) {
909 case 0x5415:
910 card->flags |= UM_FLAG_NO_BYTE_STATUS | UM_FLAG_NO_BATTREG;
911 magic_number = 0x59;
912 break;
914 case 0x5425:
915 card->flags |= UM_FLAG_NO_BYTE_STATUS;
916 magic_number = 0x5C;
917 break;
919 case 0x6155:
920 card->flags |= UM_FLAG_NO_BYTE_STATUS | UM_FLAG_NO_BATTREG | UM_FLAG_NO_BATT;
921 magic_number = 0x99;
922 break;
924 default:
925 magic_number = 0x100;
926 break;
929 if (readb(card->csr_remap + MEMCTRLSTATUS_MAGIC) != magic_number) {
930 printk(KERN_ERR "MM%d: Magic number invalid\n", card->card_number);
931 ret = -ENOMEM;
932 goto failed_magic;
935 card->mm_pages[0].desc = pci_alloc_consistent(card->dev,
936 PAGE_SIZE*2,
937 &card->mm_pages[0].page_dma);
938 card->mm_pages[1].desc = pci_alloc_consistent(card->dev,
939 PAGE_SIZE*2,
940 &card->mm_pages[1].page_dma);
941 if (card->mm_pages[0].desc == NULL ||
942 card->mm_pages[1].desc == NULL) {
943 printk(KERN_ERR "MM%d: alloc failed\n", card->card_number);
944 goto failed_alloc;
946 reset_page(&card->mm_pages[0]);
947 reset_page(&card->mm_pages[1]);
948 card->Ready = 0; /* page 0 is ready */
949 card->Active = -1; /* no page is active */
950 card->bio = NULL;
951 card->biotail = &card->bio;
953 card->queue = blk_alloc_queue(GFP_KERNEL);
954 if (!card->queue)
955 goto failed_alloc;
957 blk_queue_make_request(card->queue, mm_make_request);
958 card->queue->queuedata = card;
959 card->queue->unplug_fn = mm_unplug_device;
961 tasklet_init(&card->tasklet, process_page, (unsigned long)card);
963 card->check_batteries = 0;
965 mem_present = readb(card->csr_remap + MEMCTRLSTATUS_MEMORY);
966 switch (mem_present) {
967 case MEM_128_MB:
968 card->mm_size = 1024 * 128;
969 break;
970 case MEM_256_MB:
971 card->mm_size = 1024 * 256;
972 break;
973 case MEM_512_MB:
974 card->mm_size = 1024 * 512;
975 break;
976 case MEM_1_GB:
977 card->mm_size = 1024 * 1024;
978 break;
979 case MEM_2_GB:
980 card->mm_size = 1024 * 2048;
981 break;
982 default:
983 card->mm_size = 0;
984 break;
987 /* Clear the LED's we control */
988 set_led(card, LED_REMOVE, LED_OFF);
989 set_led(card, LED_FAULT, LED_OFF);
991 batt_status = readb(card->csr_remap + MEMCTRLSTATUS_BATTERY);
993 card->battery[0].good = !(batt_status & BATTERY_1_FAILURE);
994 card->battery[1].good = !(batt_status & BATTERY_2_FAILURE);
995 card->battery[0].last_change = card->battery[1].last_change = jiffies;
997 if (card->flags & UM_FLAG_NO_BATT)
998 printk(KERN_INFO "MM%d: Size %d KB\n",
999 card->card_number, card->mm_size);
1000 else {
1001 printk(KERN_INFO "MM%d: Size %d KB, Battery 1 %s (%s), Battery 2 %s (%s)\n",
1002 card->card_number, card->mm_size,
1003 (batt_status & BATTERY_1_DISABLED ? "Disabled" : "Enabled"),
1004 card->battery[0].good ? "OK" : "FAILURE",
1005 (batt_status & BATTERY_2_DISABLED ? "Disabled" : "Enabled"),
1006 card->battery[1].good ? "OK" : "FAILURE");
1008 set_fault_to_battery_status(card);
1011 pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &saved_bar);
1012 data = 0xffffffff;
1013 pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, data);
1014 pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &data);
1015 pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, saved_bar);
1016 data &= 0xfffffff0;
1017 data = ~data;
1018 data += 1;
1020 card->win_size = data;
1023 if (request_irq(dev->irq, mm_interrupt, IRQF_SHARED, "pci-umem", card)) {
1024 printk(KERN_ERR "MM%d: Unable to allocate IRQ\n", card->card_number);
1025 ret = -ENODEV;
1027 goto failed_req_irq;
1030 card->irq = dev->irq;
1031 printk(KERN_INFO "MM%d: Window size %d bytes, IRQ %d\n", card->card_number,
1032 card->win_size, card->irq);
1034 spin_lock_init(&card->lock);
1036 pci_set_drvdata(dev, card);
1038 if (pci_write_cmd != 0x0F) /* If not Memory Write & Invalidate */
1039 pci_write_cmd = 0x07; /* then Memory Write command */
1041 if (pci_write_cmd & 0x08) { /* use Memory Write and Invalidate */
1042 unsigned short cfg_command;
1043 pci_read_config_word(dev, PCI_COMMAND, &cfg_command);
1044 cfg_command |= 0x10; /* Memory Write & Invalidate Enable */
1045 pci_write_config_word(dev, PCI_COMMAND, cfg_command);
1047 pci_cmds = (pci_read_cmd << 28) | (pci_write_cmd << 24);
1049 num_cards++;
1051 if (!get_userbit(card, MEMORY_INITIALIZED)) {
1052 printk(KERN_INFO "MM%d: memory NOT initialized. Consider over-writing whole device.\n", card->card_number);
1053 card->init_size = 0;
1054 } else {
1055 printk(KERN_INFO "MM%d: memory already initialized\n", card->card_number);
1056 card->init_size = card->mm_size;
1059 /* Enable ECC */
1060 writeb(EDC_STORE_CORRECT, card->csr_remap + MEMCTRLCMD_ERRCTRL);
1062 return 0;
1064 failed_req_irq:
1065 failed_alloc:
1066 if (card->mm_pages[0].desc)
1067 pci_free_consistent(card->dev, PAGE_SIZE*2,
1068 card->mm_pages[0].desc,
1069 card->mm_pages[0].page_dma);
1070 if (card->mm_pages[1].desc)
1071 pci_free_consistent(card->dev, PAGE_SIZE*2,
1072 card->mm_pages[1].desc,
1073 card->mm_pages[1].page_dma);
1074 failed_magic:
1075 iounmap(card->csr_remap);
1076 failed_remap_csr:
1077 release_mem_region(card->csr_base, card->csr_len);
1078 failed_req_csr:
1080 return ret;
1083 -----------------------------------------------------------------------------------
1084 -- mm_pci_remove
1085 -----------------------------------------------------------------------------------
1087 static void mm_pci_remove(struct pci_dev *dev)
1089 struct cardinfo *card = pci_get_drvdata(dev);
1091 tasklet_kill(&card->tasklet);
1092 iounmap(card->csr_remap);
1093 release_mem_region(card->csr_base, card->csr_len);
1094 free_irq(card->irq, card);
1096 if (card->mm_pages[0].desc)
1097 pci_free_consistent(card->dev, PAGE_SIZE*2,
1098 card->mm_pages[0].desc,
1099 card->mm_pages[0].page_dma);
1100 if (card->mm_pages[1].desc)
1101 pci_free_consistent(card->dev, PAGE_SIZE*2,
1102 card->mm_pages[1].desc,
1103 card->mm_pages[1].page_dma);
1104 blk_cleanup_queue(card->queue);
1107 static const struct pci_device_id mm_pci_ids[] = {
1108 {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY,PCI_DEVICE_ID_MICRO_MEMORY_5415CN)},
1109 {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY,PCI_DEVICE_ID_MICRO_MEMORY_5425CN)},
1110 {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY,PCI_DEVICE_ID_MICRO_MEMORY_6155)},
1112 .vendor = 0x8086,
1113 .device = 0xB555,
1114 .subvendor= 0x1332,
1115 .subdevice= 0x5460,
1116 .class = 0x050000,
1117 .class_mask= 0,
1118 }, { /* end: all zeroes */ }
1121 MODULE_DEVICE_TABLE(pci, mm_pci_ids);
1123 static struct pci_driver mm_pci_driver = {
1124 .name = "umem",
1125 .id_table = mm_pci_ids,
1126 .probe = mm_pci_probe,
1127 .remove = mm_pci_remove,
1130 -----------------------------------------------------------------------------------
1131 -- mm_init
1132 -----------------------------------------------------------------------------------
1135 static int __init mm_init(void)
1137 int retval, i;
1138 int err;
1140 printk(KERN_INFO DRIVER_VERSION " : " DRIVER_DESC "\n");
1142 retval = pci_register_driver(&mm_pci_driver);
1143 if (retval)
1144 return -ENOMEM;
1146 err = major_nr = register_blkdev(0, "umem");
1147 if (err < 0) {
1148 pci_unregister_driver(&mm_pci_driver);
1149 return -EIO;
1152 for (i = 0; i < num_cards; i++) {
1153 mm_gendisk[i] = alloc_disk(1 << MM_SHIFT);
1154 if (!mm_gendisk[i])
1155 goto out;
1158 for (i = 0; i < num_cards; i++) {
1159 struct gendisk *disk = mm_gendisk[i];
1160 sprintf(disk->disk_name, "umem%c", 'a'+i);
1161 spin_lock_init(&cards[i].lock);
1162 disk->major = major_nr;
1163 disk->first_minor = i << MM_SHIFT;
1164 disk->fops = &mm_fops;
1165 disk->private_data = &cards[i];
1166 disk->queue = cards[i].queue;
1167 set_capacity(disk, cards[i].mm_size << 1);
1168 add_disk(disk);
1171 init_battery_timer();
1172 printk("MM: desc_per_page = %ld\n", DESC_PER_PAGE);
1173 /* printk("mm_init: Done. 10-19-01 9:00\n"); */
1174 return 0;
1176 out:
1177 pci_unregister_driver(&mm_pci_driver);
1178 unregister_blkdev(major_nr, "umem");
1179 while (i--)
1180 put_disk(mm_gendisk[i]);
1181 return -ENOMEM;
1184 -----------------------------------------------------------------------------------
1185 -- mm_cleanup
1186 -----------------------------------------------------------------------------------
1188 static void __exit mm_cleanup(void)
1190 int i;
1192 del_battery_timer();
1194 for (i=0; i < num_cards ; i++) {
1195 del_gendisk(mm_gendisk[i]);
1196 put_disk(mm_gendisk[i]);
1199 pci_unregister_driver(&mm_pci_driver);
1201 unregister_blkdev(major_nr, "umem");
1204 module_init(mm_init);
1205 module_exit(mm_cleanup);
1207 MODULE_AUTHOR(DRIVER_AUTHOR);
1208 MODULE_DESCRIPTION(DRIVER_DESC);
1209 MODULE_LICENSE("GPL");