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