umem.c 30.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 * mm.c - Micro Memory(tm) PCI memory board block device driver - v2.3
 *
 * (C) 2001 San Mehat <nettwerk@valinux.com>
 * (C) 2001 Johannes Erdfelt <jerdfelt@valinux.com>
 * (C) 2001 NeilBrown <neilb@cse.unsw.edu.au>
 *
 * This driver for the Micro Memory PCI Memory Module with Battery Backup
 * is Copyright Micro Memory Inc 2001-2002.  All rights reserved.
 *
 * This driver is released to the public under the terms of the
 *  GNU GENERAL PUBLIC LICENSE version 2
 * See the file COPYING for details.
 *
 * This driver provides a standard block device interface for Micro Memory(tm)
 * PCI based RAM boards.
 * 10/05/01: Phap Nguyen - Rebuilt the driver
 * 10/22/01: Phap Nguyen - v2.1 Added disk partitioning
 * 29oct2001:NeilBrown   - Use make_request_fn instead of request_fn
 *                       - use stand disk partitioning (so fdisk works).
 * 08nov2001:NeilBrown	 - change driver name from "mm" to "umem"
 *			 - incorporate into main kernel
 * 08apr2002:NeilBrown   - Move some of interrupt handle to tasklet
 *			 - use spin_lock_bh instead of _irq
 *			 - Never block on make_request.  queue
 *			   bh's instead.
 *			 - unregister umem from devfs at mod unload
 *			 - Change version to 2.3
 * 07Nov2001:Phap Nguyen - Select pci read command: 06, 12, 15 (Decimal)
 * 07Jan2002: P. Nguyen  - Used PCI Memory Write & Invalidate for DMA
 * 15May2002:NeilBrown   - convert to bio for 2.5
 * 17May2002:NeilBrown   - remove init_mem initialisation.  Instead detect
 *			 - a sequence of writes that cover the card, and
 *			 - set initialised bit then.
 */

37
#undef DEBUG	/* #define DEBUG if you want debugging info (pr_debug) */
L
Linus Torvalds 已提交
38 39 40 41 42
#include <linux/fs.h>
#include <linux/bio.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/mman.h>
43
#include <linux/gfp.h>
L
Linus Torvalds 已提交
44 45 46 47 48 49
#include <linux/ioctl.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/timer.h>
#include <linux/pci.h>
50
#include <linux/dma-mapping.h>
L
Linus Torvalds 已提交
51 52 53 54

#include <linux/fcntl.h>        /* O_ACCMODE */
#include <linux/hdreg.h>  /* HDIO_GETGEO */

55
#include "umem.h"
L
Linus Torvalds 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69

#include <asm/uaccess.h>
#include <asm/io.h>

#define MM_MAXCARDS 4
#define MM_RAHEAD 2      /* two sectors */
#define MM_BLKSIZE 1024  /* 1k blocks */
#define MM_HARDSECT 512  /* 512-byte hardware sectors */
#define MM_SHIFT 6       /* max 64 partitions on 4 cards  */

/*
 * Version Information
 */

J
Jeff Garzik 已提交
70 71 72 73
#define DRIVER_NAME	"umem"
#define DRIVER_VERSION	"v2.3"
#define DRIVER_AUTHOR	"San Mehat, Johannes Erdfelt, NeilBrown"
#define DRIVER_DESC	"Micro Memory(tm) PCI memory board block driver"
L
Linus Torvalds 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

static int debug;
/* #define HW_TRACE(x)     writeb(x,cards[0].csr_remap + MEMCTRLSTATUS_MAGIC) */
#define HW_TRACE(x)

#define DEBUG_LED_ON_TRANSFER	0x01
#define DEBUG_BATTERY_POLLING	0x02

module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "Debug bitmask");

static int pci_read_cmd = 0x0C;		/* Read Multiple */
module_param(pci_read_cmd, int, 0);
MODULE_PARM_DESC(pci_read_cmd, "PCI read command");

static int pci_write_cmd = 0x0F;	/* Write and Invalidate */
module_param(pci_write_cmd, int, 0);
MODULE_PARM_DESC(pci_write_cmd, "PCI write command");

static int pci_cmds;

static int major_nr;

#include <linux/blkdev.h>
#include <linux/blkpg.h>

struct cardinfo {
	struct pci_dev	*dev;

	unsigned char	__iomem *csr_remap;
	unsigned int	mm_size;  /* size in kbytes */

	unsigned int	init_size; /* initial segment, in sectors,
				    * that we know to
				    * have been written
				    */
	struct bio	*bio, *currentbio, **biotail;
111 112
	int		current_idx;
	sector_t	current_sector;
L
Linus Torvalds 已提交
113

114
	struct request_queue *queue;
L
Linus Torvalds 已提交
115 116 117 118 119 120

	struct mm_page {
		dma_addr_t		page_dma;
		struct mm_dma_desc	*desc;
		int	 		cnt, headcnt;
		struct bio		*bio, **biotail;
121
		int			idx;
L
Linus Torvalds 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	} mm_pages[2];
#define DESC_PER_PAGE ((PAGE_SIZE*2)/sizeof(struct mm_dma_desc))

	int  Active, Ready;

	struct tasklet_struct	tasklet;
	unsigned int dma_status;

	struct {
		int		good;
		int		warned;
		unsigned long	last_change;
	} battery[2];

	spinlock_t 	lock;
	int		check_batteries;

	int		flags;
};

static struct cardinfo cards[MM_MAXCARDS];
static struct timer_list battery_timer;

145
static int num_cards;
L
Linus Torvalds 已提交
146 147 148 149 150 151 152 153 154 155 156 157

static struct gendisk *mm_gendisk[MM_MAXCARDS];

static void check_batteries(struct cardinfo *card);

static int get_userbit(struct cardinfo *card, int bit)
{
	unsigned char led;

	led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
	return led & bit;
}
158

L
Linus Torvalds 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171
static int set_userbit(struct cardinfo *card, int bit, unsigned char state)
{
	unsigned char led;

	led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
	if (state)
		led |= bit;
	else
		led &= ~bit;
	writeb(led, card->csr_remap + MEMCTRLCMD_LEDCTRL);

	return 0;
}
172

L
Linus Torvalds 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
/*
 * NOTE: For the power LED, use the LED_POWER_* macros since they differ
 */
