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 #undef DEBUG /* #define DEBUG 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/gfp.h>
44 #include <linux/ioctl.h>
45 #include <linux/module.h>
46 #include <linux/init.h>
47 #include <linux/interrupt.h>
48 #include <linux/timer.h>
49 #include <linux/pci.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 timer_list battery_timer
;
145 static int num_cards
;
147 static struct gendisk
*mm_gendisk
[MM_MAXCARDS
];
149 static void check_batteries(struct cardinfo
*card
);
151 static int get_userbit(struct cardinfo
*card
, int bit
)
155 led
= readb(card
->csr_remap
+ MEMCTRLCMD_LEDCTRL
);
159 static int set_userbit(struct cardinfo
*card
, int bit
, unsigned char state
)
163 led
= readb(card
->csr_remap
+ MEMCTRLCMD_LEDCTRL
);
168 writeb(led
, card
->csr_remap
+ MEMCTRLCMD_LEDCTRL
);
174 * NOTE: For the power LED, use the LED_POWER_* macros since they differ
176 static void set_led(struct cardinfo
*card
, int shift
, unsigned char state
)
180 led
= readb(card
->csr_remap
+ MEMCTRLCMD_LEDCTRL
);
181 if (state
== LED_FLIP
)
184 led
&= ~(0x03 << shift
);
185 led
|= (state
<< shift
);
187 writeb(led
, card
->csr_remap
+ MEMCTRLCMD_LEDCTRL
);
192 static void dump_regs(struct cardinfo
*card
)
198 for (i
= 0; i
< 8; i
++) {
199 printk(KERN_DEBUG
"%p ", p
);
201 for (i1
= 0; i1
< 16; i1
++)
202 printk("%02x ", *p
++);
209 static void dump_dmastat(struct cardinfo
*card
, unsigned int dmastat
)
211 dev_printk(KERN_DEBUG
, &card
->dev
->dev
, "DMAstat - ");
212 if (dmastat
& DMASCR_ANY_ERR
)
213 printk(KERN_CONT
"ANY_ERR ");
214 if (dmastat
& DMASCR_MBE_ERR
)
215 printk(KERN_CONT
"MBE_ERR ");
216 if (dmastat
& DMASCR_PARITY_ERR_REP
)
217 printk(KERN_CONT
"PARITY_ERR_REP ");
218 if (dmastat
& DMASCR_PARITY_ERR_DET
)
219 printk(KERN_CONT
"PARITY_ERR_DET ");
220 if (dmastat
& DMASCR_SYSTEM_ERR_SIG
)
221 printk(KERN_CONT
"SYSTEM_ERR_SIG ");
222 if (dmastat
& DMASCR_TARGET_ABT
)
223 printk(KERN_CONT
"TARGET_ABT ");
224 if (dmastat
& DMASCR_MASTER_ABT
)
225 printk(KERN_CONT
"MASTER_ABT ");
226 if (dmastat
& DMASCR_CHAIN_COMPLETE
)
227 printk(KERN_CONT
"CHAIN_COMPLETE ");
228 if (dmastat
& DMASCR_DMA_COMPLETE
)
229 printk(KERN_CONT
"DMA_COMPLETE ");
234 * Theory of request handling
236 * Each bio is assigned to one mm_dma_desc - which may not be enough FIXME
237 * We have two pages of mm_dma_desc, holding about 64 descriptors
238 * each. These are allocated at init time.
239 * One page is "Ready" and is either full, or can have request added.
240 * The other page might be "Active", which DMA is happening on it.
242 * Whenever IO on the active page completes, the Ready page is activated
243 * and the ex-Active page is clean out and made Ready.
244 * Otherwise the Ready page is only activated when it becomes full.
246 * If a request arrives while both pages a full, it is queued, and b_rdev is
247 * overloaded to record whether it was a read or a write.
249 * The interrupt handler only polls the device to clear the interrupt.
250 * The processing of the result is done in a tasklet.
253 static void mm_start_io(struct cardinfo
*card
)
255 /* we have the lock, we know there is
256 * no IO active, and we know that card->Active
259 struct mm_dma_desc
*desc
;
260 struct mm_page
*page
;
263 /* make the last descriptor end the chain */
264 page
= &card
->mm_pages
[card
->Active
];
265 pr_debug("start_io: %d %d->%d\n",
266 card
->Active
, page
->headcnt
, page
->cnt
- 1);
267 desc
= &page
->desc
[page
->cnt
-1];
269 desc
->control_bits
|= cpu_to_le32(DMASCR_CHAIN_COMP_EN
);
270 desc
->control_bits
&= ~cpu_to_le32(DMASCR_CHAIN_EN
);
271 desc
->sem_control_bits
= desc
->control_bits
;
274 if (debug
& DEBUG_LED_ON_TRANSFER
)
275 set_led(card
, LED_REMOVE
, LED_ON
);
277 desc
= &page
->desc
[page
->headcnt
];
278 writel(0, card
->csr_remap
+ DMA_PCI_ADDR
);
279 writel(0, card
->csr_remap
+ DMA_PCI_ADDR
+ 4);
281 writel(0, card
->csr_remap
+ DMA_LOCAL_ADDR
);
282 writel(0, card
->csr_remap
+ DMA_LOCAL_ADDR
+ 4);
284 writel(0, card
->csr_remap
+ DMA_TRANSFER_SIZE
);
285 writel(0, card
->csr_remap
+ DMA_TRANSFER_SIZE
+ 4);
287 writel(0, card
->csr_remap
+ DMA_SEMAPHORE_ADDR
);
288 writel(0, card
->csr_remap
+ DMA_SEMAPHORE_ADDR
+ 4);
290 offset
= ((char *)desc
) - ((char *)page
->desc
);
291 writel(cpu_to_le32((page
->page_dma
+offset
) & 0xffffffff),
292 card
->csr_remap
+ DMA_DESCRIPTOR_ADDR
);
293 /* Force the value to u64 before shifting otherwise >> 32 is undefined C
294 * and on some ports will do nothing ! */
295 writel(cpu_to_le32(((u64
)page
->page_dma
)>>32),
296 card
->csr_remap
+ DMA_DESCRIPTOR_ADDR
+ 4);
299 writel(cpu_to_le32(DMASCR_GO
| DMASCR_CHAIN_EN
| pci_cmds
),
300 card
->csr_remap
+ DMA_STATUS_CTRL
);
303 static int add_bio(struct cardinfo
*card
);
305 static void activate(struct cardinfo
*card
)
307 /* if No page is Active, and Ready is
308 * not empty, then switch Ready page
309 * to active and start IO.
310 * Then add any bh's that are available to Ready
314 while (add_bio(card
))
317 if (card
->Active
== -1 &&
318 card
->mm_pages
[card
->Ready
].cnt
> 0) {
319 card
->Active
= card
->Ready
;
320 card
->Ready
= 1-card
->Ready
;
324 } while (card
->Active
== -1 && add_bio(card
));
327 static inline void reset_page(struct mm_page
*page
)
332 page
->biotail
= &page
->bio
;
336 * If there is room on Ready page, take
337 * one bh off list and add it.
338 * return 1 if there was room, else 0.
340 static int add_bio(struct cardinfo
*card
)
343 struct mm_dma_desc
*desc
;
344 dma_addr_t dma_handle
;
352 bio
= card
->currentbio
;
353 if (!bio
&& card
->bio
) {
354 card
->currentbio
= card
->bio
;
355 card
->current_idx
= card
->bio
->bi_idx
;
356 card
->current_sector
= card
->bio
->bi_sector
;
357 card
->bio
= card
->bio
->bi_next
;
358 if (card
->bio
== NULL
)
359 card
->biotail
= &card
->bio
;
360 card
->currentbio
->bi_next
= NULL
;
365 idx
= card
->current_idx
;
368 if (card
->mm_pages
[card
->Ready
].cnt
>= DESC_PER_PAGE
)
371 vec
= bio_iovec_idx(bio
, idx
);
373 dma_handle
= pci_map_page(card
->dev
,
378 PCI_DMA_FROMDEVICE
: PCI_DMA_TODEVICE
);
380 p
= &card
->mm_pages
[card
->Ready
];
381 desc
= &p
->desc
[p
->cnt
];
385 if ((p
->biotail
) != &bio
->bi_next
) {
387 p
->biotail
= &(bio
->bi_next
);
391 desc
->data_dma_handle
= dma_handle
;
393 desc
->pci_addr
= cpu_to_le64((u64
)desc
->data_dma_handle
);
394 desc
->local_addr
= cpu_to_le64(card
->current_sector
<< 9);
395 desc
->transfer_size
= cpu_to_le32(len
);
396 offset
= (((char *)&desc
->sem_control_bits
) - ((char *)p
->desc
));
397 desc
->sem_addr
= cpu_to_le64((u64
)(p
->page_dma
+offset
));
398 desc
->zero1
= desc
->zero2
= 0;
399 offset
= (((char *)(desc
+1)) - ((char *)p
->desc
));
400 desc
->next_desc_addr
= cpu_to_le64(p
->page_dma
+offset
);
401 desc
->control_bits
= cpu_to_le32(DMASCR_GO
|DMASCR_ERR_INT_EN
|
402 DMASCR_PARITY_INT_EN
|
407 desc
->control_bits
|= cpu_to_le32(DMASCR_TRANSFER_READ
);
408 desc
->sem_control_bits
= desc
->control_bits
;
410 card
->current_sector
+= (len
>> 9);
412 card
->current_idx
= idx
;
413 if (idx
>= bio
->bi_vcnt
)
414 card
->currentbio
= NULL
;
419 static void process_page(unsigned long data
)
421 /* check if any of the requests in the page are DMA_COMPLETE,
422 * and deal with them appropriately.
423 * If we find a descriptor without DMA_COMPLETE in the semaphore, then
424 * dma must have hit an error on that descriptor, so use dma_status
425 * instead and assume that all following descriptors must be re-tried.
427 struct mm_page
*page
;
428 struct bio
*return_bio
= NULL
;
429 struct cardinfo
*card
= (struct cardinfo
*)data
;
430 unsigned int dma_status
= card
->dma_status
;
432 spin_lock_bh(&card
->lock
);
433 if (card
->Active
< 0)
435 page
= &card
->mm_pages
[card
->Active
];
437 while (page
->headcnt
< page
->cnt
) {
438 struct bio
*bio
= page
->bio
;
439 struct mm_dma_desc
*desc
= &page
->desc
[page
->headcnt
];
440 int control
= le32_to_cpu(desc
->sem_control_bits
);
444 if (!(control
& DMASCR_DMA_COMPLETE
)) {
445 control
= dma_status
;
451 if (page
->idx
>= bio
->bi_vcnt
) {
452 page
->bio
= bio
->bi_next
;
454 page
->idx
= page
->bio
->bi_idx
;
457 pci_unmap_page(card
->dev
, desc
->data_dma_handle
,
458 bio_iovec_idx(bio
, idx
)->bv_len
,
459 (control
& DMASCR_TRANSFER_READ
) ?
460 PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
461 if (control
& DMASCR_HARD_ERROR
) {
463 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
464 dev_printk(KERN_WARNING
, &card
->dev
->dev
,
465 "I/O error on sector %d/%d\n",
466 le32_to_cpu(desc
->local_addr
)>>9,
467 le32_to_cpu(desc
->transfer_size
));
468 dump_dmastat(card
, control
);
469 } else if ((bio
->bi_rw
& REQ_WRITE
) &&
470 le32_to_cpu(desc
->local_addr
) >> 9 ==
472 card
->init_size
+= le32_to_cpu(desc
->transfer_size
) >> 9;
473 if (card
->init_size
>> 1 >= card
->mm_size
) {
474 dev_printk(KERN_INFO
, &card
->dev
->dev
,
475 "memory now initialised\n");
476 set_userbit(card
, MEMORY_INITIALIZED
, 1);
479 if (bio
!= page
->bio
) {
480 bio
->bi_next
= return_bio
;
488 if (debug
& DEBUG_LED_ON_TRANSFER
)
489 set_led(card
, LED_REMOVE
, LED_OFF
);
491 if (card
->check_batteries
) {
492 card
->check_batteries
= 0;
493 check_batteries(card
);
495 if (page
->headcnt
>= page
->cnt
) {
500 /* haven't finished with this one yet */
501 pr_debug("do some more\n");
505 spin_unlock_bh(&card
->lock
);
508 struct bio
*bio
= return_bio
;
510 return_bio
= bio
->bi_next
;
517 struct blk_plug_cb cb
;
518 struct cardinfo
*card
;
521 static void mm_unplug(struct blk_plug_cb
*cb
)
523 struct mm_plug_cb
*mmcb
= container_of(cb
, struct mm_plug_cb
, cb
);
525 spin_lock_irq(&mmcb
->card
->lock
);
526 activate(mmcb
->card
);
527 spin_unlock_irq(&mmcb
->card
->lock
);
531 static int mm_check_plugged(struct cardinfo
*card
)
533 struct blk_plug
*plug
= current
->plug
;
534 struct mm_plug_cb
*mmcb
;
539 list_for_each_entry(mmcb
, &plug
->cb_list
, cb
.list
) {
540 if (mmcb
->cb
.callback
== mm_unplug
&& mmcb
->card
== card
)
543 /* Not currently on the callback list */
544 mmcb
= kmalloc(sizeof(*mmcb
), GFP_ATOMIC
);
549 mmcb
->cb
.callback
= mm_unplug
;
550 list_add(&mmcb
->cb
.list
, &plug
->cb_list
);
554 static void mm_make_request(struct request_queue
*q
, struct bio
*bio
)
556 struct cardinfo
*card
= q
->queuedata
;
557 pr_debug("mm_make_request %llu %u\n",
558 (unsigned long long)bio
->bi_sector
, bio
->bi_size
);
560 spin_lock_irq(&card
->lock
);
561 *card
->biotail
= bio
;
563 card
->biotail
= &bio
->bi_next
;
564 if (bio
->bi_rw
& REQ_SYNC
|| !mm_check_plugged(card
))
566 spin_unlock_irq(&card
->lock
);
571 static irqreturn_t
mm_interrupt(int irq
, void *__card
)
573 struct cardinfo
*card
= (struct cardinfo
*) __card
;
574 unsigned int dma_status
;
575 unsigned short cfg_status
;
579 dma_status
= le32_to_cpu(readl(card
->csr_remap
+ DMA_STATUS_CTRL
));
581 if (!(dma_status
& (DMASCR_ERROR_MASK
| DMASCR_CHAIN_COMPLETE
))) {
582 /* interrupt wasn't for me ... */
586 /* clear COMPLETION interrupts */
587 if (card
->flags
& UM_FLAG_NO_BYTE_STATUS
)
588 writel(cpu_to_le32(DMASCR_DMA_COMPLETE
|DMASCR_CHAIN_COMPLETE
),
589 card
->csr_remap
+ DMA_STATUS_CTRL
);
591 writeb((DMASCR_DMA_COMPLETE
|DMASCR_CHAIN_COMPLETE
) >> 16,
592 card
->csr_remap
+ DMA_STATUS_CTRL
+ 2);
594 /* log errors and clear interrupt status */
595 if (dma_status
& DMASCR_ANY_ERR
) {
596 unsigned int data_log1
, data_log2
;
597 unsigned int addr_log1
, addr_log2
;
598 unsigned char stat
, count
, syndrome
, check
;
600 stat
= readb(card
->csr_remap
+ MEMCTRLCMD_ERRSTATUS
);
602 data_log1
= le32_to_cpu(readl(card
->csr_remap
+
604 data_log2
= le32_to_cpu(readl(card
->csr_remap
+
605 ERROR_DATA_LOG
+ 4));
606 addr_log1
= le32_to_cpu(readl(card
->csr_remap
+
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
);
676 * If both batteries are good, no LED
677 * If either battery has been warned, solid LED
678 * If both batteries are bad, flash the LED quickly
679 * If either battery is bad, flash the LED semi quickly
681 static void set_fault_to_battery_status(struct cardinfo
*card
)
683 if (card
->battery
[0].good
&& card
->battery
[1].good
)
684 set_led(card
, LED_FAULT
, LED_OFF
);
685 else if (card
->battery
[0].warned
|| card
->battery
[1].warned
)
686 set_led(card
, LED_FAULT
, LED_ON
);
687 else if (!card
->battery
[0].good
&& !card
->battery
[1].good
)
688 set_led(card
, LED_FAULT
, LED_FLASH_7_0
);
690 set_led(card
, LED_FAULT
, LED_FLASH_3_5
);
693 static void init_battery_timer(void);
695 static int check_battery(struct cardinfo
*card
, int battery
, int status
)
697 if (status
!= card
->battery
[battery
].good
) {
698 card
->battery
[battery
].good
= !card
->battery
[battery
].good
;
699 card
->battery
[battery
].last_change
= jiffies
;
701 if (card
->battery
[battery
].good
) {
702 dev_printk(KERN_ERR
, &card
->dev
->dev
,
703 "Battery %d now good\n", battery
+ 1);
704 card
->battery
[battery
].warned
= 0;
706 dev_printk(KERN_ERR
, &card
->dev
->dev
,
707 "Battery %d now FAILED\n", battery
+ 1);
710 } else if (!card
->battery
[battery
].good
&&
711 !card
->battery
[battery
].warned
&&
712 time_after_eq(jiffies
, card
->battery
[battery
].last_change
+
713 (HZ
* 60 * 60 * 5))) {
714 dev_printk(KERN_ERR
, &card
->dev
->dev
,
715 "Battery %d still FAILED after 5 hours\n", battery
+ 1);
716 card
->battery
[battery
].warned
= 1;
724 static void check_batteries(struct cardinfo
*card
)
726 /* NOTE: this must *never* be called while the card
727 * is doing (bus-to-card) DMA, or you will need the
730 unsigned char status
;
733 status
= readb(card
->csr_remap
+ MEMCTRLSTATUS_BATTERY
);
734 if (debug
& DEBUG_BATTERY_POLLING
)
735 dev_printk(KERN_DEBUG
, &card
->dev
->dev
,
736 "checking battery status, 1 = %s, 2 = %s\n",
737 (status
& BATTERY_1_FAILURE
) ? "FAILURE" : "OK",
738 (status
& BATTERY_2_FAILURE
) ? "FAILURE" : "OK");
740 ret1
= check_battery(card
, 0, !(status
& BATTERY_1_FAILURE
));
741 ret2
= check_battery(card
, 1, !(status
& BATTERY_2_FAILURE
));
744 set_fault_to_battery_status(card
);
747 static void check_all_batteries(unsigned long ptr
)
751 for (i
= 0; i
< num_cards
; i
++)
752 if (!(cards
[i
].flags
& UM_FLAG_NO_BATT
)) {
753 struct cardinfo
*card
= &cards
[i
];
754 spin_lock_bh(&card
->lock
);
755 if (card
->Active
>= 0)
756 card
->check_batteries
= 1;
758 check_batteries(card
);
759 spin_unlock_bh(&card
->lock
);
762 init_battery_timer();
765 static void init_battery_timer(void)
767 init_timer(&battery_timer
);
768 battery_timer
.function
= check_all_batteries
;
769 battery_timer
.expires
= jiffies
+ (HZ
* 60);
770 add_timer(&battery_timer
);
773 static void del_battery_timer(void)
775 del_timer(&battery_timer
);
779 * Note no locks taken out here. In a worst case scenario, we could drop
780 * a chunk of system memory. But that should never happen, since validation
781 * happens at open or mount time, when locks are held.
783 * That's crap, since doing that while some partitions are opened
784 * or mounted will give you really nasty results.
786 static int mm_revalidate(struct gendisk
*disk
)
788 struct cardinfo
*card
= disk
->private_data
;
789 set_capacity(disk
, card
->mm_size
<< 1);
793 static int mm_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
795 struct cardinfo
*card
= bdev
->bd_disk
->private_data
;
796 int size
= card
->mm_size
* (1024 / MM_HARDSECT
);
799 * get geometry: we have to fake one... trim the size to a
800 * multiple of 2048 (1M): tell we have 32 sectors, 64 heads,
801 * whatever cylinders.
805 geo
->cylinders
= size
/ (geo
->heads
* geo
->sectors
);
809 static const struct block_device_operations mm_fops
= {
810 .owner
= THIS_MODULE
,
812 .revalidate_disk
= mm_revalidate
,
815 static int __devinit
mm_pci_probe(struct pci_dev
*dev
,
816 const struct pci_device_id
*id
)
819 struct cardinfo
*card
= &cards
[num_cards
];
820 unsigned char mem_present
;
821 unsigned char batt_status
;
822 unsigned int saved_bar
, data
;
823 unsigned long csr_base
;
824 unsigned long csr_len
;
826 static int printed_version
;
828 if (!printed_version
++)
829 printk(KERN_INFO DRIVER_VERSION
" : " DRIVER_DESC
"\n");
831 ret
= pci_enable_device(dev
);
835 pci_write_config_byte(dev
, PCI_LATENCY_TIMER
, 0xF8);
840 csr_base
= pci_resource_start(dev
, 0);
841 csr_len
= pci_resource_len(dev
, 0);
842 if (!csr_base
|| !csr_len
)
845 dev_printk(KERN_INFO
, &dev
->dev
,
846 "Micro Memory(tm) controller found (PCI Mem Module (Battery Backup))\n");
848 if (pci_set_dma_mask(dev
, DMA_BIT_MASK(64)) &&
849 pci_set_dma_mask(dev
, DMA_BIT_MASK(32))) {
850 dev_printk(KERN_WARNING
, &dev
->dev
, "NO suitable DMA found\n");
854 ret
= pci_request_regions(dev
, DRIVER_NAME
);
856 dev_printk(KERN_ERR
, &card
->dev
->dev
,
857 "Unable to request memory region\n");
861 card
->csr_remap
= ioremap_nocache(csr_base
, csr_len
);
862 if (!card
->csr_remap
) {
863 dev_printk(KERN_ERR
, &card
->dev
->dev
,
864 "Unable to remap memory region\n");
867 goto failed_remap_csr
;
870 dev_printk(KERN_INFO
, &card
->dev
->dev
,
871 "CSR 0x%08lx -> 0x%p (0x%lx)\n",
872 csr_base
, card
->csr_remap
, csr_len
);
874 switch (card
->dev
->device
) {
876 card
->flags
|= UM_FLAG_NO_BYTE_STATUS
| UM_FLAG_NO_BATTREG
;
881 card
->flags
|= UM_FLAG_NO_BYTE_STATUS
;
886 card
->flags
|= UM_FLAG_NO_BYTE_STATUS
|
887 UM_FLAG_NO_BATTREG
| UM_FLAG_NO_BATT
;
892 magic_number
= 0x100;
896 if (readb(card
->csr_remap
+ MEMCTRLSTATUS_MAGIC
) != magic_number
) {
897 dev_printk(KERN_ERR
, &card
->dev
->dev
, "Magic number invalid\n");
902 card
->mm_pages
[0].desc
= pci_alloc_consistent(card
->dev
,
904 &card
->mm_pages
[0].page_dma
);
905 card
->mm_pages
[1].desc
= pci_alloc_consistent(card
->dev
,
907 &card
->mm_pages
[1].page_dma
);
908 if (card
->mm_pages
[0].desc
== NULL
||
909 card
->mm_pages
[1].desc
== NULL
) {
910 dev_printk(KERN_ERR
, &card
->dev
->dev
, "alloc failed\n");
913 reset_page(&card
->mm_pages
[0]);
914 reset_page(&card
->mm_pages
[1]);
915 card
->Ready
= 0; /* page 0 is ready */
916 card
->Active
= -1; /* no page is active */
918 card
->biotail
= &card
->bio
;
920 card
->queue
= blk_alloc_queue(GFP_KERNEL
);
924 blk_queue_make_request(card
->queue
, mm_make_request
);
925 card
->queue
->queue_lock
= &card
->lock
;
926 card
->queue
->queuedata
= card
;
928 tasklet_init(&card
->tasklet
, process_page
, (unsigned long)card
);
930 card
->check_batteries
= 0;
932 mem_present
= readb(card
->csr_remap
+ MEMCTRLSTATUS_MEMORY
);
933 switch (mem_present
) {
935 card
->mm_size
= 1024 * 128;
938 card
->mm_size
= 1024 * 256;
941 card
->mm_size
= 1024 * 512;
944 card
->mm_size
= 1024 * 1024;
947 card
->mm_size
= 1024 * 2048;
954 /* Clear the LED's we control */
955 set_led(card
, LED_REMOVE
, LED_OFF
);
956 set_led(card
, LED_FAULT
, LED_OFF
);
958 batt_status
= readb(card
->csr_remap
+ MEMCTRLSTATUS_BATTERY
);
960 card
->battery
[0].good
= !(batt_status
& BATTERY_1_FAILURE
);
961 card
->battery
[1].good
= !(batt_status
& BATTERY_2_FAILURE
);
962 card
->battery
[0].last_change
= card
->battery
[1].last_change
= jiffies
;
964 if (card
->flags
& UM_FLAG_NO_BATT
)
965 dev_printk(KERN_INFO
, &card
->dev
->dev
,
966 "Size %d KB\n", card
->mm_size
);
968 dev_printk(KERN_INFO
, &card
->dev
->dev
,
969 "Size %d KB, Battery 1 %s (%s), Battery 2 %s (%s)\n",
971 batt_status
& BATTERY_1_DISABLED
? "Disabled" : "Enabled",
972 card
->battery
[0].good
? "OK" : "FAILURE",
973 batt_status
& BATTERY_2_DISABLED
? "Disabled" : "Enabled",
974 card
->battery
[1].good
? "OK" : "FAILURE");
976 set_fault_to_battery_status(card
);
979 pci_read_config_dword(dev
, PCI_BASE_ADDRESS_1
, &saved_bar
);
981 pci_write_config_dword(dev
, PCI_BASE_ADDRESS_1
, data
);
982 pci_read_config_dword(dev
, PCI_BASE_ADDRESS_1
, &data
);
983 pci_write_config_dword(dev
, PCI_BASE_ADDRESS_1
, saved_bar
);
988 if (request_irq(dev
->irq
, mm_interrupt
, IRQF_SHARED
, DRIVER_NAME
,
990 dev_printk(KERN_ERR
, &card
->dev
->dev
,
991 "Unable to allocate IRQ\n");
996 dev_printk(KERN_INFO
, &card
->dev
->dev
,
997 "Window size %d bytes, IRQ %d\n", data
, dev
->irq
);
999 spin_lock_init(&card
->lock
);
1001 pci_set_drvdata(dev
, card
);
1003 if (pci_write_cmd
!= 0x0F) /* If not Memory Write & Invalidate */
1004 pci_write_cmd
= 0x07; /* then Memory Write command */
1006 if (pci_write_cmd
& 0x08) { /* use Memory Write and Invalidate */
1007 unsigned short cfg_command
;
1008 pci_read_config_word(dev
, PCI_COMMAND
, &cfg_command
);
1009 cfg_command
|= 0x10; /* Memory Write & Invalidate Enable */
1010 pci_write_config_word(dev
, PCI_COMMAND
, cfg_command
);
1012 pci_cmds
= (pci_read_cmd
<< 28) | (pci_write_cmd
<< 24);
1016 if (!get_userbit(card
, MEMORY_INITIALIZED
)) {
1017 dev_printk(KERN_INFO
, &card
->dev
->dev
,
1018 "memory NOT initialized. Consider over-writing whole device.\n");
1019 card
->init_size
= 0;
1021 dev_printk(KERN_INFO
, &card
->dev
->dev
,
1022 "memory already initialized\n");
1023 card
->init_size
= card
->mm_size
;
1027 writeb(EDC_STORE_CORRECT
, card
->csr_remap
+ MEMCTRLCMD_ERRCTRL
);
1033 if (card
->mm_pages
[0].desc
)
1034 pci_free_consistent(card
->dev
, PAGE_SIZE
*2,
1035 card
->mm_pages
[0].desc
,
1036 card
->mm_pages
[0].page_dma
);
1037 if (card
->mm_pages
[1].desc
)
1038 pci_free_consistent(card
->dev
, PAGE_SIZE
*2,
1039 card
->mm_pages
[1].desc
,
1040 card
->mm_pages
[1].page_dma
);
1042 iounmap(card
->csr_remap
);
1044 pci_release_regions(dev
);
1050 static void mm_pci_remove(struct pci_dev
*dev
)
1052 struct cardinfo
*card
= pci_get_drvdata(dev
);
1054 tasklet_kill(&card
->tasklet
);
1055 free_irq(dev
->irq
, card
);
1056 iounmap(card
->csr_remap
);
1058 if (card
->mm_pages
[0].desc
)
1059 pci_free_consistent(card
->dev
, PAGE_SIZE
*2,
1060 card
->mm_pages
[0].desc
,
1061 card
->mm_pages
[0].page_dma
);
1062 if (card
->mm_pages
[1].desc
)
1063 pci_free_consistent(card
->dev
, PAGE_SIZE
*2,
1064 card
->mm_pages
[1].desc
,
1065 card
->mm_pages
[1].page_dma
);
1066 blk_cleanup_queue(card
->queue
);
1068 pci_release_regions(dev
);
1069 pci_disable_device(dev
);
1072 static const struct pci_device_id mm_pci_ids
[] = {
1073 {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY
, PCI_DEVICE_ID_MICRO_MEMORY_5415CN
)},
1074 {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY
, PCI_DEVICE_ID_MICRO_MEMORY_5425CN
)},
1075 {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY
, PCI_DEVICE_ID_MICRO_MEMORY_6155
)},
1079 .subvendor
= 0x1332,
1080 .subdevice
= 0x5460,
1083 }, { /* end: all zeroes */ }
1086 MODULE_DEVICE_TABLE(pci
, mm_pci_ids
);
1088 static struct pci_driver mm_pci_driver
= {
1089 .name
= DRIVER_NAME
,
1090 .id_table
= mm_pci_ids
,
1091 .probe
= mm_pci_probe
,
1092 .remove
= mm_pci_remove
,
1095 static int __init
mm_init(void)
1100 retval
= pci_register_driver(&mm_pci_driver
);
1104 err
= major_nr
= register_blkdev(0, DRIVER_NAME
);
1106 pci_unregister_driver(&mm_pci_driver
);
1110 for (i
= 0; i
< num_cards
; i
++) {
1111 mm_gendisk
[i
] = alloc_disk(1 << MM_SHIFT
);
1116 for (i
= 0; i
< num_cards
; i
++) {
1117 struct gendisk
*disk
= mm_gendisk
[i
];
1118 sprintf(disk
->disk_name
, "umem%c", 'a'+i
);
1119 spin_lock_init(&cards
[i
].lock
);
1120 disk
->major
= major_nr
;
1121 disk
->first_minor
= i
<< MM_SHIFT
;
1122 disk
->fops
= &mm_fops
;
1123 disk
->private_data
= &cards
[i
];
1124 disk
->queue
= cards
[i
].queue
;
1125 set_capacity(disk
, cards
[i
].mm_size
<< 1);
1129 init_battery_timer();
1130 printk(KERN_INFO
"MM: desc_per_page = %ld\n", DESC_PER_PAGE
);
1131 /* printk("mm_init: Done. 10-19-01 9:00\n"); */
1135 pci_unregister_driver(&mm_pci_driver
);
1136 unregister_blkdev(major_nr
, DRIVER_NAME
);
1138 put_disk(mm_gendisk
[i
]);
1142 static void __exit
mm_cleanup(void)
1146 del_battery_timer();
1148 for (i
= 0; i
< num_cards
; i
++) {
1149 del_gendisk(mm_gendisk
[i
]);
1150 put_disk(mm_gendisk
[i
]);
1153 pci_unregister_driver(&mm_pci_driver
);
1155 unregister_blkdev(major_nr
, DRIVER_NAME
);
1158 module_init(mm_init
);
1159 module_exit(mm_cleanup
);
1161 MODULE_AUTHOR(DRIVER_AUTHOR
);
1162 MODULE_DESCRIPTION(DRIVER_DESC
);
1163 MODULE_LICENSE("GPL");