Blame view

drivers/block/hd.c 18.9 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  /*
   *  Copyright (C) 1991, 1992  Linus Torvalds
   *
   * This is the low-level hd interrupt support. It traverses the
   * request-list, using interrupts to jump between functions. As
   * all the functions are called within interrupts, we may not
   * sleep. Special care is recommended.
   *
   *  modified by Drew Eckhardt to check nr of hd's from the CMOS.
   *
   *  Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
   *  in the early extended-partition checks and added DM partitions
   *
   *  IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
   *  and general streamlining by Mark Lord.
   *
   *  Removed 99% of above. Use Mark's ide driver for those options.
   *  This is now a lightweight ST-506 driver. (Paul Gortmaker)
   *
   *  Modified 1995 Russell King for ARM processor.
   *
   *  Bugfix: max_sectors must be <= 255 or the wheels tend to come
   *  off in a hurry once you queue things up - Paul G. 02/2001
   */
  
  /* Uncomment the following if you want verbose error reports. */
  /* #define VERBOSE_ERRORS */
  
  #include <linux/blkdev.h>
  #include <linux/errno.h>
  #include <linux/signal.h>
  #include <linux/interrupt.h>
  #include <linux/timer.h>
  #include <linux/fs.h>
  #include <linux/kernel.h>
  #include <linux/genhd.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
38
  #include <linux/string.h>
  #include <linux/ioport.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
40
  #include <linux/init.h>
  #include <linux/blkpg.h>
f26b3d759   Bartlomiej Zolnierkiewicz   hd: WIN_* -> ATA_...
41
  #include <linux/ata.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
42
  #include <linux/hdreg.h>
4fe6e3064   Bartlomiej Zolnierkiewicz   include/linux/hdr...
43
  #define HD_IRQ 14
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44
  #define REALLY_SLOW_IO
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  #include <asm/io.h>
  #include <asm/uaccess.h>
  
  #ifdef __arm__
  #undef  HD_IRQ
  #endif
  #include <asm/irq.h>
  #ifdef __arm__
  #define HD_IRQ IRQ_HARDDISK
  #endif
  
  /* Hd controller regster ports */
  
  #define HD_DATA		0x1f0		/* _CTL when writing */
  #define HD_ERROR	0x1f1		/* see err-bits */
  #define HD_NSECTOR	0x1f2		/* nr of sectors to read/write */
  #define HD_SECTOR	0x1f3		/* starting sector */
  #define HD_LCYL		0x1f4		/* starting cylinder */
  #define HD_HCYL		0x1f5		/* high byte of starting cyl */
  #define HD_CURRENT	0x1f6		/* 101dhhhh , d=drive, hhhh=head */
  #define HD_STATUS	0x1f7		/* see status-bits */
  #define HD_FEATURE	HD_ERROR	/* same io address, read=error, write=feature */
  #define HD_PRECOMP	HD_FEATURE	/* obsolete use of this port - predates IDE */
  #define HD_COMMAND	HD_STATUS	/* same io address, read=status, write=cmd */
  
  #define HD_CMD		0x3f6		/* used for resets */
  #define HD_ALTSTATUS	0x3f6		/* same as HD_STATUS but doesn't clear irq */
  
  /* Bits of HD_STATUS */
  #define ERR_STAT		0x01
  #define INDEX_STAT		0x02
  #define ECC_STAT		0x04	/* Corrected error */
  #define DRQ_STAT		0x08
  #define SEEK_STAT		0x10
  #define SERVICE_STAT		SEEK_STAT
  #define WRERR_STAT		0x20
  #define READY_STAT		0x40
  #define BUSY_STAT		0x80
  
  /* Bits for HD_ERROR */
  #define MARK_ERR		0x01	/* Bad address mark */
  #define TRK0_ERR		0x02	/* couldn't find track 0 */
  #define ABRT_ERR		0x04	/* Command aborted */
  #define MCR_ERR			0x08	/* media change request */
  #define ID_ERR			0x10	/* ID field not found */
  #define MC_ERR			0x20	/* media changed */
  #define ECC_ERR			0x40	/* Uncorrectable ECC error */
  #define BBD_ERR			0x80	/* pre-EIDE meaning:  block marked bad */
  #define ICRC_ERR		0x80	/* new meaning:  CRC error during transfer */
  
  static DEFINE_SPINLOCK(hd_lock);
  static struct request_queue *hd_queue;