static void set_led(struct cardinfo *card, int shift, unsigned char state)
{
	unsigned char led;

	led = readb(card->csr_remap + MEMCTRLCMD_LEDCTRL);
	if (state == LED_FLIP)
		led ^= (1<<shift);
	else {
		led &= ~(0x03 << shift);
		led |= (state << shift);
	}
	writeb(led, card->csr_remap + MEMCTRLCMD_LEDCTRL);

}

#ifdef MM_DIAG
static void dump_regs(struct cardinfo *card)
{
	unsigned char *p;
	int i, i1;

	p = card->csr_remap;
	for (i = 0; i < 8; i++) {
		printk(KERN_DEBUG "%p   ", p);

		for (i1 = 0; i1 < 16; i1++)
			printk("%02x ", *p++);

		printk("\n");
	}
}
#endif
208

L
Linus Torvalds 已提交
209 210
static void dump_dmastat(struct cardinfo *card, unsigned int dmastat)
{
J
Jeff Garzik 已提交
211
	dev_printk(KERN_DEBUG, &card->dev->dev, "DMAstat - ");
L
Linus Torvalds 已提交
212
	if (dmastat & DMASCR_ANY_ERR)
213
		printk(KERN_CONT "ANY_ERR ");
L
Linus Torvalds 已提交
214
	if (dmastat & DMASCR_MBE_ERR)
215
		printk(KERN_CONT "MBE_ERR ");
L
Linus Torvalds 已提交
216
	if (dmastat & DMASCR_PARITY_ERR_REP)
217
		printk(KERN_CONT "PARITY_ERR_REP ");
L
Linus Torvalds 已提交
218
	if (dmastat & DMASCR_PARITY_ERR_DET)
219
		printk(KERN_CONT "PARITY_ERR_DET ");
L
Linus Torvalds 已提交
220
	if (dmastat & DMASCR_SYSTEM_ERR_SIG)
221
		printk(KERN_CONT "SYSTEM_ERR_SIG ");
L
Linus Torvalds 已提交
222
	if (dmastat & DMASCR_TARGET_ABT)
223
		printk(KERN_CONT "TARGET_ABT ");
L
Linus Torvalds 已提交
224
	if (dmastat & DMASCR_MASTER_ABT)
225
		printk(KERN_CONT "MASTER_ABT ");
L
Linus Torvalds 已提交
226
	if (dmastat & DMASCR_CHAIN_COMPLETE)
227
		printk(KERN_CONT "CHAIN_COMPLETE ");
L
Linus Torvalds 已提交
228
	if (dmastat & DMASCR_DMA_COMPLETE)
229
		printk(KERN_CONT "DMA_COMPLETE ");
L
Linus Torvalds 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243
	printk("\n");
}

/*
 * Theory of request handling
 *
 * Each bio is assigned to one mm_dma_desc - which may not be enough FIXME
 * We have two pages of mm_dma_desc, holding about 64 descriptors
 * each.  These are allocated at init time.
 * One page is "Ready" and is either full, or can have request added.
 * The other page might be "Active", which DMA is happening on it.
 *
 * Whenever IO on the active page completes, the Ready page is activated
 * and the ex-Active page is clean out and made Ready.
J
Jens Axboe 已提交
244
 * Otherwise the Ready page is only activated when it becomes full.
L
Linus Torvalds 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
 *
 * If a request arrives while both pages a full, it is queued, and b_rdev is
 * overloaded to record whether it was a read or a write.
 *
 * The interrupt handler only polls the device to clear the interrupt.
 * The processing of the result is done in a tasklet.
 */

static void mm_start_io(struct cardinfo *card)
{
	/* we have the lock, we know there is
	 * no IO active, and we know that card->Active
	 * is set
	 */
	struct mm_dma_desc *desc;
	struct mm_page *page;
	int offset;

	/* make the last descriptor end the chain */
	page = &card->mm_pages[card->Active];
265 266
	pr_debug("start_io: %d %d->%d\n",
		card->Active, page->headcnt, page->cnt - 1);
L
Linus Torvalds 已提交
267 268 269 270 271 272
	desc = &page->desc[page->cnt-1];

	desc->control_bits |= cpu_to_le32(DMASCR_CHAIN_COMP_EN);
	desc->control_bits &= ~cpu_to_le32(DMASCR_CHAIN_EN);
	desc->sem_control_bits = desc->control_bits;

273

L
Linus Torvalds 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
	if (debug & DEBUG_LED_ON_TRANSFER)
		set_led(card, LED_REMOVE, LED_ON);

	desc = &page->desc[page->headcnt];
	writel(0, card->csr_remap + DMA_PCI_ADDR);
	writel(0, card->csr_remap + DMA_PCI_ADDR + 4);

	writel(0, card->csr_remap + DMA_LOCAL_ADDR);
	writel(0, card->csr_remap + DMA_LOCAL_ADDR + 4);

	writel(0, card->csr_remap + DMA_TRANSFER_SIZE);
	writel(0, card->csr_remap + DMA_TRANSFER_SIZE + 4);

	writel(0, card->csr_remap + DMA_SEMAPHORE_ADDR);
	writel(0, card->csr_remap + DMA_SEMAPHORE_ADDR + 4);

290 291
	offset = ((char *)desc) - ((char *)page->desc);
	writel(cpu_to_le32((page->page_dma+offset) & 0xffffffff),
L
Linus Torvalds 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
	       card->csr_remap + DMA_DESCRIPTOR_ADDR);
	/* Force the value to u64 before shifting otherwise >> 32 is undefined C
	 * and on some ports will do nothing ! */
	writel(cpu_to_le32(((u64)page->page_dma)>>32),
	       card->csr_remap + DMA_DESCRIPTOR_ADDR + 4);

	/* Go, go, go */
	writel(cpu_to_le32(DMASCR_GO | DMASCR_CHAIN_EN | pci_cmds),
	       card->csr_remap + DMA_STATUS_CTRL);
}

static int add_bio(struct cardinfo *card);

