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_NAME "umem"
71 #define DRIVER_VERSION "v2.3"
72 #define DRIVER_AUTHOR "San Mehat, Johannes Erdfelt, NeilBrown"
73 #define DRIVER_DESC "Micro Memory(tm) PCI memory board block driver"
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_param(debug
, int, 0644);
83 MODULE_PARM_DESC(debug
, "Debug bitmask");
85 static int pci_read_cmd
= 0x0C; /* Read Multiple */
86 module_param(pci_read_cmd
, int, 0);
87 MODULE_PARM_DESC(pci_read_cmd
, "PCI read command");
89 static int pci_write_cmd
= 0x0F; /* Write and Invalidate */
90 module_param(pci_write_cmd
, int, 0);
91 MODULE_PARM_DESC(pci_write_cmd
, "PCI write command");
97 #include <linux/blkdev.h>
98 #include <linux/blkpg.h>
103 unsigned char __iomem
*csr_remap
;
104 unsigned int mm_size
; /* size in kbytes */
106 unsigned int init_size
; /* initial segment, in sectors,
110 struct bio
*bio
, *currentbio
, **biotail
;
112 sector_t current_sector
;
114 struct request_queue
*queue
;
118 struct mm_dma_desc
*desc
;
120 struct bio
*bio
, **biotail
;
123 #define DESC_PER_PAGE ((PAGE_SIZE*2)/sizeof(struct mm_dma_desc))
127 struct tasklet_struct tasklet
;
128 unsigned int dma_status
;
133 unsigned long last_change
;
142 static struct cardinfo cards
[MM_MAXCARDS
];
143 static struct block_device_operations mm_fops
;
144 static struct timer_list battery_timer
;
146 static int num_cards
= 0;
148 static struct gendisk
*mm_gendisk
[MM_MAXCARDS
];
150 static void check_batteries(struct cardinfo
*card
);
153 -----------------------------------------------------------------------------------
155 -----------------------------------------------------------------------------------
157 static int get_userbit(struct cardinfo
*card
, int bit
)
161 led
= readb(card
->csr_remap
+ MEMCTRLCMD_LEDCTRL
);
165 -----------------------------------------------------------------------------------
167 -----------------------------------------------------------------------------------
169 static int set_userbit(struct cardinfo
*card
, int bit
, unsigned char state
)
173 led
= readb(card
->csr_remap
+ MEMCTRLCMD_LEDCTRL
);
178 writeb(led
, card
->csr_remap
+ MEMCTRLCMD_LEDCTRL
);
183 -----------------------------------------------------------------------------------
185 -----------------------------------------------------------------------------------
188 * NOTE: For the power LED, use the LED_POWER_* macros since they differ
190 static void set_led(struct cardinfo
*card
, int shift
, unsigned char state
)
194 led
= readb(card
->csr_remap
+ MEMCTRLCMD_LEDCTRL
);
195 if (state
== LED_FLIP
)
198 led
&= ~(0x03 << shift
);
199 led
|= (state
<< shift
);
201 writeb(led
, card
->csr_remap
+ MEMCTRLCMD_LEDCTRL
);
207 -----------------------------------------------------------------------------------
209 -----------------------------------------------------------------------------------
211 static void dump_regs(struct cardinfo
*card
)
217 for (i
= 0; i
< 8; i
++) {
218 printk(KERN_DEBUG
"%p ", p
);
220 for (i1
= 0; i1
< 16; i1
++)
221 printk("%02x ", *p
++);
228 -----------------------------------------------------------------------------------
230 -----------------------------------------------------------------------------------
232 static void dump_dmastat(struct cardinfo
*card
, unsigned int dmastat
)
234 dev_printk(KERN_DEBUG
, &card
->dev
->dev
, "DMAstat - ");
235 if (dmastat
& DMASCR_ANY_ERR
)
237 if (dmastat
& DMASCR_MBE_ERR
)
239 if (dmastat
& DMASCR_PARITY_ERR_REP
)
240 printk("PARITY_ERR_REP ");
241 if (dmastat
& DMASCR_PARITY_ERR_DET
)
242 printk("PARITY_ERR_DET ");
243 if (dmastat
& DMASCR_SYSTEM_ERR_SIG
)
244 printk("SYSTEM_ERR_SIG ");
245 if (dmastat
& DMASCR_TARGET_ABT
)
246 printk("TARGET_ABT ");
247 if (dmastat
& DMASCR_MASTER_ABT
)
248 printk("MASTER_ABT ");
249 if (dmastat
& DMASCR_CHAIN_COMPLETE
)
250 printk("CHAIN_COMPLETE ");
251 if (dmastat
& DMASCR_DMA_COMPLETE
)
252 printk("DMA_COMPLETE ");
257 * Theory of request handling
259 * Each bio is assigned to one mm_dma_desc - which may not be enough FIXME
260 * We have two pages of mm_dma_desc, holding about 64 descriptors
261 * each. These are allocated at init time.
262 * One page is "Ready" and is either full, or can have request added.
263 * The other page might be "Active", which DMA is happening on it.
265 * Whenever IO on the active page completes, the Ready page is activated
266 * and the ex-Active page is clean out and made Ready.
267 * Otherwise the Ready page is only activated when it becomes full, or
268 * when mm_unplug_device is called via the unplug_io_fn.
270 * If a request arrives while both pages a full, it is queued, and b_rdev is
271 * overloaded to record whether it was a read or a write.
273 * The interrupt handler only polls the device to clear the interrupt.
274 * The processing of the result is done in a tasklet.
277 static void mm_start_io(struct cardinfo
*card
)
279 /* we have the lock, we know there is
280 * no IO active, and we know that card->Active
283 struct mm_dma_desc
*desc
;
284 struct mm_page
*page
;
287 /* make the last descriptor end the chain */
288 page
= &card
->mm_pages
[card
->Active
];
289 pr_debug("start_io: %d %d->%d\n", card
->Active
, page
->headcnt
, page
->cnt
-1);
290 desc
= &page
->desc
[page
->cnt
-1];
292 desc
->control_bits
|= cpu_to_le32(DMASCR_CHAIN_COMP_EN
);
293 desc
->control_bits
&= ~cpu_to_le32(DMASCR_CHAIN_EN
);
294 desc
->sem_control_bits
= desc
->control_bits
;
297 if (debug
& DEBUG_LED_ON_TRANSFER
)
298 set_led(card
, LED_REMOVE
, LED_ON
);
300 desc
= &page
->desc
[page
->headcnt
];
301 writel(0, card
->csr_remap
+ DMA_PCI_ADDR
);
302 writel(0, card
->csr_remap
+ DMA_PCI_ADDR
+ 4);
304 writel(0, card
->csr_remap
+ DMA_LOCAL_ADDR
);
305 writel(0, card
->csr_remap
+ DMA_LOCAL_ADDR
+ 4);
307 writel(0, card
->csr_remap
+ DMA_TRANSFER_SIZE
);
308 writel(0, card
->csr_remap
+ DMA_TRANSFER_SIZE
+ 4);
310 writel(0, card
->csr_remap
+ DMA_SEMAPHORE_ADDR
);
311 writel(0, card
->csr_remap
+ DMA_SEMAPHORE_ADDR
+ 4);
313 offset
= ((char*)desc
) - ((char*)page
->desc
);
314 writel(cpu_to_le32((page
->page_dma
+offset
)&0xffffffff),
315 card
->csr_remap
+ DMA_DESCRIPTOR_ADDR
);
316 /* Force the value to u64 before shifting otherwise >> 32 is undefined C
317 * and on some ports will do nothing ! */
318 writel(cpu_to_le32(((u64
)page
->page_dma
)>>32),
319 card
->csr_remap
+ DMA_DESCRIPTOR_ADDR
+ 4);
322 writel(cpu_to_le32(DMASCR_GO
| DMASCR_CHAIN_EN
| pci_cmds
),
323 card
->csr_remap
+ DMA_STATUS_CTRL
);
326 static int add_bio(struct cardinfo
*card
);
328 static void activate(struct cardinfo
*card
)
330 /* if No page is Active, and Ready is
331 * not empty, then switch Ready page
332 * to active and start IO.
333 * Then add any bh's that are available to Ready
337 while (add_bio(card
))
340 if (card
->Active
== -1 &&
341 card
->mm_pages
[card
->Ready
].cnt
> 0) {
342 card
->Active
= card
->Ready
;
343 card
->Ready
= 1-card
->Ready
;
347 } while (card
->Active
== -1 && add_bio(card
));
350 static inline void reset_page(struct mm_page
*page
)
355 page
->biotail
= & page
->bio
;
358 static void mm_unplug_device(struct request_queue
*q
)
360 struct cardinfo
*card
= q
->queuedata
;
363 spin_lock_irqsave(&card
->lock
, flags
);
364 if (blk_remove_plug(q
))
366 spin_unlock_irqrestore(&card
->lock
, flags
);
370 * If there is room on Ready page, take
371 * one bh off list and add it.
372 * return 1 if there was room, else 0.
374 static int add_bio(struct cardinfo
*card
)
377 struct mm_dma_desc
*desc
;
378 dma_addr_t dma_handle
;
386 bio
= card
->currentbio
;
387 if (!bio
&& card
->bio
) {
388 card
->currentbio
= card
->bio
;
389 card
->current_idx
= card
->bio
->bi_idx
;
390 card
->current_sector
= card
->bio
->bi_sector
;
391 card
->bio
= card
->bio
->bi_next
;
392 if (card
->bio
== NULL
)
393 card
->biotail
= &card
->bio
;
394 card
->currentbio
->bi_next
= NULL
;
399 idx
= card
->current_idx
;
402 if (card
->mm_pages
[card
->Ready
].cnt
>= DESC_PER_PAGE
)
405 vec
= bio_iovec_idx(bio
, idx
);
407 dma_handle
= pci_map_page(card
->dev
,
412 PCI_DMA_FROMDEVICE
: PCI_DMA_TODEVICE
);
414 p
= &card
->mm_pages
[card
->Ready
];
415 desc
= &p
->desc
[p
->cnt
];
419 if ((p
->biotail
) != &bio
->bi_next
) {
421 p
->biotail
= &(bio
->bi_next
);
425 desc
->data_dma_handle
= dma_handle
;
427 desc
->pci_addr
= cpu_to_le64((u64
)desc
->data_dma_handle
);
428 desc
->local_addr
= cpu_to_le64(card
->current_sector
<< 9);
429 desc
->transfer_size
= cpu_to_le32(len
);
430 offset
= ( ((char*)&desc
->sem_control_bits
) - ((char*)p
->desc
));
431 desc
->sem_addr
= cpu_to_le64((u64
)(p
->page_dma
+offset
));
432 desc
->zero1
= desc
->zero2
= 0;
433 offset
= ( ((char*)(desc
+1)) - ((char*)p
->desc
));
434 desc
->next_desc_addr
= cpu_to_le64(p
->page_dma
+offset
);
435 desc
->control_bits
= cpu_to_le32(DMASCR_GO
|DMASCR_ERR_INT_EN
|
436 DMASCR_PARITY_INT_EN
|
441 desc
->control_bits
|= cpu_to_le32(DMASCR_TRANSFER_READ
);
442 desc
->sem_control_bits
= desc
->control_bits
;
444 card
->current_sector
+= (len
>> 9);
446 card
->current_idx
= idx
;
447 if (idx
>= bio
->bi_vcnt
)
448 card
->currentbio
= NULL
;
453 static void process_page(unsigned long data
)
455 /* check if any of the requests in the page are DMA_COMPLETE,
456 * and deal with them appropriately.
457 * If we find a descriptor without DMA_COMPLETE in the semaphore, then
458 * dma must have hit an error on that descriptor, so use dma_status instead
459 * and assume that all following descriptors must be re-tried.
461 struct mm_page
*page
;
462 struct bio
*return_bio
=NULL
;
463 struct cardinfo
*card
= (struct cardinfo
*)data
;
464 unsigned int dma_status
= card
->dma_status
;
466 spin_lock_bh(&card
->lock
);
467 if (card
->Active
< 0)
469 page
= &card
->mm_pages
[card
->Active
];
471 while (page
->headcnt
< page
->cnt
) {
472 struct bio
*bio
= page
->bio
;
473 struct mm_dma_desc
*desc
= &page
->desc
[page
->headcnt
];
474 int control
= le32_to_cpu(desc
->sem_control_bits
);
478 if (!(control
& DMASCR_DMA_COMPLETE
)) {
479 control
= dma_status
;
485 if (page
->idx
>= bio
->bi_vcnt
) {
486 page
->bio
= bio
->bi_next
;
488 page
->idx
= page
->bio
->bi_idx
;
491 pci_unmap_page(card
->dev
, desc
->data_dma_handle
,
492 bio_iovec_idx(bio
,idx
)->bv_len
,
493 (control
& DMASCR_TRANSFER_READ
) ?
494 PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
495 if (control
& DMASCR_HARD_ERROR
) {
497 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
498 dev_printk(KERN_WARNING
, &card
->dev
->dev
,
499 "I/O error on sector %d/%d\n",
500 le32_to_cpu(desc
->local_addr
)>>9,
501 le32_to_cpu(desc
->transfer_size
));
502 dump_dmastat(card
, control
);
503 } else if (test_bit(BIO_RW
, &bio
->bi_rw
) &&
504 le32_to_cpu(desc
->local_addr
)>>9 == card
->init_size
) {
505 card
->init_size
+= le32_to_cpu(desc
->transfer_size
)>>9;
506 if (card
->init_size
>>1 >= card
->mm_size
) {
507 dev_printk(KERN_INFO
, &card
->dev
->dev
,
508 "memory now initialised\n");
509 set_userbit(card
, MEMORY_INITIALIZED
, 1);
512 if (bio
!= page
->bio
) {
513 bio
->bi_next
= return_bio
;
520 if (debug
& DEBUG_LED_ON_TRANSFER
)
521 set_led(card
, LED_REMOVE
, LED_OFF
);
523 if (card
->check_batteries
) {
524 card
->check_batteries
= 0;
525 check_batteries(card
);
527 if (page
->headcnt
>= page
->cnt
) {
532 /* haven't finished with this one yet */
533 pr_debug("do some more\n");
537 spin_unlock_bh(&card
->lock
);
540 struct bio
*bio
= return_bio
;
542 return_bio
= bio
->bi_next
;
549 -----------------------------------------------------------------------------------
551 -----------------------------------------------------------------------------------
553 static int mm_make_request(struct request_queue
*q
, struct bio
*bio
)
555 struct cardinfo
*card
= q
->queuedata
;
556 pr_debug("mm_make_request %llu %u\n",
557 (unsigned long long)bio
->bi_sector
, bio
->bi_size
);
559 spin_lock_irq(&card
->lock
);
560 *card
->biotail
= bio
;
562 card
->biotail
= &bio
->bi_next
;
564 spin_unlock_irq(&card
->lock
);
570 -----------------------------------------------------------------------------------
572 -----------------------------------------------------------------------------------
574 static irqreturn_t
mm_interrupt(int irq
, void *__card
)
576 struct cardinfo
*card
= (struct cardinfo
*) __card
;
577 unsigned int dma_status
;
578 unsigned short cfg_status
;
582 dma_status
= le32_to_cpu(readl(card
->csr_remap
+ DMA_STATUS_CTRL
));
584 if (!(dma_status
& (DMASCR_ERROR_MASK
| DMASCR_CHAIN_COMPLETE
))) {
585 /* interrupt wasn't for me ... */
589 /* clear COMPLETION interrupts */
590 if (card
->flags
& UM_FLAG_NO_BYTE_STATUS
)
591 writel(cpu_to_le32(DMASCR_DMA_COMPLETE
|DMASCR_CHAIN_COMPLETE
),
592 card
->csr_remap
+ DMA_STATUS_CTRL
);
594 writeb((DMASCR_DMA_COMPLETE
|DMASCR_CHAIN_COMPLETE
) >> 16,
595 card
->csr_remap
+ DMA_STATUS_CTRL
+ 2);
597 /* log errors and clear interrupt status */
598 if (dma_status
& DMASCR_ANY_ERR
) {
599 unsigned int data_log1
, data_log2
;
600 unsigned int addr_log1
, addr_log2
;
601 unsigned char stat
, count
, syndrome
, check
;
603 stat
= readb(card
->csr_remap
+ MEMCTRLCMD_ERRSTATUS
);
605 data_log1
= le32_to_cpu(readl(card
->csr_remap
+ ERROR_DATA_LOG
));
606 data_log2
= le32_to_cpu(readl(card
->csr_remap
+ ERROR_DATA_LOG
+ 4));
607 addr_log1
= le32_to_cpu(readl(card
->csr_remap
+ ERROR_ADDR_LOG
));
608 addr_log2
= readb(card
->csr_remap
+ ERROR_ADDR_LOG
+ 4);
610 count
= readb(card
->csr_remap
+ ERROR_COUNT
);
611 syndrome
= readb(card
->csr_remap
+ ERROR_SYNDROME
);
612 check
= readb(card
->csr_remap
+ ERROR_CHECK
);
614 dump_dmastat(card
, dma_status
);
617 dev_printk(KERN_ERR
, &card
->dev
->dev
,
618 "Memory access error detected (err count %d)\n",
621 dev_printk(KERN_ERR
, &card
->dev
->dev
,
622 "Multi-bit EDC error\n");
624 dev_printk(KERN_ERR
, &card
->dev
->dev
,
625 "Fault Address 0x%02x%08x, Fault Data 0x%08x%08x\n",
626 addr_log2
, addr_log1
, data_log2
, data_log1
);
627 dev_printk(KERN_ERR
, &card
->dev
->dev
,
628 "Fault Check 0x%02x, Fault Syndrome 0x%02x\n",
631 writeb(0, card
->csr_remap
+ ERROR_COUNT
);
634 if (dma_status
& DMASCR_PARITY_ERR_REP
) {
635 dev_printk(KERN_ERR
, &card
->dev
->dev
,
636 "PARITY ERROR REPORTED\n");
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 dev_printk(KERN_ERR
, &card
->dev
->dev
,
643 "PARITY ERROR DETECTED\n");
644 pci_read_config_word(card
->dev
, PCI_STATUS
, &cfg_status
);
645 pci_write_config_word(card
->dev
, PCI_STATUS
, cfg_status
);
648 if (dma_status
& DMASCR_SYSTEM_ERR_SIG
) {
649 dev_printk(KERN_ERR
, &card
->dev
->dev
, "SYSTEM ERROR\n");
650 pci_read_config_word(card
->dev
, PCI_STATUS
, &cfg_status
);
651 pci_write_config_word(card
->dev
, PCI_STATUS
, cfg_status
);
654 if (dma_status
& DMASCR_TARGET_ABT
) {
655 dev_printk(KERN_ERR
, &card
->dev
->dev
, "TARGET ABORT\n");
656 pci_read_config_word(card
->dev
, PCI_STATUS
, &cfg_status
);
657 pci_write_config_word(card
->dev
, PCI_STATUS
, cfg_status
);
660 if (dma_status
& DMASCR_MASTER_ABT
) {
661 dev_printk(KERN_ERR
, &card
->dev
->dev
, "MASTER ABORT\n");
662 pci_read_config_word(card
->dev
, PCI_STATUS
, &cfg_status
);
663 pci_write_config_word(card
->dev
, PCI_STATUS
, cfg_status
);
666 /* and process the DMA descriptors */
667 card
->dma_status
= dma_status
;
668 tasklet_schedule(&card
->tasklet
);
675 -----------------------------------------------------------------------------------
676 -- set_fault_to_battery_status
677 -----------------------------------------------------------------------------------
680 * If both batteries are good, no LED
681 * If either battery has been warned, solid LED
682 * If both batteries are bad, flash the LED quickly
683 * If either battery is bad, flash the LED semi quickly
685 static void set_fault_to_battery_status(struct cardinfo
*card
)
687 if (card
->battery
[0].good
&& card
->battery
[1].good
)
688 set_led(card
, LED_FAULT
, LED_OFF
);
689 else if (card
->battery
[0].warned
|| card
->battery
[1].warned
)
690 set_led(card
, LED_FAULT
, LED_ON
);
691 else if (!card
->battery
[0].good
&& !card
->battery
[1].good
)
692 set_led(card
, LED_FAULT
, LED_FLASH_7_0
);
694 set_led(card
, LED_FAULT
, LED_FLASH_3_5
);
697 static void init_battery_timer(void);
701 -----------------------------------------------------------------------------------
703 -----------------------------------------------------------------------------------
705 static int check_battery(struct cardinfo
*card
, int battery
, int status
)
707 if (status
!= card
->battery
[battery
].good
) {
708 card
->battery
[battery
].good
= !card
->battery
[battery
].good
;
709 card
->battery
[battery
].last_change
= jiffies
;
711 if (card
->battery
[battery
].good
) {
712 dev_printk(KERN_ERR
, &card
->dev
->dev
,
713 "Battery %d now good\n", battery
+ 1);
714 card
->battery
[battery
].warned
= 0;
716 dev_printk(KERN_ERR
, &card
->dev
->dev
,
717 "Battery %d now FAILED\n", battery
+ 1);
720 } else if (!card
->battery
[battery
].good
&&
721 !card
->battery
[battery
].warned
&&
722 time_after_eq(jiffies
, card
->battery
[battery
].last_change
+
723 (HZ
* 60 * 60 * 5))) {
724 dev_printk(KERN_ERR
, &card
->dev
->dev
,
725 "Battery %d still FAILED after 5 hours\n", battery
+ 1);
726 card
->battery
[battery
].warned
= 1;
734 -----------------------------------------------------------------------------------
736 -----------------------------------------------------------------------------------
738 static void check_batteries(struct cardinfo
*card
)
740 /* NOTE: this must *never* be called while the card
741 * is doing (bus-to-card) DMA, or you will need the
744 unsigned char status
;
747 status
= readb(card
->csr_remap
+ MEMCTRLSTATUS_BATTERY
);
748 if (debug
& DEBUG_BATTERY_POLLING
)
749 dev_printk(KERN_DEBUG
, &card
->dev
->dev
,
750 "checking battery status, 1 = %s, 2 = %s\n",
751 (status
& BATTERY_1_FAILURE
) ? "FAILURE" : "OK",
752 (status
& BATTERY_2_FAILURE
) ? "FAILURE" : "OK");
754 ret1
= check_battery(card
, 0, !(status
& BATTERY_1_FAILURE
));
755 ret2
= check_battery(card
, 1, !(status
& BATTERY_2_FAILURE
));
758 set_fault_to_battery_status(card
);
761 static void check_all_batteries(unsigned long ptr
)
765 for (i
= 0; i
< num_cards
; i
++)
766 if (!(cards
[i
].flags
& UM_FLAG_NO_BATT
)) {
767 struct cardinfo
*card
= &cards
[i
];
768 spin_lock_bh(&card
->lock
);
769 if (card
->Active
>= 0)
770 card
->check_batteries
= 1;
772 check_batteries(card
);
773 spin_unlock_bh(&card
->lock
);
776 init_battery_timer();
779 -----------------------------------------------------------------------------------
780 -- init_battery_timer
781 -----------------------------------------------------------------------------------
783 static void init_battery_timer(void)
785 init_timer(&battery_timer
);
786 battery_timer
.function
= check_all_batteries
;
787 battery_timer
.expires
= jiffies
+ (HZ
* 60);
788 add_timer(&battery_timer
);
791 -----------------------------------------------------------------------------------
793 -----------------------------------------------------------------------------------
795 static void del_battery_timer(void)
797 del_timer(&battery_timer
);
800 -----------------------------------------------------------------------------------
802 -----------------------------------------------------------------------------------
805 * Note no locks taken out here. In a worst case scenario, we could drop
806 * a chunk of system memory. But that should never happen, since validation
807 * happens at open or mount time, when locks are held.
809 * That's crap, since doing that while some partitions are opened
810 * or mounted will give you really nasty results.
812 static int mm_revalidate(struct gendisk
*disk
)
814 struct cardinfo
*card
= disk
->private_data
;
815 set_capacity(disk
, card
->mm_size
<< 1);
819 static int mm_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
821 struct cardinfo
*card
= bdev
->bd_disk
->private_data
;
822 int size
= card
->mm_size
* (1024 / MM_HARDSECT
);
825 * get geometry: we have to fake one... trim the size to a
826 * multiple of 2048 (1M): tell we have 32 sectors, 64 heads,
827 * whatever cylinders.
831 geo
->cylinders
= size
/ (geo
->heads
* geo
->sectors
);
836 -----------------------------------------------------------------------------------
838 -----------------------------------------------------------------------------------
839 Future support for removable devices
841 static int mm_check_change(struct gendisk
*disk
)
843 /* struct cardinfo *dev = disk->private_data; */
847 -----------------------------------------------------------------------------------
849 -----------------------------------------------------------------------------------
851 static struct block_device_operations mm_fops
= {
852 .owner
= THIS_MODULE
,
854 .revalidate_disk
= mm_revalidate
,
855 .media_changed
= mm_check_change
,
858 -----------------------------------------------------------------------------------
860 -----------------------------------------------------------------------------------
862 static int __devinit
mm_pci_probe(struct pci_dev
*dev
, const struct pci_device_id
*id
)
865 struct cardinfo
*card
= &cards
[num_cards
];
866 unsigned char mem_present
;
867 unsigned char batt_status
;
868 unsigned int saved_bar
, data
;
869 unsigned long csr_base
;
870 unsigned long csr_len
;
872 static int printed_version
;
874 if (!printed_version
++)
875 printk(KERN_INFO DRIVER_VERSION
" : " DRIVER_DESC
"\n");
877 ret
= pci_enable_device(dev
);
881 pci_write_config_byte(dev
, PCI_LATENCY_TIMER
, 0xF8);
886 csr_base
= pci_resource_start(dev
, 0);
887 csr_len
= pci_resource_len(dev
, 0);
888 if (!csr_base
|| !csr_len
)
891 dev_printk(KERN_INFO
, &dev
->dev
,
892 "Micro Memory(tm) controller found (PCI Mem Module (Battery Backup))\n");
894 if (pci_set_dma_mask(dev
, DMA_64BIT_MASK
) &&
895 pci_set_dma_mask(dev
, DMA_32BIT_MASK
)) {
896 dev_printk(KERN_WARNING
, &dev
->dev
, "NO suitable DMA found\n");
900 ret
= pci_request_regions(dev
, DRIVER_NAME
);
902 dev_printk(KERN_ERR
, &card
->dev
->dev
,
903 "Unable to request memory region\n");
907 card
->csr_remap
= ioremap_nocache(csr_base
, csr_len
);
908 if (!card
->csr_remap
) {
909 dev_printk(KERN_ERR
, &card
->dev
->dev
,
910 "Unable to remap memory region\n");
913 goto failed_remap_csr
;
916 dev_printk(KERN_INFO
, &card
->dev
->dev
,
917 "CSR 0x%08lx -> 0x%p (0x%lx)\n",
918 csr_base
, card
->csr_remap
, csr_len
);
920 switch(card
->dev
->device
) {
922 card
->flags
|= UM_FLAG_NO_BYTE_STATUS
| UM_FLAG_NO_BATTREG
;
927 card
->flags
|= UM_FLAG_NO_BYTE_STATUS
;
932 card
->flags
|= UM_FLAG_NO_BYTE_STATUS
| UM_FLAG_NO_BATTREG
| UM_FLAG_NO_BATT
;
937 magic_number
= 0x100;
941 if (readb(card
->csr_remap
+ MEMCTRLSTATUS_MAGIC
) != magic_number
) {
942 dev_printk(KERN_ERR
, &card
->dev
->dev
, "Magic number invalid\n");
947 card
->mm_pages
[0].desc
= pci_alloc_consistent(card
->dev
,
949 &card
->mm_pages
[0].page_dma
);
950 card
->mm_pages
[1].desc
= pci_alloc_consistent(card
->dev
,
952 &card
->mm_pages
[1].page_dma
);
953 if (card
->mm_pages
[0].desc
== NULL
||
954 card
->mm_pages
[1].desc
== NULL
) {
955 dev_printk(KERN_ERR
, &card
->dev
->dev
, "alloc failed\n");
958 reset_page(&card
->mm_pages
[0]);
959 reset_page(&card
->mm_pages
[1]);
960 card
->Ready
= 0; /* page 0 is ready */
961 card
->Active
= -1; /* no page is active */
963 card
->biotail
= &card
->bio
;
965 card
->queue
= blk_alloc_queue(GFP_KERNEL
);
969 blk_queue_make_request(card
->queue
, mm_make_request
);
970 card
->queue
->queuedata
= card
;
971 card
->queue
->unplug_fn
= mm_unplug_device
;
973 tasklet_init(&card
->tasklet
, process_page
, (unsigned long)card
);
975 card
->check_batteries
= 0;
977 mem_present
= readb(card
->csr_remap
+ MEMCTRLSTATUS_MEMORY
);
978 switch (mem_present
) {
980 card
->mm_size
= 1024 * 128;
983 card
->mm_size
= 1024 * 256;
986 card
->mm_size
= 1024 * 512;
989 card
->mm_size
= 1024 * 1024;
992 card
->mm_size
= 1024 * 2048;
999 /* Clear the LED's we control */
1000 set_led(card
, LED_REMOVE
, LED_OFF
);
1001 set_led(card
, LED_FAULT
, LED_OFF
);
1003 batt_status
= readb(card
->csr_remap
+ MEMCTRLSTATUS_BATTERY
);
1005 card
->battery
[0].good
= !(batt_status
& BATTERY_1_FAILURE
);
1006 card
->battery
[1].good
= !(batt_status
& BATTERY_2_FAILURE
);
1007 card
->battery
[0].last_change
= card
->battery
[1].last_change
= jiffies
;
1009 if (card
->flags
& UM_FLAG_NO_BATT
)
1010 dev_printk(KERN_INFO
, &card
->dev
->dev
,
1011 "Size %d KB\n", card
->mm_size
);
1013 dev_printk(KERN_INFO
, &card
->dev
->dev
,
1014 "Size %d KB, Battery 1 %s (%s), Battery 2 %s (%s)\n",
1016 (batt_status
& BATTERY_1_DISABLED
? "Disabled" : "Enabled"),
1017 card
->battery
[0].good
? "OK" : "FAILURE",
1018 (batt_status
& BATTERY_2_DISABLED
? "Disabled" : "Enabled"),
1019 card
->battery
[1].good
? "OK" : "FAILURE");
1021 set_fault_to_battery_status(card
);
1024 pci_read_config_dword(dev
, PCI_BASE_ADDRESS_1
, &saved_bar
);
1026 pci_write_config_dword(dev
, PCI_BASE_ADDRESS_1
, data
);
1027 pci_read_config_dword(dev
, PCI_BASE_ADDRESS_1
, &data
);
1028 pci_write_config_dword(dev
, PCI_BASE_ADDRESS_1
, saved_bar
);
1033 if (request_irq(dev
->irq
, mm_interrupt
, IRQF_SHARED
, DRIVER_NAME
, card
)) {
1034 dev_printk(KERN_ERR
, &card
->dev
->dev
,
1035 "Unable to allocate IRQ\n");
1038 goto failed_req_irq
;
1041 dev_printk(KERN_INFO
, &card
->dev
->dev
,
1042 "Window size %d bytes, IRQ %d\n", data
, dev
->irq
);
1044 spin_lock_init(&card
->lock
);
1046 pci_set_drvdata(dev
, card
);
1048 if (pci_write_cmd
!= 0x0F) /* If not Memory Write & Invalidate */
1049 pci_write_cmd
= 0x07; /* then Memory Write command */
1051 if (pci_write_cmd
& 0x08) { /* use Memory Write and Invalidate */
1052 unsigned short cfg_command
;
1053 pci_read_config_word(dev
, PCI_COMMAND
, &cfg_command
);
1054 cfg_command
|= 0x10; /* Memory Write & Invalidate Enable */
1055 pci_write_config_word(dev
, PCI_COMMAND
, cfg_command
);
1057 pci_cmds
= (pci_read_cmd
<< 28) | (pci_write_cmd
<< 24);
1061 if (!get_userbit(card
, MEMORY_INITIALIZED
)) {
1062 dev_printk(KERN_INFO
, &card
->dev
->dev
,
1063 "memory NOT initialized. Consider over-writing whole device.\n");
1064 card
->init_size
= 0;
1066 dev_printk(KERN_INFO
, &card
->dev
->dev
,
1067 "memory already initialized\n");
1068 card
->init_size
= card
->mm_size
;
1072 writeb(EDC_STORE_CORRECT
, card
->csr_remap
+ MEMCTRLCMD_ERRCTRL
);
1078 if (card
->mm_pages
[0].desc
)
1079 pci_free_consistent(card
->dev
, PAGE_SIZE
*2,
1080 card
->mm_pages
[0].desc
,
1081 card
->mm_pages
[0].page_dma
);
1082 if (card
->mm_pages
[1].desc
)
1083 pci_free_consistent(card
->dev
, PAGE_SIZE
*2,
1084 card
->mm_pages
[1].desc
,
1085 card
->mm_pages
[1].page_dma
);
1087 iounmap(card
->csr_remap
);
1089 pci_release_regions(dev
);
1095 -----------------------------------------------------------------------------------
1097 -----------------------------------------------------------------------------------
1099 static void mm_pci_remove(struct pci_dev
*dev
)
1101 struct cardinfo
*card
= pci_get_drvdata(dev
);
1103 tasklet_kill(&card
->tasklet
);
1104 free_irq(dev
->irq
, card
);
1105 iounmap(card
->csr_remap
);
1107 if (card
->mm_pages
[0].desc
)
1108 pci_free_consistent(card
->dev
, PAGE_SIZE
*2,
1109 card
->mm_pages
[0].desc
,
1110 card
->mm_pages
[0].page_dma
);
1111 if (card
->mm_pages
[1].desc
)
1112 pci_free_consistent(card
->dev
, PAGE_SIZE
*2,
1113 card
->mm_pages
[1].desc
,
1114 card
->mm_pages
[1].page_dma
);
1115 blk_cleanup_queue(card
->queue
);
1117 pci_release_regions(dev
);
1118 pci_disable_device(dev
);
1121 static const struct pci_device_id mm_pci_ids
[] = {
1122 {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY
,PCI_DEVICE_ID_MICRO_MEMORY_5415CN
)},
1123 {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY
,PCI_DEVICE_ID_MICRO_MEMORY_5425CN
)},
1124 {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY
,PCI_DEVICE_ID_MICRO_MEMORY_6155
)},
1132 }, { /* end: all zeroes */ }
1135 MODULE_DEVICE_TABLE(pci
, mm_pci_ids
);
1137 static struct pci_driver mm_pci_driver
= {
1138 .name
= DRIVER_NAME
,
1139 .id_table
= mm_pci_ids
,
1140 .probe
= mm_pci_probe
,
1141 .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, DRIVER_NAME
);
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
, DRIVER_NAME
);
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
, DRIVER_NAME
);
1217 module_init(mm_init
);
1218 module_exit(mm_cleanup
);
1220 MODULE_AUTHOR(DRIVER_AUTHOR
);
1221 MODULE_DESCRIPTION(DRIVER_DESC
);
1222 MODULE_LICENSE("GPL");