drivers/block/umem: use dev_printk()
[linux-2.6.git] / drivers / block / umem.c
1 /*
2  * mm.c - Micro Memory(tm) PCI memory board block device driver - v2.3
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>
7  *
8  * This driver for the Micro Memory PCI Memory Module with Battery Backup
9  * is Copyright Micro Memory Inc 2001-2002.  All rights reserved.
10  *
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.
14  *
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
26  *                         bh's instead.
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.
35  */
36
37 //#define DEBUG /* uncomment if you want debugging info (pr_debug) */
38 #include <linux/fs.h>
39 #include <linux/bio.h>
40 #include <linux/kernel.h>
41 #include <linux/mm.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>
51
52 #include <linux/fcntl.h>        /* O_ACCMODE */
53 #include <linux/hdreg.h>  /* HDIO_GETGEO */
54
55 #include "umem.h"
56
57 #include <asm/uaccess.h>
58 #include <asm/io.h>
59
60 #define MM_MAXCARDS 4
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
66 /*
67  * Version Information
68  */
69
70 #define DRIVER_VERSION "v2.3"
71 #define DRIVER_AUTHOR "San Mehat, Johannes Erdfelt, NeilBrown"
72 #define DRIVER_DESC "Micro Memory(tm) PCI memory board block driver"
73
74 static int debug;
75 /* #define HW_TRACE(x)     writeb(x,cards[0].csr_remap + MEMCTRLSTATUS_MAGIC) */
76 #define HW_TRACE(x)
77
78 #define DEBUG_LED_ON_TRANSFER   0x01
79 #define DEBUG_BATTERY_POLLING   0x02
80
81 module_param(debug, int, 0644);
82 MODULE_PARM_DESC(debug, "Debug bitmask");
83
84 static int pci_read_cmd = 0x0C;         /* Read Multiple */
85 module_param(pci_read_cmd, int, 0);
86 MODULE_PARM_DESC(pci_read_cmd, "PCI read command");
87
88 static int pci_write_cmd = 0x0F;        /* Write and Invalidate */
89 module_param(pci_write_cmd, int, 0);
90 MODULE_PARM_DESC(pci_write_cmd, "PCI write command");
91
92 static int pci_cmds;
93
94 static int major_nr;
95
96 #include <linux/blkdev.h>
97 #include <linux/blkpg.h>
98
99 struct cardinfo {
100         struct pci_dev  *dev;
101
102         int             irq;
103
104         unsigned long   csr_base;
105         unsigned char   __iomem *csr_remap;
106         unsigned long   csr_len;
107         unsigned int    win_size; /* PCI window size */
108         unsigned int    mm_size;  /* size in kbytes */
109
110         unsigned int    init_size; /* initial segment, in sectors,
111                                     * that we know to
112                                     * have been written
113                                     */
114         struct bio      *bio, *currentbio, **biotail;
115         int             current_idx;
116         sector_t        current_sector;
117
118         struct request_queue *queue;
119
120         struct mm_page {
121                 dma_addr_t              page_dma;
122                 struct mm_dma_desc      *desc;
123                 int                     cnt, headcnt;
124                 struct bio              *bio, **biotail;
125                 int                     idx;
126         } mm_pages[2];
127 #define DESC_PER_PAGE ((PAGE_SIZE*2)/sizeof(struct mm_dma_desc))
128
129         int  Active, Ready;
130
131         struct tasklet_struct   tasklet;
132         unsigned int dma_status;
133
134         struct {
135                 int             good;
136                 int             warned;
137                 unsigned long   last_change;
138         } battery[2];
139
140         spinlock_t      lock;
141         int             check_batteries;
142
143         int             flags;
144 };
145
146 static struct cardinfo cards[MM_MAXCARDS];
147 static struct block_device_operations mm_fops;
148 static struct timer_list battery_timer;
149
150 static int num_cards = 0;
151
152 static struct gendisk *mm_gendisk[MM_MAXCARDS];
153
154 static void check_batteries(struct cardinfo *card);
155
156 /*
157 -----------------------------------------------------------------------------------
158 --                           get_userbit
159 -----------------------------------------------------------------------------------
160 */
161 static int get_userbit(struct cardinfo *card, int bit)
162 {
163         unsigned char led;
164
165         led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
166         return led & bit;
167 }
168 /*
169 -----------------------------------------------------------------------------------
170 --                            set_userbit
171 -----------------------------------------------------------------------------------
172 */
173 static int set_userbit(struct cardinfo *card, int bit, unsigned char state)
174 {
175         unsigned char led;
176
177         led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
178         if (state)
179                 led |= bit;
180         else
181                 led &= ~bit;
182         writeb(led, card->csr_remap + MEMCTRLCMD_LEDCTRL);
183
184         return 0;
185 }
186 /*
187 -----------------------------------------------------------------------------------
188 --                             set_led
189 -----------------------------------------------------------------------------------
190 */
191 /*
192  * NOTE: For the power LED, use the LED_POWER_* macros since they differ
193  */
194 static void set_led(struct cardinfo *card, int shift, unsigned char state)
195 {
196         unsigned char led;
197
198         led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
199         if (state == LED_FLIP)
200                 led ^= (1<<shift);
201         else {
202                 led &= ~(0x03 << shift);
203                 led |= (state << shift);
204         }
205         writeb(led, card->csr_remap + MEMCTRLCMD_LEDCTRL);
206
207 }
208
209 #ifdef MM_DIAG
210 /*
211 -----------------------------------------------------------------------------------
212 --                              dump_regs
213 -----------------------------------------------------------------------------------
214 */
215 static void dump_regs(struct cardinfo *card)
216 {
217         unsigned char *p;
218         int i, i1;
219
220         p = card->csr_remap;
221         for (i = 0; i < 8; i++) {
222                 printk(KERN_DEBUG "%p   ", p);
223
224                 for (i1 = 0; i1 < 16; i1++)
225                         printk("%02x ", *p++);
226
227                 printk("\n");
228         }
229 }
230 #endif
231 /*
232 -----------------------------------------------------------------------------------
233 --                            dump_dmastat
234 -----------------------------------------------------------------------------------
235 */
236 static void dump_dmastat(struct cardinfo *card, unsigned int dmastat)
237 {
238         dev_printk(KERN_DEBUG, &card->dev->dev, "DMAstat - ");
239         if (dmastat & DMASCR_ANY_ERR)
240                 printk("ANY_ERR ");
241         if (dmastat & DMASCR_MBE_ERR)
242                 printk("MBE_ERR ");
243         if (dmastat & DMASCR_PARITY_ERR_REP)
244                 printk("PARITY_ERR_REP ");
245         if (dmastat & DMASCR_PARITY_ERR_DET)
246                 printk("PARITY_ERR_DET ");
247         if (dmastat & DMASCR_SYSTEM_ERR_SIG)
248                 printk("SYSTEM_ERR_SIG ");
249         if (dmastat & DMASCR_TARGET_ABT)
250                 printk("TARGET_ABT ");
251         if (dmastat & DMASCR_MASTER_ABT)
252                 printk("MASTER_ABT ");
253         if (dmastat & DMASCR_CHAIN_COMPLETE)
254                 printk("CHAIN_COMPLETE ");
255         if (dmastat & DMASCR_DMA_COMPLETE)
256                 printk("DMA_COMPLETE ");
257         printk("\n");
258 }
259
260 /*
261  * Theory of request handling
262  *
263  * Each bio is assigned to one mm_dma_desc - which may not be enough FIXME
264  * We have two pages of mm_dma_desc, holding about 64 descriptors
265  * each.  These are allocated at init time.
266  * One page is "Ready" and is either full, or can have request added.
267  * The other page might be "Active", which DMA is happening on it.
268  *
269  * Whenever IO on the active page completes, the Ready page is activated
270  * and the ex-Active page is clean out and made Ready.
271  * Otherwise the Ready page is only activated when it becomes full, or
272  * when mm_unplug_device is called via the unplug_io_fn.
273  *
274  * If a request arrives while both pages a full, it is queued, and b_rdev is
275  * overloaded to record whether it was a read or a write.
276  *
277  * The interrupt handler only polls the device to clear the interrupt.
278  * The processing of the result is done in a tasklet.
279  */
280
281 static void mm_start_io(struct cardinfo *card)
282 {
283         /* we have the lock, we know there is
284          * no IO active, and we know that card->Active
285          * is set
286          */
287         struct mm_dma_desc *desc;
288         struct mm_page *page;
289         int offset;
290
291         /* make the last descriptor end the chain */
292         page = &card->mm_pages[card->Active];
293         pr_debug("start_io: %d %d->%d\n", card->Active, page->headcnt, page->cnt-1);
294         desc = &page->desc[page->cnt-1];
295
296         desc->control_bits |= cpu_to_le32(DMASCR_CHAIN_COMP_EN);
297         desc->control_bits &= ~cpu_to_le32(DMASCR_CHAIN_EN);
298         desc->sem_control_bits = desc->control_bits;
299
300                                
301         if (debug & DEBUG_LED_ON_TRANSFER)
302                 set_led(card, LED_REMOVE, LED_ON);
303
304         desc = &page->desc[page->headcnt];
305         writel(0, card->csr_remap + DMA_PCI_ADDR);
306         writel(0, card->csr_remap + DMA_PCI_ADDR + 4);
307
308         writel(0, card->csr_remap + DMA_LOCAL_ADDR);
309         writel(0, card->csr_remap + DMA_LOCAL_ADDR + 4);
310
311         writel(0, card->csr_remap + DMA_TRANSFER_SIZE);
312         writel(0, card->csr_remap + DMA_TRANSFER_SIZE + 4);
313
314         writel(0, card->csr_remap + DMA_SEMAPHORE_ADDR);
315         writel(0, card->csr_remap + DMA_SEMAPHORE_ADDR + 4);
316
317         offset = ((char*)desc) - ((char*)page->desc);
318         writel(cpu_to_le32((page->page_dma+offset)&0xffffffff),
319                card->csr_remap + DMA_DESCRIPTOR_ADDR);
320         /* Force the value to u64 before shifting otherwise >> 32 is undefined C
321          * and on some ports will do nothing ! */
322         writel(cpu_to_le32(((u64)page->page_dma)>>32),
323                card->csr_remap + DMA_DESCRIPTOR_ADDR + 4);
324
325         /* Go, go, go */
326         writel(cpu_to_le32(DMASCR_GO | DMASCR_CHAIN_EN | pci_cmds),
327                card->csr_remap + DMA_STATUS_CTRL);
328 }
329
330 static int add_bio(struct cardinfo *card);
331
332 static void activate(struct cardinfo *card)
333 {
334         /* if No page is Active, and Ready is 
335          * not empty, then switch Ready page
336          * to active and start IO.
337          * Then add any bh's that are available to Ready
338          */
339
340         do {
341                 while (add_bio(card))
342                         ;
343
344                 if (card->Active == -1 &&
345                     card->mm_pages[card->Ready].cnt > 0) {
346                         card->Active = card->Ready;
347                         card->Ready = 1-card->Ready;
348                         mm_start_io(card);
349                 }
350
351         } while (card->Active == -1 && add_bio(card));
352 }
353
354 static inline void reset_page(struct mm_page *page)
355 {
356         page->cnt = 0;
357         page->headcnt = 0;
358         page->bio = NULL;
359         page->biotail = & page->bio;
360 }
361
362 static void mm_unplug_device(struct request_queue *q)
363 {
364         struct cardinfo *card = q->queuedata;
365         unsigned long flags;
366
367         spin_lock_irqsave(&card->lock, flags);
368         if (blk_remove_plug(q))
369                 activate(card);
370         spin_unlock_irqrestore(&card->lock, flags);
371 }
372
373 /* 
374  * If there is room on Ready page, take
375  * one bh off list and add it.
376  * return 1 if there was room, else 0.
377  */
378 static int add_bio(struct cardinfo *card)
379 {
380         struct mm_page *p;
381         struct mm_dma_desc *desc;
382         dma_addr_t dma_handle;
383         int offset;
384         struct bio *bio;
385         struct bio_vec *vec;
386         int idx;
387         int rw;
388         int len;
389
390         bio = card->currentbio;
391         if (!bio && card->bio) {
392                 card->currentbio = card->bio;
393                 card->current_idx = card->bio->bi_idx;
394                 card->current_sector = card->bio->bi_sector;
395                 card->bio = card->bio->bi_next;
396                 if (card->bio == NULL)
397                         card->biotail = &card->bio;
398                 card->currentbio->bi_next = NULL;
399                 return 1;
400         }
401         if (!bio)
402                 return 0;
403         idx = card->current_idx;
404
405         rw = bio_rw(bio);
406         if (card->mm_pages[card->Ready].cnt >= DESC_PER_PAGE)
407                 return 0;
408
409         vec = bio_iovec_idx(bio, idx);
410         len = vec->bv_len;
411         dma_handle = pci_map_page(card->dev,
412                                   vec->bv_page,
413                                   vec->bv_offset,
414                                   len,
415                                   (rw==READ) ?
416                                   PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);
417
418         p = &card->mm_pages[card->Ready];
419         desc = &p->desc[p->cnt];
420         p->cnt++;
421         if (p->bio == NULL)
422                 p->idx = idx;
423         if ((p->biotail) != &bio->bi_next) {
424                 *(p->biotail) = bio;
425                 p->biotail = &(bio->bi_next);
426                 bio->bi_next = NULL;
427         }
428
429         desc->data_dma_handle = dma_handle;
430
431         desc->pci_addr = cpu_to_le64((u64)desc->data_dma_handle);
432         desc->local_addr = cpu_to_le64(card->current_sector << 9);
433         desc->transfer_size = cpu_to_le32(len);
434         offset = ( ((char*)&desc->sem_control_bits) - ((char*)p->desc));
435         desc->sem_addr = cpu_to_le64((u64)(p->page_dma+offset));
436         desc->zero1 = desc->zero2 = 0;
437         offset = ( ((char*)(desc+1)) - ((char*)p->desc));
438         desc->next_desc_addr = cpu_to_le64(p->page_dma+offset);
439         desc->control_bits = cpu_to_le32(DMASCR_GO|DMASCR_ERR_INT_EN|
440                                          DMASCR_PARITY_INT_EN|
441                                          DMASCR_CHAIN_EN |
442                                          DMASCR_SEM_EN |
443                                          pci_cmds);
444         if (rw == WRITE)
445                 desc->control_bits |= cpu_to_le32(DMASCR_TRANSFER_READ);
446         desc->sem_control_bits = desc->control_bits;
447
448         card->current_sector += (len >> 9);
449         idx++;
450         card->current_idx = idx;
451         if (idx >= bio->bi_vcnt)
452                 card->currentbio = NULL;
453
454         return 1;
455 }
456
457 static void process_page(unsigned long data)
458 {
459         /* check if any of the requests in the page are DMA_COMPLETE,
460          * and deal with them appropriately.
461          * If we find a descriptor without DMA_COMPLETE in the semaphore, then
462          * dma must have hit an error on that descriptor, so use dma_status instead
463          * and assume that all following descriptors must be re-tried.
464          */
465         struct mm_page *page;
466         struct bio *return_bio=NULL;
467         struct cardinfo *card = (struct cardinfo *)data;
468         unsigned int dma_status = card->dma_status;
469
470         spin_lock_bh(&card->lock);
471         if (card->Active < 0)
472                 goto out_unlock;
473         page = &card->mm_pages[card->Active];
474         
475         while (page->headcnt < page->cnt) {
476                 struct bio *bio = page->bio;
477                 struct mm_dma_desc *desc = &page->desc[page->headcnt];
478                 int control = le32_to_cpu(desc->sem_control_bits);
479                 int last=0;
480                 int idx;
481
482                 if (!(control & DMASCR_DMA_COMPLETE)) {
483                         control = dma_status;
484                         last=1; 
485                 }
486                 page->headcnt++;
487                 idx = page->idx;
488                 page->idx++;
489                 if (page->idx >= bio->bi_vcnt) {
490                         page->bio = bio->bi_next;
491                         page->idx = page->bio->bi_idx;
492                 }
493
494                 pci_unmap_page(card->dev, desc->data_dma_handle, 
495                                bio_iovec_idx(bio,idx)->bv_len,
496                                  (control& DMASCR_TRANSFER_READ) ?
497                                 PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
498                 if (control & DMASCR_HARD_ERROR) {
499                         /* error */
500                         clear_bit(BIO_UPTODATE, &bio->bi_flags);
501                         dev_printk(KERN_WARNING, &card->dev->dev,
502                                 "I/O error on sector %d/%d\n",
503                                 le32_to_cpu(desc->local_addr)>>9,
504                                 le32_to_cpu(desc->transfer_size));
505                         dump_dmastat(card, control);
506                 } else if (test_bit(BIO_RW, &bio->bi_rw) &&
507                            le32_to_cpu(desc->local_addr)>>9 == card->init_size) {
508                         card->init_size += le32_to_cpu(desc->transfer_size)>>9;
509                         if (card->init_size>>1 >= card->mm_size) {
510                                 dev_printk(KERN_INFO, &card->dev->dev,
511                                         "memory now initialised\n");
512                                 set_userbit(card, MEMORY_INITIALIZED, 1);
513                         }
514                 }
515                 if (bio != page->bio) {
516                         bio->bi_next = return_bio;
517                         return_bio = bio;
518                 }
519
520                 if (last) break;
521         }
522
523         if (debug & DEBUG_LED_ON_TRANSFER)
524                 set_led(card, LED_REMOVE, LED_OFF);
525
526         if (card->check_batteries) {
527                 card->check_batteries = 0;
528                 check_batteries(card);
529         }
530         if (page->headcnt >= page->cnt) {
531                 reset_page(page);
532                 card->Active = -1;
533                 activate(card);
534         } else {
535                 /* haven't finished with this one yet */
536                 pr_debug("do some more\n");
537                 mm_start_io(card);
538         }
539  out_unlock:
540         spin_unlock_bh(&card->lock);
541
542         while(return_bio) {
543                 struct bio *bio = return_bio;
544
545                 return_bio = bio->bi_next;
546                 bio->bi_next = NULL;
547                 bio_endio(bio, 0);
548         }
549 }
550
551 /*
552 -----------------------------------------------------------------------------------
553 --                              mm_make_request
554 -----------------------------------------------------------------------------------
555 */
556 static int mm_make_request(struct request_queue *q, struct bio *bio)
557 {
558         struct cardinfo *card = q->queuedata;
559         pr_debug("mm_make_request %llu %u\n",
560                  (unsigned long long)bio->bi_sector, bio->bi_size);
561
562         spin_lock_irq(&card->lock);
563         *card->biotail = bio;
564         bio->bi_next = NULL;
565         card->biotail = &bio->bi_next;
566         blk_plug_device(q);
567         spin_unlock_irq(&card->lock);
568
569         return 0;
570 }
571
572 /*
573 -----------------------------------------------------------------------------------
574 --                              mm_interrupt
575 -----------------------------------------------------------------------------------
576 */
577 static irqreturn_t mm_interrupt(int irq, void *__card)
578 {
579         struct cardinfo *card = (struct cardinfo *) __card;
580         unsigned int dma_status;
581         unsigned short cfg_status;
582
583 HW_TRACE(0x30);
584
585         dma_status = le32_to_cpu(readl(card->csr_remap + DMA_STATUS_CTRL));
586
587         if (!(dma_status & (DMASCR_ERROR_MASK | DMASCR_CHAIN_COMPLETE))) {
588                 /* interrupt wasn't for me ... */
589                 return IRQ_NONE;
590         }
591
592         /* clear COMPLETION interrupts */
593         if (card->flags & UM_FLAG_NO_BYTE_STATUS)
594                 writel(cpu_to_le32(DMASCR_DMA_COMPLETE|DMASCR_CHAIN_COMPLETE),
595                        card->csr_remap+ DMA_STATUS_CTRL);
596         else
597                 writeb((DMASCR_DMA_COMPLETE|DMASCR_CHAIN_COMPLETE) >> 16,
598                        card->csr_remap+ DMA_STATUS_CTRL + 2);
599         
600         /* log errors and clear interrupt status */
601         if (dma_status & DMASCR_ANY_ERR) {
602                 unsigned int    data_log1, data_log2;
603                 unsigned int    addr_log1, addr_log2;
604                 unsigned char   stat, count, syndrome, check;
605
606                 stat = readb(card->csr_remap + MEMCTRLCMD_ERRSTATUS);
607
608                 data_log1 = le32_to_cpu(readl(card->csr_remap + ERROR_DATA_LOG));
609                 data_log2 = le32_to_cpu(readl(card->csr_remap + ERROR_DATA_LOG + 4));
610                 addr_log1 = le32_to_cpu(readl(card->csr_remap + ERROR_ADDR_LOG));
611                 addr_log2 = readb(card->csr_remap + ERROR_ADDR_LOG + 4);
612
613                 count = readb(card->csr_remap + ERROR_COUNT);
614                 syndrome = readb(card->csr_remap + ERROR_SYNDROME);
615                 check = readb(card->csr_remap + ERROR_CHECK);
616
617                 dump_dmastat(card, dma_status);
618
619                 if (stat & 0x01)
620                         dev_printk(KERN_ERR, &card->dev->dev,
621                                 "Memory access error detected (err count %d)\n",
622                                 count);
623                 if (stat & 0x02)
624                         dev_printk(KERN_ERR, &card->dev->dev,
625                                 "Multi-bit EDC error\n");
626
627                 dev_printk(KERN_ERR, &card->dev->dev,
628                         "Fault Address 0x%02x%08x, Fault Data 0x%08x%08x\n",
629                         addr_log2, addr_log1, data_log2, data_log1);
630                 dev_printk(KERN_ERR, &card->dev->dev,
631                         "Fault Check 0x%02x, Fault Syndrome 0x%02x\n",
632                         check, syndrome);
633
634                 writeb(0, card->csr_remap + ERROR_COUNT);
635         }
636
637         if (dma_status & DMASCR_PARITY_ERR_REP) {
638                 dev_printk(KERN_ERR, &card->dev->dev,
639                         "PARITY ERROR REPORTED\n");
640                 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
641                 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
642         }
643
644         if (dma_status & DMASCR_PARITY_ERR_DET) {
645                 dev_printk(KERN_ERR, &card->dev->dev,
646                         "PARITY ERROR DETECTED\n");
647                 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
648                 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
649         }
650
651         if (dma_status & DMASCR_SYSTEM_ERR_SIG) {
652                 dev_printk(KERN_ERR, &card->dev->dev, "SYSTEM ERROR\n");
653                 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
654                 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
655         }
656
657         if (dma_status & DMASCR_TARGET_ABT) {
658                 dev_printk(KERN_ERR, &card->dev->dev, "TARGET ABORT\n");
659                 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
660                 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
661         }
662
663         if (dma_status & DMASCR_MASTER_ABT) {
664                 dev_printk(KERN_ERR, &card->dev->dev, "MASTER ABORT\n");
665                 pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
666                 pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
667         }
668
669         /* and process the DMA descriptors */
670         card->dma_status = dma_status;
671         tasklet_schedule(&card->tasklet);
672
673 HW_TRACE(0x36);
674
675         return IRQ_HANDLED; 
676 }
677 /*
678 -----------------------------------------------------------------------------------
679 --                         set_fault_to_battery_status
680 -----------------------------------------------------------------------------------
681 */
682 /*
683  * If both batteries are good, no LED
684  * If either battery has been warned, solid LED
685  * If both batteries are bad, flash the LED quickly
686  * If either battery is bad, flash the LED semi quickly
687  */
688 static void set_fault_to_battery_status(struct cardinfo *card)
689 {
690         if (card->battery[0].good && card->battery[1].good)
691                 set_led(card, LED_FAULT, LED_OFF);
692         else if (card->battery[0].warned || card->battery[1].warned)
693                 set_led(card, LED_FAULT, LED_ON);
694         else if (!card->battery[0].good && !card->battery[1].good)
695                 set_led(card, LED_FAULT, LED_FLASH_7_0);
696         else
697                 set_led(card, LED_FAULT, LED_FLASH_3_5);
698 }
699
700 static void init_battery_timer(void);
701
702
703 /*
704 -----------------------------------------------------------------------------------
705 --                            check_battery
706 -----------------------------------------------------------------------------------
707 */
708 static int check_battery(struct cardinfo *card, int battery, int status)
709 {
710         if (status != card->battery[battery].good) {
711                 card->battery[battery].good = !card->battery[battery].good;
712                 card->battery[battery].last_change = jiffies;
713
714                 if (card->battery[battery].good) {
715                         dev_printk(KERN_ERR, &card->dev->dev,
716                                 "Battery %d now good\n", battery + 1);
717                         card->battery[battery].warned = 0;
718                 } else
719                         dev_printk(KERN_ERR, &card->dev->dev,
720                                 "Battery %d now FAILED\n", battery + 1);
721
722                 return 1;
723         } else if (!card->battery[battery].good &&
724                    !card->battery[battery].warned &&
725                    time_after_eq(jiffies, card->battery[battery].last_change +
726                                  (HZ * 60 * 60 * 5))) {
727                 dev_printk(KERN_ERR, &card->dev->dev,
728                         "Battery %d still FAILED after 5 hours\n", battery + 1);
729                 card->battery[battery].warned = 1;
730
731                 return 1;
732         }
733
734         return 0;
735 }
736 /*
737 -----------------------------------------------------------------------------------
738 --                              check_batteries
739 -----------------------------------------------------------------------------------
740 */
741 static void check_batteries(struct cardinfo *card)
742 {
743         /* NOTE: this must *never* be called while the card
744          * is doing (bus-to-card) DMA, or you will need the
745          * reset switch
746          */
747         unsigned char status;
748         int ret1, ret2;
749
750         status = readb(card->csr_remap + MEMCTRLSTATUS_BATTERY);
751         if (debug & DEBUG_BATTERY_POLLING)
752                 dev_printk(KERN_DEBUG, &card->dev->dev,
753                         "checking battery status, 1 = %s, 2 = %s\n",
754                        (status & BATTERY_1_FAILURE) ? "FAILURE" : "OK",
755                        (status & BATTERY_2_FAILURE) ? "FAILURE" : "OK");
756
757         ret1 = check_battery(card, 0, !(status & BATTERY_1_FAILURE));
758         ret2 = check_battery(card, 1, !(status & BATTERY_2_FAILURE));
759
760         if (ret1 || ret2)
761                 set_fault_to_battery_status(card);
762 }
763
764 static void check_all_batteries(unsigned long ptr)
765 {
766         int i;
767
768         for (i = 0; i < num_cards; i++) 
769                 if (!(cards[i].flags & UM_FLAG_NO_BATT)) {
770                         struct cardinfo *card = &cards[i];
771                         spin_lock_bh(&card->lock);
772                         if (card->Active >= 0)
773                                 card->check_batteries = 1;
774                         else
775                                 check_batteries(card);
776                         spin_unlock_bh(&card->lock);
777                 }
778
779         init_battery_timer();
780 }
781 /*
782 -----------------------------------------------------------------------------------
783 --                            init_battery_timer
784 -----------------------------------------------------------------------------------
785 */
786 static void init_battery_timer(void)
787 {
788         init_timer(&battery_timer);
789         battery_timer.function = check_all_batteries;
790         battery_timer.expires = jiffies + (HZ * 60);
791         add_timer(&battery_timer);
792 }
793 /*
794 -----------------------------------------------------------------------------------
795 --                              del_battery_timer
796 -----------------------------------------------------------------------------------
797 */
798 static void del_battery_timer(void)
799 {
800         del_timer(&battery_timer);
801 }
802 /*
803 -----------------------------------------------------------------------------------
804 --                                mm_revalidate
805 -----------------------------------------------------------------------------------
806 */
807 /*
808  * Note no locks taken out here.  In a worst case scenario, we could drop
809  * a chunk of system memory.  But that should never happen, since validation
810  * happens at open or mount time, when locks are held.
811  *
812  *      That's crap, since doing that while some partitions are opened
813  * or mounted will give you really nasty results.
814  */
815 static int mm_revalidate(struct gendisk *disk)
816 {
817         struct cardinfo *card = disk->private_data;
818         set_capacity(disk, card->mm_size << 1);
819         return 0;
820 }
821
822 static int mm_getgeo(struct block_device *bdev, struct hd_geometry *geo)
823 {
824         struct cardinfo *card = bdev->bd_disk->private_data;
825         int size = card->mm_size * (1024 / MM_HARDSECT);
826
827         /*
828          * get geometry: we have to fake one...  trim the size to a
829          * multiple of 2048 (1M): tell we have 32 sectors, 64 heads,
830          * whatever cylinders.
831          */
832         geo->heads     = 64;
833         geo->sectors   = 32;
834         geo->cylinders = size / (geo->heads * geo->sectors);
835         return 0;
836 }
837
838 /*
839 -----------------------------------------------------------------------------------
840 --                                mm_check_change
841 -----------------------------------------------------------------------------------
842   Future support for removable devices
843 */
844 static int mm_check_change(struct gendisk *disk)
845 {
846 /*  struct cardinfo *dev = disk->private_data; */
847         return 0;
848 }
849 /*
850 -----------------------------------------------------------------------------------
851 --                             mm_fops
852 -----------------------------------------------------------------------------------
853 */
854 static struct block_device_operations mm_fops = {
855         .owner          = THIS_MODULE,
856         .getgeo         = mm_getgeo,
857         .revalidate_disk= mm_revalidate,
858         .media_changed  = mm_check_change,
859 };
860 /*
861 -----------------------------------------------------------------------------------
862 --                                mm_pci_probe
863 -----------------------------------------------------------------------------------
864 */
865 static int __devinit mm_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
866 {
867         int ret = -ENODEV;
868         struct cardinfo *card = &cards[num_cards];
869         unsigned char   mem_present;
870         unsigned char   batt_status;
871         unsigned int    saved_bar, data;
872         int             magic_number;
873         static int      printed_version;
874
875         if (!printed_version++)
876                 printk(KERN_INFO DRIVER_VERSION " : " DRIVER_DESC "\n");
877
878         if (pci_enable_device(dev) < 0)
879                 return -ENODEV;
880
881         pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0xF8);
882         pci_set_master(dev);
883
884         card->dev         = dev;
885
886         card->csr_base = pci_resource_start(dev, 0);
887         card->csr_len  = pci_resource_len(dev, 0);
888
889         dev_printk(KERN_INFO, &dev->dev,
890                 "Micro Memory(tm) controller found (PCI Mem Module (Battery Backup))\n");
891
892         if (pci_set_dma_mask(dev, DMA_64BIT_MASK) &&
893             pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
894                 dev_printk(KERN_WARNING, &dev->dev, "NO suitable DMA found\n");
895                 return  -ENOMEM;
896         }
897         if (!request_mem_region(card->csr_base, card->csr_len, "Micro Memory")) {
898                 dev_printk(KERN_ERR, &card->dev->dev,
899                         "Unable to request memory region\n");
900                 ret = -ENOMEM;
901
902                 goto failed_req_csr;
903         }
904
905         card->csr_remap = ioremap_nocache(card->csr_base, card->csr_len);
906         if (!card->csr_remap) {
907                 dev_printk(KERN_ERR, &card->dev->dev,
908                         "Unable to remap memory region\n");
909                 ret = -ENOMEM;
910
911                 goto failed_remap_csr;
912         }
913
914         dev_printk(KERN_INFO, &card->dev->dev,
915                 "CSR 0x%08lx -> 0x%p (0x%lx)\n",
916                card->csr_base, card->csr_remap, card->csr_len);
917
918         switch(card->dev->device) {
919         case 0x5415:
920                 card->flags |= UM_FLAG_NO_BYTE_STATUS | UM_FLAG_NO_BATTREG;
921                 magic_number = 0x59;
922                 break;
923
924         case 0x5425:
925                 card->flags |= UM_FLAG_NO_BYTE_STATUS;
926                 magic_number = 0x5C;
927                 break;
928
929         case 0x6155:
930                 card->flags |= UM_FLAG_NO_BYTE_STATUS | UM_FLAG_NO_BATTREG | UM_FLAG_NO_BATT;
931                 magic_number = 0x99;
932                 break;
933
934         default:
935                 magic_number = 0x100;
936                 break;
937         }
938
939         if (readb(card->csr_remap + MEMCTRLSTATUS_MAGIC) != magic_number) {
940                 dev_printk(KERN_ERR, &card->dev->dev, "Magic number invalid\n");
941                 ret = -ENOMEM;
942                 goto failed_magic;
943         }
944
945         card->mm_pages[0].desc = pci_alloc_consistent(card->dev,
946                                                       PAGE_SIZE*2,
947                                                       &card->mm_pages[0].page_dma);
948         card->mm_pages[1].desc = pci_alloc_consistent(card->dev,
949                                                       PAGE_SIZE*2,
950                                                       &card->mm_pages[1].page_dma);
951         if (card->mm_pages[0].desc == NULL ||
952             card->mm_pages[1].desc == NULL) {
953                 dev_printk(KERN_ERR, &card->dev->dev, "alloc failed\n");
954                 goto failed_alloc;
955         }
956         reset_page(&card->mm_pages[0]);
957         reset_page(&card->mm_pages[1]);
958         card->Ready = 0;        /* page 0 is ready */
959         card->Active = -1;      /* no page is active */
960         card->bio = NULL;
961         card->biotail = &card->bio;
962
963         card->queue = blk_alloc_queue(GFP_KERNEL);
964         if (!card->queue)
965                 goto failed_alloc;
966
967         blk_queue_make_request(card->queue, mm_make_request);
968         card->queue->queuedata = card;
969         card->queue->unplug_fn = mm_unplug_device;
970
971         tasklet_init(&card->tasklet, process_page, (unsigned long)card);
972
973         card->check_batteries = 0;
974         
975         mem_present = readb(card->csr_remap + MEMCTRLSTATUS_MEMORY);
976         switch (mem_present) {
977         case MEM_128_MB:
978                 card->mm_size = 1024 * 128;
979                 break;
980         case MEM_256_MB:
981                 card->mm_size = 1024 * 256;
982                 break;
983         case MEM_512_MB:
984                 card->mm_size = 1024 * 512;
985                 break;
986         case MEM_1_GB:
987                 card->mm_size = 1024 * 1024;
988                 break;
989         case MEM_2_GB:
990                 card->mm_size = 1024 * 2048;
991                 break;
992         default:
993                 card->mm_size = 0;
994                 break;
995         }
996
997         /* Clear the LED's we control */
998         set_led(card, LED_REMOVE, LED_OFF);
999         set_led(card, LED_FAULT, LED_OFF);
1000
1001         batt_status = readb(card->csr_remap + MEMCTRLSTATUS_BATTERY);
1002
1003         card->battery[0].good = !(batt_status & BATTERY_1_FAILURE);
1004         card->battery[1].good = !(batt_status & BATTERY_2_FAILURE);
1005         card->battery[0].last_change = card->battery[1].last_change = jiffies;
1006
1007         if (card->flags & UM_FLAG_NO_BATT) 
1008                 dev_printk(KERN_INFO, &card->dev->dev,
1009                         "Size %d KB\n", card->mm_size);
1010         else {
1011                 dev_printk(KERN_INFO, &card->dev->dev,
1012                         "Size %d KB, Battery 1 %s (%s), Battery 2 %s (%s)\n",
1013                        card->mm_size,
1014                        (batt_status & BATTERY_1_DISABLED ? "Disabled" : "Enabled"),
1015                        card->battery[0].good ? "OK" : "FAILURE",
1016                        (batt_status & BATTERY_2_DISABLED ? "Disabled" : "Enabled"),
1017                        card->battery[1].good ? "OK" : "FAILURE");
1018
1019                 set_fault_to_battery_status(card);
1020         }
1021
1022         pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &saved_bar);
1023         data = 0xffffffff;
1024         pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, data);
1025         pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &data);
1026         pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, saved_bar);
1027         data &= 0xfffffff0;
1028         data = ~data;
1029         data += 1;
1030
1031         card->win_size = data;
1032
1033
1034         if (request_irq(dev->irq, mm_interrupt, IRQF_SHARED, "pci-umem", card)) {
1035                 dev_printk(KERN_ERR, &card->dev->dev,
1036                         "Unable to allocate IRQ\n");
1037                 ret = -ENODEV;
1038
1039                 goto failed_req_irq;
1040         }
1041
1042         card->irq = dev->irq;
1043         dev_printk(KERN_INFO, &card->dev->dev,
1044                 "Window size %d bytes, IRQ %d\n",
1045                card->win_size, card->irq);
1046
1047         spin_lock_init(&card->lock);
1048
1049         pci_set_drvdata(dev, card);
1050
1051         if (pci_write_cmd != 0x0F)      /* If not Memory Write & Invalidate */
1052                 pci_write_cmd = 0x07;   /* then Memory Write command */
1053
1054         if (pci_write_cmd & 0x08) { /* use Memory Write and Invalidate */
1055                 unsigned short cfg_command;
1056                 pci_read_config_word(dev, PCI_COMMAND, &cfg_command);
1057                 cfg_command |= 0x10; /* Memory Write & Invalidate Enable */
1058                 pci_write_config_word(dev, PCI_COMMAND, cfg_command);
1059         }
1060         pci_cmds = (pci_read_cmd << 28) | (pci_write_cmd << 24);
1061
1062         num_cards++;
1063
1064         if (!get_userbit(card, MEMORY_INITIALIZED)) {
1065                 dev_printk(KERN_INFO, &card->dev->dev,
1066                         "memory NOT initialized. Consider over-writing whole device.\n");
1067                 card->init_size = 0;
1068         } else {
1069                 dev_printk(KERN_INFO, &card->dev->dev,
1070                         "memory already initialized\n");
1071                 card->init_size = card->mm_size;
1072         }
1073
1074         /* Enable ECC */
1075         writeb(EDC_STORE_CORRECT, card->csr_remap + MEMCTRLCMD_ERRCTRL);
1076
1077         return 0;
1078
1079  failed_req_irq:
1080  failed_alloc:
1081         if (card->mm_pages[0].desc)
1082                 pci_free_consistent(card->dev, PAGE_SIZE*2,
1083                                     card->mm_pages[0].desc,
1084                                     card->mm_pages[0].page_dma);
1085         if (card->mm_pages[1].desc)
1086                 pci_free_consistent(card->dev, PAGE_SIZE*2,
1087                                     card->mm_pages[1].desc,
1088                                     card->mm_pages[1].page_dma);
1089  failed_magic:
1090         iounmap(card->csr_remap);
1091  failed_remap_csr:
1092         release_mem_region(card->csr_base, card->csr_len);
1093  failed_req_csr:
1094
1095         return ret;
1096 }
1097 /*
1098 -----------------------------------------------------------------------------------
1099 --                              mm_pci_remove
1100 -----------------------------------------------------------------------------------
1101 */
1102 static void mm_pci_remove(struct pci_dev *dev)
1103 {
1104         struct cardinfo *card = pci_get_drvdata(dev);
1105
1106         tasklet_kill(&card->tasklet);
1107         iounmap(card->csr_remap);
1108         release_mem_region(card->csr_base, card->csr_len);
1109         free_irq(card->irq, card);
1110
1111         if (card->mm_pages[0].desc)
1112                 pci_free_consistent(card->dev, PAGE_SIZE*2,
1113                                     card->mm_pages[0].desc,
1114                                     card->mm_pages[0].page_dma);
1115         if (card->mm_pages[1].desc)
1116                 pci_free_consistent(card->dev, PAGE_SIZE*2,
1117                                     card->mm_pages[1].desc,
1118                                     card->mm_pages[1].page_dma);
1119         blk_cleanup_queue(card->queue);
1120 }
1121
1122 static const struct pci_device_id mm_pci_ids[] = {
1123     {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY,PCI_DEVICE_ID_MICRO_MEMORY_5415CN)},
1124     {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY,PCI_DEVICE_ID_MICRO_MEMORY_5425CN)},
1125     {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY,PCI_DEVICE_ID_MICRO_MEMORY_6155)},
1126     {
1127         .vendor =       0x8086,
1128         .device =       0xB555,
1129         .subvendor=     0x1332,
1130         .subdevice=     0x5460,
1131         .class  =       0x050000,
1132         .class_mask=    0,
1133     }, { /* end: all zeroes */ }
1134 };
1135
1136 MODULE_DEVICE_TABLE(pci, mm_pci_ids);
1137
1138 static struct pci_driver mm_pci_driver = {
1139         .name =         "umem",
1140         .id_table =     mm_pci_ids,
1141         .probe =        mm_pci_probe,
1142         .remove =       mm_pci_remove,
1143 };
1144 /*
1145 -----------------------------------------------------------------------------------
1146 --                               mm_init
1147 -----------------------------------------------------------------------------------
1148 */
1149
1150 static int __init mm_init(void)
1151 {
1152         int retval, i;
1153         int err;
1154
1155         retval = pci_register_driver(&mm_pci_driver);
1156         if (retval)
1157                 return -ENOMEM;
1158
1159         err = major_nr = register_blkdev(0, "umem");
1160         if (err < 0) {
1161                 pci_unregister_driver(&mm_pci_driver);
1162                 return -EIO;
1163         }
1164
1165         for (i = 0; i < num_cards; i++) {
1166                 mm_gendisk[i] = alloc_disk(1 << MM_SHIFT);
1167                 if (!mm_gendisk[i])
1168                         goto out;
1169         }
1170
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);
1181                 add_disk(disk);
1182         }
1183
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"); */
1187         return 0;
1188
1189 out:
1190         pci_unregister_driver(&mm_pci_driver);
1191         unregister_blkdev(major_nr, "umem");
1192         while (i--)
1193                 put_disk(mm_gendisk[i]);
1194         return -ENOMEM;
1195 }
1196 /*
1197 -----------------------------------------------------------------------------------
1198 --                             mm_cleanup
1199 -----------------------------------------------------------------------------------
1200 */
1201 static void __exit mm_cleanup(void)
1202 {
1203         int i;
1204
1205         del_battery_timer();
1206
1207         for (i=0; i < num_cards ; i++) {
1208                 del_gendisk(mm_gendisk[i]);
1209                 put_disk(mm_gendisk[i]);
1210         }
1211
1212         pci_unregister_driver(&mm_pci_driver);
1213
1214         unregister_blkdev(major_nr, "umem");
1215 }
1216
1217 module_init(mm_init);
1218 module_exit(mm_cleanup);
1219
1220 MODULE_AUTHOR(DRIVER_AUTHOR);
1221 MODULE_DESCRIPTION(DRIVER_DESC);
1222 MODULE_LICENSE("GPL");