static void activate(struct cardinfo *card)
{
307
	/* if No page is Active, and Ready is
L
Linus Torvalds 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
	 * not empty, then switch Ready page
	 * to active and start IO.
	 * Then add any bh's that are available to Ready
	 */

	do {
		while (add_bio(card))
			;

		if (card->Active == -1 &&
		    card->mm_pages[card->Ready].cnt > 0) {
			card->Active = card->Ready;
			card->Ready = 1-card->Ready;
			mm_start_io(card);
		}

	} while (card->Active == -1 && add_bio(card));
}

static inline void reset_page(struct mm_page *page)
{
	page->cnt = 0;
	page->headcnt = 0;
	page->bio = NULL;
332
	page->biotail = &page->bio;
L
Linus Torvalds 已提交
333 334
}

335
/*
L
Linus Torvalds 已提交
336 337 338 339 340 341 342 343 344 345 346
 * If there is room on Ready page, take
 * one bh off list and add it.
 * return 1 if there was room, else 0.
 */
static int add_bio(struct cardinfo *card)
{
	struct mm_page *p;
	struct mm_dma_desc *desc;
	dma_addr_t dma_handle;
	int offset;
	struct bio *bio;
347 348
	struct bio_vec *vec;
	int idx;
L
Linus Torvalds 已提交
349 350 351 352 353 354
	int rw;
	int len;

	bio = card->currentbio;
	if (!bio && card->bio) {
		card->currentbio = card->bio;
355 356
		card->current_idx = card->bio->bi_idx;
		card->current_sector = card->bio->bi_sector;
L
Linus Torvalds 已提交
357 358 359 360 361 362 363 364
		card->bio = card->bio->bi_next;
		if (card->bio == NULL)
			card->biotail = &card->bio;
		card->currentbio->bi_next = NULL;
		return 1;
	}
	if (!bio)
		return 0;
365
	idx = card->current_idx;
L
Linus Torvalds 已提交
366 367 368 369 370

	rw = bio_rw(bio);
	if (card->mm_pages[card->Ready].cnt >= DESC_PER_PAGE)
		return 0;

371 372 373 374 375
	vec = bio_iovec_idx(bio, idx);
	len = vec->bv_len;
	dma_handle = pci_map_page(card->dev,
				  vec->bv_page,
				  vec->bv_offset,
L
Linus Torvalds 已提交
376
				  len,
377
				  (rw == READ) ?
L
Linus Torvalds 已提交
378 379 380 381 382
				  PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);

	p = &card->mm_pages[card->Ready];
	desc = &p->desc[p->cnt];
	p->cnt++;
383 384
	if (p->bio == NULL)
		p->idx = idx;
L
Linus Torvalds 已提交
385 386 387 388 389 390 391 392 393
	if ((p->biotail) != &bio->bi_next) {
		*(p->biotail) = bio;
		p->biotail = &(bio->bi_next);
		bio->bi_next = NULL;
	}

	desc->data_dma_handle = dma_handle;

	desc->pci_addr = cpu_to_le64((u64)desc->data_dma_handle);
394
	desc->local_addr = cpu_to_le64(card->current_sector << 9);
L
Linus Torvalds 已提交
395
	desc->transfer_size = cpu_to_le32(len);
396
	offset = (((char *)&desc->sem_control_bits) - ((char *)p->desc));
L
Linus Torvalds 已提交
397 398
	desc->sem_addr = cpu_to_le64((u64)(p->page_dma+offset));
	desc->zero1 = desc->zero2 = 0;
399
	offset = (((char *)(desc+1)) - ((char *)p->desc));
L
Linus Torvalds 已提交
400 401 402 403 404 405 406 407 408 409
	desc->next_desc_addr = cpu_to_le64(p->page_dma+offset);
	desc->control_bits = cpu_to_le32(DMASCR_GO|DMASCR_ERR_INT_EN|
					 DMASCR_PARITY_INT_EN|
					 DMASCR_CHAIN_EN |
					 DMASCR_SEM_EN |
					 pci_cmds);
	if (rw == WRITE)
		desc->control_bits |= cpu_to_le32(DMASCR_TRANSFER_READ);
	desc->sem_control_bits = desc->control_bits;

410 411 412 413
	card->current_sector += (len >> 9);
	idx++;
	card->current_idx = idx;
	if (idx >= bio->bi_vcnt)
L
Linus Torvalds 已提交
414 415 416 417 418 419 420 421 422 423
		card->currentbio = NULL;

	return 1;
}