8a12c4a45   Tejun Heo   hd: dequeue and t...
97
  static struct request *hd_req;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  #define TIMEOUT_VALUE	(6*HZ)
  #define	HD_DELAY	0
  
  #define MAX_ERRORS     16	/* Max read/write errors/sector */
  #define RESET_FREQ      8	/* Reset controller every 8th retry */
  #define RECAL_FREQ      4	/* Recalibrate every 4th retry */
  #define MAX_HD		2
  
  #define STAT_OK		(READY_STAT|SEEK_STAT)
  #define OK_STATUS(s)	(((s)&(STAT_OK|(BUSY_STAT|WRERR_STAT|ERR_STAT)))==STAT_OK)
  
  static void recal_intr(void);
  static void bad_rw_intr(void);
  
  static int reset;
  static int hd_error;
  
  /*
   *  This struct defines the HD's and their types.
   */
  struct hd_i_struct {
ec29782ba   Paolo Ciarrocchi   IDE: Coding Style...
120
  	unsigned int head, sect, cyl, wpcom, lzone, ctl;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121
122
123
124
  	int unit;
  	int recalibrate;
  	int special_op;
  };
ec29782ba   Paolo Ciarrocchi   IDE: Coding Style...
125

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
127
  #ifdef HD_TYPE
  static struct hd_i_struct hd_info[] = { HD_TYPE };
ecea57309   Andi Drebes   drivers/ide/legac...
128
  static int NR_HD = ARRAY_SIZE(hd_info);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  #else
  static struct hd_i_struct hd_info[MAX_HD];
  static int NR_HD;
  #endif
  
  static struct gendisk *hd_gendisk[MAX_HD];
  
  static struct timer_list device_timer;
  
  #define TIMEOUT_VALUE (6*HZ)
  
  #define SET_TIMER							\
  	do {								\
  		mod_timer(&device_timer, jiffies + TIMEOUT_VALUE);	\
  	} while (0)
  
  static void (*do_hd)(void) = NULL;
  #define SET_HANDLER(x) \
  if ((do_hd = (x)) != NULL) \
  	SET_TIMER; \
  else \
  	del_timer(&device_timer);
  
  
  #if (HD_DELAY > 0)
306e440da   Ingo Molnar   [PATCH] x86: i825...
154

334955ef9   Ralf Baechle   i8253: Create lin...
155
  #include <linux/i8253.h>
306e440da   Ingo Molnar   [PATCH] x86: i825...
156

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157
158
159
160
  unsigned long last_req;
  
  unsigned long read_timer(void)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
161
162
  	unsigned long t, flags;
  	int i;
