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
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) */
39 #include <linux/bio.h>
40 #include <linux/kernel.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 */
57 #include <asm/uaccess.h>
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 */
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"
75 /* #define HW_TRACE(x) writeb(x,cards[0].csr_remap + MEMCTRLSTATUS_MAGIC) */
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");
96 #include <linux/blkdev.h>
97 #include <linux/blkpg.h>
104 unsigned long csr_base;
105 unsigned char __iomem *csr_remap;
106 unsigned long csr_len;
107 unsigned int win_size; /* PCI window size */
108 unsigned int mm_size; /* size in kbytes */
110 unsigned int init_size; /* initial segment, in sectors,
114 struct bio *bio, *currentbio, **biotail;
116 sector_t current_sector;
118 struct request_queue *queue;
122 struct mm_dma_desc *desc;
124 struct bio *bio, **biotail;
127 #define DESC_PER_PAGE ((PAGE_SIZE*2)/sizeof(struct mm_dma_desc))
131 struct tasklet_struct tasklet;
132 unsigned int dma_status;
137 unsigned long last_change;
146 static struct cardinfo cards[MM_MAXCARDS];
147 static struct block_device_operations mm_fops;
148 static struct timer_list battery_timer;
150 static int num_cards = 0;
152 static struct gendisk *mm_gendisk[MM_MAXCARDS];
154 static void check_batteries(struct cardinfo *card);
157 -----------------------------------------------------------------------------------
159 -----------------------------------------------------------------------------------
161 static int get_userbit(struct cardinfo *card, int bit)
165 led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
169 -----------------------------------------------------------------------------------
171 -----------------------------------------------------------------------------------
173 static int set_userbit(struct cardinfo *card, int bit, unsigned char state)
177 led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
182 writeb(led, card->csr_remap + MEMCTRLCMD_LEDCTRL);
187 -----------------------------------------------------------------------------------
189 -----------------------------------------------------------------------------------
192 * NOTE: For the power LED, use the LED_POWER_* macros since they differ
194 static void set_led(struct cardinfo *card, int shift, unsigned char state)
198 led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
199 if (state == LED_FLIP)
202 led &= ~(0x03 << shift);
203 led |= (state << shift);
205 writeb(led, card->csr_remap + MEMCTRLCMD_LEDCTRL);
211 -----------------------------------------------------------------------------------
213 -----------------------------------------------------------------------------------
215 static void dump_regs(struct cardinfo *card)
221 for (i = 0; i < 8; i++) {
222 printk(KERN_DEBUG "%p ", p);
224 for (i1 = 0; i1 < 16; i1++)
225 printk("%02x ", *p++);
232 -----------------------------------------------------------------------------------
234 -----------------------------------------------------------------------------------
236 static void dump_dmastat(struct cardinfo *card, unsigned int dmastat)
238 dev_printk(KERN_DEBUG, &card->dev->dev, "DMAstat - ");
239 if (dmastat & DMASCR_ANY_ERR)
241 if (dmastat & DMASCR_MBE_ERR)
243 if (dmastat & DMASCR_PARITY_ERR_REP)
244 printk("PARITY_ERR_REP ");
245 if (dmastat & DMASCR_PARITY_ERR_DET)
246 printk("PARITY_ERR_DET ");
247 if (dmastat & DMASCR_SYSTEM_ERR_SIG)
248 printk("SYSTEM_ERR_SIG ");
249 if (dmastat & DMASCR_TARGET_ABT)
250 printk("TARGET_ABT ");
251 if (dmastat & DMASCR_MASTER_ABT)
252 printk("MASTER_ABT ");
253 if (dmastat & DMASCR_CHAIN_COMPLETE)
254 printk("CHAIN_COMPLETE ");
255 if (dmastat & DMASCR_DMA_COMPLETE)
256 printk("DMA_COMPLETE ");
261 * Theory of request handling
263 * Each bio is assigned to one mm_dma_desc - which may not be enough FIXME
264 * We have two pages of mm_dma_desc, holding about 64 descriptors
265 * each. These are allocated at init time.
266 * One page is "Ready" and is either full, or can have request added.
267 * The other page might be "Active", which DMA is happening on it.
269 * Whenever IO on the active page completes, the Ready page is activated
270 * and the ex-Active page is clean out and made Ready.
271 * Otherwise the Ready page is only activated when it becomes full, or
272 * when mm_unplug_device is called via the unplug_io_fn.
274 * If a request arrives while both pages a full, it is queued, and b_rdev is
275 * overloaded to record whether it was a read or a write.
277 * The interrupt handler only polls the device to clear the interrupt.
278 * The processing of the result is done in a tasklet.
281 static void mm_start_io(struct cardinfo *card)
283 /* we have the lock, we know there is
284 * no IO active, and we know that card->Active
287 struct mm_dma_desc *desc;
288 struct mm_page *page;
291 /* make the last descriptor end the chain */
292 page = &card->mm_pages[card->Active];
293 pr_debug("start_io: %d %d->%d\n", card->Active, page->headcnt, page->cnt-1);
294 desc = &page->desc[page->cnt-1];
296 desc->control_bits |= cpu_to_le32(DMASCR_CHAIN_COMP_EN);
297 desc->control_bits &= ~cpu_to_le32(DMASCR_CHAIN_EN);
298 desc->sem_control_bits = desc->control_bits;
301 if (debug & DEBUG_LED_ON_TRANSFER)
302 set_led(card, LED_REMOVE, LED_ON);
304 desc = &page->desc[page->headcnt];
305 writel(0, card->csr_remap + DMA_PCI_ADDR);
306 writel(0, card->csr_remap + DMA_PCI_ADDR + 4);
308 writel(0, card->csr_remap + DMA_LOCAL_ADDR);
309 writel(0, card->csr_remap + DMA_LOCAL_ADDR + 4);
311 writel(0, card->csr_remap + DMA_TRANSFER_SIZE);
312 writel(0, card->csr_remap + DMA_TRANSFER_SIZE + 4);
314 writel(0, card->csr_remap + DMA_SEMAPHORE_ADDR);
315 writel(0, card->csr_remap + DMA_SEMAPHORE_ADDR + 4);
317 offset = ((char*)desc) - ((char*)page->desc);
318 writel(cpu_to_le32((page->page_dma+offset)&0xffffffff),
319 card->csr_remap + DMA_DESCRIPTOR_ADDR);
320 /* Force the value to u64 before shifting otherwise >> 32 is undefined C
321 * and on some ports will do nothing ! */
322 writel(cpu_to_le32(((u64)page->page_dma)>>32),
323 card->csr_remap + DMA_DESCRIPTOR_ADDR + 4);
326 writel(cpu_to_le32(DMASCR_GO | DMASCR_CHAIN_EN | pci_cmds),
327 card->csr_remap + DMA_STATUS_CTRL);
330 static int add_bio(struct cardinfo *card);
332 static void activate(struct cardinfo *card)
334 /* if No page is Active, and Ready is
335 * not empty, then switch Ready page
336 * to active and start IO.
337 * Then add any bh's that are available to Ready
341 while (add_bio(card))
344 if (card->Active == -1 &&
345 card->mm_pages[card->Ready].cnt > 0) {
346 card->Active = card->Ready;
347 card->Ready = 1-card->Ready;
351 } while (card->Active == -1 && add_bio(card));
354 static inline void reset_page(struct mm_page *page)
359 page->biotail = & page->bio;
362 static void mm_unplug_device(struct request_queue *q)
364 struct cardinfo *card = q->queuedata;
367 spin_lock_irqsave(&card->lock, flags);
368 if (blk_remove_plug(q))
370 spin_unlock_irqrestore(&card->lock, flags);
374 * If there is room on Ready page, take
375 * one bh off list and add it.
376 * return 1 if there was room, else 0.
378 static int add_bio(struct cardinfo *card)
381 struct mm_dma_desc *desc;
382 dma_addr_t dma_handle;
390 bio = card->currentbio;
391 if (!bio && card->bio) {
392 card->currentbio = card->bio;
393 card->current_idx = card->bio->bi_idx;
394 card->current_sector = card->bio->bi_sector;
395 card->bio = card->bio->bi_next;
396 if (card->bio == NULL)
397 card->biotail = &card->bio;
398 card->currentbio->bi_next = NULL;
403 idx = card->current_idx;
406 if (card->mm_pages[card->Ready].cnt >= DESC_PER_PAGE)
409 vec = bio_iovec_idx(bio, idx);
411 dma_handle = pci_map_page(card->dev,
416 PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);
418 p = &card->mm_pages[card->Ready];
419 desc = &p->desc[p->cnt];
423 if ((p->biotail) != &bio->bi_next) {
425 p->biotail = &(bio->bi_next);
429 desc->data_dma_handle = dma_handle;
431 desc->pci_addr = cpu_to_le64((u64)desc->data_dma_handle);
432 desc->local_addr = cpu_to_le64(card->current_sector << 9);
433 desc->transfer_size = cpu_to_le32(len);
434 offset = ( ((char*)&desc->sem_control_bits) - ((char*)p->desc));
435 desc->sem_addr = cpu_to_le64((u64)(p->page_dma+offset));
436 desc->zero1 = desc->zero2 = 0;
437 offset = ( ((char*)(desc+1)) - ((char*)p->desc));
438 desc->next_desc_addr = cpu_to_le64(p->page_dma+offset);
439 desc->control_bits = cpu_to_le32(DMASCR_GO|DMASCR_ERR_INT_EN|
440 DMASCR_PARITY_INT_EN|
445 desc->control_bits |= cpu_to_le32(DMASCR_TRANSFER_READ);
446 desc->sem_control_bits = desc->control_bits;
448 card->current_sector += (len >> 9);
450 card->current_idx = idx;
451 if (idx >= bio->bi_vcnt)
452 card->currentbio = NULL;
457 static void process_page(unsigned long data)
459 /* check if any of the requests in the page are DMA_COMPLETE,
460 * and deal with them appropriately.
461 * If we find a descriptor without DMA_COMPLETE in the semaphore, then
462 * dma must have hit an error on that descriptor, so use dma_status instead
463 * and assume that all following descriptors must be re-tried.
465 struct mm_page *page;
466 struct bio *return_bio=NULL;
467 struct cardinfo *card = (struct cardinfo *)data;
468 unsigned int dma_status = card->dma_status;
470 spin_lock_bh(&card->lock);
471 if (card->Active < 0)
473 page = &card->mm_pages[card->Active];
475 while (page->headcnt < page->cnt) {
476 struct bio *bio = page->bio;
477 struct mm_dma_desc *desc = &page->desc[page->headcnt];
478 int control = le32_to_cpu(desc->sem_control_bits);
482 if (!(control & DMASCR_DMA_COMPLETE)) {
483 control = dma_status;
489 if (page->idx >= bio->bi_vcnt) {
490 page->bio = bio->bi_next;
491 page->idx = page->bio->bi_idx;
494 pci_unmap_page(card->dev, desc->data_dma_handle,
495 bio_iovec_idx(bio,idx)->bv_len,
496 (control& DMASCR_TRANSFER_READ) ?
497 PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
498 if (control & DMASCR_HARD_ERROR) {
500 clear_bit(BIO_UPTODATE, &bio->bi_flags);
501 dev_printk(KERN_WARNING, &card->dev->dev,
502 "I/O error on sector %d/%d\n",
503 le32_to_cpu(desc->local_addr)>>9,
504 le32_to_cpu(desc->transfer_size));
505 dump_dmastat(card, control);
506 } else if (test_bit(BIO_RW, &bio->bi_rw) &&
507 le32_to_cpu(desc->local_addr)>>9 == card->init_size) {
508 card->init_size += le32_to_cpu(desc->transfer_size)>>9;
509 if (card->init_size>>1 >= card->mm_size) {
510 dev_printk(KERN_INFO, &card->dev->dev,
511 "memory now initialised\n");
512 set_userbit(card, MEMORY_INITIALIZED, 1);
515 if (bio != page->bio) {
516 bio->bi_next = return_bio;
523 if (debug & DEBUG_LED_ON_TRANSFER)
524 set_led(card, LED_REMOVE, LED_OFF);
526 if (card->check_batteries) {
527 card->check_batteries = 0;
528 check_batteries(card);
530 if (page->headcnt >= page->cnt) {
535 /* haven't finished with this one yet */
536 pr_debug("do some more\n");
540 spin_unlock_bh(&card->lock);
543 struct bio *bio = return_bio;
545 return_bio = bio->bi_next;
552 -----------------------------------------------------------------------------------
554 -----------------------------------------------------------------------------------
556 static int mm_make_request(struct request_queue *q, struct bio *bio)
558 struct cardinfo *card = q->queuedata;
559 pr_debug("mm_make_request %llu %u\n",
560 (unsigned long long)bio->bi_sector, bio->bi_size);
562 spin_lock_irq(&card->lock);
563 *card->biotail = bio;
565 card->biotail = &bio->bi_next;
567 spin_unlock_irq(&card->lock);
573 -----------------------------------------------------------------------------------
575 -----------------------------------------------------------------------------------
577 static irqreturn_t mm_interrupt(int irq, void *__card)
579 struct cardinfo *card = (struct cardinfo *) __card;
580 unsigned int dma_status;
581 unsigned short cfg_status;
585 dma_status = le32_to_cpu(readl(card->csr_remap + DMA_STATUS_CTRL));
587 if (!(dma_status & (DMASCR_ERROR_MASK | DMASCR_CHAIN_COMPLETE))) {
588 /* interrupt wasn't for me ... */
592 /* clear COMPLETION interrupts */
593 if (card->flags & UM_FLAG_NO_BYTE_STATUS)
594 writel(cpu_to_le32(DMASCR_DMA_COMPLETE|DMASCR_CHAIN_COMPLETE),
595 card->csr_remap+ DMA_STATUS_CTRL);
597 writeb((DMASCR_DMA_COMPLETE|DMASCR_CHAIN_COMPLETE) >> 16,
598 card->csr_remap+ DMA_STATUS_CTRL + 2);
600 /* log errors and clear interrupt status */
601 if (dma_status & DMASCR_ANY_ERR) {
602 unsigned int data_log1, data_log2;
603 unsigned int addr_log1, addr_log2;
604 unsigned char stat, count, syndrome, check;
606 stat = readb(card->csr_remap + MEMCTRLCMD_ERRSTATUS);
608 data_log1 = le32_to_cpu(readl(card->csr_remap + ERROR_DATA_LOG));
609 data_log2 = le32_to_cpu(readl(card->csr_remap + ERROR_DATA_LOG + 4));
610 addr_log1 = le32_to_cpu(readl(card->csr_remap + ERROR_ADDR_LOG));
611 addr_log2 = readb(card->csr_remap + ERROR_ADDR_LOG + 4);
613 count = readb(card->csr_remap + ERROR_COUNT);
614 syndrome = readb(card->csr_remap + ERROR_SYNDROME);
615 check = readb(card->csr_remap + ERROR_CHECK);
617 dump_dmastat(card, dma_status);
620 dev_printk(KERN_ERR, &card->dev->dev,
621 "Memory access error detected (err count %d)\n",
624 dev_printk(KERN_ERR, &card->dev->dev,
625 "Multi-bit EDC error\n");
627 dev_printk(KERN_ERR, &card->dev->dev,
628 "Fault Address 0x%02x%08x, Fault Data 0x%08x%08x\n",
629 addr_log2, addr_log1, data_log2, data_log1);
630 dev_printk(KERN_ERR, &card->dev->dev,
631 "Fault Check 0x%02x, Fault Syndrome 0x%02x\n",
634 writeb(0, card->csr_remap + ERROR_COUNT);
637 if (dma_status & DMASCR_PARITY_ERR_REP) {
638 dev_printk(KERN_ERR, &card->dev->dev,
639 "PARITY ERROR REPORTED\n");
640 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
641 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
644 if (dma_status & DMASCR_PARITY_ERR_DET) {
645 dev_printk(KERN_ERR, &card->dev->dev,
646 "PARITY ERROR DETECTED\n");
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_SYSTEM_ERR_SIG) {
652 dev_printk(KERN_ERR, &card->dev->dev, "SYSTEM ERROR\n");
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_TARGET_ABT) {
658 dev_printk(KERN_ERR, &card->dev->dev, "TARGET ABORT\n");
659 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
660 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
663 if (dma_status & DMASCR_MASTER_ABT) {
664 dev_printk(KERN_ERR, &card->dev->dev, "MASTER ABORT\n");
665 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
666 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
669 /* and process the DMA descriptors */
670 card->dma_status = dma_status;
671 tasklet_schedule(&card->tasklet);
678 -----------------------------------------------------------------------------------
679 -- set_fault_to_battery_status
680 -----------------------------------------------------------------------------------
683 * If both batteries are good, no LED
684 * If either battery has been warned, solid LED
685 * If both batteries are bad, flash the LED quickly
686 * If either battery is bad, flash the LED semi quickly
688 static void set_fault_to_battery_status(struct cardinfo *card)
690 if (card->battery[0].good && card->battery[1].good)
691 set_led(card, LED_FAULT, LED_OFF);
692 else if (card->battery[0].warned || card->battery[1].warned)
693 set_led(card, LED_FAULT, LED_ON);
694 else if (!card->battery[0].good && !card->battery[1].good)
695 set_led(card, LED_FAULT, LED_FLASH_7_0);
697 set_led(card, LED_FAULT, LED_FLASH_3_5);
700 static void init_battery_timer(void);
704 -----------------------------------------------------------------------------------
706 -----------------------------------------------------------------------------------
708 static int check_battery(struct cardinfo *card, int battery, int status)
710 if (status != card->battery[battery].good) {
711 card->battery[battery].good = !card->battery[battery].good;
712 card->battery[battery].last_change = jiffies;
714 if (card->battery[battery].good) {
715 dev_printk(KERN_ERR, &card->dev->dev,
716 "Battery %d now good\n", battery + 1);
717 card->battery[battery].warned = 0;
719 dev_printk(KERN_ERR, &card->dev->dev,
720 "Battery %d now FAILED\n", battery + 1);
723 } else if (!card->battery[battery].good &&
724 !card->battery[battery].warned &&
725 time_after_eq(jiffies, card->battery[battery].last_change +
726 (HZ * 60 * 60 * 5))) {
727 dev_printk(KERN_ERR, &card->dev->dev,
728 "Battery %d still FAILED after 5 hours\n", battery + 1);
729 card->battery[battery].warned = 1;
737 -----------------------------------------------------------------------------------
739 -----------------------------------------------------------------------------------
741 static void check_batteries(struct cardinfo *card)
743 /* NOTE: this must *never* be called while the card
744 * is doing (bus-to-card) DMA, or you will need the
747 unsigned char status;
750 status = readb(card->csr_remap + MEMCTRLSTATUS_BATTERY);
751 if (debug & DEBUG_BATTERY_POLLING)
752 dev_printk(KERN_DEBUG, &card->dev->dev,
753 "checking battery status, 1 = %s, 2 = %s\n",
754 (status & BATTERY_1_FAILURE) ? "FAILURE" : "OK",
755 (status & BATTERY_2_FAILURE) ? "FAILURE" : "OK");
757 ret1 = check_battery(card, 0, !(status & BATTERY_1_FAILURE));
758 ret2 = check_battery(card, 1, !(status & BATTERY_2_FAILURE));
761 set_fault_to_battery_status(card);
764 static void check_all_batteries(unsigned long ptr)
768 for (i = 0; i < num_cards; i++)
769 if (!(cards[i].flags & UM_FLAG_NO_BATT)) {
770 struct cardinfo *card = &cards[i];
771 spin_lock_bh(&card->lock);
772 if (card->Active >= 0)
773 card->check_batteries = 1;
775 check_batteries(card);
776 spin_unlock_bh(&card->lock);
779 init_battery_timer();
782 -----------------------------------------------------------------------------------
783 -- init_battery_timer
784 -----------------------------------------------------------------------------------
786 static void init_battery_timer(void)
788 init_timer(&battery_timer);
789 battery_timer.function = check_all_batteries;
790 battery_timer.expires = jiffies + (HZ * 60);
791 add_timer(&battery_timer);
794 -----------------------------------------------------------------------------------
796 -----------------------------------------------------------------------------------
798 static void del_battery_timer(void)
800 del_timer(&battery_timer);
803 -----------------------------------------------------------------------------------
805 -----------------------------------------------------------------------------------
808 * Note no locks taken out here. In a worst case scenario, we could drop
809 * a chunk of system memory. But that should never happen, since validation
810 * happens at open or mount time, when locks are held.
812 * That's crap, since doing that while some partitions are opened
813 * or mounted will give you really nasty results.
815 static int mm_revalidate(struct gendisk *disk)
817 struct cardinfo *card = disk->private_data;
818 set_capacity(disk, card->mm_size << 1);
822 static int mm_getgeo(struct block_device *bdev, struct hd_geometry *geo)
824 struct cardinfo *card = bdev->bd_disk->private_data;
825 int size = card->mm_size * (1024 / MM_HARDSECT);
828 * get geometry: we have to fake one... trim the size to a
829 * multiple of 2048 (1M): tell we have 32 sectors, 64 heads,
830 * whatever cylinders.
834 geo->cylinders = size / (geo->heads * geo->sectors);
839 -----------------------------------------------------------------------------------
841 -----------------------------------------------------------------------------------
842 Future support for removable devices
844 static int mm_check_change(struct gendisk *disk)
846 /* struct cardinfo *dev = disk->private_data; */
850 -----------------------------------------------------------------------------------
852 -----------------------------------------------------------------------------------
854 static struct block_device_operations mm_fops = {
855 .owner = THIS_MODULE,
857 .revalidate_disk= mm_revalidate,
858 .media_changed = mm_check_change,
861 -----------------------------------------------------------------------------------
863 -----------------------------------------------------------------------------------
865 static int __devinit mm_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
868 struct cardinfo *card = &cards[num_cards];
869 unsigned char mem_present;
870 unsigned char batt_status;
871 unsigned int saved_bar, data;
873 static int printed_version;
875 if (!printed_version++)
876 printk(KERN_INFO DRIVER_VERSION " : " DRIVER_DESC "\n");
878 if (pci_enable_device(dev) < 0)
881 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0xF8);
886 card->csr_base = pci_resource_start(dev, 0);
887 card->csr_len = pci_resource_len(dev, 0);
889 dev_printk(KERN_INFO, &dev->dev,
890 "Micro Memory(tm) controller found (PCI Mem Module (Battery Backup))\n");
892 if (pci_set_dma_mask(dev, DMA_64BIT_MASK) &&
893 pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
894 dev_printk(KERN_WARNING, &dev->dev, "NO suitable DMA found\n");
897 if (!request_mem_region(card->csr_base, card->csr_len, "Micro Memory")) {
898 dev_printk(KERN_ERR, &card->dev->dev,
899 "Unable to request memory region\n");
905 card->csr_remap = ioremap_nocache(card->csr_base, card->csr_len);
906 if (!card->csr_remap) {
907 dev_printk(KERN_ERR, &card->dev->dev,
908 "Unable to remap memory region\n");
911 goto failed_remap_csr;
914 dev_printk(KERN_INFO, &card->dev->dev,
915 "CSR 0x%08lx -> 0x%p (0x%lx)\n",
916 card->csr_base, card->csr_remap, card->csr_len);
918 switch(card->dev->device) {
920 card->flags |= UM_FLAG_NO_BYTE_STATUS | UM_FLAG_NO_BATTREG;
925 card->flags |= UM_FLAG_NO_BYTE_STATUS;
930 card->flags |= UM_FLAG_NO_BYTE_STATUS | UM_FLAG_NO_BATTREG | UM_FLAG_NO_BATT;
935 magic_number = 0x100;
939 if (readb(card->csr_remap + MEMCTRLSTATUS_MAGIC) != magic_number) {
940 dev_printk(KERN_ERR, &card->dev->dev, "Magic number invalid\n");
945 card->mm_pages[0].desc = pci_alloc_consistent(card->dev,
947 &card->mm_pages[0].page_dma);
948 card->mm_pages[1].desc = pci_alloc_consistent(card->dev,
950 &card->mm_pages[1].page_dma);
951 if (card->mm_pages[0].desc == NULL ||
952 card->mm_pages[1].desc == NULL) {
953 dev_printk(KERN_ERR, &card->dev->dev, "alloc failed\n");
956 reset_page(&card->mm_pages[0]);
957 reset_page(&card->mm_pages[1]);
958 card->Ready = 0; /* page 0 is ready */
959 card->Active = -1; /* no page is active */
961 card->biotail = &card->bio;
963 card->queue = blk_alloc_queue(GFP_KERNEL);
967 blk_queue_make_request(card->queue, mm_make_request);
968 card->queue->queuedata = card;
969 card->queue->unplug_fn = mm_unplug_device;
971 tasklet_init(&card->tasklet, process_page, (unsigned long)card);
973 card->check_batteries = 0;
975 mem_present = readb(card->csr_remap + MEMCTRLSTATUS_MEMORY);
976 switch (mem_present) {
978 card->mm_size = 1024 * 128;
981 card->mm_size = 1024 * 256;
984 card->mm_size = 1024 * 512;
987 card->mm_size = 1024 * 1024;
990 card->mm_size = 1024 * 2048;
997 /* Clear the LED's we control */
998 set_led(card, LED_REMOVE, LED_OFF);
999 set_led(card, LED_FAULT, LED_OFF);
1001 batt_status = readb(card->csr_remap + MEMCTRLSTATUS_BATTERY);
1003 card->battery[0].good = !(batt_status & BATTERY_1_FAILURE);
1004 card->battery[1].good = !(batt_status & BATTERY_2_FAILURE);
1005 card->battery[0].last_change = card->battery[1].last_change = jiffies;
1007 if (card->flags & UM_FLAG_NO_BATT)
1008 dev_printk(KERN_INFO, &card->dev->dev,
1009 "Size %d KB\n", card->mm_size);
1011 dev_printk(KERN_INFO, &card->dev->dev,
1012 "Size %d KB, Battery 1 %s (%s), Battery 2 %s (%s)\n",
1014 (batt_status & BATTERY_1_DISABLED ? "Disabled" : "Enabled"),
1015 card->battery[0].good ? "OK" : "FAILURE",
1016 (batt_status & BATTERY_2_DISABLED ? "Disabled" : "Enabled"),
1017 card->battery[1].good ? "OK" : "FAILURE");
1019 set_fault_to_battery_status(card);
1022 pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &saved_bar);
1024 pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, data);
1025 pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &data);
1026 pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, saved_bar);
1031 card->win_size = data;
1034 if (request_irq(dev->irq, mm_interrupt, IRQF_SHARED, "pci-umem", card)) {
1035 dev_printk(KERN_ERR, &card->dev->dev,
1036 "Unable to allocate IRQ\n");
1039 goto failed_req_irq;
1042 card->irq = dev->irq;
1043 dev_printk(KERN_INFO, &card->dev->dev,
1044 "Window size %d bytes, IRQ %d\n",
1045 card->win_size, card->irq);
1047 spin_lock_init(&card->lock);
1049 pci_set_drvdata(dev, card);
1051 if (pci_write_cmd != 0x0F) /* If not Memory Write & Invalidate */
1052 pci_write_cmd = 0x07; /* then Memory Write command */
1054 if (pci_write_cmd & 0x08) { /* use Memory Write and Invalidate */
1055 unsigned short cfg_command;
1056 pci_read_config_word(dev, PCI_COMMAND, &cfg_command);
1057 cfg_command |= 0x10; /* Memory Write & Invalidate Enable */
1058 pci_write_config_word(dev, PCI_COMMAND, cfg_command);
1060 pci_cmds = (pci_read_cmd << 28) | (pci_write_cmd << 24);
1064 if (!get_userbit(card, MEMORY_INITIALIZED)) {
1065 dev_printk(KERN_INFO, &card->dev->dev,
1066 "memory NOT initialized. Consider over-writing whole device.\n");
1067 card->init_size = 0;
1069 dev_printk(KERN_INFO, &card->dev->dev,
1070 "memory already initialized\n");
1071 card->init_size = card->mm_size;
1075 writeb(EDC_STORE_CORRECT, card->csr_remap + MEMCTRLCMD_ERRCTRL);
1081 if (card->mm_pages[0].desc)
1082 pci_free_consistent(card->dev, PAGE_SIZE*2,
1083 card->mm_pages[0].desc,
1084 card->mm_pages[0].page_dma);
1085 if (card->mm_pages[1].desc)
1086 pci_free_consistent(card->dev, PAGE_SIZE*2,
1087 card->mm_pages[1].desc,
1088 card->mm_pages[1].page_dma);
1090 iounmap(card->csr_remap);
1092 release_mem_region(card->csr_base, card->csr_len);
1098 -----------------------------------------------------------------------------------
1100 -----------------------------------------------------------------------------------
1102 static void mm_pci_remove(struct pci_dev *dev)
1104 struct cardinfo *card = pci_get_drvdata(dev);
1106 tasklet_kill(&card->tasklet);
1107 iounmap(card->csr_remap);
1108 release_mem_region(card->csr_base, card->csr_len);
1109 free_irq(card->irq, card);
1111 if (card->mm_pages[0].desc)
1112 pci_free_consistent(card->dev, PAGE_SIZE*2,
1113 card->mm_pages[0].desc,
1114 card->mm_pages[0].page_dma);
1115 if (card->mm_pages[1].desc)
1116 pci_free_consistent(card->dev, PAGE_SIZE*2,
1117 card->mm_pages[1].desc,
1118 card->mm_pages[1].page_dma);
1119 blk_cleanup_queue(card->queue);
1122 static const struct pci_device_id mm_pci_ids[] = {
1123 {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY,PCI_DEVICE_ID_MICRO_MEMORY_5415CN)},
1124 {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY,PCI_DEVICE_ID_MICRO_MEMORY_5425CN)},
1125 {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY,PCI_DEVICE_ID_MICRO_MEMORY_6155)},
1133 }, { /* end: all zeroes */ }
1136 MODULE_DEVICE_TABLE(pci, mm_pci_ids);
1138 static struct pci_driver mm_pci_driver = {
1140 .id_table = mm_pci_ids,
1141 .probe = mm_pci_probe,
1142 .remove = mm_pci_remove,
1145 -----------------------------------------------------------------------------------
1147 -----------------------------------------------------------------------------------
1150 static int __init mm_init(void)
1155 retval = pci_register_driver(&mm_pci_driver);
1159 err = major_nr = register_blkdev(0, "umem");
1161 pci_unregister_driver(&mm_pci_driver);
1165 for (i = 0; i < num_cards; i++) {
1166 mm_gendisk[i] = alloc_disk(1 << MM_SHIFT);
1171 for (i = 0; i < num_cards; i++) {
1172 struct gendisk *disk = mm_gendisk[i];
1173 sprintf(disk->disk_name, "umem%c", 'a'+i);
1174 spin_lock_init(&cards[i].lock);
1175 disk->major = major_nr;
1176 disk->first_minor = i << MM_SHIFT;
1177 disk->fops = &mm_fops;
1178 disk->private_data = &cards[i];
1179 disk->queue = cards[i].queue;
1180 set_capacity(disk, cards[i].mm_size << 1);
1184 init_battery_timer();
1185 printk(KERN_INFO "MM: desc_per_page = %ld\n", DESC_PER_PAGE);
1186 /* printk("mm_init: Done. 10-19-01 9:00\n"); */
1190 pci_unregister_driver(&mm_pci_driver);
1191 unregister_blkdev(major_nr, "umem");
1193 put_disk(mm_gendisk[i]);
1197 -----------------------------------------------------------------------------------
1199 -----------------------------------------------------------------------------------
1201 static void __exit mm_cleanup(void)
1205 del_battery_timer();
1207 for (i=0; i < num_cards ; i++) {
1208 del_gendisk(mm_gendisk[i]);
1209 put_disk(mm_gendisk[i]);
1212 pci_unregister_driver(&mm_pci_driver);
1214 unregister_blkdev(major_nr, "umem");
1217 module_init(mm_init);
1218 module_exit(mm_cleanup);
1220 MODULE_AUTHOR(DRIVER_AUTHOR);
1221 MODULE_DESCRIPTION(DRIVER_DESC);
1222 MODULE_LICENSE("GPL");