static void process_page(unsigned long data)
{
	/* check if any of the requests in the page are DMA_COMPLETE,
	 * and deal with them appropriately.
	 * If we find a descriptor without DMA_COMPLETE in the semaphore, then
424 425
	 * dma must have hit an error on that descriptor, so use dma_status
	 * instead and assume that all following descriptors must be re-tried.
L
Linus Torvalds 已提交
426 427
	 */
	struct mm_page *page;
428
	struct bio *return_bio = NULL;
L
Linus Torvalds 已提交
429 430 431 432 433 434 435
	struct cardinfo *card = (struct cardinfo *)data;
	unsigned int dma_status = card->dma_status;

	spin_lock_bh(&card->lock);
	if (card->Active < 0)
		goto out_unlock;
	page = &card->mm_pages[card->Active];
436

L
Linus Torvalds 已提交
437 438 439 440
	while (page->headcnt < page->cnt) {
		struct bio *bio = page->bio;
		struct mm_dma_desc *desc = &page->desc[page->headcnt];
		int control = le32_to_cpu(desc->sem_control_bits);
441
		int last = 0;
L
Linus Torvalds 已提交
442 443 444 445
		int idx;

		if (!(control & DMASCR_DMA_COMPLETE)) {
			control = dma_status;
446
			last = 1;
L
Linus Torvalds 已提交
447 448
		}
		page->headcnt++;
449 450 451
		idx = page->idx;
		page->idx++;
		if (page->idx >= bio->bi_vcnt) {
L
Linus Torvalds 已提交
452
			page->bio = bio->bi_next;
N
Neil Brown 已提交
453 454
			if (page->bio)
				page->idx = page->bio->bi_idx;
455
		}
L
Linus Torvalds 已提交
456

457
		pci_unmap_page(card->dev, desc->data_dma_handle,
458 459
			       bio_iovec_idx(bio, idx)->bv_len,
				 (control & DMASCR_TRANSFER_READ) ?
L
Linus Torvalds 已提交
460 461 462 463
				PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
		if (control & DMASCR_HARD_ERROR) {
			/* error */
			clear_bit(BIO_UPTODATE, &bio->bi_flags);
J
Jeff Garzik 已提交
464 465 466 467
			dev_printk(KERN_WARNING, &card->dev->dev,
				"I/O error on sector %d/%d\n",
				le32_to_cpu(desc->local_addr)>>9,
				le32_to_cpu(desc->transfer_size));
L
Linus Torvalds 已提交
468
			dump_dmastat(card, control);
469
		} else if ((bio->bi_rw & REQ_WRITE) &&
470 471 472 473
			   le32_to_cpu(desc->local_addr) >> 9 ==
				card->init_size) {
			card->init_size += le32_to_cpu(desc->transfer_size) >> 9;
			if (card->init_size >> 1 >= card->mm_size) {
J
Jeff Garzik 已提交
474 475
				dev_printk(KERN_INFO, &card->dev->dev,
					"memory now initialised\n");
L
Linus Torvalds 已提交
476 477 478 479 480 481 482 483
				set_userbit(card, MEMORY_INITIALIZED, 1);
			}
		}
		if (bio != page->bio) {
			bio->bi_next = return_bio;
			return_bio = bio;
		}

484 485
		if (last)
			break;
L
Linus Torvalds 已提交
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
	}

	if (debug & DEBUG_LED_ON_TRANSFER)
		set_led(card, LED_REMOVE, LED_OFF);

	if (card->check_batteries) {
		card->check_batteries = 0;
		check_batteries(card);
	}
	if (page->headcnt >= page->cnt) {
		reset_page(page);
		card->Active = -1;
		activate(card);
	} else {
		/* haven't finished with this one yet */
501
		pr_debug("do some more\n");
L
Linus Torvalds 已提交
502 503 504 505 506
		mm_start_io(card);
	}
 out_unlock:
	spin_unlock_bh(&card->lock);

507
	while (return_bio) {
L
Linus Torvalds 已提交
508 509 510 511
		struct bio *bio = return_bio;

		return_bio = bio->bi_next;
		bio->bi_next = NULL;
512
		bio_endio(bio, 0);
L
Linus Torvalds 已提交
513 514 515
	}
}

T
Tao Guo 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
struct mm_plug_cb {
	struct blk_plug_cb cb;
	struct cardinfo *card;
};

static void mm_unplug(struct blk_plug_cb *cb)
{
	struct mm_plug_cb *mmcb = container_of(cb, struct mm_plug_cb, cb);

	spin_lock_irq(&mmcb->card->lock);
	activate(mmcb->card);
	spin_unlock_irq(&mmcb->card->lock);
	kfree(mmcb);
}

static int mm_check_plugged(struct cardinfo *card)
{
	struct blk_plug *plug = current->plug;
	struct mm_plug_cb *mmcb;

	if (!plug)
		return 0;

	list_for_each_entry(mmcb, &plug->cb_list, cb.list) {
		if (mmcb->cb.callback == mm_unplug && mmcb->card == card)
			return 1;
	}
	/* Not currently on the callback list */
	mmcb = kmalloc(sizeof(*mmcb), GFP_ATOMIC);
	if (!mmcb)
		return 0;

	mmcb->card = card;
	mmcb->cb.callback = mm_unplug;
	list_add(&mmcb->cb.list, &plug->cb_list);
	return 1;
}

554
static void mm_make_request(struct request_queue *q, struct bio *bio)
L
Linus Torvalds 已提交
555 556
{
	struct cardinfo *card = q->queuedata;
557 558
	pr_debug("mm_make_request %llu %u\n",
		 (unsigned long long)bio->bi_sector, bio->bi_size);
L
Linus Torvalds 已提交
559 560 561 562 563

	spin_lock_irq(&card->lock);
	*card->biotail = bio;
	bio->bi_next = NULL;
	card->biotail = &bio->bi_next;
T
Tao Guo 已提交
564 565
	if (bio->bi_rw & REQ_SYNC || !mm_check_plugged(card))
		activate(card);
L
Linus Torvalds 已提交
566 567
	spin_unlock_irq(&card->lock);

568
	return;
L
Linus Torvalds 已提交
569 570
}

571
static irqreturn_t mm_interrupt(int irq, void *__card)
L
Linus Torvalds 已提交
572 573 574 575 576 577 578 579 580 581 582 583
{
	struct cardinfo *card = (struct cardinfo *) __card;
	unsigned int dma_status;
	unsigned short cfg_status;

HW_TRACE(0x30);

	dma_status = le32_to_cpu(readl(card->csr_remap + DMA_STATUS_CTRL));

	if (!(dma_status & (DMASCR_ERROR_MASK | DMASCR_CHAIN_COMPLETE))) {
		/* interrupt wasn't for me ... */
		return IRQ_NONE;
584
	}
L
Linus Torvalds 已提交
585 586 587 588

	/* clear COMPLETION interrupts */
	if (card->flags & UM_FLAG_NO_BYTE_STATUS)
		writel(cpu_to_le32(DMASCR_DMA_COMPLETE|DMASCR_CHAIN_COMPLETE),
589
		       card->csr_remap + DMA_STATUS_CTRL);
L
Linus Torvalds 已提交
590 591
	else
		writeb((DMASCR_DMA_COMPLETE|DMASCR_CHAIN_COMPLETE) >> 16,
592
		       card->csr_remap + DMA_STATUS_CTRL + 2);
593

L
Linus Torvalds 已提交
594 595 596 597 598 599 600 601
	/* log errors and clear interrupt status */
	if (dma_status & DMASCR_ANY_ERR) {
		unsigned int	data_log1, data_log2;
		unsigned int	addr_log1, addr_log2;
		unsigned char	stat, count, syndrome, check;

		stat = readb(card->csr_remap + MEMCTRLCMD_ERRSTATUS);

602 603 604 605 606 607
		data_log1 = le32_to_cpu(readl(card->csr_remap +
						ERROR_DATA_LOG));
		data_log2 = le32_to_cpu(readl(card->csr_remap +
						ERROR_DATA_LOG + 4));
		addr_log1 = le32_to_cpu(readl(card->csr_remap +
						ERROR_ADDR_LOG));
L
Linus Torvalds 已提交
608 609 610 611 612 613 614 615 616
		addr_log2 = readb(card->csr_remap + ERROR_ADDR_LOG + 4);

		count = readb(card->csr_remap + ERROR_COUNT);
		syndrome = readb(card->csr_remap + ERROR_SYNDROME);
		check = readb(card->csr_remap + ERROR_CHECK);

		dump_dmastat(card, dma_status);

		if (stat & 0x01)
J
Jeff Garzik 已提交
617 618 619
			dev_printk(KERN_ERR, &card->dev->dev,
				"Memory access error detected (err count %d)\n",
				count);
L
Linus Torvalds 已提交
620
		if (stat & 0x02)
J
Jeff Garzik 已提交
621 622
			dev_printk(KERN_ERR, &card->dev->dev,
				"Multi-bit EDC error\n");
L
Linus Torvalds 已提交
623

J
Jeff Garzik 已提交
624 625 626 627 628 629
		dev_printk(KERN_ERR, &card->dev->dev,
			"Fault Address 0x%02x%08x, Fault Data 0x%08x%08x\n",
			addr_log2, addr_log1, data_log2, data_log1);
		dev_printk(KERN_ERR, &card->dev->dev,
			"Fault Check 0x%02x, Fault Syndrome 0x%02x\n",
			check, syndrome);
L
Linus Torvalds 已提交
630 631 632 633 634

		writeb(0, card->csr_remap + ERROR_COUNT);
	}

	if (dma_status & DMASCR_PARITY_ERR_REP) {
J
Jeff Garzik 已提交
635 636
		dev_printk(KERN_ERR, &card->dev->dev,
			"PARITY ERROR REPORTED\n");
L
Linus Torvalds 已提交
637 638 639 640 641
		pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
		pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
	}

	if (dma_status & DMASCR_PARITY_ERR_DET) {
J
Jeff Garzik 已提交
642 643
		dev_printk(KERN_ERR, &card->dev->dev,
			"PARITY ERROR DETECTED\n");
L
Linus Torvalds 已提交
644 645 646 647 648
		pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
		pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
	}

	if (dma_status & DMASCR_SYSTEM_ERR_SIG) {
J
Jeff Garzik 已提交
649
		dev_printk(KERN_ERR, &card->dev->dev, "SYSTEM ERROR\n");
L
Linus Torvalds 已提交
650 651 652 653 654
		pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
		pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
	}

	if (dma_status & DMASCR_TARGET_ABT) {
J
Jeff Garzik 已提交
655
		dev_printk(KERN_ERR, &card->dev->dev, "TARGET ABORT\n");
L
Linus Torvalds 已提交
656 657 658 659 660
		pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
		pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
	}

	if (dma_status & DMASCR_MASTER_ABT) {
J
Jeff Garzik 已提交
661
		dev_printk(KERN_ERR, &card->dev->dev, "MASTER ABORT\n");
L
Linus Torvalds 已提交
662 663 664 665 666 667 668 669 670 671
		pci_read_config_word(card->dev, PCI_STATUS, &cfg_status);
		pci_write_config_word(card->dev, PCI_STATUS, cfg_status);
	}

	/* and process the DMA descriptors */
	card->dma_status = dma_status;
	tasklet_schedule(&card->tasklet);

HW_TRACE(0x36);

672
	return IRQ_HANDLED;
L
Linus Torvalds 已提交
673
}
674

L
Linus Torvalds 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
/*
 * If both batteries are good, no LED
 * If either battery has been warned, solid LED
 * If both batteries are bad, flash the LED quickly
 * If either battery is bad, flash the LED semi quickly
 */
static void set_fault_to_battery_status(struct cardinfo *card)
{
	if (card->battery[0].good && card->battery[1].good)
		set_led(card, LED_FAULT, LED_OFF);
	else if (card->battery[0].warned || card->battery[1].warned)
		set_led(card, LED_FAULT, LED_ON);
	else if (!card->battery[0].good && !card->battery[1].good)
		set_led(card, LED_FAULT, LED_FLASH_7_0);
	else
		set_led(card, LED_FAULT, LED_FLASH_3_5);
}

static void init_battery_timer(void);

static int check_battery(struct cardinfo *card, int battery, int status)
{
	if (status != card->battery[battery].good) {
		card->battery[battery].good = !card->battery[battery].good;
		card->battery[battery].last_change = jiffies;

		if (card->battery[battery].good) {
J
Jeff Garzik 已提交
702 703
			dev_printk(KERN_ERR, &card->dev->dev,
				"Battery %d now good\n", battery + 1);
L
Linus Torvalds 已提交
704 705
			card->battery[battery].warned = 0;
		} else
J
Jeff Garzik 已提交
706 707
			dev_printk(KERN_ERR, &card->dev->dev,
				"Battery %d now FAILED\n", battery + 1);
L
Linus Torvalds 已提交
708 709 710 711 712 713

		return 1;
	} else if (!card->battery[battery].good &&
		   !card->battery[battery].warned &&
		   time_after_eq(jiffies, card->battery[battery].last_change +
				 (HZ * 60 * 60 * 5))) {
J
Jeff Garzik 已提交
714 715
		dev_printk(KERN_ERR, &card->dev->dev,
			"Battery %d still FAILED after 5 hours\n", battery + 1);
L
Linus Torvalds 已提交
716 717 718 719 720 721 722
		card->battery[battery].warned = 1;

		return 1;
	}

	return 0;
}
723

L
Linus Torvalds 已提交
724 725 726 727 728 729 730 731 732 733 734
static void check_batteries(struct cardinfo *card)
{
	/* NOTE: this must *never* be called while the card
	 * is doing (bus-to-card) DMA, or you will need the
	 * reset switch
	 */
	unsigned char status;
	int ret1, ret2;

	status = readb(card->csr_remap + MEMCTRLSTATUS_BATTERY);
	if (debug & DEBUG_BATTERY_POLLING)
J
Jeff Garzik 已提交
735 736
		dev_printk(KERN_DEBUG, &card->dev->dev,
			"checking battery status, 1 = %s, 2 = %s\n",
L
Linus Torvalds 已提交
737 738 739 740 741 742 743 744 745 746 747 748 749 750
		       (status & BATTERY_1_FAILURE) ? "FAILURE" : "OK",
		       (status & BATTERY_2_FAILURE) ? "FAILURE" : "OK");

	ret1 = check_battery(card, 0, !(status & BATTERY_1_FAILURE));
	ret2 = check_battery(card, 1, !(status & BATTERY_2_FAILURE));

	if (ret1 || ret2)
		set_fault_to_battery_status(card);
}

static void check_all_batteries(unsigned long ptr)
{
	int i;

751
	for (i = 0; i < num_cards; i++)
L
Linus Torvalds 已提交
752 753 754 755 756 757 758 759 760 761 762 763
		if (!(cards[i].flags & UM_FLAG_NO_BATT)) {
			struct cardinfo *card = &cards[i];
			spin_lock_bh(&card->lock);
			if (card->Active >= 0)
				card->check_batteries = 1;
			else
				check_batteries(card);
			spin_unlock_bh(&card->lock);
		}

	init_battery_timer();
}
764

L
Linus Torvalds 已提交
765 766 767 768 769 770 771
static void init_battery_timer(void)
{
	init_timer(&battery_timer);
	battery_timer.function = check_all_batteries;
	battery_timer.expires = jiffies + (HZ * 60);
	add_timer(&battery_timer);
}
772

L
Linus Torvalds 已提交
773 774 775 776
static void del_battery_timer(void)
{
	del_timer(&battery_timer);
}
777

L
Linus Torvalds 已提交
778 779 780 781 782 783 784 785 786 787 788 789 790 791
/*
 * Note no locks taken out here.  In a worst case scenario, we could drop
 * a chunk of system memory.  But that should never happen, since validation
 * happens at open or mount time, when locks are held.
 *
 *	That's crap, since doing that while some partitions are opened
 * or mounted will give you really nasty results.
 */
static int mm_revalidate(struct gendisk *disk)
{
	struct cardinfo *card = disk->private_data;
	set_capacity(disk, card->mm_size << 1);
	return 0;
}
792 793

static int mm_getgeo(struct block_device *bdev, struct hd_geometry *geo)
L
Linus Torvalds 已提交
794
{
795 796
	struct cardinfo *card = bdev->bd_disk->private_data;
	int size = card->mm_size * (1024 / MM_HARDSECT);
L
Linus Torvalds 已提交
797

798 799 800 801 802 803 804 805 806
	/*
	 * get geometry: we have to fake one...  trim the size to a
	 * multiple of 2048 (1M): tell we have 32 sectors, 64 heads,
	 * whatever cylinders.
	 */
	geo->heads     = 64;
	geo->sectors   = 32;
	geo->cylinders = size / (geo->heads * geo->sectors);
	return 0;
L
Linus Torvalds 已提交
807
}
808

809
static const struct block_device_operations mm_fops = {
L
Linus Torvalds 已提交
810
	.owner		= THIS_MODULE,
811
	.getgeo		= mm_getgeo,
812
	.revalidate_disk = mm_revalidate,
L
Linus Torvalds 已提交
813
};
814 815 816

static int __devinit mm_pci_probe(struct pci_dev *dev,
				const struct pci_device_id *id)
L
Linus Torvalds 已提交
817 818 819 820 821 822
{
	int ret = -ENODEV;
	struct cardinfo *card = &cards[num_cards];
	unsigned char	mem_present;
	unsigned char	batt_status;
	unsigned int	saved_bar, data;
J
Jeff Garzik 已提交
823 824
	unsigned long	csr_base;
	unsigned long	csr_len;
L
Linus Torvalds 已提交
825
	int		magic_number;
J
Jeff Garzik 已提交
826 827 828 829
	static int	printed_version;

	if (!printed_version++)
		printk(KERN_INFO DRIVER_VERSION " : " DRIVER_DESC "\n");
L
Linus Torvalds 已提交
830

J
Jeff Garzik 已提交
831 832 833
	ret = pci_enable_device(dev);
	if (ret)
		return ret;
L
Linus Torvalds 已提交
834 835 836 837 838 839

	pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0xF8);
	pci_set_master(dev);

	card->dev         = dev;

J
Jeff Garzik 已提交
840 841 842 843
	csr_base = pci_resource_start(dev, 0);
	csr_len  = pci_resource_len(dev, 0);
	if (!csr_base || !csr_len)
		return -ENODEV;
L
Linus Torvalds 已提交
844

J
Jeff Garzik 已提交
845
	dev_printk(KERN_INFO, &dev->dev,
846
	  "Micro Memory(tm) controller found (PCI Mem Module (Battery Backup))\n");
L
Linus Torvalds 已提交
847

848
	if (pci_set_dma_mask(dev, DMA_BIT_MASK(64)) &&
849
	    pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
J
Jeff Garzik 已提交
850
		dev_printk(KERN_WARNING, &dev->dev, "NO suitable DMA found\n");
L
Linus Torvalds 已提交
851 852
		return  -ENOMEM;
	}
J
Jeff Garzik 已提交
853 854 855

	ret = pci_request_regions(dev, DRIVER_NAME);
	if (ret) {
J
Jeff Garzik 已提交
856 857
		dev_printk(KERN_ERR, &card->dev->dev,
			"Unable to request memory region\n");
L
Linus Torvalds 已提交
858 859 860
		goto failed_req_csr;
	}

J
Jeff Garzik 已提交
861
	card->csr_remap = ioremap_nocache(csr_base, csr_len);
L
Linus Torvalds 已提交
862
	if (!card->csr_remap) {
J
Jeff Garzik 已提交
863 864
		dev_printk(KERN_ERR, &card->dev->dev,
			"Unable to remap memory region\n");
L
Linus Torvalds 已提交
865 866 867 868 869
		ret = -ENOMEM;

		goto failed_remap_csr;
	}

J
Jeff Garzik 已提交
870 871
	dev_printk(KERN_INFO, &card->dev->dev,
		"CSR 0x%08lx -> 0x%p (0x%lx)\n",
J
Jeff Garzik 已提交
872
	       csr_base, card->csr_remap, csr_len);
L
Linus Torvalds 已提交
873

874
	switch (card->dev->device) {
L
Linus Torvalds 已提交
875 876 877 878 879 880 881 882 883 884 885
	case 0x5415:
		card->flags |= UM_FLAG_NO_BYTE_STATUS | UM_FLAG_NO_BATTREG;
		magic_number = 0x59;
		break;

	case 0x5425:
		card->flags |= UM_FLAG_NO_BYTE_STATUS;
		magic_number = 0x5C;
		break;

	case 0x6155:
886 887
		card->flags |= UM_FLAG_NO_BYTE_STATUS |
				UM_FLAG_NO_BATTREG | UM_FLAG_NO_BATT;
L
Linus Torvalds 已提交
888 889 890 891 892 893 894 895 896
		magic_number = 0x99;
		break;

	default:
		magic_number = 0x100;
		break;
	}

	if (readb(card->csr_remap + MEMCTRLSTATUS_MAGIC) != magic_number) {
J
Jeff Garzik 已提交
897
		dev_printk(KERN_ERR, &card->dev->dev, "Magic number invalid\n");
L
Linus Torvalds 已提交
898 899 900 901 902
		ret = -ENOMEM;
		goto failed_magic;
	}

	card->mm_pages[0].desc = pci_alloc_consistent(card->dev,
903 904
						PAGE_SIZE * 2,
						&card->mm_pages[0].page_dma);
L
Linus Torvalds 已提交
905
	card->mm_pages[1].desc = pci_alloc_consistent(card->dev,
906 907
						PAGE_SIZE * 2,
						&card->mm_pages[1].page_dma);
L
Linus Torvalds 已提交
908 909
	if (card->mm_pages[0].desc == NULL ||
	    card->mm_pages[1].desc == NULL) {
J
Jeff Garzik 已提交
910
		dev_printk(KERN_ERR, &card->dev->dev, "alloc failed\n");
L
Linus Torvalds 已提交
911 912 913 914 915 916 917 918 919 920 921 922 923 924
		goto failed_alloc;
	}
	reset_page(&card->mm_pages[0]);
	reset_page(&card->mm_pages[1]);
	card->Ready = 0;	/* page 0 is ready */
	card->Active = -1;	/* no page is active */
	card->bio = NULL;
	card->biotail = &card->bio;

	card->queue = blk_alloc_queue(GFP_KERNEL);
	if (!card->queue)
		goto failed_alloc;

	blk_queue_make_request(card->queue, mm_make_request);
S
Sage Weil 已提交
925
	card->queue->queue_lock = &card->lock;
L
Linus Torvalds 已提交
926 927 928 929 930
	card->queue->queuedata = card;

	tasklet_init(&card->tasklet, process_page, (unsigned long)card);

	card->check_batteries = 0;
931

L
Linus Torvalds 已提交
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
	mem_present = readb(card->csr_remap + MEMCTRLSTATUS_MEMORY);
	switch (mem_present) {
	case MEM_128_MB:
		card->mm_size = 1024 * 128;
		break;
	case MEM_256_MB:
		card->mm_size = 1024 * 256;
		break;
	case MEM_512_MB:
		card->mm_size = 1024 * 512;
		break;
	case MEM_1_GB:
		card->mm_size = 1024 * 1024;
		break;
	case MEM_2_GB:
		card->mm_size = 1024 * 2048;
		break;
	default:
		card->mm_size = 0;
		break;
	}

	/* Clear the LED's we control */
	set_led(card, LED_REMOVE, LED_OFF);
	set_led(card, LED_FAULT, LED_OFF);

	batt_status = readb(card->csr_remap + MEMCTRLSTATUS_BATTERY);

	card->battery[0].good = !(batt_status & BATTERY_1_FAILURE);
	card->battery[1].good = !(batt_status & BATTERY_2_FAILURE);
	card->battery[0].last_change = card->battery[1].last_change = jiffies;

964
	if (card->flags & UM_FLAG_NO_BATT)
J
Jeff Garzik 已提交
965 966
		dev_printk(KERN_INFO, &card->dev->dev,
			"Size %d KB\n", card->mm_size);
L
Linus Torvalds 已提交
967
	else {
J
Jeff Garzik 已提交
968 969 970
		dev_printk(KERN_INFO, &card->dev->dev,
			"Size %d KB, Battery 1 %s (%s), Battery 2 %s (%s)\n",
		       card->mm_size,
971
		       batt_status & BATTERY_1_DISABLED ? "Disabled" : "Enabled",
L
Linus Torvalds 已提交
972
		       card->battery[0].good ? "OK" : "FAILURE",
973
		       batt_status & BATTERY_2_DISABLED ? "Disabled" : "Enabled",
L
Linus Torvalds 已提交
974 975 976 977 978 979 980 981 982 983 984 985 986 987
		       card->battery[1].good ? "OK" : "FAILURE");

		set_fault_to_battery_status(card);
	}

	pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &saved_bar);
	data = 0xffffffff;
	pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, data);
	pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &data);
	pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, saved_bar);
	data &= 0xfffffff0;
	data = ~data;
	data += 1;

