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