40 #include <linux/kernel.h>
42 #include <linux/mman.h>
45 #include <linux/module.h>
49 #include <linux/pci.h>
52 #include <linux/fcntl.h>
57 #include <asm/uaccess.h>
62 #define MM_BLKSIZE 1024
63 #define MM_HARDSECT 512
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"
79 #define DEBUG_LED_ON_TRANSFER 0x01
80 #define DEBUG_BATTERY_POLLING 0x02
85 static int pci_read_cmd = 0x0C;
89 static int pci_write_cmd = 0x0F;
123 #define DESC_PER_PAGE ((PAGE_SIZE*2)/sizeof(struct mm_dma_desc))
145 static int num_cards;
176 static void set_led(
struct cardinfo *card,
int shift,
unsigned char state)
184 led &= ~(0x03 << shift);
185 led |= (state << shift);
192 static void dump_regs(
struct cardinfo *card)
198 for (i = 0; i < 8; i++) {
201 for (i1 = 0; i1 < 16; i1++)
253 static void mm_start_io(
struct cardinfo *card)
260 struct mm_page *
page;
266 card->
Active, page->headcnt, page->cnt - 1);
267 desc = &page->desc[page->cnt-1];
277 desc = &page->desc[page->headcnt];
290 offset = ((
char *)desc) - ((
char *)page->desc);
303 static int add_bio(
struct cardinfo *card);
305 static void activate(
struct cardinfo *card)
314 while (add_bio(card))
324 }
while (card->
Active == -1 && add_bio(card));
327 static inline void reset_page(
struct mm_page *page)
332 page->biotail = &page->bio;
340 static int add_bio(
struct cardinfo *card)
353 if (!bio && card->
bio) {
357 card->
bio = card->
bio->bi_next;
371 vec = bio_iovec_idx(bio, idx);
373 dma_handle = pci_map_page(card->
dev,
381 desc = &p->desc[p->cnt];
385 if ((p->biotail) != &bio->bi_next) {
387 p->biotail = &(bio->bi_next);
399 offset = (((
char *)(desc+1)) - ((
char *)p->desc));
413 if (idx >= bio->bi_vcnt)
419 static void process_page(
unsigned long data)
427 struct mm_page *
page;
428 struct bio *return_bio =
NULL;
432 spin_lock_bh(&card->
lock);
437 while (page->headcnt < page->cnt) {
438 struct bio *bio = page->bio;
439 struct mm_dma_desc *desc = &page->desc[page->headcnt];
451 if (page->idx >= bio->bi_vcnt) {
452 page->bio = bio->bi_next;
454 page->idx = page->bio->bi_idx;
458 bio_iovec_idx(bio, idx)->bv_len,
465 "I/O error on sector %d/%d\n",
468 dump_dmastat(card, control);
475 "memory now initialised\n");
479 if (bio != page->bio) {
480 bio->bi_next = return_bio;
488 if (
debug & DEBUG_LED_ON_TRANSFER)
493 check_batteries(card);
495 if (page->headcnt >= page->cnt) {
505 spin_unlock_bh(&card->
lock);
508 struct bio *bio = return_bio;
510 return_bio = bio->bi_next;
516 static void mm_unplug(
struct blk_plug_cb *
cb,
bool from_schedule)
520 spin_lock_irq(&card->
lock);
522 spin_unlock_irq(&card->
lock);
526 static int mm_check_plugged(
struct cardinfo *card)
531 static void mm_make_request(
struct request_queue *
q,
struct bio *bio)
533 struct cardinfo *card = q->queuedata;
534 pr_debug(
"mm_make_request %llu %u\n",
535 (
unsigned long long)bio->bi_sector, bio->bi_size);
537 spin_lock_irq(&card->
lock);
541 if (bio->bi_rw &
REQ_SYNC || !mm_check_plugged(card))
543 spin_unlock_irq(&card->
lock);
548 static irqreturn_t mm_interrupt(
int irq,
void *__card)
552 unsigned short cfg_status;
568 writeb((DMASCR_DMA_COMPLETE|DMASCR_CHAIN_COMPLETE) >> 16,
572 if (dma_status & DMASCR_ANY_ERR) {
573 unsigned int data_log1, data_log2;
574 unsigned int addr_log1, addr_log2;
591 dump_dmastat(card, dma_status);
595 "Memory access error detected (err count %d)\n",
599 "Multi-bit EDC error\n");
602 "Fault Address 0x%02x%08x, Fault Data 0x%08x%08x\n",
603 addr_log2, addr_log1, data_log2, data_log1);
605 "Fault Check 0x%02x, Fault Syndrome 0x%02x\n",
611 if (dma_status & DMASCR_PARITY_ERR_REP) {
613 "PARITY ERROR REPORTED\n");
618 if (dma_status & DMASCR_PARITY_ERR_DET) {
620 "PARITY ERROR DETECTED\n");
625 if (dma_status & DMASCR_SYSTEM_ERR_SIG) {
626 dev_printk(
KERN_ERR, &card->
dev->dev,
"SYSTEM ERROR\n");
631 if (dma_status & DMASCR_TARGET_ABT) {
632 dev_printk(
KERN_ERR, &card->
dev->dev,
"TARGET ABORT\n");
637 if (dma_status & DMASCR_MASTER_ABT) {
638 dev_printk(
KERN_ERR, &card->
dev->dev,
"MASTER ABORT\n");
645 tasklet_schedule(&card->
tasklet);
658 static void set_fault_to_battery_status(
struct cardinfo *card)
670 static void init_battery_timer(
void);
674 if (status != card->
battery[battery].good) {
678 if (card->
battery[battery].good) {
680 "Battery %d now good\n", battery + 1);
684 "Battery %d now FAILED\n", battery + 1);
687 }
else if (!card->
battery[battery].good &&
688 !card->
battery[battery].warned &&
690 (
HZ * 60 * 60 * 5))) {
692 "Battery %d still FAILED after 5 hours\n", battery + 1);
713 "checking battery status, 1 = %s, 2 = %s\n",
721 set_fault_to_battery_status(card);
724 static void check_all_batteries(
unsigned long ptr)
728 for (i = 0; i < num_cards; i++)
731 spin_lock_bh(&card->
lock);
736 spin_unlock_bh(&card->
lock);
739 init_battery_timer();
742 static void init_battery_timer(
void)
745 battery_timer.function = check_all_batteries;
746 battery_timer.expires =
jiffies + (
HZ * 60);
750 static void del_battery_timer(
void)
763 static int mm_revalidate(
struct gendisk *disk)
765 struct cardinfo *card = disk->private_data;
766 set_capacity(disk, card->
mm_size << 1);
786 static const struct block_device_operations mm_fops = {
789 .revalidate_disk = mm_revalidate,
796 struct cardinfo *card = &cards[num_cards];
797 unsigned char mem_present;
798 unsigned char batt_status;
799 unsigned int saved_bar,
data;
800 unsigned long csr_base;
801 unsigned long csr_len;
803 static int printed_version;
805 if (!printed_version++)
819 if (!csr_base || !csr_len)
823 "Micro Memory(tm) controller found (PCI Mem Module (Battery Backup))\n");
834 "Unable to request memory region\n");
841 "Unable to remap memory region\n");
844 goto failed_remap_csr;
848 "CSR 0x%08lx -> 0x%p (0x%lx)\n",
851 switch (card->
dev->device) {
869 magic_number = 0x100;
874 dev_printk(
KERN_ERR, &card->
dev->dev,
"Magic number invalid\n");
887 dev_printk(
KERN_ERR, &card->
dev->dev,
"alloc failed\n");
910 switch (mem_present) {
941 if (card->
flags & UM_FLAG_NO_BATT)
943 "Size %d KB\n", card->
mm_size);
946 "Size %d KB, Battery 1 %s (%s), Battery 2 %s (%s)\n",
949 card->
battery[0].good ?
"OK" :
"FAILURE",
951 card->
battery[1].good ?
"OK" :
"FAILURE");
953 set_fault_to_battery_status(card);
968 "Unable to allocate IRQ\n");
974 "Window size %d bytes, IRQ %d\n", data, dev->
irq);
978 pci_set_drvdata(dev, card);
980 if (pci_write_cmd != 0x0F)
981 pci_write_cmd = 0x07;
983 if (pci_write_cmd & 0x08) {
984 unsigned short cfg_command;
985 pci_read_config_word(dev,
PCI_COMMAND, &cfg_command);
987 pci_write_config_word(dev,
PCI_COMMAND, cfg_command);
989 pci_cmds = (pci_read_cmd << 28) | (pci_write_cmd << 24);
995 "memory NOT initialized. Consider over-writing whole device.\n");
999 "memory already initialized\n");
1027 static void mm_pci_remove(
struct pci_dev *dev)
1029 struct cardinfo *card = pci_get_drvdata(dev);
1056 .subvendor = 0x1332,
1057 .subdevice = 0x5460,
1067 .id_table = mm_pci_ids,
1068 .probe = mm_pci_probe,
1069 .remove = mm_pci_remove,
1072 static int __init mm_init(
void)
1077 retval = pci_register_driver(&mm_pci_driver);
1087 for (i = 0; i < num_cards; i++) {
1093 for (i = 0; i < num_cards; i++) {
1094 struct gendisk *disk = mm_gendisk[
i];
1095 sprintf(disk->disk_name,
"umem%c",
'a'+i);
1097 disk->major = major_nr;
1099 disk->fops = &mm_fops;
1100 disk->private_data = &cards[
i];
1101 disk->queue = cards[
i].queue;
1102 set_capacity(disk, cards[i].mm_size << 1);
1106 init_battery_timer();
1119 static void __exit mm_cleanup(
void)
1123 del_battery_timer();
1125 for (i = 0; i < num_cards ; i++) {