988 989
	if (request_irq(dev->irq, mm_interrupt, IRQF_SHARED, DRIVER_NAME,
			card)) {
J
Jeff Garzik 已提交
990 991
		dev_printk(KERN_ERR, &card->dev->dev,
			"Unable to allocate IRQ\n");
L
Linus Torvalds 已提交
992 993 994 995
		ret = -ENODEV;
		goto failed_req_irq;
	}

J
Jeff Garzik 已提交
996
	dev_printk(KERN_INFO, &card->dev->dev,
J
Jeff Garzik 已提交
997
		"Window size %d bytes, IRQ %d\n", data, dev->irq);
L
Linus Torvalds 已提交
998

999
	spin_lock_init(&card->lock);
L
Linus Torvalds 已提交
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016

	pci_set_drvdata(dev, card);

	if (pci_write_cmd != 0x0F) 	/* If not Memory Write & Invalidate */
		pci_write_cmd = 0x07;	/* then Memory Write command */

	if (pci_write_cmd & 0x08) { /* use Memory Write and Invalidate */
		unsigned short cfg_command;
		pci_read_config_word(dev, PCI_COMMAND, &cfg_command);
		cfg_command |= 0x10; /* Memory Write & Invalidate Enable */
		pci_write_config_word(dev, PCI_COMMAND, cfg_command);
	}
	pci_cmds = (pci_read_cmd << 28) | (pci_write_cmd << 24);

	num_cards++;

	if (!get_userbit(card, MEMORY_INITIALIZED)) {
J
Jeff Garzik 已提交
1017
		dev_printk(KERN_INFO, &card->dev->dev,
1018
		  "memory NOT initialized. Consider over-writing whole device.\n");
L
Linus Torvalds 已提交
1019 1020
		card->init_size = 0;
	} else {
J
Jeff Garzik 已提交
1021 1022
		dev_printk(KERN_INFO, &card->dev->dev,
			"memory already initialized\n");
L
Linus Torvalds 已提交
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
		card->init_size = card->mm_size;
	}

	/* Enable ECC */
	writeb(EDC_STORE_CORRECT, card->csr_remap + MEMCTRLCMD_ERRCTRL);

	return 0;

 failed_req_irq:
 failed_alloc:
	if (card->mm_pages[0].desc)
		pci_free_consistent(card->dev, PAGE_SIZE*2,
				    card->mm_pages[0].desc,
				    card->mm_pages[0].page_dma);
	if (card->mm_pages[1].desc)
		pci_free_consistent(card->dev, PAGE_SIZE*2,
				    card->mm_pages[1].desc,
				    card->mm_pages[1].page_dma);
 failed_magic:
	iounmap(card->csr_remap);
 failed_remap_csr:
