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
33 #include <linux/config.h>
34 #include <linux/sched.h>
36 #include <linux/kernel.h>
38 #include <linux/mman.h>
39 #include <linux/ioctl.h>
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/interrupt.h>
43 #include <linux/smp_lock.h>
44 #include <linux/timer.h>
45 #include <linux/pci.h>
46 #include <linux/slab.h>
48 #include <linux/fcntl.h> /* O_ACCMODE */
49 #include <linux/hdreg.h> /* HDIO_GETGEO */
51 #include <linux/umem.h>
53 #include <asm/uaccess.h>
56 #define PRINTK(x...) do {} while (0)
57 #define dprintk(x...) do {} while (0)
58 /*#define dprintk(x...) printk(x) */
61 #define MM_RAHEAD 2 /* two sectors */
62 #define MM_BLKSIZE 1024 /* 1k blocks */
63 #define MM_HARDSECT 512 /* 512-byte hardware sectors */
64 #define MM_SHIFT 6 /* max 64 partitions on 4 cards */
65 #define DEVICE_NR(device) (MINOR(device)>>MM_SHIFT)
71 #define DRIVER_VERSION "v2.3"
72 #define DRIVER_AUTHOR "San Mehat, Johannes Erdfelt, NeilBrown"
73 #define DRIVER_DESC "Micro Memory(tm) PCI memory board block driver"
76 /* #define HW_TRACE(x) writeb(x,cards[0].csr_remap + MEMCTRLSTATUS_MAGIC) */
79 #define DEBUG_LED_ON_TRANSFER 0x01
80 #define DEBUG_BATTERY_POLLING 0x02
82 MODULE_PARM(debug, "i");
83 MODULE_PARM_DESC(debug, "Debug bitmask");
85 static int init_mem = 0;
87 MODULE_PARM(init_mem, "i");
88 MODULE_PARM_DESC(init_mem, "Initialize memory");
90 static int pci_read_cmd = 0x0C; /* Read Multiple */
91 MODULE_PARM(pci_read_cmd, "i");
92 MODULE_PARM_DESC(pci_read_cmd, "PCI read command");
94 static int pci_write_cmd = 0x0F; /* Write and Invalidate */
95 MODULE_PARM(pci_write_cmd, "i");
96 MODULE_PARM_DESC(pci_write_cmd, "PCI write command");
100 #define MAJOR_NR UMEM_MAJOR
102 #include <linux/blk.h>
103 #include <linux/blkpg.h>
106 static devfs_handle_t devfs_handle; /* For the directory */
115 unsigned long csr_base;
116 unsigned char *csr_remap;
117 unsigned long csr_len;
118 #ifdef CONFIG_MM_MAP_MEMORY
119 unsigned long mem_base;
120 unsigned char *mem_remap;
121 unsigned long mem_len;
124 unsigned int win_size; /* PCI window size */
125 unsigned int mm_size; /* size in kbytes */
127 struct buffer_head *bh, **bhtail;
131 struct mm_dma_desc *desc;
133 struct buffer_head *bh, **bhtail;
135 #define DESC_PER_PAGE ((PAGE_SIZE*2)/sizeof(struct mm_dma_desc))
139 struct tasklet_struct tasklet;
140 unsigned int dma_status;
142 struct tq_struct plug_tq;
147 unsigned long last_change;
156 static struct cardinfo cards[MM_MAXCARDS];
157 static struct block_device_operations mm_fops;
158 static struct timer_list battery_timer;
161 static int mm_hardsect [MM_MAXCARDS << MM_SHIFT];
162 static int mm_blocksizes[MM_MAXCARDS << MM_SHIFT];
163 static int mm_sizes[MM_MAXCARDS << MM_SHIFT];
164 static struct hd_struct mm_partitions[MM_MAXCARDS << MM_SHIFT];
166 static int num_cards = 0;
168 struct gendisk mm_gendisk = {
169 major: MAJOR_NR, /* Major number assigned later */
170 major_name: "umem", /* Name of the major device */
171 minor_shift: MM_SHIFT, /* Shift to get device number */
172 max_p: 1 << MM_SHIFT, /* Number of partitions */
173 fops: &mm_fops, /* Block dev operations */
174 /* everything else is dynamic */
178 static void check_batteries(struct cardinfo *card);
181 -----------------------------------------------------------------------------------
183 -----------------------------------------------------------------------------------
185 static int get_userbit(struct cardinfo *card, int bit)
189 led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
193 -----------------------------------------------------------------------------------
195 -----------------------------------------------------------------------------------
197 static int set_userbit(struct cardinfo *card, int bit, unsigned char state)
201 led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
206 writeb(led, card->csr_remap + MEMCTRLCMD_LEDCTRL);
211 -----------------------------------------------------------------------------------
213 -----------------------------------------------------------------------------------
216 * NOTE: For the power LED, use the LED_POWER_* macros since they differ
218 static void set_led(struct cardinfo *card, int shift, unsigned char state)
222 led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
223 if (state == LED_FLIP)
226 led &= ~(0x03 << shift);
227 led |= (state << shift);
229 writeb(led, card->csr_remap + MEMCTRLCMD_LEDCTRL);
235 -----------------------------------------------------------------------------------
237 -----------------------------------------------------------------------------------
239 static void dump_regs(struct cardinfo *card)
245 for (i = 0; i < 8; i++) {
246 printk(KERN_DEBUG "%p ", p);
248 for (i1 = 0; i1 < 16; i1++)
249 printk("%02x ", *p++);
256 -----------------------------------------------------------------------------------
258 -----------------------------------------------------------------------------------
260 static void dump_dmastat(struct cardinfo *card, unsigned int dmastat)
262 printk(KERN_DEBUG "MM%d*: DMAstat - ", card->card_number);
263 if (dmastat & DMASCR_ANY_ERR)
265 if (dmastat & DMASCR_MBE_ERR)
267 if (dmastat & DMASCR_PARITY_ERR_REP)
268 printk("PARITY_ERR_REP ");
269 if (dmastat & DMASCR_PARITY_ERR_DET)
270 printk("PARITY_ERR_DET ");
271 if (dmastat & DMASCR_SYSTEM_ERR_SIG)
272 printk("SYSTEM_ERR_SIG ");
273 if (dmastat & DMASCR_TARGET_ABT)
274 printk("TARGET_ABT ");
275 if (dmastat & DMASCR_MASTER_ABT)
276 printk("MASTER_ABT ");
277 if (dmastat & DMASCR_CHAIN_COMPLETE)
278 printk("CHAIN_COMPLETE ");
279 if (dmastat & DMASCR_DMA_COMPLETE)
280 printk("DMA_COMPLETE ");
285 * Theory of request handling
287 * Each buffer_head is assigned to one mm_dma_desc.
288 * We have two pages of mm_dma_desc, holding about 64 descriptors
289 * each. These are allocated at init time.
290 * One page is "Ready" and is either full, or can have request added.
291 * The other page might be "Active", which DMA is happening on it.
293 * Whenever IO on the active page completes, the Ready page is activated
294 * and the ex-Active page is clean out and made Ready.
295 * Otherwise the Ready page is only activated when it becomes full, or
296 * when mm_unplug_device is called via run_task_queue(&tq_disk).
298 * If a request arrives while both pages a full, it is queued, and b_rdev is
299 * overloaded to record whether it was a read or a write.
301 * The interrupt handler only polls the device to clear the interrupt.
302 * The processing of the result is done in a tasklet.
305 static void mm_start_io(struct cardinfo *card)
307 /* we have the lock, we know there is
308 * no IO active, and we know that card->Active
311 struct mm_dma_desc *desc;
312 struct mm_page *page;
315 /* make the last descriptor end the chain */
316 page = &card->mm_pages[card->Active];
317 PRINTK("start_io: %d %d->%d\n", card->Active, page->headcnt, page->cnt-1);
318 desc = &page->desc[page->cnt-1];
320 desc->control_bits |= cpu_to_le32(DMASCR_CHAIN_COMP_EN);
321 desc->control_bits &= ~cpu_to_le32(DMASCR_CHAIN_EN);
322 desc->sem_control_bits = desc->control_bits;
325 if (debug & DEBUG_LED_ON_TRANSFER)
326 set_led(card, LED_REMOVE, LED_ON);
328 desc = &page->desc[page->headcnt];
329 writel(0, card->csr_remap + DMA_PCI_ADDR);
330 writel(0, card->csr_remap + DMA_PCI_ADDR + 4);
332 writel(0, card->csr_remap + DMA_LOCAL_ADDR);
333 writel(0, card->csr_remap + DMA_LOCAL_ADDR + 4);
335 writel(0, card->csr_remap + DMA_TRANSFER_SIZE);
336 writel(0, card->csr_remap + DMA_TRANSFER_SIZE + 4);
338 writel(0, card->csr_remap + DMA_SEMAPHORE_ADDR);
339 writel(0, card->csr_remap + DMA_SEMAPHORE_ADDR + 4);
341 offset = ((char*)desc) - ((char*)page->desc);
342 writel(cpu_to_le32((page->page_dma+offset)&0xffffffff),
343 card->csr_remap + DMA_DESCRIPTOR_ADDR);
344 writel(cpu_to_le32((page->page_dma)>>31>>1),
345 card->csr_remap + DMA_DESCRIPTOR_ADDR + 4);
348 writel(cpu_to_le32(DMASCR_GO | DMASCR_CHAIN_EN | pci_cmds),
349 card->csr_remap + DMA_STATUS_CTRL);
352 static int add_bh(struct cardinfo *card);
354 static void activate(struct cardinfo *card)
356 /* if No page is Active, and Ready is
357 * not empty, then switch Ready page
358 * to active and start IO.
359 * Then add any bh's that are available to Ready
366 if (card->Active == -1 &&
367 card->mm_pages[card->Ready].cnt > 0) {
368 card->Active = card->Ready;
369 card->Ready = 1-card->Ready;
373 } while (card->Active == -1 && add_bh(card));
376 static inline void reset_page(struct mm_page *page)
381 page->bhtail = & page->bh;
384 static void mm_unplug_device(void *data)
386 struct cardinfo *card = data;
388 spin_lock_bh(&card->lock);
390 spin_unlock_bh(&card->lock);
394 * If there is room on Ready page, take
395 * one bh off list and add it.
396 * return 1 if there was room, else 0.
398 static int add_bh(struct cardinfo *card)
401 struct mm_dma_desc *desc;
402 dma_addr_t dma_handle;
404 struct buffer_head *bh;
411 if (card->mm_pages[card->Ready].cnt >= DESC_PER_PAGE)
414 card->bh = bh->b_reqnext;
415 if (card->bh == NULL)
416 card->bhtail = &card->bh;
419 dma_handle = pci_map_page(card->dev, bh->b_page, bh_offset(bh),
422 PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);
424 p = &card->mm_pages[card->Ready];
425 desc = &p->desc[p->cnt];
428 p->bhtail = &(bh->b_reqnext);
429 bh->b_reqnext = NULL;
431 desc->data_dma_handle = dma_handle;
433 desc->pci_addr = cpu_to_le64((u64)desc->data_dma_handle);
434 desc->local_addr= cpu_to_le64(bh->b_rsector << 9);
435 desc->transfer_size = cpu_to_le32(bh->b_size);
436 offset = ( ((char*)&desc->sem_control_bits) - ((char*)p->desc));
437 desc->sem_addr = cpu_to_le64((u64)(p->page_dma+offset));
438 desc->zero1 = desc->zero2 = 0;
439 offset = ( ((char*)(desc+1)) - ((char*)p->desc));
440 desc->next_desc_addr = cpu_to_le64(p->page_dma+offset);
441 desc->control_bits = cpu_to_le32(DMASCR_GO|DMASCR_ERR_INT_EN|
442 DMASCR_PARITY_INT_EN|
447 desc->control_bits |= cpu_to_le32(DMASCR_TRANSFER_READ);
448 desc->sem_control_bits = desc->control_bits;
452 static void process_page(unsigned long data)
454 /* check if any of the requests in the page are DMA_COMPLETE,
455 * and deal with them appropriately.
456 * If we find a descriptor without DMA_COMPLETE in the semaphore, then
457 * dma must have hit an error on that descriptor, so use dma_status instead
458 * and assume that all following descriptors must be re-tried.
460 struct mm_page *page;
461 struct buffer_head *ok=NULL, *fail=NULL;
462 struct cardinfo *card = (struct cardinfo *)data;
463 unsigned int dma_status = card->dma_status;
465 spin_lock_bh(&card->lock);
466 if (card->Active < 0)
468 page = &card->mm_pages[card->Active];
470 while (page->bh != NULL) {
471 struct buffer_head *bh = page->bh;
472 struct mm_dma_desc *desc = &page->desc[page->headcnt];
473 int control = le32_to_cpu(desc->sem_control_bits);
476 if (!(control & DMASCR_DMA_COMPLETE)) {
477 control = dma_status;
481 page->bh = bh->b_reqnext;
483 pci_unmap_page(card->dev, desc->data_dma_handle, bh->b_size,
484 (control& DMASCR_TRANSFER_READ) ?
485 PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
486 if (!(control & DMASCR_HARD_ERROR)) {
487 /* it worked just fine */
492 bh->b_reqnext = fail;
494 printk(KERN_WARNING "MM%d: I/O error on sector %lx/%x\n",
495 card->card_number, bh->b_rsector, bh->b_size);
496 dump_dmastat(card, control);
501 if (debug & DEBUG_LED_ON_TRANSFER)
502 set_led(card, LED_REMOVE, LED_OFF);
504 if (card->check_batteries) {
505 card->check_batteries = 0;
506 check_batteries(card);
508 if (page->headcnt >= page->cnt) {
513 /* haven't finished with this one yet */
514 PRINTK("do some more\n");
518 spin_unlock_bh(&card->lock);
521 struct buffer_head *bh = ok;
523 bh->b_reqnext = NULL;
527 struct buffer_head *bh = fail;
528 fail = bh->b_reqnext;
529 bh->b_reqnext = NULL;
536 -----------------------------------------------------------------------------------
538 -----------------------------------------------------------------------------------
540 static int mm_make_request(request_queue_t *q, int rw, struct buffer_head *bh)
542 struct cardinfo *card = &cards[DEVICE_NR(bh->b_rdev)];
543 PRINTK("mm_make_request %d %ld %d\n", rw, bh->b_rsector, bh->b_size);
544 bh->b_rsector += mm_partitions[MINOR(bh->b_rdev)].start_sect;
545 bh->b_rdev = rw; /* overloading... */
547 spin_lock_bh(&card->lock);
549 bh->b_reqnext = NULL;
550 card->bhtail = &bh->b_reqnext;
551 spin_unlock_bh(&card->lock);
553 queue_task(&card->plug_tq, &tq_disk);
558 -----------------------------------------------------------------------------------
560 -----------------------------------------------------------------------------------
562 static void mm_interrupt(int irq, void *__card, struct pt_regs *regs)
564 struct cardinfo *card = (struct cardinfo *) __card;
565 unsigned int dma_status;
566 unsigned short cfg_status;
570 dma_status = le32_to_cpu(readl(card->csr_remap + DMA_STATUS_CTRL));
572 if (!(dma_status & (DMASCR_ERROR_MASK | DMASCR_CHAIN_COMPLETE))) {
573 /* interrupt wasn't for me ... */
577 /* clear COMPLETION interrupts */
578 writel(cpu_to_le32(DMASCR_DMA_COMPLETE|DMASCR_CHAIN_COMPLETE),
579 card->csr_remap+ DMA_STATUS_CTRL);
582 /* log errors and clear interrupt status */
583 if (dma_status & DMASCR_ANY_ERR) {
584 unsigned int data_log1, data_log2;
585 unsigned int addr_log1, addr_log2;
586 unsigned char stat, count, syndrome, check;
588 stat = readb(card->csr_remap + MEMCTRLCMD_ERRSTATUS);
590 data_log1 = le32_to_cpu(readl(card->csr_remap + ERROR_DATA_LOG));
591 data_log2 = le32_to_cpu(readl(card->csr_remap + ERROR_DATA_LOG + 4));
592 addr_log1 = le32_to_cpu(readl(card->csr_remap + ERROR_ADDR_LOG));
593 addr_log2 = readb(card->csr_remap + ERROR_ADDR_LOG + 4);
595 count = readb(card->csr_remap + ERROR_COUNT);
596 syndrome = readb(card->csr_remap + ERROR_SYNDROME);
597 check = readb(card->csr_remap + ERROR_CHECK);
599 dump_dmastat(card, dma_status);
602 printk(KERN_ERR "MM%d*: Memory access error detected (err count %d)\n",
603 card->card_number, count);
605 printk(KERN_ERR "MM%d*: Multi-bit EDC error\n",
608 printk(KERN_ERR "MM%d*: Fault Address 0x%02x%08x, Fault Data 0x%08x%08x\n",
609 card->card_number, addr_log2, addr_log1, data_log2, data_log1);
610 printk(KERN_ERR "MM%d*: Fault Check 0x%02x, Fault Syndrome 0x%02x\n",
611 card->card_number, check, syndrome);
613 writeb(0, card->csr_remap + ERROR_COUNT);
616 if (dma_status & DMASCR_PARITY_ERR_REP) {
617 printk(KERN_ERR "MM%d*: PARITY ERROR REPORTED\n", card->card_number);
618 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
619 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
622 if (dma_status & DMASCR_PARITY_ERR_DET) {
623 printk(KERN_ERR "MM%d*: PARITY ERROR DETECTED\n", card->card_number);
624 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
625 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
628 if (dma_status & DMASCR_SYSTEM_ERR_SIG) {
629 printk(KERN_ERR "MM%d*: SYSTEM ERROR\n", card->card_number);
630 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
631 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
634 if (dma_status & DMASCR_TARGET_ABT) {
635 printk(KERN_ERR "MM%d*: TARGET ABORT\n", card->card_number);
636 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
637 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
640 if (dma_status & DMASCR_MASTER_ABT) {
641 printk(KERN_ERR "MM%d*: MASTER ABORT\n", card->card_number);
642 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
643 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
646 /* and process the DMA descriptors */
647 card->dma_status = dma_status;
648 tasklet_schedule(&card->tasklet);
654 -----------------------------------------------------------------------------------
655 -- set_fault_to_battery_status
656 -----------------------------------------------------------------------------------
659 * If both batteries are good, no LED
660 * If either battery has been warned, solid LED
661 * If both batteries are bad, flash the LED quickly
662 * If either battery is bad, flash the LED semi quickly
664 static void set_fault_to_battery_status(struct cardinfo *card)
666 if (card->battery[0].good && card->battery[1].good)
667 set_led(card, LED_FAULT, LED_OFF);
668 else if (card->battery[0].warned || card->battery[1].warned)
669 set_led(card, LED_FAULT, LED_ON);
670 else if (!card->battery[0].good && !card->battery[1].good)
671 set_led(card, LED_FAULT, LED_FLASH_7_0);
673 set_led(card, LED_FAULT, LED_FLASH_3_5);
676 static void init_battery_timer(void);
680 -----------------------------------------------------------------------------------
682 -----------------------------------------------------------------------------------
684 static int check_battery(struct cardinfo *card, int battery, int status)
686 if (status != card->battery[battery].good) {
687 card->battery[battery].good = !card->battery[battery].good;
688 card->battery[battery].last_change = jiffies;
690 if (card->battery[battery].good) {
691 printk(KERN_ERR "MM%d: Battery %d now good\n",
692 card->card_number, battery + 1);
693 card->battery[battery].warned = 0;
695 printk(KERN_ERR "MM%d: Battery %d now FAILED\n",
696 card->card_number, battery + 1);
699 } else if (!card->battery[battery].good &&
700 !card->battery[battery].warned &&
701 time_after_eq(jiffies, card->battery[battery].last_change +
702 (HZ * 60 * 60 * 5))) {
703 printk(KERN_ERR "MM%d: Battery %d still FAILED after 5 hours\n",
704 card->card_number, battery + 1);
705 card->battery[battery].warned = 1;
713 -----------------------------------------------------------------------------------
715 -----------------------------------------------------------------------------------
717 static void check_batteries(struct cardinfo *card)
719 /* NOTE: this must *never* be called while the card
720 * is doing (bus-to-card) DMA, or you will need the
723 unsigned char status;
726 status = readb(card->csr_remap + MEMCTRLSTATUS_BATTERY);
727 if (debug & DEBUG_BATTERY_POLLING)
728 printk(KERN_DEBUG "MM%d: checking battery status, 1 = %s, 2 = %s\n",
730 (status & BATTERY_1_FAILURE) ? "FAILURE" : "OK",
731 (status & BATTERY_2_FAILURE) ? "FAILURE" : "OK");
733 ret1 = check_battery(card, 0, !(status & BATTERY_1_FAILURE));
734 ret2 = check_battery(card, 1, !(status & BATTERY_2_FAILURE));
737 set_fault_to_battery_status(card);
740 static void check_all_batteries(unsigned long ptr)
744 for (i = 0; i < num_cards; i++) {
745 struct cardinfo *card = &cards[i];
746 spin_lock_bh(&card->lock);
747 if (card->Active >= 0)
748 card->check_batteries = 1;
750 check_batteries(card);
751 spin_unlock_bh(&card->lock);
754 init_battery_timer();
757 -----------------------------------------------------------------------------------
758 -- init_battery_timer
759 -----------------------------------------------------------------------------------
761 static void init_battery_timer(void)
763 init_timer(&battery_timer);
764 battery_timer.function = check_all_batteries;
765 battery_timer.expires = jiffies + (HZ * 60);
766 add_timer(&battery_timer);
769 -----------------------------------------------------------------------------------
771 -----------------------------------------------------------------------------------
773 static void del_battery_timer(void)
775 del_timer(&battery_timer);
778 -----------------------------------------------------------------------------------
780 -----------------------------------------------------------------------------------
783 * Note no locks taken out here. In a worst case scenario, we could drop
784 * a chunk of system memory. But that should never happen, since validation
785 * happens at open or mount time, when locks are held.
787 static int mm_revalidate(kdev_t i_rdev)
791 int card_number = DEVICE_NR(i_rdev);
792 /* first partition, # of partitions */
793 int part1 = (DEVICE_NR(i_rdev) << MM_SHIFT) + 1;
794 int npart = (1 << MM_SHIFT) -1;
796 /* first clear old partition information */
797 for (i=0; i<npart ;i++) {
798 mm_gendisk.sizes[part1+i]=0;
799 mm_gendisk.part[part1+i].start_sect = 0;
800 mm_gendisk.part[part1+i].nr_sects = 0;
803 mm_gendisk.part[card_number << MM_SHIFT].nr_sects =
804 cards[card_number].mm_size << 1;
807 /* then fill new info */
808 printk(KERN_INFO "mm partition check: (%d)\n", DEVICE_NR(i_rdev));
809 grok_partitions(&mm_gendisk, card_number, 1<<MM_SHIFT,
810 mm_gendisk.sizes[card_number<<MM_SHIFT]);
814 -----------------------------------------------------------------------------------
816 -----------------------------------------------------------------------------------
818 static int mm_ioctl(struct inode *i, struct file *f, unsigned int cmd, unsigned long arg)
820 int err, size, card_number;
821 struct hd_geometry geo;
824 if (!i || !i->i_rdev)
827 minor = MINOR(i->i_rdev);
828 card_number = (minor >> MM_SHIFT);
834 /* Return the device size, expressed in sectors */
835 err = ! access_ok (VERIFY_WRITE, arg, sizeof(long));
836 if (err) return -EFAULT;
837 size = mm_gendisk.part[minor].nr_sects;
838 if (copy_to_user((long *) arg, &size, sizeof (long)))
843 case BLKRAGET: /* return the readahead value */
844 err = ! access_ok(VERIFY_WRITE, arg, sizeof(long));
845 if (err) return -EFAULT;
846 copy_to_user((long *)arg, &read_ahead[MAJOR(i->i_rdev)],sizeof(long));
849 case BLKRASET: /* set the readahead value */
850 if (!capable(CAP_SYS_RAWIO)) return -EACCES;
851 if (arg > 0xff) return -EINVAL; /* limit it */
852 read_ahead[MAJOR(i->i_rdev)] = arg;
856 return (mm_revalidate(i->i_rdev));
860 * get geometry: we have to fake one... trim the size to a
861 * multiple of 2048 (1M): tell we have 32 sectors, 64 heads,
862 * whatever cylinders.
864 err = ! access_ok(VERIFY_WRITE, arg, sizeof(geo));
865 if (err) return -EFAULT;
866 size = cards[card_number].mm_size * (1024 / MM_HARDSECT);
869 geo.start = mm_gendisk.part[minor].start_sect;
870 geo.cylinders = size / (geo.heads * geo.sectors);
872 if (copy_to_user((void *) arg, &geo, sizeof(geo)))
878 return blk_ioctl(i->i_rdev, cmd, arg);
881 return -ENOTTY; /* unknown command */
884 -----------------------------------------------------------------------------------
886 -----------------------------------------------------------------------------------
887 Future support for removable devices
889 static int mm_check_change(kdev_t i_rdev)
891 int card_number = DEVICE_NR(i_rdev);
892 /* struct cardinfo *dev = cards + card_number; */
893 if (card_number >= num_cards) /* paranoid */
900 -----------------------------------------------------------------------------------
902 -----------------------------------------------------------------------------------
904 static int mm_open(struct inode *i, struct file *filp)
907 struct cardinfo *card;
909 num = DEVICE_NR(i->i_rdev);
910 if (num >= num_cards)
915 atomic_inc(&card->usage);
921 -----------------------------------------------------------------------------------
923 -----------------------------------------------------------------------------------
925 static int mm_do_release(struct inode *i, struct file *filp)
928 struct cardinfo *card;
930 num = DEVICE_NR(i->i_rdev);
934 if (atomic_dec_and_test(&card->usage))
935 invalidate_device(i->i_rdev, 1);
940 #define INITIALIZE_BHS 32
942 -----------------------------------------------------------------------------------
944 -----------------------------------------------------------------------------------
946 static void mm_end_buffer_io_sync(struct buffer_head *bh, int uptodate)
948 mark_buffer_uptodate(bh, uptodate);
953 static int __devinit mm_init_mem(struct cardinfo *card)
955 struct buffer_head *bhlist, *bhactive, *bh;
958 struct page *zero_page;
960 /* Turn this off when we flush the contents of memory and have */
961 /* the card rebuild ECC */
962 writeb(EDC_NONE_DEFAULT, card->csr_remap + MEMCTRLCMD_ERRCTRL);
964 printk("MM%d: initializing memory\n", card->card_number);
966 zero_page = alloc_page(GFP_KERNEL);
968 printk(KERN_ERR "unable to allocate page for zeroing memory\n");
970 goto out_alloc_zero_page;
973 memset(page_address(zero_page), 0, PAGE_SIZE);
976 while (i < INITIALIZE_BHS) {
977 bh = kmem_cache_alloc(bh_cachep, SLAB_KERNEL);
983 bh->b_size = PAGE_SIZE;
984 init_buffer(bh, mm_end_buffer_io_sync, NULL);
985 set_bh_page(bh, zero_page, 0);
986 bh->b_dev = bh->b_rdev =
987 MKDEV(MAJOR_NR, card->card_number<<MM_SHIFT);
988 init_waitqueue_head(&bh->b_wait);
992 if (bhlist == NULL) {
993 printk(KERN_ERR "MM: count not allocate buffer heads!!\n");
999 for (i = 0; i < card->mm_size / (PAGE_SIZE / 1024);i++) {
1000 if (bhlist == NULL) {
1001 /* time to wait for some buffers */
1004 bhactive = bh->b_next;
1006 if (!test_bit(BH_Uptodate, &bh->b_state))
1008 bh->b_next = bhlist;
1013 bhlist = bh->b_next;
1016 bh->b_rsector = i*(PAGE_SIZE/512);
1017 set_bit(BH_Lock, &bh->b_state);
1018 clear_bit(BH_Uptodate, &bh->b_state);
1019 mm_make_request(NULL, WRITE, bh);
1020 bh->b_next = bhactive;
1025 bhactive = bh->b_next;
1027 bh->b_next = bhlist;
1032 set_userbit(card, MEMORY_INITIALIZED, 1);
1036 bhlist = bh->b_next;
1037 kmem_cache_free(bh_cachep, bh);
1042 free_page((unsigned long) zero_page);
1043 out_alloc_zero_page:
1048 -----------------------------------------------------------------------------------
1050 -----------------------------------------------------------------------------------
1052 static struct block_device_operations mm_fops = {
1055 release: mm_do_release,
1057 revalidate: mm_revalidate,
1058 check_media_change: mm_check_change,
1061 -----------------------------------------------------------------------------------
1063 -----------------------------------------------------------------------------------
1065 static int __devinit mm_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
1068 struct cardinfo *card = &cards[num_cards];
1069 unsigned char mem_present;
1070 unsigned char batt_status;
1071 unsigned int saved_bar, data;
1073 if (pci_enable_device(dev) < 0)
1076 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0xF8);
1077 pci_set_master(dev);
1080 card->card_number = num_cards;
1082 card->csr_base = pci_resource_start(dev, 0);
1083 card->csr_len = pci_resource_len(dev, 0);
1084 #ifdef CONFIG_MM_MAP_MEMORY
1085 card->mem_base = pci_resource_start(dev, 1);
1086 card->mem_len = pci_resource_len(dev, 1);
1089 printk(KERN_INFO "Micro Memory(tm) controller #%d found at %02x:%02x (PCI Mem Module (Battery Backup))\n",
1090 card->card_number, dev->bus->number, dev->devfn);
1092 if (pci_set_dma_mask(dev, 0xffffffffffffffffLL) &&
1093 !pci_set_dma_mask(dev, 0xffffffffLL)) {
1094 printk(KERN_WARNING "MM%d: NO suitable DMA found\n",num_cards);
1097 if (!request_mem_region(card->csr_base, card->csr_len, "Micro Memory")) {
1098 printk(KERN_ERR "MM%d: Unable to request memory region\n", card->card_number);
1101 goto failed_req_csr;
1104 card->csr_remap = ioremap_nocache(card->csr_base, card->csr_len);
1105 if (!card->csr_remap) {
1106 printk(KERN_ERR "MM%d: Unable to remap memory region\n", card->card_number);
1109 goto failed_remap_csr;
1112 printk(KERN_INFO "MM%d: CSR 0x%08lx -> 0x%p (0x%lx)\n", card->card_number,
1113 card->csr_base, card->csr_remap, card->csr_len);
1115 #ifdef CONFIG_MM_MAP_MEMORY
1116 if (!request_mem_region(card->mem_base, card->mem_len, "Micro Memory")) {
1117 printk(KERN_ERR "MM%d: Unable to request memory region\n", card->card_number);
1120 goto failed_req_mem;
1123 if (!(card->mem_remap = (unsigned char *)ioremap(card->mem_base, cards->mem_len))) {
1124 printk(KERN_ERR "MM%d: Unable to remap memory region\n", card->card_number);
1127 goto failed_remap_mem;
1130 printk(KERN_INFO "MM%d: MEM 0x%8lx -> 0x%8lx (0x%lx)\n", card->card_number,
1131 card->mem_base, card->mem_remap, card->mem_len);
1133 printk(KERN_INFO "MM%d: MEM area not remapped (CONFIG_MM_MAP_MEMORY not set)\n",
1136 if (readb(card->csr_remap + MEMCTRLSTATUS_MAGIC) != MM_MAGIC_VALUE) {
1137 printk(KERN_ERR "MM%d: Magic number invalid\n", card->card_number);
1142 card->mm_pages[0].desc = pci_alloc_consistent(card->dev,
1144 &card->mm_pages[0].page_dma);
1145 card->mm_pages[1].desc = pci_alloc_consistent(card->dev,
1147 &card->mm_pages[1].page_dma);
1148 if (card->mm_pages[0].desc == NULL ||
1149 card->mm_pages[1].desc == NULL) {
1150 printk(KERN_ERR "MM%d: alloc failed\n", card->card_number);
1153 reset_page(&card->mm_pages[0]);
1154 reset_page(&card->mm_pages[1]);
1155 card->Ready = 0; /* page 0 is ready */
1156 card->Active = -1; /* no page is active */
1158 card->bhtail = &card->bh;
1160 tasklet_init(&card->tasklet, process_page, (unsigned long)card);
1162 card->plug_tq.sync = 0;
1163 card->plug_tq.routine = &mm_unplug_device;
1164 card->plug_tq.data = card;
1165 card->check_batteries = 0;
1167 mem_present = readb(card->csr_remap + MEMCTRLSTATUS_MEMORY);
1168 switch (mem_present) {
1170 card->mm_size = 1024 * 128;
1173 card->mm_size = 1024 * 256;
1176 card->mm_size = 1024 * 512;
1179 card->mm_size = 1024 * 1024;
1182 card->mm_size = 1024 * 2048;
1189 /* Clear the LED's we control */
1190 set_led(card, LED_REMOVE, LED_OFF);
1191 set_led(card, LED_FAULT, LED_OFF);
1193 batt_status = readb(card->csr_remap + MEMCTRLSTATUS_BATTERY);
1195 card->battery[0].good = !(batt_status & BATTERY_1_FAILURE);
1196 card->battery[1].good = !(batt_status & BATTERY_2_FAILURE);
1197 card->battery[0].last_change = card->battery[1].last_change = jiffies;
1199 printk(KERN_INFO "MM%d: Size %d KB, Battery 1 %s (%s), Battery 2 %s (%s)\n",
1200 card->card_number, card->mm_size,
1201 (batt_status & BATTERY_1_DISABLED ? "Disabled" : "Enabled"),
1202 card->battery[0].good ? "OK" : "FAILURE",
1203 (batt_status & BATTERY_2_DISABLED ? "Disabled" : "Enabled"),
1204 card->battery[1].good ? "OK" : "FAILURE");
1206 set_fault_to_battery_status(card);
1208 pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &saved_bar);
1210 pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, data);
1211 pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &data);
1212 pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, saved_bar);
1217 card->win_size = data;
1220 if (request_irq(dev->irq, mm_interrupt, SA_SHIRQ, "pci-umem", card)) {
1221 printk(KERN_ERR "MM%d: Unable to allocate IRQ\n", card->card_number);
1224 goto failed_req_irq;
1227 card->irq = dev->irq;
1228 printk(KERN_INFO "MM%d: Window size %d bytes, IRQ %d\n", card->card_number,
1229 card->win_size, card->irq);
1231 spin_lock_init(&card->lock);
1233 pci_set_drvdata(dev, card);
1235 if (pci_write_cmd != 0x0F) /* If not Memory Write & Invalidate */
1236 pci_write_cmd = 0x07; /* then Memory Write command */
1238 if (pci_write_cmd & 0x08) { /* use Memory Write and Invalidate */
1239 unsigned short cfg_command;
1240 pci_read_config_word(dev, PCI_COMMAND, &cfg_command);
1241 cfg_command |= 0x10; /* Memory Write & Invalidate Enable */
1242 pci_write_config_word(dev, PCI_COMMAND, cfg_command);
1244 pci_cmds = (pci_read_cmd << 28) | (pci_write_cmd << 24);
1251 if (!get_userbit(card, MEMORY_INITIALIZED))
1252 printk(KERN_INFO "MM%d: memory NOT initialized. perhaps load with init_mem=1?\n", card->card_number);
1254 printk(KERN_INFO "MM%d: memory already initialized\n", card->card_number);
1258 writeb(EDC_STORE_CORRECT, card->csr_remap + MEMCTRLCMD_ERRCTRL);
1264 if (card->mm_pages[0].desc)
1265 pci_free_consistent(card->dev, PAGE_SIZE*2,
1266 card->mm_pages[0].desc,
1267 card->mm_pages[0].page_dma);
1268 if (card->mm_pages[1].desc)
1269 pci_free_consistent(card->dev, PAGE_SIZE*2,
1270 card->mm_pages[1].desc,
1271 card->mm_pages[1].page_dma);
1273 #ifdef CONFIG_MM_MAP_MEMORY
1274 iounmap((void *) card->mem_remap);
1276 release_mem_region(card->mem_base, card->mem_len);
1279 iounmap((void *) card->csr_base);
1281 release_mem_region(card->csr_base, card->csr_len);
1287 -----------------------------------------------------------------------------------
1289 -----------------------------------------------------------------------------------
1291 static void mm_pci_remove(struct pci_dev *dev)
1293 struct cardinfo *card = pci_get_drvdata(dev);
1295 tasklet_kill(&card->tasklet);
1296 iounmap(card->csr_remap);
1297 release_mem_region(card->csr_base, card->csr_len);
1298 #ifdef CONFIG_MM_MAP_MEMORY
1299 iounmap(card->mem_remap);
1300 release_mem_region(card->mem_base, card->mem_len);
1302 free_irq(card->irq, card);
1304 if (card->mm_pages[0].desc)
1305 pci_free_consistent(card->dev, PAGE_SIZE*2,
1306 card->mm_pages[0].desc,
1307 card->mm_pages[0].page_dma);
1308 if (card->mm_pages[1].desc)
1309 pci_free_consistent(card->dev, PAGE_SIZE*2,
1310 card->mm_pages[1].desc,
1311 card->mm_pages[1].page_dma);
1314 static const struct pci_device_id __devinitdata mm_pci_ids[] = { {
1315 vendor: PCI_VENDOR_ID_MICRO_MEMORY,
1316 device: PCI_DEVICE_ID_MICRO_MEMORY_5415CN,
1317 }, { /* end: all zeroes */ }
1320 MODULE_DEVICE_TABLE(pci, mm_pci_ids);
1322 static struct pci_driver mm_pci_driver = {
1324 id_table: mm_pci_ids,
1325 probe: mm_pci_probe,
1326 remove: mm_pci_remove,
1329 -----------------------------------------------------------------------------------
1331 -----------------------------------------------------------------------------------
1333 int __init mm_init(void)
1338 printk(KERN_INFO DRIVER_VERSION " : " DRIVER_DESC "\n");
1340 memset (cards, 0, MM_MAXCARDS * sizeof(struct cardinfo));
1341 memset (mm_sizes, 0, (MM_MAXCARDS << MM_SHIFT) * sizeof (int));
1342 memset (mm_partitions, 0,
1343 (MM_MAXCARDS << MM_SHIFT) * sizeof(struct hd_struct));
1345 retval = pci_module_init(&mm_pci_driver);
1349 err = devfs_register_blkdev(MAJOR_NR, "umem", &mm_fops);
1351 printk(KERN_ERR "MM: Could not register block device\n");
1354 devfs_handle = devfs_mk_dir(NULL, "umem", NULL);
1356 read_ahead[MAJOR_NR] = MM_RAHEAD;
1359 /* Initialize partition size: partion 0 of each card is the entire card */
1360 for (i = 0; i < num_cards; i++) {
1361 mm_sizes[i << MM_SHIFT] = cards[i].mm_size;
1363 mm_gendisk.sizes = mm_sizes;
1365 for (i = 0; i < num_cards; i++) {
1366 spin_lock_init(&cards[i].lock);
1367 mm_partitions[i << MM_SHIFT].nr_sects =
1368 cards[i].mm_size * (1024 / MM_HARDSECT);
1371 mm_gendisk.part = mm_partitions;
1372 mm_gendisk.nr_real = num_cards;
1374 add_gendisk(&mm_gendisk);
1376 blk_queue_make_request(BLK_DEFAULT_QUEUE(MAJOR_NR),
1379 for (i = 0; i < num_cards << MM_SHIFT; i++) {
1380 mm_hardsect[i] = MM_HARDSECT;
1381 mm_blocksizes[i] = MM_BLKSIZE;
1384 hardsect_size[MAJOR_NR] = mm_hardsect;
1385 blksize_size[MAJOR_NR] = mm_blocksizes;
1386 blk_size[MAJOR_NR] = mm_gendisk.sizes;
1387 for (i = 0; i < num_cards; i++) {
1388 register_disk(&mm_gendisk, MKDEV(MAJOR_NR, i<<MM_SHIFT), MM_SHIFT,
1389 &mm_fops, cards[i].mm_size << 1);
1392 init_battery_timer();
1393 printk("MM: desc_per_page = %ld\n", DESC_PER_PAGE);
1394 /* printk("mm_init: Done. 10-19-01 9:00\n"); */
1398 -----------------------------------------------------------------------------------
1400 -----------------------------------------------------------------------------------
1402 void __exit mm_cleanup(void)
1406 del_battery_timer();
1408 for (i=0; i < num_cards ; i++)
1409 devfs_register_partitions(&mm_gendisk, i<<MM_SHIFT, 1);
1411 devfs_unregister(devfs_handle);
1412 devfs_handle = NULL;
1414 pci_unregister_driver(&mm_pci_driver);
1416 devfs_unregister_blkdev(MAJOR_NR, "umem");
1418 for (i = 0; i < (num_cards << MM_SHIFT); i++)
1419 invalidate_device (MKDEV(MAJOR_NR,i), 1);
1421 hardsect_size[MAJOR_NR] = NULL;
1422 blksize_size [MAJOR_NR] = NULL;
1423 blk_size [MAJOR_NR] = NULL;
1424 read_ahead [MAJOR_NR] = 0;
1427 * Get our gendisk structure off the list.
1429 del_gendisk(&mm_gendisk);
1432 module_init(mm_init);
1433 module_exit(mm_cleanup);
1435 MODULE_AUTHOR(DRIVER_AUTHOR);
1436 MODULE_DESCRIPTION(DRIVER_DESC);
1437 MODULE_LICENSE("GPL");