ced918eb7   Thomas Gleixner   i8253: Convert i8...
163
  	raw_spin_lock_irqsave(&i8253_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
164
  	t = jiffies * 11932;
ec29782ba   Paolo Ciarrocchi   IDE: Coding Style...
165
  	outb_p(0, 0x43);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
166
167
  	i = inb_p(0x40);
  	i |= inb(0x40) << 8;
ced918eb7   Thomas Gleixner   i8253: Convert i8...
168
  	raw_spin_unlock_irqrestore(&i8253_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
169
170
171
172
173
174
175
176
177
178
179
  	return(t - i);
  }
  #endif
  
  static void __init hd_setup(char *str, int *ints)
  {
  	int hdind = 0;
  
  	if (ints[0] != 3)
  		return;
  	if (hd_info[0].head != 0)
ec29782ba   Paolo Ciarrocchi   IDE: Coding Style...
180
  		hdind = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
181
182
183
184
185
186
187
188
  	hd_info[hdind].head = ints[2];
  	hd_info[hdind].sect = ints[3];
  	hd_info[hdind].cyl = ints[1];
  	hd_info[hdind].wpcom = 0;
  	hd_info[hdind].lzone = ints[1];
  	hd_info[hdind].ctl = (ints[2] > 8 ? 8 : 0);
  	NR_HD = hdind+1;
  }
8a12c4a45   Tejun Heo   hd: dequeue and t...
189
190
191
192
193
194
195
196
197
198
199
200
  static bool hd_end_request(int err, unsigned int bytes)
  {
  	if (__blk_end_request(hd_req, err, bytes))
  		return true;
  	hd_req = NULL;
  	return false;
  }
  
  static bool hd_end_request_cur(int err)
  {
  	return hd_end_request(err, blk_rq_cur_bytes(hd_req));
  }
ec29782ba   Paolo Ciarrocchi   IDE: Coding Style...
201
  static void dump_status(const char *msg, unsigned int stat)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
202
203
  {
  	char *name = "hd?";
8a12c4a45   Tejun Heo   hd: dequeue and t...
204
205
  	if (hd_req)
  		name = hd_req->rq_disk->disk_name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
  
  #ifdef VERBOSE_ERRORS
  	printk("%s: %s: status=0x%02x { ", name, msg, stat & 0xff);
  	if (stat & BUSY_STAT)	printk("Busy ");
  	if (stat & READY_STAT)	printk("DriveReady ");
  	if (stat & WRERR_STAT)	printk("WriteFault ");
  	if (stat & SEEK_STAT)	printk("SeekComplete ");
  	if (stat & DRQ_STAT)	printk("DataRequest ");
  	if (stat & ECC_STAT)	printk("CorrectedError ");
  	if (stat & INDEX_STAT)	printk("Index ");
  	if (stat & ERR_STAT)	printk("Error ");
  	printk("}
  ");
  	if ((stat & ERR_STAT) == 0) {
  		hd_error = 0;
  	} else {
  		hd_error = inb(HD_ERROR);
  		printk("%s: %s: error=0x%02x { ", name, msg, hd_error & 0xff);
  		if (hd_error & BBD_ERR)		printk("BadSector ");
  		if (hd_error & ECC_ERR)		printk("UncorrectableError ");
  		if (hd_error & ID_ERR)		printk("SectorIdNotFound ");
  		if (hd_error & ABRT_ERR)	printk("DriveStatusError ");
  		if (hd_error & TRK0_ERR)	printk("TrackZeroNotFound ");
  		if (hd_error & MARK_ERR)	printk("AddrMarkNotFound ");
  		printk("}");
  		if (hd_error & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
  			printk(", CHS=%d/%d/%d", (inb(HD_HCYL)<<8) + inb(HD_LCYL),
  				inb(HD_CURRENT) & 0xf, inb(HD_SECTOR));
8a12c4a45   Tejun Heo   hd: dequeue and t...
234
235
  			if (hd_req)
  				printk(", sector=%ld", blk_rq_pos(hd_req));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
  		}
  		printk("
  ");
  	}
  #else
  	printk("%s: %s: status=0x%02x.
  ", name, msg, stat & 0xff);
  	if ((stat & ERR_STAT) == 0) {
  		hd_error = 0;
  	} else {
  		hd_error = inb(HD_ERROR);
  		printk("%s: %s: error=0x%02x.
  ", name, msg, hd_error & 0xff);
  	}
  #endif
  }
  
  static void check_status(void)
  {
  	int i = inb_p(HD_STATUS);
  
  	if (!OK_STATUS(i)) {
  		dump_status("check_status", i);
  		bad_rw_intr();
  	}
  }
  
  static int controller_busy(void)
  {
  	int retries = 100000;
  	unsigned char status;
  
  	do {
  		status = inb_p(HD_STATUS);
  	} while ((status & BUSY_STAT) && --retries);
  	return status;
  }
  
  static int status_ok(void)
  {
  	unsigned char status = inb_p(HD_STATUS);
  
  	if (status & BUSY_STAT)
  		return 1;	/* Ancient, but does it make sense??? */
  	if (status & WRERR_STAT)
  		return 0;
  	if (!(status & READY_STAT))
  		return 0;
  	if (!(status & SEEK_STAT))
  		return 0;
  	return 1;
  }
  
  static int controller_ready(unsigned int drive, unsigned int head)
  {
  	int retry = 100;
  
  	do {
  		if (controller_busy() & BUSY_STAT)
  			return 0;
  		outb_p(0xA0 | (drive<<4) | head, HD_CURRENT);
  		if (status_ok())
  			return 1;
  	} while (--retry);
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
  static void hd_out(struct hd_i_struct *disk,
  		   unsigned int nsect,
  		   unsigned int sect,
  		   unsigned int head,
  		   unsigned int cyl,
  		   unsigned int cmd,
  		   void (*intr_addr)(void))
  {
  	unsigned short port;
  
  #if (HD_DELAY > 0)
  	while (read_timer() - last_req < HD_DELAY)
  		/* nothing */;
  #endif
  	if (reset)
  		return;
  	if (!controller_ready(disk->unit, head)) {
  		reset = 1;
  		return;
  	}
  	SET_HANDLER(intr_addr);
ec29782ba   Paolo Ciarrocchi   IDE: Coding Style...
323
324
325
326
327
328
329
330
331
  	outb_p(disk->ctl, HD_CMD);
  	port = HD_DATA;
  	outb_p(disk->wpcom >> 2, ++port);
  	outb_p(nsect, ++port);
  	outb_p(sect, ++port);
  	outb_p(cyl, ++port);
  	outb_p(cyl >> 8, ++port);
  	outb_p(0xA0 | (disk->unit << 4) | head, ++port);
  	outb_p(cmd, ++port);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
  }
  
  static void hd_request (void);
  
  static int drive_busy(void)
  {
  	unsigned int i;
  	unsigned char c;
  
  	for (i = 0; i < 500000 ; i++) {
  		c = inb_p(HD_STATUS);
  		if ((c & (BUSY_STAT | READY_STAT | SEEK_STAT)) == STAT_OK)
  			return 0;
  	}
  	dump_status("reset timed out", c);
  	return 1;
  }
  
  static void reset_controller(void)
  {
  	int	i;
ec29782ba   Paolo Ciarrocchi   IDE: Coding Style...
353
354
355
356
  	outb_p(4, HD_CMD);
  	for (i = 0; i < 1000; i++) barrier();
  	outb_p(hd_info[0].ctl & 0x0f, HD_CMD);
  	for (i = 0; i < 1000; i++) barrier();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
357
358
359
360
  	if (drive_busy())
  		printk("hd: controller still busy
  ");
  	else if ((hd_error = inb(HD_ERROR)) != 1)
ec29782ba   Paolo Ciarrocchi   IDE: Coding Style...
361
362
  		printk("hd: controller reset failed: %02x
  ", hd_error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
  }
  
  static void reset_hd(void)
  {
  	static int i;
  
  repeat:
  	if (reset) {
  		reset = 0;
  		i = -1;
  		reset_controller();
  	} else {
  		check_status();
  		if (reset)
  			goto repeat;
  	}
  	if (++i < NR_HD) {
  		struct hd_i_struct *disk = &hd_info[i];
  		disk->special_op = disk->recalibrate = 1;
ec29782ba   Paolo Ciarrocchi   IDE: Coding Style...
382
  		hd_out(disk, disk->sect, disk->sect, disk->head-1,
f26b3d759   Bartlomiej Zolnierkiewicz   hd: WIN_* -> ATA_...
383
  			disk->cyl, ATA_CMD_INIT_DEV_PARAMS, &reset_hd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
  		if (reset)
  			goto repeat;
  	} else
  		hd_request();
  }
  
  /*
   * Ok, don't know what to do with the unexpected interrupts: on some machines
   * doing a reset and a retry seems to result in an eternal loop. Right now I
   * ignore it, and just set the timeout.
   *
   * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
   * drive enters "idle", "standby", or "sleep" mode, so if the status looks
   * "good", we just ignore the interrupt completely.
   */
  static void unexpected_hd_interrupt(void)
  {
  	unsigned int stat = inb_p(HD_STATUS);
  
  	if (stat & (BUSY_STAT|DRQ_STAT|ECC_STAT|ERR_STAT)) {
ec29782ba   Paolo Ciarrocchi   IDE: Coding Style...
404
  		dump_status("unexpected interrupt", stat);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
405
406
407
408
409
410
411
412
413
414
415
  		SET_TIMER;
  	}
  }
  
  /*
   * bad_rw_intr() now tries to be a bit smarter and does things
   * according to the error returned by the controller.
   * -Mika Liljeberg (liljeber@cs.Helsinki.FI)
   */
  static void bad_rw_intr(void)
  {
8a12c4a45   Tejun Heo   hd: dequeue and t...
416
  	struct request *req = hd_req;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
417
418
419
  	if (req != NULL) {
  		struct hd_i_struct *disk = req->rq_disk->private_data;
  		if (++req->errors >= MAX_ERRORS || (hd_error & BBD_ERR)) {
8a12c4a45   Tejun Heo   hd: dequeue and t...
420
  			hd_end_request_cur(-EIO);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
421
422
423
424
425
426
427
428
429
430
431
  			disk->special_op = disk->recalibrate = 1;
  		} else if (req->errors % RESET_FREQ == 0)
  			reset = 1;
  		else if ((hd_error & TRK0_ERR) || req->errors % RECAL_FREQ == 0)
  			disk->special_op = disk->recalibrate = 1;
  		/* Otherwise just retry */
  	}
  }
  
  static inline int wait_DRQ(void)
  {
b004223db   Andrew Morton   drivers/ide/legac...
432
433
  	int retries;
  	int stat;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
434

b004223db   Andrew Morton   drivers/ide/legac...
435
436
437
  	for (retries = 0; retries < 100000; retries++) {
  		stat = inb_p(HD_STATUS);
  		if (stat & DRQ_STAT)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
438
  			return 0;
b004223db   Andrew Morton   drivers/ide/legac...
439
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
  	dump_status("wait_DRQ", stat);
  	return -1;
  }
  
  static void read_intr(void)
  {
  	struct request *req;
  	int i, retries = 100000;
  
  	do {
  		i = (unsigned) inb_p(HD_STATUS);
  		if (i & BUSY_STAT)
  			continue;
  		if (!OK_STATUS(i))
  			break;
  		if (i & DRQ_STAT)
  			goto ok_to_read;
  	} while (--retries > 0);
  	dump_status("read_intr", i);
  	bad_rw_intr();
  	hd_request();
  	return;
e091eb67a   Tejun Heo   hd: clean up requ...
462

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
463
  ok_to_read:
8a12c4a45   Tejun Heo   hd: dequeue and t...
464
  	req = hd_req;
b4f42e283   Jens Axboe   block: remove str...
465
  	insw(HD_DATA, bio_data(req->bio), 256);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
466
  #ifdef DEBUG
83096ebf1   Tejun Heo   block: convert to...
467
468
469
  	printk("%s: read: sector %ld, remaining = %u, buffer=%p
  ",
  	       req->rq_disk->disk_name, blk_rq_pos(req) + 1,
b4f42e283   Jens Axboe   block: remove str...
470
  	       blk_rq_sectors(req) - 1, bio_data(req->bio)+512);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
471
  #endif
8a12c4a45   Tejun Heo   hd: dequeue and t...
472
  	if (hd_end_request(0, 512)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
473
474
475
  		SET_HANDLER(&read_intr);
  		return;
  	}
e091eb67a   Tejun Heo   hd: clean up requ...
476

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
477
478
479
480
  	(void) inb_p(HD_STATUS);
  #if (HD_DELAY > 0)
  	last_req = read_timer();
  #endif
e091eb67a   Tejun Heo   hd: clean up requ...
481
  	hd_request();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
482
483
484
485
  }
  
  static void write_intr(void)
  {
8a12c4a45   Tejun Heo   hd: dequeue and t...
486
  	struct request *req = hd_req;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
487
488
489
490
491
492
493
494
495
  	int i;
  	int retries = 100000;
  
  	do {
  		i = (unsigned) inb_p(HD_STATUS);
  		if (i & BUSY_STAT)
  			continue;
  		if (!OK_STATUS(i))
  			break;
83096ebf1   Tejun Heo   block: convert to...
496
  		if ((blk_rq_sectors(req) <= 1) || (i & DRQ_STAT))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
497
498
499
500
501
502
  			goto ok_to_write;
  	} while (--retries > 0);
  	dump_status("write_intr", i);
  	bad_rw_intr();
  	hd_request();
  	return;
e091eb67a   Tejun Heo   hd: clean up requ...
503

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
504
  ok_to_write:
8a12c4a45   Tejun Heo   hd: dequeue and t...
505
  	if (hd_end_request(0, 512)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
506
  		SET_HANDLER(&write_intr);
b4f42e283   Jens Axboe   block: remove str...
507
  		outsw(HD_DATA, bio_data(req->bio), 256);
e091eb67a   Tejun Heo   hd: clean up requ...
508
509
  		return;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
510
  #if (HD_DELAY > 0)
e091eb67a   Tejun Heo   hd: clean up requ...
511
  	last_req = read_timer();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
512
  #endif
e091eb67a   Tejun Heo   hd: clean up requ...
513
  	hd_request();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
  }
  
  static void recal_intr(void)
  {
  	check_status();
  #if (HD_DELAY > 0)
  	last_req = read_timer();
  #endif
  	hd_request();
  }
  
  /*
   * This is another of the error-routines I don't know what to do with. The
   * best idea seems to just set reset, and start all over again.
   */
  static void hd_times_out(unsigned long dummy)
  {
  	char *name;
  
  	do_hd = NULL;
8a12c4a45   Tejun Heo   hd: dequeue and t...
534
  	if (!hd_req)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
535
  		return;
e93b9fb7d   Tejun Heo   hd: fix locking
536
  	spin_lock_irq(hd_queue->queue_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
537
  	reset = 1;
8a12c4a45   Tejun Heo   hd: dequeue and t...
538
  	name = hd_req->rq_disk->disk_name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
539
540
  	printk("%s: timeout
  ", name);
8a12c4a45   Tejun Heo   hd: dequeue and t...
541
  	if (++hd_req->errors >= MAX_ERRORS) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
542
543
544
545
  #ifdef DEBUG
  		printk("%s: too many errors
  ", name);
  #endif
8a12c4a45   Tejun Heo   hd: dequeue and t...
546
  		hd_end_request_cur(-EIO);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
547
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
548
  	hd_request();
e93b9fb7d   Tejun Heo   hd: fix locking
549
  	spin_unlock_irq(hd_queue->queue_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
550
551
552
553
554
555
  }
  
  static int do_special_op(struct hd_i_struct *disk, struct request *req)
  {
  	if (disk->recalibrate) {
  		disk->recalibrate = 0;
f26b3d759   Bartlomiej Zolnierkiewicz   hd: WIN_* -> ATA_...
556
  		hd_out(disk, disk->sect, 0, 0, 0, ATA_CMD_RESTORE, &recal_intr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
557
558
559
  		return reset;
  	}
  	if (disk->head > 16) {
ec29782ba   Paolo Ciarrocchi   IDE: Coding Style...
560
561
  		printk("%s: cannot handle device with more than 16 heads - giving up
  ", req->rq_disk->disk_name);
8a12c4a45   Tejun Heo   hd: dequeue and t...
562
  		hd_end_request_cur(-EIO);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
  	}
  	disk->special_op = 0;
  	return 1;
  }
  
  /*
   * The driver enables interrupts as much as possible.  In order to do this,
   * (a) the device-interrupt is disabled before entering hd_request(),
   * and (b) the timeout-interrupt is disabled before the sti().
   *
   * Interrupts are still masked (by default) whenever we are exchanging
   * data/cmds with a drive, because some drives seem to have very poor
   * tolerance for latency during I/O. The IDE driver has support to unmask
   * interrupts for non-broken hardware, so use that driver if required.
   */
  static void hd_request(void)
  {
  	unsigned int block, nsect, sec, track, head, cyl;
  	struct hd_i_struct *disk;
  	struct request *req;
  
  	if (do_hd)
  		return;
  repeat:
  	del_timer(&device_timer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
588

8a12c4a45   Tejun Heo   hd: dequeue and t...
589
  	if (!hd_req) {
9934c8c04   Tejun Heo   block: implement ...
590
  		hd_req = blk_fetch_request(hd_queue);
8a12c4a45   Tejun Heo   hd: dequeue and t...
591
592
593
594
  		if (!hd_req) {
  			do_hd = NULL;
  			return;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
595
  	}
8a12c4a45   Tejun Heo   hd: dequeue and t...
596
  	req = hd_req;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
597
598
  
  	if (reset) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
599
600
601
602
  		reset_hd();
  		return;
  	}
  	disk = req->rq_disk->private_data;
83096ebf1   Tejun Heo   block: convert to...
603
604
  	block = blk_rq_pos(req);
  	nsect = blk_rq_sectors(req);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
605
606
607
608
609
  	if (block >= get_capacity(req->rq_disk) ||
  	    ((block+nsect) > get_capacity(req->rq_disk))) {
  		printk("%s: bad access: block=%d, count=%d
  ",
  			req->rq_disk->disk_name, block, nsect);
8a12c4a45   Tejun Heo   hd: dequeue and t...
610
  		hd_end_request_cur(-EIO);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
  		goto repeat;
  	}
  
  	if (disk->special_op) {
  		if (do_special_op(disk, req))
  			goto repeat;
  		return;
  	}
  	sec   = block % disk->sect + 1;
  	track = block / disk->sect;
  	head  = track % disk->head;
  	cyl   = track / disk->head;
  #ifdef DEBUG
  	printk("%s: %sing: CHS=%d/%d/%d, sectors=%d, buffer=%p
  ",
e654bc439   Boaz Harrosh   [PATCH] fix reque...
626
627
  		req->rq_disk->disk_name,
  		req_data_dir(req) == READ ? "read" : "writ",
b4f42e283   Jens Axboe   block: remove str...
628
  		cyl, head, sec, nsect, bio_data(req->bio));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
629
  #endif
33659ebba   Christoph Hellwig   block: remove wra...
630
  	if (req->cmd_type == REQ_TYPE_FS) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
631
632
  		switch (rq_data_dir(req)) {
  		case READ:
f26b3d759   Bartlomiej Zolnierkiewicz   hd: WIN_* -> ATA_...
633
  			hd_out(disk, nsect, sec, head, cyl, ATA_CMD_PIO_READ,
ec29782ba   Paolo Ciarrocchi   IDE: Coding Style...
634
  				&read_intr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
635
636
637
638
  			if (reset)
  				goto repeat;
  			break;
  		case WRITE:
f26b3d759   Bartlomiej Zolnierkiewicz   hd: WIN_* -> ATA_...
639
  			hd_out(disk, nsect, sec, head, cyl, ATA_CMD_PIO_WRITE,
ec29782ba   Paolo Ciarrocchi   IDE: Coding Style...
640
  				&write_intr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
641
642
643
644
645
646
  			if (reset)
  				goto repeat;
  			if (wait_DRQ()) {
  				bad_rw_intr();
  				goto repeat;
  			}
b4f42e283   Jens Axboe   block: remove str...
647
  			outsw(HD_DATA, bio_data(req->bio), 256);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
648
649
650
651
  			break;
  		default:
  			printk("unknown hd-command
  ");
8a12c4a45   Tejun Heo   hd: dequeue and t...
652
  			hd_end_request_cur(-EIO);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
653
654
655
656
  			break;
  		}
  	}
  }
ec29782ba   Paolo Ciarrocchi   IDE: Coding Style...
657
  static void do_hd_request(struct request_queue *q)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
658
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
659
  	hd_request();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
660
  }
a885c8c43   Christoph Hellwig   [PATCH] Add block...
661
  static int hd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
662
  {
a885c8c43   Christoph Hellwig   [PATCH] Add block...
663
664
665
666
667
668
  	struct hd_i_struct *disk = bdev->bd_disk->private_data;
  
  	geo->heads = disk->head;
  	geo->sectors = disk->sect;
  	geo->cylinders = disk->cyl;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
669
670
671
672
673
674
  }
  
  /*
   * Releasing a block device means we sync() it, so that it can safely
   * be forgotten about...
   */
7d12e780e   David Howells   IRQ: Maintain reg...
675
  static irqreturn_t hd_interrupt(int irq, void *dev_id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
676
677
  {
  	void (*handler)(void) = do_hd;
e93b9fb7d   Tejun Heo   hd: fix locking
678
  	spin_lock(hd_queue->queue_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
679
680
681
682
683
  	do_hd = NULL;
  	del_timer(&device_timer);
  	if (!handler)
  		handler = unexpected_hd_interrupt;
  	handler();
e93b9fb7d   Tejun Heo   hd: fix locking
684
685
  
  	spin_unlock(hd_queue->queue_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
686
687
  	return IRQ_HANDLED;
  }
83d5cde47   Alexey Dobriyan   const: make block...
688
  static const struct block_device_operations hd_fops = {
a885c8c43   Christoph Hellwig   [PATCH] Add block...
689
  	.getgeo =	hd_getgeo,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
690
691
692
  };
  
  /*
362537b9a   Thomas Gleixner   [PATCH] irq-flags...
693
   * This is the hard disk IRQ description. The IRQF_DISABLED in sa_flags
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
694
695
696
697
698
699
700
701
702
703
704
   * means we run the IRQ-handler with interrupts disabled:  this is bad for
   * interrupt latency, but anything else has led to problems on some
   * machines.
   *
   * We enable interrupts in some of the routines after making sure it's
   * safe.
   */
  
  static int __init hd_init(void)
  {
  	int drive;
ddeb9c3e9   Christoph Hellwig   hd: stop defining...
705
  	if (register_blkdev(HD_MAJOR, "hd"))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
706
707
708
709
  		return -1;
  
  	hd_queue = blk_init_queue(do_hd_request, &hd_lock);
  	if (!hd_queue) {
ddeb9c3e9   Christoph Hellwig   hd: stop defining...
710
  		unregister_blkdev(HD_MAJOR, "hd");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
711
712
  		return -ENOMEM;
  	}
086fa5ff0   Martin K. Petersen   block: Rename blk...
713
  	blk_queue_max_hw_sectors(hd_queue, 255);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
714
715
  	init_timer(&device_timer);
  	device_timer.function = hd_times_out;
e1defc4ff   Martin K. Petersen   block: Do away wi...
716
  	blk_queue_logical_block_size(hd_queue, 512);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
717

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
718
  	if (!NR_HD) {
48dd643c3   H. Peter Anvin   hd.c: remove BIOS...
719
720
  		/*
  		 * We don't know anything about the drive.  This means
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
721
722
  		 * that you *MUST* specify the drive parameters to the
  		 * kernel yourself.
48dd643c3   H. Peter Anvin   hd.c: remove BIOS...
723
724
725
726
727
  		 *
  		 * If we were on an i386, we used to read this info from
  		 * the BIOS or CMOS.  This doesn't work all that well,
  		 * since this assumes that this is a primary or secondary
  		 * drive, and if we're using this legacy driver, it's
25985edce   Lucas De Marchi   Fix common misspe...
728
  		 * probably an auxiliary controller added to recover
48dd643c3   H. Peter Anvin   hd.c: remove BIOS...
729
730
731
  		 * legacy data off an ST-506 drive.  Either way, it's
  		 * definitely safest to have the user explicitly specify
  		 * the information.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
732
733
734
735
  		 */
  		printk("hd: no drives specified - use hd=cyl,head,sectors"
  			" on kernel command line
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
736
  		goto out;
48dd643c3   H. Peter Anvin   hd.c: remove BIOS...
737
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
738

ec29782ba   Paolo Ciarrocchi   IDE: Coding Style...
739
  	for (drive = 0 ; drive < NR_HD ; drive++) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
740
741
742
743
  		struct gendisk *disk = alloc_disk(64);
  		struct hd_i_struct *p = &hd_info[drive];
  		if (!disk)
  			goto Enomem;
ddeb9c3e9   Christoph Hellwig   hd: stop defining...
744
  		disk->major = HD_MAJOR;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
745
746
747
748
749
750
751
752
  		disk->first_minor = drive << 6;
  		disk->fops = &hd_fops;
  		sprintf(disk->disk_name, "hd%c", 'a'+drive);
  		disk->private_data = p;
  		set_capacity(disk, p->head * p->sect * p->cyl);
  		disk->queue = hd_queue;
  		p->unit = drive;
  		hd_gendisk[drive] = disk;
ec29782ba   Paolo Ciarrocchi   IDE: Coding Style...
753
754
  		printk("%s: %luMB, CHS=%d/%d/%d
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
755
756
757
  			disk->disk_name, (unsigned long)get_capacity(disk)/2048,
  			p->cyl, p->head, p->sect);
  	}
362537b9a   Thomas Gleixner   [PATCH] irq-flags...
758
  	if (request_irq(HD_IRQ, hd_interrupt, IRQF_DISABLED, "hd", NULL)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
  		printk("hd: unable to get IRQ%d for the hard disk driver
  ",
  			HD_IRQ);
  		goto out1;
  	}
  	if (!request_region(HD_DATA, 8, "hd")) {
  		printk(KERN_WARNING "hd: port 0x%x busy
  ", HD_DATA);
  		goto out2;
  	}
  	if (!request_region(HD_CMD, 1, "hd(cmd)")) {
  		printk(KERN_WARNING "hd: port 0x%x busy
  ", HD_CMD);
  		goto out3;
  	}
  
  	/* Let them fly */
ec29782ba   Paolo Ciarrocchi   IDE: Coding Style...
776
  	for (drive = 0; drive < NR_HD; drive++)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
777
778
779
780
781
782
783
784
785
786
787
788
789
790
  		add_disk(hd_gendisk[drive]);
  
  	return 0;
  
  out3:
  	release_region(HD_DATA, 8);
  out2:
  	free_irq(HD_IRQ, NULL);
  out1:
  	for (drive = 0; drive < NR_HD; drive++)
  		put_disk(hd_gendisk[drive]);
  	NR_HD = 0;
  out:
  	del_timer(&device_timer);
ddeb9c3e9   Christoph Hellwig   hd: stop defining...
791
  	unregister_blkdev(HD_MAJOR, "hd");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
792
793
794
795
796
797
798
  	blk_cleanup_queue(hd_queue);
  	return -1;
  Enomem:
  	while (drive--)
  		put_disk(hd_gendisk[drive]);
  	goto out;
  }
ec29782ba   Paolo Ciarrocchi   IDE: Coding Style...
799
800
  static int __init parse_hd_setup(char *line)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
801
802
803
804
805
806
807
808
  	int ints[6];
  
  	(void) get_options(line, ARRAY_SIZE(ints), ints);
  	hd_setup(NULL, ints);
  
  	return 1;
  }
  __setup("hd=", parse_hd_setup);
01c22bfc3   Adrian Bunk   ide/legacy/hd.c: ...
809
  late_initcall(hd_init);