J
Jeff Garzik 已提交
1044
	pci_release_regions(dev);
L
Linus Torvalds 已提交
1045 1046 1047 1048
 failed_req_csr:

	return ret;
}
1049

L
Linus Torvalds 已提交
1050 1051 1052 1053 1054
static void mm_pci_remove(struct pci_dev *dev)
{
	struct cardinfo *card = pci_get_drvdata(dev);

	tasklet_kill(&card->tasklet);
J
Jeff Garzik 已提交
1055
	free_irq(dev->irq, card);
L
Linus Torvalds 已提交
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
	iounmap(card->csr_remap);

	if (card->mm_pages[0].desc)
		pci_free_consistent(card->dev, PAGE_SIZE*2,
				    card->mm_pages[0].desc,
				    card->mm_pages[0].page_dma);
	if (card->mm_pages[1].desc)
		pci_free_consistent(card->dev, PAGE_SIZE*2,
				    card->mm_pages[1].desc,
				    card->mm_pages[1].page_dma);
1066
	blk_cleanup_queue(card->queue);
J
Jeff Garzik 已提交
1067 1068 1069

	pci_release_regions(dev);
	pci_disable_device(dev);
L
Linus Torvalds 已提交
1070 1071
}

1072
static const struct pci_device_id mm_pci_ids[] = {
1073 1074 1075
    {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY, PCI_DEVICE_ID_MICRO_MEMORY_5415CN)},
    {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY, PCI_DEVICE_ID_MICRO_MEMORY_5425CN)},
    {PCI_DEVICE(PCI_VENDOR_ID_MICRO_MEMORY, PCI_DEVICE_ID_MICRO_MEMORY_6155)},
