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