1076
    {
L
Linus Torvalds 已提交
1077 1078
	.vendor	=	0x8086,
	.device	=	0xB555,
1079 1080 1081 1082
	.subvendor =	0x1332,
	.subdevice =	0x5460,
	.class =	0x050000,
	.class_mask =	0,
1083
    }, { /* end: all zeroes */ }
L
Linus Torvalds 已提交
1084 1085 1086 1087 1088
};

MODULE_DEVICE_TABLE(pci, mm_pci_ids);

static struct pci_driver mm_pci_driver = {
J
Jeff Garzik 已提交
1089 1090 1091 1092
	.name		= DRIVER_NAME,
	.id_table	= mm_pci_ids,
	.probe		= mm_pci_probe,
	.remove		= mm_pci_remove,
L
Linus Torvalds 已提交
1093
};
J
Jeff Garzik 已提交
1094

L
Linus Torvalds 已提交
1095 1096 1097 1098 1099
static int __init mm_init(void)
{
	int retval, i;
	int err;

1100
	retval = pci_register_driver(&mm_pci_driver);
L
Linus Torvalds 已提交
1101 1102 1103
	if (retval)
		return -ENOMEM;

1104
	err = major_nr = register_blkdev(0, DRIVER_NAME);
1105 1106
	if (err < 0) {
		pci_unregister_driver(&mm_pci_driver);
L
Linus Torvalds 已提交
1107
		return -EIO;
1108
	}
L
Linus Torvalds 已提交
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129

	for (i = 0; i < num_cards; i++) {
		mm_gendisk[i] = alloc_disk(1 << MM_SHIFT);
		if (!mm_gendisk[i])
			goto out;
	}

	for (i = 0; i < num_cards; i++) {
		struct gendisk *disk = mm_gendisk[i];
		sprintf(disk->disk_name, "umem%c", 'a'+i);
		spin_lock_init(&cards[i].lock);
		disk->major = major_nr;
		disk->first_minor  = i << MM_SHIFT;
		disk->fops = &mm_fops;
		disk->private_data = &cards[i];
		disk->queue = cards[i].queue;
		set_capacity(disk, cards[i].mm_size << 1);
		add_disk(disk);
	}

	init_battery_timer();
J
Jeff Garzik 已提交
1130
	printk(KERN_INFO "MM: desc_per_page = %ld\n", DESC_PER_PAGE);
L
Linus Torvalds 已提交
1131 1132 1133 1134
/* printk("mm_init: Done. 10-19-01 9:00\n"); */
	return 0;

out:
1135
	pci_unregister_driver(&mm_pci_driver);
1136
	unregister_blkdev(major_nr, DRIVER_NAME);
L
Linus Torvalds 已提交
1137 1138 1139 1140
	while (i--)
		put_disk(mm_gendisk[i]);
	return -ENOMEM;
}
1141

L
Linus Torvalds 已提交
1142 1143 1144 1145 1146 1147
static void __exit mm_cleanup(void)
{
	int i;

	del_battery_timer();

1148
	for (i = 0; i < num_cards ; i++) {
L
Linus Torvalds 已提交
1149 1150 1151 1152 1153 1154
		del_gendisk(mm_gendisk[i]);
		put_disk(mm_gendisk[i]);
	}

	pci_unregister_driver(&mm_pci_driver);

1155
	unregister_blkdev(major_nr, DRIVER_NAME);
L
Linus Torvalds 已提交
1156 1157 1158 1159 1160 1161 1162 1163
}

module_init(mm_init);
module_exit(mm_cleanup);

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");