Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 20919
Collapse All | Expand All

(-)linux-2.4.20/Documentation/Configure.help (+9 lines)
Lines 8346-8351 Link Here
8346
  say M here and read <file:Documentation/modules.txt>.  The module
8346
  say M here and read <file:Documentation/modules.txt>.  The module
8347
  will be called megaraid.o.
8347
  will be called megaraid.o.
8348
8348
8349
ITE ATA RAID support
8350
CONFIG_SCSI_ITERAID
8351
  This driver supports the ITE IT8212 ATA RAID host adapter.
8352
8353
  If you want to compile this driver as a module ( = code which can be
8354
  inserted in and removed from the running kernel whenever you want),
8355
  say M here and read <file:Documentation/modules.txt>.  The module
8356
  will be called iteraid.o.
8357
8349
Intel/ICP (former GDT SCSI Disk Array) RAID Controller support
8358
Intel/ICP (former GDT SCSI Disk Array) RAID Controller support
8350
CONFIG_SCSI_GDTH
8359
CONFIG_SCSI_GDTH
8351
  Formerly called GDT SCSI Disk Array Controller Support.
8360
  Formerly called GDT SCSI Disk Array Controller Support.
(-)linux-2.4.20/drivers/scsi/Config.in (+1 lines)
Lines 65-70 Link Here
65
dep_tristate 'Always IN2000 SCSI support' CONFIG_SCSI_IN2000 $CONFIG_SCSI
65
dep_tristate 'Always IN2000 SCSI support' CONFIG_SCSI_IN2000 $CONFIG_SCSI
66
dep_tristate 'AM53/79C974 PCI SCSI support' CONFIG_SCSI_AM53C974 $CONFIG_SCSI $CONFIG_PCI
66
dep_tristate 'AM53/79C974 PCI SCSI support' CONFIG_SCSI_AM53C974 $CONFIG_SCSI $CONFIG_PCI
67
dep_tristate 'AMI MegaRAID support' CONFIG_SCSI_MEGARAID $CONFIG_SCSI
67
dep_tristate 'AMI MegaRAID support' CONFIG_SCSI_MEGARAID $CONFIG_SCSI
68
dep_tristate 'ITE ATA RAID support' CONFIG_SCSI_ITERAID $CONFIG_SCSI
68
69
69
dep_tristate 'BusLogic SCSI support' CONFIG_SCSI_BUSLOGIC $CONFIG_SCSI
70
dep_tristate 'BusLogic SCSI support' CONFIG_SCSI_BUSLOGIC $CONFIG_SCSI
70
if [ "$CONFIG_SCSI_BUSLOGIC" != "n" ]; then
71
if [ "$CONFIG_SCSI_BUSLOGIC" != "n" ]; then
(-)linux-2.4.20/drivers/scsi/Makefile (+1 lines)
Lines 106-111 Link Here
106
obj-$(CONFIG_SCSI_DC390T)	+= tmscsim.o
106
obj-$(CONFIG_SCSI_DC390T)	+= tmscsim.o
107
obj-$(CONFIG_SCSI_AM53C974)	+= AM53C974.o
107
obj-$(CONFIG_SCSI_AM53C974)	+= AM53C974.o
108
obj-$(CONFIG_SCSI_MEGARAID)	+= megaraid.o
108
obj-$(CONFIG_SCSI_MEGARAID)	+= megaraid.o
109
obj-$(CONFIG_SCSI_ITERAID)	+= iteraid.o
109
obj-$(CONFIG_SCSI_ACARD)	+= atp870u.o
110
obj-$(CONFIG_SCSI_ACARD)	+= atp870u.o
110
obj-$(CONFIG_SCSI_SUNESP)	+= esp.o
111
obj-$(CONFIG_SCSI_SUNESP)	+= esp.o
111
obj-$(CONFIG_SCSI_GDTH)		+= gdth.o
112
obj-$(CONFIG_SCSI_GDTH)		+= gdth.o
(-)linux-2.4.20/drivers/scsi/README.iteraid (+8 lines)
Line 0 Link Here
1
2
You can check for newer driver version from
3
4
http://www.ite.com.tw/productInfo/pc_userinfo.asp?FileName=L2
5
6
WARNING that page was very bad and didn't work with mozilla.
7
With old netsacape one might have better luck.
8
(-)linux-2.4.20/drivers/scsi/iteraid.c (+5747 lines)
Line 0 Link Here
1
/*
2
 * linux/drivers/scsi/iteraid.c
3
 *
4
 * (C) Copyright 2002-2003 Integrated Technology Express, inc.
5
 *
6
 * Nov 11, 2002	Mark Lu	file created.
7
 *
8
 * ITE IT8212 RAID controller device driver for Linux.
9
 *
10
 * This program is free software; you can redistribute it and/or modify it
11
 * under the terms of the GNU General Public License as published by the
12
 * Free Software Foundation; either version 2, or (at your option) any
13
 * later version.
14
 *
15
 * This program is distributed in the hope that it will be useful, but
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
 * General Public License for more details.
19
 *
20
 * Revision 0.0  2002/12/05	15:12:12  root
21
 * Empty function bodies; detect() works.
22
 *
23
 * Revision 0.1 2002/12/17	19:21:37  root
24
 * First "dma thing doesn't work" version.
25
 *
26
 * Revision 0.3 2002/12/23	17:12:09  root
27
 * Rewrite the dma routines. Reference the ide-dma.c.
28
 *
29
 * Revision 0.4 2002/12/26	10:19:29  root
30
 * The dma read/write works, using some ways to prove it. But there is a
31
 * problem about the "unknown partition table". The fdisk is ok, but
32
 * after writing the created partitions, it still shows the "unknown
33
 * partition table" and i can't access the created partitions.
34
 *
35
 * Revision 0.5 2003/01/07	21:49:30 root
36
 * The problem of "unknown partition table" has been solved.
37
 * We must "ENABLE CLUSTERING". There is still a another problem about
38
 * the SCATTER/GATHER.
39
 *
40
 * Revision 0.6 2003/01/10	17:45:32 root
41
 * The SCATTER/GATHER problem has been solved. Now verify the read/write
42
 * function and make sure each RAID configurations are workable. If testing
43
 * is OK, then it will be a version 1.0.....
44
 *
45
 * Revision 1.0 2003/01/16	17:45:32 root
46
 * First release version.
47
 *
48
 * FixME 1:
49
 * In RedHat 7.3, if using SG_ALL, the SCSI will timeout. It looks like
50
 * an command is requested but the interrupt is not been asserted. So
51
 * try to add a watchdog timer to monitor the interrupts. But this kind
52
 * of situration will not happen in Mandrake 9.0 and also when using
53
 * SG_NONE in RedHat 7.3.
54
 *
55
 * FixME 2:
56
 * Module load problem in RedHat 7.3.
57
 *
58
 * Fixed: Compile in the graphic mode (GNOME or KDE) will fix the
59
 *	  module load problem.
60
 *
61
 * Revision 1.1 2003/02/10	10:32:21 root
62
 * Compile in the graphic mode (GNOME or KDE) will fix the
63
 * module load problem.
64
 *
65
 * Revision 1.2 2003/02/18	14:10:35 root
66
 * Fix the interrupt service routine for share irq problem.
67
 *
68
 * ATAPI support ---> schedule is three weeks. (2003/02/28)
69
 *
70
 * Revision 1.3 2003/02/27
71
 * First relase ATAPI version. But there will be an error if no disc in the
72
 * CD-ROM. Because the commands like TEST_UNIT_READY and READ_CAPACITY will
73
 * get the error response. This situration in WINDOWS will be then send the
74
 * REQUEST SENSE command to the device but in Linux, it will never get
75
 * REQUEST SENSE command. So can we send by ourself???
76
 *
77
 * 2003/03/05	root
78
 *
79
 * Note 1:
80
 * According to "The Linux SCSI Generic (sg) HOWTO", the device will respond
81
 * with a single byte value call the 'scsi_status'. GOOD is the scsi status
82
 * indicating everything has gone well. The most common other status is
83
 * CHECK CONDITION. In this latter case, the SCSI mid layer issues a REQUEST
84
 * SENSE SCSI command. The response of the REQUEST SENSE is 18 bytes or more
85
 * in length and is called the "sense buffer". It will indicate why the original
86
 * command may not have been executed. It is important to realize that a CHECK
87
 * CONDITION may very in severity from informative (e.g. command needed to be
88
 * retried before succeeding) to fatal (e.g. 'medium error' which often indicates
89
 * it is time to replace the disk).
90
 *
91
 * Note 2:
92
 * When using the ATAPI BIOS, we also do not need to set up the timimg in linux
93
 * driver. But it is necessary to write the timing routine in win system, cause it
94
 * has a s1, s2, s3 mode and devices wake up from these modes need to be initialized
95
 * again and do not pass through the BIOS.
96
 *
97
 * Note 3:
98
 * The 48-bit support and AP for RAID in linux will the next job.
99
 *
100
 * Revision 1.31 2003/03/14	09:40:35 root
101
 * Fix the error when no disc is on the CD-ROM and the audio cd is ready to play.
102
 *
103
 * 2003/04/08	root
104
 * The ioctl code sklection is finished. But there is a problem about
105
 * "Bad address" when copy_from_user() is called.
106
 *
107
 * Fixed: Use kmalloc() and kfree() to allocate the buffer instead of automatic
108
 * variables (use stack). The stack size is limited in kernel space.
109
 *
110
 * Revision 1.32 2003/04/14	18:20:23 root
111
 * Complete the IOCTLs code.
112
 *
113
 * The IOCTLs are listed below
114
 * ===========================
115
 *
116
 * (1) ITE_IOC_GET_IDENTIFY_DATA
117
 *
118
 *     Return virtual drive 512 bytes identification data.
119
 *
120
 * (2) ITE_IOC_GET_PHY_DISK_STATUS
121
 *
122
 *     Developer can decide to return 4 physical disk information in
123
 *     512 bytes (data structure should be defined) or 512 bytes
124
 *     identification data of the physical disk specified by AP.
125
 *
126
 * (3) ITE_IOC_CREATE_DISK_ARRAY
127
 *
128
 *     Create a new array and erase (or keep) boot sector.
129
 *
130
 * (4) ITE_IOC_REBUILD_START
131
 *
132
 *     AP nees to specify target/source drive, starting LBA and length.
133
 *
134
 * (5) ITE_IOC_GET_REBUILD_STATUS
135
 *
136
 *     Return rebuild percentage or last LBA No.
137
 *
138
 * (6) ITE_IOC_RESET_ADAPTER
139
 *
140
 *     Reset the controller.
141
 *
142
 * Revision 1.33 2003/04/15	11:10:08 root
143
 * The 48-bit support.
144
 *
145
 * Revision 1.34 2003/04/20	13:20:38 root
146
 * Change some values in iteraid.h, so it will not hang in Red Hat Linux
147
 * and improve the performance.
148
 *
149
 * can_queue: 1 --------------------> can_queue: 124
150
 * sg_tablesize: SG_NONE -----------> sg_tablesize: 16
151
 * cmd_per_lun: 128 ----------------> cmd_per_lun: 1
152
 * use_clustering: ENABLE_CLUSTER --> use_clustering: DISABLE_CLUSTER
153
 *
154
 * 2003/04/25	root
155
 * The code will hang on Gigabyte's motherboard when the sourth bridge is
156
 * sis 962L and 963.
157
 *
158
 * 2003/04/28	root
159
 * Fixed: Do not enable interrupt again when send each command in
160
 * IdeSendCommand() routine.
161
 *
162
 * TODO list:
163
 *
164
 * (1) Using the new error code handler instead of the old style.
165
 *     Because the old one will not be used above kernl-2.5.
166
 *
167
 * (2) Rewrite the abort and reset functions.
168
 */
169
170
#include <linux/module.h>
171
172
MODULE_AUTHOR ("ITE,Inc.");
173
MODULE_DESCRIPTION ("ITE IT8212 RAID Controller Linux Driver");
174
175
#include <linux/kernel.h>
176
#include <linux/pci.h>
177
#include <linux/time.h>
178
#include <linux/proc_fs.h>
179
#include <linux/sched.h>
180
#include <linux/ioport.h>
181
#include <linux/blk.h>
182
#include <linux/hdreg.h>
183
#include <linux/string.h>
184
#include <linux/delay.h>
185
#include <linux/reboot.h>
186
#include <linux/spinlock.h>
187
#include <linux/fs.h>
188
189
#include <asm/errno.h>
190
#include <asm/io.h>
191
#include <asm/irq.h>
192
#include <asm/uaccess.h>
193
194
#include "sd.h"
195
#include "scsi.h"
196
#include "hosts.h"
197
198
#include "iteraid.h"
199
200
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,10)
201
MODULE_LICENSE("GPL");
202
#endif
203
204
#define MARK_DEBUG_DUMP_MEM	0	/* 1=Enable dump memory	content	*/
205
#define	MARK_DEBUG_BYPASS_MODE	0	/* 1=Enable use bypass mode	*/
206
207
#define	PRD_BYTES		8	/* PRD table size		*/
208
#define PRD_ENTRIES		(PAGE_SIZE / (2 * PRD_BYTES))
209
210
struct Scsi_Host * ite_vhost	= 0;	/* SCSI virtual host		*/
211
Scsi_Cmnd *	   it8212_req_last = 0;	/* SRB request list		*/
212
213
unsigned int  NumAdapters = 0;		/* Adapters number		*/
214
PITE_ADAPTER ite_adapters[2];		/* How many adapters support	*/
215
216
static struct semaphore	mimd_entry_mtx;
217
218
static spinlock_t queue_request_lock	= SPIN_LOCK_UNLOCKED;
219
static spinlock_t io_request_lock	= SPIN_LOCK_UNLOCKED;
220
static spinlock_t gio_request_lock	= SPIN_LOCK_UNLOCKED;
221
222
static int	driver_ver = 134;	/* Current driver version	*/
223
224
/************************************************************************
225
 * The File Operations structure for the ioctl interface of the driver.
226
 ************************************************************************/
227
static struct file_operations itedev_fops =
228
{
229
 ioctl:itedev_ioctl_entry,
230
 open:itedev_open,
231
 release:itedev_close,
232
};
233
234
#if (MARK_DEBUG_DUMP_MEM)
235
/************************************************************************
236
 * Dump buffer
237
 ************************************************************************/
238
static void
239
HexDump(unsigned char *buf, int length)
240
{
241
242
 unsigned int	i = 0;
243
 unsigned int	j = 0;
244
 printk("\n");
245
246
 for (i = 0; i < length; i += 16)
247
    {
248
     printk("%04X    ", i);
249
     for (j = i; (j < i + 8) && (j < length); j++)
250
	{
251
	 printk(" %02X", buf[j]);
252
	}
253
     if (j == i + 8) printk("-");
254
     for (j = i + 8; (j < i + 16) && (j < length); j++)
255
	{
256
	 printk("%02X ", buf[j]);
257
	}
258
     printk("\n");
259
    }
260
261
} /* end HexDump */
262
#endif
263
264
/************************************************************************
265
 * This routine maps ATAPI and IDE errors to specific SRB statuses.
266
 ************************************************************************/
267
UCHAR
268
MapError(PChannel pChan, PSCSI_REQUEST_BLOCK Srb)
269
{
270
271
 UCHAR		errorByte;
272
 UCHAR		srbStatus;
273
 UCHAR		scsiStatus;
274
275
 /*
276
  * Read the error register.
277
  */
278
 errorByte = inb(pChan->io_ports[IDE_ERROR_OFFSET]);
279
 printk("MapError: Error register is %x\n", errorByte);
280
281
 /*
282
  * If this is ATAPI error.
283
  */
284
 if (pChan->DeviceFlags[Srb->TargetId & 1] & DFLAGS_ATAPI_DEVICE)
285
    {
286
     switch (errorByte >> 4)
287
	{
288
	 case SCSI_SENSE_NO_SENSE:
289
	     printk("ATAPI: No sense information\n");
290
	     scsiStatus = SCSISTAT_CHECK_CONDITION;
291
	     srbStatus  = SRB_STATUS_ERROR;
292
	     break;
293
294
	 case SCSI_SENSE_RECOVERED_ERROR:
295
	     printk("ATAPI: Recovered error\n");
296
	     scsiStatus = 0;
297
	     srbStatus  = SRB_STATUS_SUCCESS;
298
	     break;
299
300
	 case SCSI_SENSE_NOT_READY:
301
	     printk("ATAPI: Device not ready\n");
302
	     scsiStatus = SCSISTAT_CHECK_CONDITION;
303
	     srbStatus  = SRB_STATUS_ERROR;
304
	     break;
305
306
	 case SCSI_SENSE_MEDIUM_ERROR:
307
	     printk("ATAPI: Media error\n");
308
	     scsiStatus = SCSISTAT_CHECK_CONDITION;
309
	     srbStatus  = SRB_STATUS_ERROR;
310
	     break;
311
312
	 case SCSI_SENSE_HARDWARE_ERROR:
313
	     printk("ATAPI: Hardware error\n");
314
	     scsiStatus = SCSISTAT_CHECK_CONDITION;
315
	     srbStatus  = SRB_STATUS_ERROR;
316
	     break;
317
318
	 case SCSI_SENSE_ILLEGAL_REQUEST:
319
	     printk("ATAPI: Illegal request\n");
320
	     scsiStatus = SCSISTAT_CHECK_CONDITION;
321
	     srbStatus  = SRB_STATUS_ERROR;
322
	     break;
323
324
	 case SCSI_SENSE_UNIT_ATTENTION:
325
	     printk("ATAPI: Unit attention\n");
326
	     scsiStatus = SCSISTAT_CHECK_CONDITION;
327
	     srbStatus  = SRB_STATUS_ERROR;
328
	     break;
329
330
	 case SCSI_SENSE_DATA_PROTECT:
331
	     printk("ATAPI: Data protect\n");
332
	     scsiStatus = SCSISTAT_CHECK_CONDITION;
333
	     srbStatus  = SRB_STATUS_ERROR;
334
	     break;
335
336
	 case SCSI_SENSE_BLANK_CHECK:
337
	     printk("ATAPI: Blank check\n");
338
	     scsiStatus = SCSISTAT_CHECK_CONDITION;
339
	     srbStatus  = SRB_STATUS_ERROR;
340
	     break;
341
342
	 case SCSI_SENSE_ABORTED_COMMAND:
343
	     printk("ATAPI: Command Aborted\n");
344
	     scsiStatus = SCSISTAT_CHECK_CONDITION;
345
	     srbStatus  = SRB_STATUS_ERROR;
346
	     break;
347
348
	 default:
349
	     printk("ATAPI: Invalid sense information\n");
350
	     scsiStatus = 0;
351
	     srbStatus  = SRB_STATUS_ERROR;
352
	     break;
353
	}
354
    }
355
 /*
356
  * IF this is IDE error.
357
  */
358
 else
359
    {
360
     scsiStatus = 0;
361
     srbStatus  = SRB_STATUS_ERROR;
362
363
     /*
364
      * Save errorByte, to be used by SCSIOP_REQUEST_SENSE.
365
      */
366
     pChan->ReturningMediaStatus = errorByte;
367
368
     if (errorByte & IDE_ERROR_MEDIA_CHANGE_REQ)
369
	{
370
	 printk("IDE: Media change\n");
371
	 scsiStatus = SCSISTAT_CHECK_CONDITION;
372
	 srbStatus  = SRB_STATUS_ERROR;
373
	}
374
     else if (errorByte & IDE_ERROR_COMMAND_ABORTED)
375
	{
376
	 printk( "IDE: Command abort\n");
377
	 srbStatus  = SRB_STATUS_ABORTED;
378
	 scsiStatus = SCSISTAT_CHECK_CONDITION;
379
380
	 if (Srb->SenseInfoBuffer)
381
	    {
382
	     PSENSE_DATA senseBuffer = (PSENSE_DATA)Srb->SenseInfoBuffer;
383
384
	     senseBuffer->ErrorCode = 0x70;
385
	     senseBuffer->Valid = 1;
386
	     senseBuffer->AdditionalSenseLength = 0xB;
387
	     senseBuffer->SenseKey = SCSI_SENSE_ABORTED_COMMAND;
388
	     senseBuffer->AdditionalSenseCode = 0;
389
	     senseBuffer->AdditionalSenseCodeQualifier = 0;
390
391
	     srbStatus |= SRB_STATUS_AUTOSENSE_VALID;
392
	    }
393
	 /*
394
	  * pChan->ErrorCount++;
395
	  */
396
	}
397
     else if (errorByte & IDE_ERROR_END_OF_MEDIA)
398
	{
399
	 printk("IDE: End of media\n");
400
401
	 scsiStatus = SCSISTAT_CHECK_CONDITION;
402
	 srbStatus = SRB_STATUS_ERROR;
403
404
	 if (!(pChan->DeviceFlags[Srb->TargetId & 1] & DFLAGS_MEDIA_STATUS_ENABLED))
405
	    {
406
	     /*
407
	      * pChan->ErrorCount++;
408
	      */
409
	    }
410
	}
411
     else if (errorByte & IDE_ERROR_ILLEGAL_LENGTH)
412
	{
413
	 printk("IDE: Illegal length\n");
414
	 srbStatus = SRB_STATUS_INVALID_REQUEST;
415
	}
416
     else if (errorByte & IDE_ERROR_BAD_BLOCK)
417
	{
418
	 printk("IDE: Bad block\n");
419
420
	 srbStatus = SRB_STATUS_ERROR;
421
	 scsiStatus = SCSISTAT_CHECK_CONDITION;
422
423
	 if (Srb->SenseInfoBuffer)
424
	    {
425
	     PSENSE_DATA senseBuffer = (PSENSE_DATA)Srb->SenseInfoBuffer;
426
427
	     senseBuffer->ErrorCode = 0x70;
428
	     senseBuffer->Valid = 1;
429
	     senseBuffer->AdditionalSenseLength = 0xB;
430
	     senseBuffer->SenseKey = SCSI_SENSE_MEDIUM_ERROR;
431
	     senseBuffer->AdditionalSenseCode = 0;
432
	     senseBuffer->AdditionalSenseCodeQualifier = 0;
433
434
	     srbStatus |= SRB_STATUS_AUTOSENSE_VALID;
435
	    }
436
	}
437
     else if (errorByte & IDE_ERROR_ID_NOT_FOUND)
438
	{
439
	 printk("IDE: Id not found\n");
440
441
	 srbStatus = SRB_STATUS_ERROR;
442
	 scsiStatus = SCSISTAT_CHECK_CONDITION;
443
444
	 if (Srb->SenseInfoBuffer)
445
	    {
446
	     PSENSE_DATA senseBuffer = (PSENSE_DATA)Srb->SenseInfoBuffer;
447
448
	     senseBuffer->ErrorCode = 0x70;
449
	     senseBuffer->Valid = 1;
450
	     senseBuffer->AdditionalSenseLength = 0xb;
451
	     senseBuffer->SenseKey = SCSI_SENSE_MEDIUM_ERROR;
452
	     senseBuffer->AdditionalSenseCode = 0;
453
	     senseBuffer->AdditionalSenseCodeQualifier = 0;
454
455
	     srbStatus |= SRB_STATUS_AUTOSENSE_VALID;
456
	    }
457
	 /*
458
	  * pChan->ErrorCount++;
459
	  */
460
	}
461
     else if (errorByte & IDE_ERROR_MEDIA_CHANGE)
462
	{
463
	 printk("IDE: Media change\n");
464
465
	 scsiStatus = SCSISTAT_CHECK_CONDITION;
466
	 srbStatus = SRB_STATUS_ERROR;
467
468
	 if (Srb->SenseInfoBuffer)
469
	    {
470
	     PSENSE_DATA senseBuffer = (PSENSE_DATA)Srb->SenseInfoBuffer;
471
472
	     senseBuffer->ErrorCode = 0x70;
473
	     senseBuffer->Valid = 1;
474
	     senseBuffer->AdditionalSenseLength = 0xb;
475
	     senseBuffer->SenseKey = SCSI_SENSE_UNIT_ATTENTION;
476
	     senseBuffer->AdditionalSenseCode = SCSI_ADSENSE_MEDIUM_CHANGED;
477
	     senseBuffer->AdditionalSenseCodeQualifier = 0;
478
479
	     srbStatus |= SRB_STATUS_AUTOSENSE_VALID;
480
	    }
481
	}
482
     else if (errorByte & IDE_ERROR_DATA_ERROR)
483
	{
484
	 printk("IDE: Data error\n");
485
486
	 scsiStatus = SCSISTAT_CHECK_CONDITION;
487
	 srbStatus = SRB_STATUS_ERROR;
488
489
	 if (!(pChan->DeviceFlags[Srb->TargetId & 1] & DFLAGS_MEDIA_STATUS_ENABLED))
490
	    {
491
	     /*
492
	      * pChan->ErrorCount++;
493
	      */
494
	    }
495
496
	 /*
497
	  * Build sense buffer.
498
	  */
499
	 if (Srb->SenseInfoBuffer)
500
	    {
501
	     PSENSE_DATA senseBuffer = (PSENSE_DATA)Srb->SenseInfoBuffer;
502
503
	     senseBuffer->ErrorCode = 0x70;
504
	     senseBuffer->Valid = 1;
505
	     senseBuffer->AdditionalSenseLength = 0xB;
506
	     senseBuffer->SenseKey = SCSI_SENSE_MEDIUM_ERROR;
507
	     senseBuffer->AdditionalSenseCode = 0;
508
	     senseBuffer->AdditionalSenseCodeQualifier = 0;
509
510
	     srbStatus |= SRB_STATUS_AUTOSENSE_VALID;
511
	    }
512
	}
513
    }
514
515
 /*
516
  * Set SCSI status to indicate a check condition.
517
  */
518
 Srb->ScsiStatus = scsiStatus;
519
520
 return srbStatus;
521
522
} /* end MapError */
523
524
/************************************************************************
525
 *
526
 ************************************************************************/
527
UCHAR
528
RaidGetHighestBit(UCHAR Number)
529
{
530
531
 char		bit;
532
533
 for (bit = 7; bit >= 0; bit--)
534
    {
535
     if (Number & (1 << bit)) return bit;
536
    }
537
538
 return 0xFF;
539
540
} /* end RaidGetHighestBit */
541
542
/************************************************************************
543
 * Reset IDE controller or ATAPI device.
544
 ************************************************************************/
545
BOOLEAN
546
AtapiResetController(PChannel pChan)
547
{
548
549
 ULONG		i;
550
 UCHAR		status;
551
 ULONG		dma_base = pChan->dma_base;
552
 BOOLEAN	resetResult = FALSE;
553
554
 printk("AtapiResetController\n");
555
556
 /*
557
  * Check and see if we are processing an internal srb.
558
  */
559
 if (pChan->OriginalSrb)
560
    {
561
     pChan->CurrentSrb = pChan->OriginalSrb;
562
     pChan->OriginalSrb = NULL;
563
    }
564
565
 /*
566
  * To avoid unexpected interrupts occurs during reset procedure.
567
  *
568
  * 1. Stop bus master operation.
569
  */
570
 outb(0, dma_base);
571
572
 for (i = 0; i < 2; i++)
573
    {
574
     outb((UCHAR)((i << 4) | 0xA0), pChan->io_ports[ATAPI_SELECT_OFFSET]);
575
576
     /*
577
      * 2. Clear interrupts if there is any.
578
      */
579
     GetBaseStatus(pChan, status);
580
581
     /*
582
      * 3. Disable interrupts.
583
      */
584
     outb(IDE_DC_DISABLE_INTERRUPTS, pChan->io_ports[ATAPI_CONTROL_OFFSET]);
585
586
     /*
587
      * 4. Clear interrupts again.
588
      */
589
     GetBaseStatus(pChan, status);
590
    }
591
592
 /*
593
  * Check if request is in progress.
594
  */
595
 if (pChan->CurrentSrb)
596
    {
597
     /*
598
      * Complete outstanding request with SRB_STATUS_BUS_RESET.
599
      */
600
601
     /*
602
      * Clear request tracking fields.
603
      */
604
     pChan->CurrentSrb = NULL;
605
     pChan->WordsLeft  = 0;
606
     pChan->DataBuffer = NULL;
607
608
     /*
609
      * Indicate ready for next request.
610
      */
611
     TaskDone(pChan, pChan->CurrentSrb);
612
    }
613
614
 /*
615
  * Clear expecting interrupt flag.
616
  */
617
 pChan->ExpectingInterrupt = FALSE;
618
 pChan->RDP = FALSE;
619
620
 resetResult = IT8212ResetAdapter();
621
622
 for (i = 0; i < 4; i++)
623
    {
624
     /*
625
      * Reenable interrupts.
626
      */
627
     outb((UCHAR)(((i & 1) << 4) | 0xA0), pChan->io_ports[ATAPI_SELECT_OFFSET]);
628
     outb(IDE_DC_REENABLE_CONTROLLER, pChan->io_ports[ATAPI_CONTROL_OFFSET]);
629
    }
630
631
 return resetResult;
632
633
} /* end AtapiResetController */
634
635
/************************************************************************
636
 * IDE start read/write transfer
637
 ************************************************************************/
638
void
639
IdeStartTransfer
640
(
641
 PChannel		pChan,
642
 PSCSI_REQUEST_BLOCK	Srb,
643
 ULONG			startingSector,
644
 ULONG			SectorNumber
645
)
646
{
647
648
 UCHAR		DiskId = (UCHAR) Srb->TargetId;
649
 UCHAR		drvSelect;
650
 UCHAR		bmClearStat;
651
 ULONG		dma_base = pChan->dma_base;
652
653
 dprintk("IdeStartTransfer enter\n");
654
655
 /*
656
  * Set watch dog timer to monitor interrupts. Timer will be removed
657
  * on DeviceInterrupt.
658
  */
659
#if (0)
660
 SetWatchDogTimer(pChan);
661
#endif
662
663
 /*
664
  * 48-bit support.
665
  */
666
 if ((startingSector + SectorNumber) > 0x0FFFFFFF)
667
    {
668
     /*
669
      * Select drive and set LBA mode.
670
      */
671
     outb((UCHAR) (((DiskId & 0x1) << 4) | 0xA0 | 0x40),
672
	     pChan->io_ports[IDE_SELECT_OFFSET]);
673
674
     /*
675
      * Sector count register.
676
      */
677
     outb((UCHAR) (SectorNumber >> 8), pChan->io_ports[IDE_NSECTOR_OFFSET]);
678
     outb((UCHAR) SectorNumber,	       pChan->io_ports[IDE_NSECTOR_OFFSET]);
679
680
     /*
681
      * LBA low register.
682
      */
683
     outb((UCHAR) (startingSector >> 24), pChan->io_ports[IDE_LOCYL_OFFSET]);
684
     outb((UCHAR) startingSector,	  pChan->io_ports[IDE_LOCYL_OFFSET]);
685
686
     /*
687
      * LBA mid register.
688
      */
689
     outb((UCHAR) 0,			 pChan->io_ports[IDE_MIDCYL_OFFSET]);
690
     outb((UCHAR) (startingSector >> 8), pChan->io_ports[IDE_MIDCYL_OFFSET]);
691
692
     /*
693
      * LBA high register.
694
      */
695
     outb((UCHAR) 0,			  pChan->io_ports[IDE_HCYL_OFFSET]);
696
     outb((UCHAR) (startingSector >> 16), pChan->io_ports[IDE_HCYL_OFFSET]);
697
698
     /*
699
      * Start the IDE read/write DMA command.
700
      */
701
     if (Srb->SrbFlags & SRB_FLAGS_DATA_IN)
702
	{
703
	 outb(IDE_COMMAND_READ_DMA_EXT, pChan->io_ports[IDE_COMMAND_OFFSET]);
704
	}
705
     else if (Srb->SrbFlags & SRB_FLAGS_DATA_OUT)
706
	{
707
	 outb(IDE_COMMAND_WRITE_DMA_EXT, pChan->io_ports[IDE_COMMAND_OFFSET]);
708
	}
709
    }
710
 /*
711
  * 28-bit addressing.
712
  */
713
 else
714
    {
715
     /*
716
      * Select drive and set LBA mode.
717
      */
718
     drvSelect = (UCHAR) (startingSector >> 24);
719
     drvSelect = drvSelect | (((UCHAR) DiskId & 0x1) << 4) | 0x40 | 0xA0;
720
721
     outb(drvSelect,			 pChan->io_ports[IDE_SELECT_OFFSET]);
722
     outb((UCHAR) SectorNumber,		 pChan->io_ports[IDE_NSECTOR_OFFSET]);
723
     outb((UCHAR) startingSector,	 pChan->io_ports[IDE_LOCYL_OFFSET]);
724
     outb((UCHAR)(startingSector >> 8),  pChan->io_ports[IDE_MIDCYL_OFFSET]);
725
     outb((UCHAR)(startingSector >> 16), pChan->io_ports[IDE_HCYL_OFFSET]);
726
727
     /*
728
      * Start the IDE read/write DMA command.
729
      */
730
     if (Srb->SrbFlags & SRB_FLAGS_DATA_IN)
731
	{
732
	 outb(IDE_COMMAND_READ_DMA, pChan->io_ports[IDE_COMMAND_OFFSET]);
733
	}
734
     else if (Srb->SrbFlags & SRB_FLAGS_DATA_OUT)
735
	{
736
	 outb(IDE_COMMAND_WRITE_DMA, pChan->io_ports[IDE_COMMAND_OFFSET]);
737
	}
738
    }
739
740
 /*
741
  * Indicate expecting an interrupt.
742
  */
743
 pChan->ExpectingInterrupt = TRUE;
744
745
 /*
746
  * Setup PRD table physical address.
747
  */
748
 outl(pChan->dmatable_dma, dma_base + 4);
749
750
 /*
751
  * Read Bus Master status.
752
  */
753
 bmClearStat = inb(dma_base + 2);
754
 if (Srb->TargetId & 1)
755
    {
756
     bmClearStat = bmClearStat	   | BM_DRV1_DMA_CAPABLE |
757
		   BM_STAT_FLG_INT | BM_STAT_FLG_ERR;
758
    }
759
 else
760
    {
761
     bmClearStat = bmClearStat	   | BM_DRV0_DMA_CAPABLE |
762
     		   BM_STAT_FLG_INT | BM_STAT_FLG_ERR;
763
    }
764
765
 outb(0, dma_base);
766
767
 /*
768
  * Clear INTR and ERROR flags.
769
  */
770
 outb(bmClearStat, dma_base + 2);
771
772
 /*
773
  * Start DMA read/write.
774
  */
775
 if (Srb->SrbFlags & SRB_FLAGS_DATA_IN)
776
    {
777
     outb(BM_CMD_FLG_START | BM_CMD_FLG_WRTTOMEM, dma_base);
778
    }
779
 else if (Srb->SrbFlags & SRB_FLAGS_DATA_OUT)
780
    {
781
     outb(BM_CMD_FLG_START | BM_CMD_FLG_WRTTODSK, dma_base);
782
    }
783
784
 dprintk("IdeStartTransfer exit\n");
785
786
} /* end IdeStartTransfer */
787
788
/************************************************************************
789
 * IDE setup the PRD table
790
 ************************************************************************/
791
static int
792
IdeBuildSglist(PChannel pChan, PSCSI_REQUEST_BLOCK Srb)
793
{
794
795
 int		nents		= 0;
796
 ULONG		bytesRemaining	= Srb->DataTransferLength;
797
 unsigned char * virt_addr	= Srb->DataBuffer;
798
 struct scatterlist * sg	= pChan->sg_table;
799
800
 if (Srb->SrbFlags & SRB_FLAGS_DATA_IN)
801
    {
802
     pChan->sg_dma_direction = PCI_DMA_FROMDEVICE;
803
    }
804
 else
805
    {
806
     pChan->sg_dma_direction = PCI_DMA_TODEVICE;
807
    }
808
809
 /*
810
  * The upper layer will never give the memory more than 64K bytes.
811
  */
812
 memset(&sg[nents], 0, sizeof(*sg));
813
 sg[nents].address = virt_addr;
814
 sg[nents].length  = bytesRemaining;
815
 nents++;
816
817
 return pci_map_sg(pChan->pPciDev, sg, nents, pChan->sg_dma_direction);
818
819
} /* end IdeBuildSglist */
820
821
/************************************************************************
822
 * Prepares a dma request
823
 ************************************************************************/
824
static int
825
IdeBuildDmaTable(PChannel pChan, PSCSI_REQUEST_BLOCK Srb)
826
{
827
 unsigned int *		table = pChan->dmatable_cpu;
828
 unsigned int		count = 0;
829
 int			i;
830
 struct scatterlist *	sg;
831
832
 pChan->sg_nents = i = IdeBuildSglist(pChan, Srb);
833
834
 sg = pChan->sg_table;
835
 while (i && sg_dma_len(sg))
836
    {
837
     u32	cur_len;
838
     u32	cur_addr;
839
840
     cur_addr = sg_dma_address(sg);
841
     cur_len  = sg_dma_len(sg);
842
843
     /*
844
      * Fill in the dma table, without crossing any 64kB boundaries.
845
      */
846
     while (cur_len)
847
	{
848
	 if (count++ >= PRD_ENTRIES)
849
	    {
850
	     printk(KERN_WARNING "@@DMA table too small\n");
851
	    }
852
	 else
853
	    {
854
	     u32 xcount, bcount = 0x10000 - (cur_addr & 0xFFFF);
855
856
	     if (bcount > cur_len) bcount = cur_len;
857
	     *table++ = cpu_to_le32(cur_addr);
858
	     xcount = bcount & 0xFFFF;
859
	     if (xcount == 0x0000)
860
		{
861
		 /*
862
		  * Most chipsets correctly interpret a length
863
		  * of 0x0000 as 64KB, but at least one
864
		  * (e.g. CS5530) misinterprets it as zero (!).
865
		  * So here we break the 64KB entry into two
866
		  * 32KB entries instead.
867
		  */
868
		 if (count++ >= PRD_ENTRIES)
869
			printk(KERN_WARNING "##DMA table too small\n");
870
871
		 *table++ = cpu_to_le32(0x8000);
872
		 *table++ = cpu_to_le32(cur_addr + 0x8000);
873
		 xcount = 0x8000;
874
		}
875
	     *table++ = cpu_to_le32(xcount);
876
	     cur_addr += bcount;
877
	     cur_len -= bcount;
878
	    }
879
	}
880
     sg++;
881
     i--;
882
    }
883
884
 if (count)
885
    {
886
     *--table |= cpu_to_le32(0x80000000);
887
     return count;
888
    }
889
 else
890
   {
891
    printk(KERN_WARNING "Empty DMA table?\n");
892
   }
893
894
 return count;
895
896
} /* end IdeBuildDmaTable */
897
898
/************************************************************************
899
 * Prepares a dma scatter/gather request
900
 ************************************************************************/
901
static void
902
IdeBuildDmaSgTable(PChannel pChan, PSCSI_REQUEST_BLOCK Srb)
903
{
904
905
 int			use_sg = 0;
906
 int			i;
907
 PPRD_TABLE_ENTRY	pSG = (PPRD_TABLE_ENTRY)pChan->dmatable_cpu;
908
 struct scatterlist *	sg = (struct scatterlist *)Srb->DataBuffer;
909
910
 if (Srb->SrbFlags & SRB_FLAGS_DATA_IN)
911
    {
912
     pChan->sg_dma_direction = PCI_DMA_FROMDEVICE;
913
    }
914
 else
915
    {
916
     pChan->sg_dma_direction = PCI_DMA_TODEVICE;
917
    }
918
919
 use_sg = pci_map_sg(pChan->pPciDev, Srb->DataBuffer, Srb->UseSg, pChan->sg_dma_direction);
920
921
 for (i = 0; i < use_sg; i++)
922
    {
923
     pSG[i].PhysicalBaseAddress	= sg_dma_address(&sg[i]);
924
     pSG[i].ByteCount		= sg_dma_len(&sg[i]);
925
     pSG[i].EndOfTable		= (i == use_sg - 1) ? SG_FLAG_EOT : 0;
926
    }
927
928
} /* end IdeBuildDmaSgTable */
929
930
/************************************************************************
931
 * Setup DMA table for channel
932
 ************************************************************************/
933
static void
934
IdeSetupDma(PChannel pChan, ULONG dma_base, USHORT num_ports)
935
{
936
937
 char *		name0 = "CHANNEL0";
938
 char *		name1 = "CHANNEL1";
939
940
 printk("Channel[%d] BM-DMA at 0x%04lX-0x%04lX\n", pChan->channel, dma_base,
941
	dma_base + num_ports - 1);
942
943
 /*
944
  * Check if this io space is used by other device.
945
  */
946
 if (check_region(dma_base, num_ports))
947
    {
948
     printk("Port addresses already in use.\n");
949
     return;
950
    }
951
952
 /*
953
  * Request the io space (region).
954
  */
955
 if (pChan->channel == 0)
956
    {
957
     request_region(dma_base, num_ports, name0);
958
    }
959
 else
960
    {
961
     request_region(dma_base, num_ports, name1);
962
    }
963
964
 /*
965
  * Allocate IDE DMA buffer.
966
  */
967
 pChan->dmatable_cpu = pci_alloc_consistent(pChan->pPciDev,
968
	 PRD_ENTRIES * PRD_BYTES, &pChan->dmatable_dma);
969
970
 if (pChan->dmatable_cpu == NULL)
971
    {
972
     printk("Allocate prd table failed.\n");
973
     return;
974
    }
975
976
 memset(pChan->dmatable_cpu, 0, PRD_ENTRIES * PRD_BYTES);
977
978
 /*
979
  * Allocate SCATTER/GATHER table buffer.
980
  */
981
 pChan->sg_table = kmalloc(sizeof(struct scatterlist) * PRD_ENTRIES,
982
				 GFP_KERNEL);
983
984
 if (pChan->sg_table == NULL)
985
    {
986
     printk("Allocate sg_table failed.\n");
987
     pci_free_consistent(pChan->pPciDev, PRD_ENTRIES * PRD_BYTES,
988
		pChan->dmatable_cpu, pChan->dmatable_dma);
989
     return;
990
    }
991
992
 return;
993
994
} /* end IdeSetupDma */
995
996
/************************************************************************
997
 *
998
 ************************************************************************/
999
void
1000
IT8212ReconfigChannel(PChannel pChan, UCHAR ArrayId, UCHAR Operation)
1001
{
1002
1003
 UCHAR			enableVirtualChannel;
1004
 struct pci_dev *	pPciDev = pChan->pPciDev;
1005
1006
 pci_read_config_byte(pPciDev, 0x43, &enableVirtualChannel);
1007
1008
 if (Operation == DisableChannel)
1009
    {
1010
     enableVirtualChannel &= ~(1 << ArrayId);
1011
     printk("IT8212ReconfigChannel: Disable channel %X\n", ArrayId);
1012
    }
1013
 else
1014
    {
1015
     enableVirtualChannel |= ~(1 << ArrayId);
1016
     printk("IT8212ReconfigChannel: Enable channel %X\n", ArrayId);
1017
    }
1018
1019
 printk("IT8212ReconfigChannel: Channel enabled after set 0x%X\n",
1020
		 enableVirtualChannel);
1021
1022
 /*
1023
  * Set enabled virtual channels.
1024
  */
1025
 pci_write_config_byte(pPciDev, 0x43, enableVirtualChannel);
1026
1027
} /* end IT8212ReconfigChannel */
1028
1029
/************************************************************************
1030
 * Get the chip status. This is a vendor specific command. According to
1031
 * all of the device configurations, the BIOS then can consider the
1032
 * existing RAID configuration reasonable. If the existing RAID configur-
1033
 * ation is not reasonable, or if there is NO existing RAID configuration
1034
 * , the BIOS can ask the user to setup the RAID configuration. Finally,
1035
 * the BIOS or AP should send the SET CHIP STATUS to every virtual device.
1036
 * Only after receiving SET CHIP STATUS command, the corresponding virtual
1037
 * device will be active.
1038
 ************************************************************************/
1039
UCHAR
1040
IT8212GetChipStatus(uioctl_t *ioc)
1041
{
1042
1043
 ULONG			PriMasterIsNull = FALSE;
1044
 UCHAR			statusByte;
1045
 UCHAR			srbStatus;
1046
 PITE_ADAPTER		pAdap;
1047
 PChannel		pChan;
1048
 PHYSICAL_DISK_STATUS *	pPhyDiskInfo;
1049
1050
 printk("IT8212GetChipStatus enter\n");
1051
1052
 /*
1053
  * Only support one controller now! In the future, we can pass the
1054
  * argument (user ioctl structure) to know which controller need to be
1055
  * identified.
1056
  */
1057
 pAdap = ite_adapters[0];
1058
 pChan = &pAdap->IDEChannel[0];
1059
1060
 /*
1061
  * Allocate space for PHYSICAL_DISK_STATUS.
1062
  */
1063
 if ((pPhyDiskInfo = kmalloc(sizeof(PHYSICAL_DISK_STATUS) * 4, GFP_KERNEL)) == NULL)
1064
    {
1065
     printk("error kmalloc on PHYSCIAL_DISK_STATUS.\n");
1066
     return -ENOMEM;
1067
    }
1068
1069
1070
 /*
1071
  * Always send GET CHIP STATUS command to primary channel master device.
1072
  * Select device.
1073
  */
1074
 outb((UCHAR) 0xA0, pChan->io_ports[IDE_SELECT_OFFSET]);
1075
1076
 /*
1077
  * If primary master channel is not enabled, enable it.
1078
  */
1079
 statusByte = inb(pChan->io_ports[IDE_ALTERNATE_OFFSET]);
1080
 if (statusByte == 0)
1081
    {
1082
     PriMasterIsNull = TRUE;
1083
     IT8212ReconfigChannel(pChan, 0, EnableChannel);
1084
    }
1085
1086
 /*
1087
  * Wait for device ready (Not BUSY and not DRQ)
1088
  */
1089
 WaitForDeviceReady(pChan, statusByte);
1090
 if ((statusByte & IDE_STATUS_BUSY) || (statusByte & IDE_STATUS_DRQ) ||
1091
     (statusByte == 0))
1092
    {
1093
     printk("IT8212GetChipStatus: Disk[0] not ready. Status=0x%X\n",
1094
		     statusByte);
1095
     srbStatus = SRB_STATUS_BUSY;
1096
     goto exit;
1097
    }
1098
1099
 /*
1100
  * Disable interrupt to avoid the unexpected interrupt.
1101
  */
1102
 outb(IDE_DC_DISABLE_INTERRUPTS, pChan->io_ports[IDE_CONTROL_OFFSET]);
1103
1104
 /*
1105
  * Issue the command.
1106
  */
1107
 outb(IDE_COMMAND_GET_CHIP_STATUS, pChan->io_ports[IDE_COMMAND_OFFSET]);
1108
1109
 /*
1110
  * Wait for BUSY = 0, DRQ = 1.
1111
  */
1112
 CheckBusyDrq(pChan, statusByte)
1113
 if (statusByte != 0x58)
1114
    {
1115
     printk("IT8212GetChipStatus: Disk[0] return unexpected status after");
1116
     printk("issue command. Status=0x%X\n", statusByte);
1117
     goto exit_error;
1118
    }
1119
1120
 /*
1121
  * Read the physical disk info.
1122
  */
1123
 ReadBuffer(pChan, (PUSHORT)pPhyDiskInfo, 256);
1124
1125
 /*
1126
  * Copy physical disk info to user area.
1127
  */
1128
 copy_to_user((PUSHORT)ioc->data, (PUSHORT)pPhyDiskInfo, 256);
1129
1130
 /*
1131
  * Check error.
1132
  */
1133
 WaitForCommandComplete(pChan, statusByte);
1134
1135
 if (statusByte != IDE_STATUS_IDLE)
1136
    {
1137
     printk("IT8212GetChipStatus: Disk[0] return unexpected status after");
1138
     printk("read data. Status=0x%X\n", statusByte);
1139
     goto exit_error;
1140
    }
1141
1142
 srbStatus = SRB_STATUS_SUCCESS;
1143
 goto exit;
1144
1145
exit_error:
1146
1147
 /*
1148
  * If fail, hard reset to avoid the DRQ status pending.
1149
  */
1150
 srbStatus = SRB_STATUS_ERROR;
1151
 IdeHardReset(pChan, statusByte);
1152
1153
exit:
1154
1155
 /*
1156
  * Reenable interrupt after command complete.
1157
  */
1158
 outb(IDE_DC_REENABLE_CONTROLLER, pChan->io_ports[IDE_CONTROL_OFFSET]);
1159
1160
 /*
1161
  * If primary master is null, disable primary master channel before we leave.
1162
  */
1163
 if (PriMasterIsNull)
1164
    {
1165
     IT8212ReconfigChannel(pChan, 0, DisableChannel);
1166
    }
1167
1168
1169
 printk("IT8212GetChipStatus exit\n");
1170
1171
 return srbStatus;
1172
1173
} /* end IT8212GetChipStatus */
1174
1175
/************************************************************************
1176
 * Erase the partition table.
1177
 ************************************************************************/
1178
UCHAR
1179
IT8212ErasePartition(uioctl_t *pioc)
1180
{
1181
1182
 UCHAR			drvSelect;
1183
 UCHAR			statusByte = 0;
1184
 UCHAR			srbStatus;
1185
 UCHAR *		buffer;
1186
 PRAID_CREATE_INFO	createInfo = (PRAID_CREATE_INFO) pioc->data;
1187
 PITE_ADAPTER		pAdap;
1188
 PChannel		pChan;
1189
1190
 if (!createInfo->ErasePartition || (createInfo->RaidType == RAID_LEVEL_NODISK))
1191
	 return SRB_STATUS_SUCCESS;
1192
1193
 pAdap = ite_adapters[0];
1194
1195
 if (createInfo->DiskArrayId < 2) pChan = &pAdap->IDEChannel[0];
1196
 pChan = &pAdap->IDEChannel[1];
1197
1198
 /*
1199
  * Allocate 512 bytes for buffer.
1200
  */
1201
 if ((buffer = kmalloc(512, GFP_KERNEL)) == NULL)
1202
    {
1203
     printk("IT8212ErasePartition:  error kmalloc.\n");
1204
     return -ENOMEM;
1205
    }
1206
1207
 memset(buffer, 0, 512);
1208
1209
 /*
1210
  * Select device.
1211
  */
1212
 drvSelect = (((UCHAR) createInfo->DiskArrayId & 0x1) << 4) | 0xA0 | 0x40;
1213
 outb(drvSelect, pChan->io_ports[IDE_SELECT_OFFSET]);
1214
1215
 /*
1216
  * Wait for device ready (not BUSY and not DRQ).
1217
  */
1218
 WaitForDeviceReady(pChan, statusByte);
1219
 if ((statusByte & IDE_STATUS_BUSY) || (statusByte & IDE_STATUS_DRQ) ||
1220
     (statusByte == 0))
1221
    {
1222
     printk("IT8212ErasePartition: Disk[%d] not ready. Status=0x%X\n",
1223
		     createInfo->DiskArrayId, statusByte);
1224
     return SRB_STATUS_BUSY;
1225
    }
1226
1227
 /*
1228
  * Disable interrupt to avoid the unexpected interrupt.
1229
  */
1230
 outb(IDE_DC_DISABLE_INTERRUPTS, pChan->io_ports[IDE_CONTROL_OFFSET]);
1231
1232
 /*
1233
  * Write LBA 0 (1 sector).
1234
  */
1235
 outb(1, pChan->io_ports[IDE_NSECTOR_OFFSET]);
1236
 outb(0, pChan->io_ports[IDE_LOCYL_OFFSET]);
1237
 outb(0, pChan->io_ports[IDE_MIDCYL_OFFSET]);
1238
 outb(0, pChan->io_ports[IDE_HCYL_OFFSET]);
1239
 outb(drvSelect, pChan->io_ports[IDE_SELECT_OFFSET]);
1240
 outb(IDE_COMMAND_WRITE_SECTOR, pChan->io_ports[IDE_COMMAND_OFFSET]);
1241
1242
 /*
1243
  * Wait for BUSY = 0, DRQ = 1.
1244
  */
1245
 CheckBusyDrq(pChan, statusByte);
1246
 if (statusByte != 0x58)
1247
    {
1248
     printk("IT8212ErasePartition: Disk[%d] error status. Status=0x%X\n",
1249
		     createInfo->DiskArrayId, statusByte);
1250
     goto exit_error;
1251
    }
1252
1253
 /*
1254
  * Start erase partition table.
1255
  */
1256
 WriteBuffer(pChan, (PUSHORT)buffer, 256);
1257
1258
 /*
1259
  * Check error.
1260
  */
1261
 WaitForCommandComplete(pChan, statusByte);
1262
1263
 if (statusByte != IDE_STATUS_IDLE)
1264
    {
1265
     printk("IT8212ErasePartition: Disk[%d] error status. Status=0x%X\n",
1266
		     createInfo->DiskArrayId, statusByte);
1267
     goto exit_error;
1268
    }
1269
1270
 srbStatus = SRB_STATUS_SUCCESS;
1271
 goto exit;
1272
1273
exit_error:
1274
1275
 /*
1276
  * If failed, hard reset to avoid the DRQ status pending.
1277
  */
1278
 IdeHardReset(pChan, statusByte);
1279
 srbStatus = SRB_STATUS_ERROR;
1280
1281
exit:
1282
1283
 /*
1284
  * Reenable interrupt after command complete.
1285
  */
1286
 outb(IDE_DC_REENABLE_CONTROLLER, pChan->io_ports[IDE_CONTROL_OFFSET]);
1287
1288
 return srbStatus;
1289
1290
} /* end IT8212ErasePartition */
1291
1292
/************************************************************************
1293
 *
1294
 ************************************************************************/
1295
UCHAR
1296
IT8212TruncateReduentSectors
1297
(
1298
 ULONG		OriginalSectorCount,
1299
 USHORT		StripeSizeInKBytes
1300
)
1301
{
1302
1303
 USHORT		stripeSizeInSector;
1304
1305
 /*
1306
  * 0 means using default value (32 sectors).
1307
  */
1308
 if (StripeSizeInKBytes == 0)
1309
    {
1310
     stripeSizeInSector = 64 * 2;
1311
    }
1312
 else
1313
    {
1314
     stripeSizeInSector = StripeSizeInKBytes * 2;
1315
    }
1316
1317
 return ((OriginalSectorCount / stripeSizeInSector) * stripeSizeInSector);
1318
1319
1320
} /* end IT8212TruncateReduentSectors */
1321
1322
/************************************************************************
1323
 *
1324
 ************************************************************************/
1325
ULONG
1326
IT8212DiskArrayAddressableSector(PUCHAR DiskArrayCreateInfo)
1327
{
1328
1329
 PRAID_CREATE_INFO	createInfo = (PRAID_CREATE_INFO) DiskArrayCreateInfo;
1330
 ULONG			MinDiskCapacity;
1331
 ULONG			ArrayCapacity;
1332
 UCHAR			DiskNo;
1333
 UCHAR			NumOfDisks;
1334
1335
 MinDiskCapacity = ArrayCapacity = NumOfDisks = 0;
1336
1337
 printk("createInfo->AddressableSectors[0] = 0x%lX\n", createInfo->AddressableSectors[0]);
1338
 printk("createInfo->AddressableSectors[1] = 0x%lX\n", createInfo->AddressableSectors[1]);
1339
 printk("createInfo->AddressableSectors[2] = 0x%lX\n", createInfo->AddressableSectors[2]);
1340
 printk("createInfo->AddressableSectors[3] = 0x%lX\n", createInfo->AddressableSectors[3]);
1341
1342
 for (DiskNo = 0; DiskNo < 4; DiskNo++)
1343
    {
1344
     /*
1345
      * If disk exist.
1346
      */
1347
     if ((createInfo->ContainingDisks >> DiskNo) & 0x1)
1348
	{
1349
	 NumOfDisks += 1;
1350
	 if (!MinDiskCapacity || (createInfo->AddressableSectors[DiskNo] <
1351
	      MinDiskCapacity))
1352
	    {
1353
	     MinDiskCapacity = createInfo->AddressableSectors[DiskNo];
1354
	    }
1355
	}
1356
    }
1357
1358
 switch (createInfo->RaidType)
1359
    {
1360
     /*
1361
      * Containing 2 or 3 or 4 disks.
1362
      */
1363
     case RAID_LEVEL_0:
1364
	 MinDiskCapacity = IT8212TruncateReduentSectors(MinDiskCapacity - 2,
1365
			createInfo->StripeSize);
1366
	 ArrayCapacity = MinDiskCapacity - 2;
1367
	 break;
1368
1369
     /*
1370
      * Containing 2 disks.
1371
      */
1372
     case RAID_LEVEL_1:
1373
	 ArrayCapacity = MinDiskCapacity - 2;
1374
	 break;
1375
1376
     /*
1377
      * Containing 4 disks.
1378
      */
1379
     case RAID_LEVEL_10:
1380
	 MinDiskCapacity = IT8212TruncateReduentSectors(MinDiskCapacity - 2,
1381
			createInfo->StripeSize);
1382
	 ArrayCapacity = MinDiskCapacity * 2;
1383
	 break;
1384
1385
     /*
1386
      * Containing 2, 3, or 4 disks.
1387
      */
1388
     case RAID_LEVEL_JBOD:
1389
	 for (DiskNo = 0; DiskNo < 4; DiskNo++)
1390
	    {
1391
	     if ((createInfo->ContainingDisks >> DiskNo) & 0x1)
1392
		{
1393
		 ArrayCapacity = ArrayCapacity + (createInfo->AddressableSectors[DiskNo] - 2);
1394
		}
1395
	    }
1396
	 break;
1397
1398
     /*
1399
      * Containing only 1 disk.
1400
      */
1401
     case RAID_LEVEL_NORMAL:
1402
	ArrayCapacity = MinDiskCapacity;
1403
	break;
1404
1405
    }
1406
1407
 return ArrayCapacity;
1408
1409
} /* end IT8212DiskArrayAddressableSector */
1410
1411
/************************************************************************
1412
 * Create a new array.
1413
 ************************************************************************/
1414
UCHAR
1415
IT8212CreateDiskArray(uioctl_t *pioc)
1416
{
1417
1418
 UCHAR				i;
1419
 UCHAR				subCommand = 0xFF;
1420
 UCHAR				statusByte;
1421
 UCHAR				dmaSupported;
1422
 UCHAR				udmaSupported;
1423
 UCHAR				srbStatus;
1424
 void *				buffer;
1425
 ULONG				UserAddressableSectors;
1426
 ULONG				PriMasterIsNull = FALSE;
1427
 PRAID_CREATE_INFO		createInfo = (PRAID_CREATE_INFO)pioc->data;
1428
 PIDENTIFY_DATA2		identifyData;
1429
 PIT8212_SET_CHIP_STATUS_INFO	setChipStatus;
1430
 PITE_ADAPTER			pAdap;
1431
 PChannel			pChan;
1432
1433
 USHORT	IT8212TimingTable[7] =
1434
 {
1435
  0x3133,			/* UDMA timimg register 01		*/
1436
  0x2121,			/* UDMA timimg register 23		*/
1437
  0x9111,			/* UDMA timimg register 45		*/
1438
  0x0091,			/* UDMA timimg register 6		*/
1439
  0x3266,			/* DMA  timimg register 01		*/
1440
  0x0021,			/* DMA  timimg register 2		*/
1441
  0x0021			/* PIO  timimg register			*/
1442
 };
1443
1444
 USHORT	IT8212ClockTable[7] =
1445
 {
1446
  0x0505,			/* UDMA clock register 01		*/
1447
  0x0005,			/* UDMA clock register 23		*/
1448
  0x0500,			/* UDMA clock register 45		*/
1449
  0x0000,			/* UDMA clock register 6		*/
1450
  0x0005,			/* DMA  clock register 01		*/
1451
  0x0005,			/* DMA  clock register 2		*/
1452
  0x0005			/* PIO  clock register			*/
1453
 };
1454
1455
 switch (createInfo->RaidType)
1456
    {
1457
     case RAID_LEVEL_0:
1458
     case RAID_LEVEL_1:
1459
     case RAID_LEVEL_10:
1460
     case RAID_LEVEL_JBOD:
1461
     case RAID_LEVEL_NORMAL:
1462
	 subCommand = 0x50;
1463
	 break;
1464
1465
     case RAID_LEVEL_NODISK:
1466
	 subCommand = 0x48;
1467
	 break;
1468
    }
1469
1470
 pAdap = ite_adapters[pioc->AdapterNo];
1471
 if (pioc->DiskNo < 2)
1472
    {
1473
     pChan = &pAdap->IDEChannel[0];
1474
    }
1475
 else
1476
    {
1477
     pChan = &pAdap->IDEChannel[1];
1478
    }
1479
1480
 /*
1481
  * Allocate 512-bytes buffer.
1482
  */
1483
 if ((buffer = kmalloc(512, GFP_KERNEL)) == NULL)
1484
    {
1485
     printk("IT8212CreateDiskArray: error kmalloc.\n");
1486
     return -ENOMEM;
1487
    }
1488
1489
 identifyData  = (PIDENTIFY_DATA2) buffer;
1490
 setChipStatus = (PIT8212_SET_CHIP_STATUS_INFO) (buffer + 256);
1491
1492
 /*
1493
  * Configure to RAID or NORMAL.
1494
  */
1495
 if (subCommand == 0x50)
1496
    {
1497
     /*
1498
      * Zero identify data structure.
1499
      */
1500
     memset((PUCHAR) identifyData, 0, sizeof(IDENTIFY_DATA));
1501
1502
     /*
1503
      * Fill up identify data.
1504
      */
1505
     memmove(identifyData->ModelNumber, createInfo->ModelNumber, 40);
1506
     memmove(identifyData->SerialNumber, &createInfo->SerialNumber, sizeof(RAID_SERIAL_NUMBER));
1507
1508
     /*
1509
      * Set disk array virtual capacity.
1510
      */
1511
     UserAddressableSectors = IT8212DiskArrayAddressableSector(pioc->data);
1512
1513
     printk("IT8212CreateDiskArray: Array Capacity = %lX\n", UserAddressableSectors);
1514
1515
     identifyData->Capacity_48bit_LOW  = UserAddressableSectors;
1516
     identifyData->Capacity_48bit_HIGH = 0;
1517
1518
     if (UserAddressableSectors > 0x0FFFFFFF)
1519
	{
1520
	 identifyData->UserAddressableSectors = 0x0FFFFFFF;
1521
	}
1522
     else
1523
	{
1524
	 identifyData->UserAddressableSectors = UserAddressableSectors;
1525
	}
1526
1527
     /*
1528
      * Get DMA supported mode and UDMA supported mode.
1529
      */
1530
     dmaSupported = udmaSupported =0xFF;
1531
1532
     for (i = 0; i < 4; i++)
1533
	{
1534
	 if ((createInfo->ContainingDisks >> i) & 1)
1535
	    {
1536
	     dmaSupported  &= (UCHAR) createInfo->DMASupported[i];
1537
	     udmaSupported &= (UCHAR) createInfo->UDMASupported[i];
1538
	    }
1539
	}
1540
1541
     identifyData->MultiWordDMASupport	= dmaSupported;
1542
     identifyData->UltraDMASupport	= udmaSupported;
1543
1544
     /*
1545
      * Fill up SET CHIP STATUS data (word 129 - 153)
1546
      */
1547
     setChipStatus->RaidType		 = createInfo->RaidType;
1548
     setChipStatus->ContainingDisks	 = createInfo->ContainingDisks;
1549
     setChipStatus->UltraDmaTiming01	 = IT8212TimingTable[0];
1550
     setChipStatus->UltraDmaTiming23	 = IT8212TimingTable[1];
1551
     setChipStatus->UltraDmaTiming45	 = IT8212TimingTable[2];
1552
     setChipStatus->UltraDmaTiming6	 = IT8212TimingTable[3];
1553
     setChipStatus->MultiWordDmaTiming01 = IT8212TimingTable[4];
1554
     setChipStatus->UltraDmaTiming2	 = IT8212TimingTable[5];
1555
     setChipStatus->PioTiming4		 = IT8212TimingTable[6];
1556
     setChipStatus->AutoRebuildEnable	 = createInfo->AutoRebuildEnable;
1557
     setChipStatus->IdeClkUDma01	 = IT8212ClockTable[0];
1558
     setChipStatus->IdeClkUDma23	 = IT8212ClockTable[1];
1559
     setChipStatus->IdeClkUDma45	 = IT8212ClockTable[2];
1560
     setChipStatus->IdeClkUDma6		 = IT8212ClockTable[3];
1561
     setChipStatus->IdeClkMDma01	 = IT8212ClockTable[4];
1562
     setChipStatus->IdeClkMDma2		 = IT8212ClockTable[5];
1563
     setChipStatus->IdeClkPio4		 = IT8212ClockTable[6];
1564
     setChipStatus->StripeSize		 = createInfo->StripeSize;
1565
     setChipStatus->BootableDisk	 = createInfo->BootableDisk;
1566
     setChipStatus->CheckHotSwapInterval = 0;
1567
     setChipStatus->TargetSourceDisk	 = createInfo->TargetSourceDisk;
1568
     setChipStatus->RebuildBlockSize	 = 0;
1569
     setChipStatus->ResetInterval1	 = 0;
1570
     setChipStatus->ResetInterval2	 = 0;
1571
     setChipStatus->RebuildRetryTimes	 = 0;
1572
     setChipStatus->NewlyCreated	 = createInfo->NewlyCreated;
1573
    }
1574
1575
 /*
1576
  * There are some contrains of disk placement. AP will take care of it.
1577
  */
1578
1579
 /*
1580
  * This command should be sent to Virtual Primary Master.
1581
  */
1582
1583
 /*
1584
  * Select device.
1585
  */
1586
 outb((UCHAR) 0xA0, pChan->io_ports[IDE_SELECT_OFFSET]);
1587
1588
 /*
1589
  * If primary master channel is not enabled, enable it.
1590
  */
1591
 statusByte = inb(pChan->io_ports[IDE_CONTROL_OFFSET]);
1592
 if (statusByte == 0)
1593
    {
1594
     PriMasterIsNull = TRUE;
1595
     IT8212ReconfigChannel(pChan, 0, EnableChannel);
1596
    }
1597
1598
 /*
1599
  * Wait for device ready (not BUSY and not DRQ)
1600
  */
1601
 WaitForDeviceReady(pChan, statusByte);
1602
 if ((statusByte & IDE_STATUS_BUSY) || (statusByte & IDE_STATUS_DRQ) ||
1603
     (statusByte == 0))
1604
    {
1605
     printk("IT8212CreateDiskArray: Disk[0] not ready. status=0x%X\n",
1606
		statusByte);
1607
     srbStatus = SRB_STATUS_BUSY;
1608
     goto exit;
1609
    }
1610
1611
 /*
1612
  * Disable interrupt to avoid the unexpected interrupt.
1613
  */
1614
 outb(IDE_DC_DISABLE_INTERRUPTS, pChan->io_ports[IDE_CONTROL_OFFSET]);
1615
1616
 outb(subCommand, pChan->io_ports[IDE_FEATURE_OFFSET]);
1617
 outb(createInfo->DiskArrayId, pChan->io_ports[IDE_SELECT_OFFSET]);
1618
 outb(IDE_COMMAND_SET_CHIP_STATUS, pChan->io_ports[IDE_COMMAND_OFFSET]);
1619
1620
 /*
1621
  * No disk (no data command protocol)
1622
  */
1623
 if (subCommand == 0x48)
1624
    {
1625
     WaitForCommandComplete(pChan, statusByte);
1626
     if (statusByte != IDE_STATUS_IDLE)
1627
	{
1628
	 printk("IT8212CreateDiskArray: Disk[0] return unexpected status after issue command.\n");
1629
	 goto exit_error;
1630
	}
1631
1632
     IT8212ReconfigChannel(pChan, createInfo->DiskArrayId, DisableChannel);
1633
     srbStatus = SRB_STATUS_SUCCESS;
1634
     goto exit;
1635
    }
1636
1637
 /*
1638
  * Create RAID (PIO data out command protocol).
1639
  */
1640
 if (subCommand == 0x50)
1641
    {
1642
     /*
1643
      * Wait for BUSY=0, DRQ=1.
1644
      */
1645
     CheckBusyDrq(pChan, statusByte);
1646
     if (statusByte != 0x58)
1647
	{
1648
	 goto exit_error;
1649
	}
1650
1651
     IT8212ReconfigChannel(pChan, createInfo->DiskArrayId, EnableChannel);
1652
     srbStatus = SRB_STATUS_ERROR;
1653
    }
1654
1655
exit_error:
1656
1657
 /*
1658
  * If fail, hard reset to avoid the DRQ pending.
1659
  */
1660
 IdeHardReset(pChan, statusByte);
1661
 srbStatus = SRB_STATUS_ERROR;
1662
1663
exit:
1664
1665
 /*
1666
  * If primary master is null, and we are not configuring array 0. Disable
1667
  * primary master channel again.
1668
  */
1669
 if (PriMasterIsNull && createInfo->DiskArrayId)
1670
    {
1671
     IT8212ReconfigChannel(pChan, 0 , DisableChannel);
1672
    }
1673
1674
 /*
1675
  * Reenable interrupt after command complete.
1676
  */
1677
 outb(IDE_DC_REENABLE_CONTROLLER, pChan->io_ports[IDE_CONTROL_OFFSET]);
1678
1679
 return srbStatus;
1680
1681
1682
} /* end IT8212CreateDiskArray */
1683
1684
/************************************************************************
1685
 * Return "virtual" drive 512 bytes identification data.
1686
 ************************************************************************/
1687
UCHAR
1688
IT8212IssueIdentify(uioctl_t *pioc)
1689
{
1690
1691
 UCHAR		channum;
1692
 UCHAR		devnum;
1693
 UCHAR		statusByte;
1694
 UCHAR		srbStatus = 0;
1695
 PITE_ADAPTER	pAdap;
1696
 PChannel	pChan;
1697
1698
 /*
1699
  * Only support one adapter now! In the future, we can pass the argument
1700
  * to know which adapter need to be identified.
1701
  */
1702
 pAdap = ite_adapters[pioc->AdapterNo];
1703
1704
 memset(pioc->data, 0, 512 * 4);
1705
1706
 /*
1707
  * Two channels per controller.
1708
  */
1709
 for (channum = 0; channum < pAdap->num_channels; channum++)
1710
    {
1711
     pChan = &pAdap->IDEChannel[channum];
1712
1713
     /*
1714
      * Two devices per channel.
1715
      */
1716
     for (devnum = 0; devnum < 2; devnum++)
1717
	{
1718
	 /*
1719
	  * Select device.
1720
	  */
1721
	 outb((UCHAR)((devnum << 4) | 0xA0), pChan->io_ports[IDE_SELECT_OFFSET]);
1722
1723
	 /*
1724
	  * Check if disk online?
1725
	  */
1726
	 statusByte = inb(pChan->io_ports[IDE_ALTERNATE_OFFSET]);
1727
	 if ((statusByte & 0x40) != 0x40)
1728
	    {
1729
	     printk("IT8212IssueIdentify: Disk[%d] is offline\n", devnum + channum * 2);
1730
	     continue;
1731
	    }
1732
1733
	 /*
1734
	  * Wait for device ready (Not busy and not DRQ)
1735
	  */
1736
	 WaitForDeviceReady(pChan, statusByte);
1737
1738
	 if ((statusByte & IDE_STATUS_BUSY) || (statusByte & IDE_STATUS_DRQ) ||
1739
	     (statusByte == 0))
1740
	    {
1741
	     printk("IT8212IssueIdentify: Disk[%d] not ready. Status=0x%X\n",
1742
			devnum + channum * 2, statusByte);
1743
	     srbStatus = SRB_STATUS_BUSY;
1744
	     goto exit;
1745
	    }
1746
1747
	 /*
1748
	  * Disable interrupt to avoid the unexpected interrupt.
1749
	  */
1750
	 outb(IDE_DC_DISABLE_INTERRUPTS, pChan->io_ports[IDE_CONTROL_OFFSET]);
1751
1752
	 /*
1753
	  * Issue command.
1754
	  */
1755
	 outb(IDE_COMMAND_IDENTIFY, pChan->io_ports[IDE_COMMAND_OFFSET]);
1756
1757
	 /*
1758
	  * Wait for BUSY = 0 and DRQ = 1.
1759
	  */
1760
	 CheckBusyDrq(pChan, statusByte);
1761
	 if (statusByte != 0x58)
1762
	    {
1763
	     printk("IT8212IssueIndetify:Disk[%d] returns unexpedted status after issue command. status=0x%X\n",
1764
			devnum + channum * 2, statusByte);
1765
	     goto error;
1766
	    }
1767
1768
	 /*
1769
	  * Read the identify data.
1770
	  */
1771
	 ReadBuffer(pChan, (PUSHORT)&pChan->FullIdentifyData, 256);
1772
1773
	 /*
1774
	  * Then copy to user area.
1775
	  * unsigned long copy_to_user(void *to, const void *from, unsigned long count).
1776
	  */
1777
	 copy_to_user((PUSHORT) (pioc->data + ((devnum + channum * 2) * 512)),
1778
	 		 (PUSHORT)&pChan->FullIdentifyData, 256);
1779
1780
	 /*
1781
	  * Check error after reading data.
1782
	  */
1783
	 WaitForCommandComplete(pChan, statusByte);
1784
	 if (statusByte != IDE_STATUS_IDLE)
1785
	    {
1786
	     printk("IT8212IssueIdentify: Disk[%d] returns unexpected status after read data. status=0x%X\n",
1787
			devnum + channum * 2, statusByte);
1788
	     goto error;
1789
	    }
1790
1791
	} /* end for each device */
1792
1793
    } /* end for each channel */
1794
1795
 srbStatus = SRB_STATUS_SUCCESS;
1796
 goto exit;
1797
1798
error:
1799
1800
 /*
1801
  * If failed, hard reset to avoid the IRQ pending.
1802
  */
1803
 IdeHardReset(pChan, statusByte);
1804
 srbStatus = SRB_STATUS_ERROR;
1805
1806
exit:
1807
1808
 /*
1809
  * Reenable interrupt after command complete.
1810
  */
1811
 for (channum = 0; channum < pAdap->num_channels; channum++)
1812
    {
1813
     pChan = &pAdap->IDEChannel[channum];
1814
     outb(IDE_DC_REENABLE_CONTROLLER, pChan->io_ports[IDE_CONTROL_OFFSET]);
1815
    }
1816
1817
 return srbStatus;
1818
1819
} /* end IT8212IssueIdentify */
1820
1821
/************************************************************************
1822
 * Reset the controller
1823
 ************************************************************************/
1824
BOOLEAN
1825
IT8212ResetAdapter(void)
1826
{
1827
1828
 BOOLEAN	resetChannel[2];
1829
 UCHAR		channel;
1830
 UCHAR		device;
1831
 UCHAR		i;
1832
 UCHAR		status[4];
1833
 PChannel	pChan;
1834
 PITE_ADAPTER	pAdap = ite_adapters[0];
1835
1836
 /*
1837
  * First, perform ATAPI softreset if ATAPI devices attached.
1838
  */
1839
 for (channel = 0; channel < 2; channel++)
1840
    {
1841
     pChan = &pAdap->IDEChannel[channel];
1842
     resetChannel[channel] = FALSE;
1843
     for (device = 0; device < 2; device++)
1844
	{
1845
	 if (pChan->DeviceFlags[device] & DFLAGS_DEVICE_PRESENT)
1846
	    {
1847
	     if (pChan->DeviceFlags[device] & DFLAGS_ATAPI_DEVICE)
1848
		{
1849
		 printk("IT8212ResetAdapter: Perform ATAPI Soft Reset (%d, %d)\n",
1850
			channel, device);
1851
		 AtapiSoftReset(pChan, device);
1852
		}
1853
	     else
1854
		{
1855
		 resetChannel[channel] = TRUE;
1856
		}
1857
	    }
1858
	}
1859
    }
1860
1861
 /*
1862
  * If ATA device present on this channel, perform channel reset.
1863
  */
1864
 for (channel = 0; channel < 2; channel++)
1865
    {
1866
     if (resetChannel[channel])
1867
	{
1868
	 printk("IT8212ResetAdapter: Reset channel %d\n", channel);
1869
	 outb(IDE_DC_RESET_CONTROLLER, pChan->io_ports[IDE_CONTROL_OFFSET]);
1870
	 mdelay(50);
1871
	 outb(IDE_DC_REENABLE_CONTROLLER, pChan->io_ports[IDE_CONTROL_OFFSET]);
1872
	}
1873
    }
1874
1875
 /*
1876
  * Check device status after reset.
1877
  */
1878
 for (i = 0; i < 1000 * 1000; i++)
1879
    {
1880
     for (channel = 0; channel < 2; channel++)
1881
	{
1882
	 for (device = 0; device < 2; device++)
1883
	    {
1884
	     if (pChan->DeviceFlags[device] & DFLAGS_DEVICE_PRESENT)
1885
		{
1886
		 outb((UCHAR)((device << 4) | 0xA0), pChan->io_ports[IDE_SELECT_OFFSET]);
1887
		 status[(channel * 2) + device] = inb(pChan->io_ports[IDE_COMMAND_OFFSET]);
1888
		}
1889
	     else
1890
		{
1891
		 status[(channel * 2) + device] = 0;
1892
		}
1893
	    }
1894
	}
1895
1896
     /*
1897
      * ATA device should present status 0x50 after reset.
1898
      * ATAPI device should present status 0 after reset.
1899
      */
1900
     if ((status[0] != IDE_STATUS_IDLE && status[0] != 0x0) || (status[1] != IDE_STATUS_IDLE && status[1] != 0x0) ||
1901
	 (status[2] != IDE_STATUS_IDLE && status[2] != 0x0) || (status[3] != IDE_STATUS_IDLE && status[3] != 0x0))
1902
	{
1903
	 udelay(30);
1904
	}
1905
     else
1906
	{
1907
	 break;
1908
	}
1909
    }
1910
1911
 if (i == 1000 * 1000)
1912
    {
1913
     printk("IT8212ResetAdapter Fail!\n");
1914
     printk("Device status after reset = [0x%x, 0x%x, 0x%x, 0x%x]\n",
1915
	status[0], status[1], status[2], status[3]);
1916
     return FALSE;
1917
    }
1918
 else
1919
    {
1920
     printk("IT8212ResetAdapter Success!\n");
1921
     return TRUE;
1922
    }
1923
1924
} /* end IT8212ResetAdapter */
1925
1926
/************************************************************************
1927
 * Rebuild disk array.
1928
 ************************************************************************/
1929
UCHAR
1930
IT8212Rebuild(uioctl_t *pioc)
1931
{
1932
1933
 UCHAR			rebuildDirection;
1934
 UCHAR			statusByte = 0;
1935
 PRAID_REBUILD_INFO	apRebuildInfo = (PRAID_REBUILD_INFO) pioc->data;
1936
 PITE_ADAPTER		pAdap;
1937
 PChannel		pChan;
1938
1939
 rebuildDirection = (apRebuildInfo->Resume << 4) | (apRebuildInfo->DestDisk << 2) |
1940
	 	     apRebuildInfo->SrcDisk;
1941
1942
 apRebuildInfo->Status = 0xFF;
1943
1944
 pAdap = ite_adapters[pioc->AdapterNo];
1945
 if (pioc->DiskNo < 2)
1946
    {
1947
     pChan = &pAdap->IDEChannel[0];
1948
    }
1949
 else
1950
    {
1951
     pChan = &pAdap->IDEChannel[1];
1952
    }
1953
1954
 /*
1955
  * Selcet device.
1956
  */
1957
 outb((UCHAR)((apRebuildInfo->DiskArrayId & 0x1) << 4 | 0xA0),
1958
		 pChan->io_ports[IDE_SELECT_OFFSET]);
1959
1960
 /*
1961
  * Wait for device ready (not BUSY and not DRQ).
1962
  */
1963
 WaitForDeviceReady(pChan, statusByte);
1964
 if ((statusByte & IDE_STATUS_BUSY) || (statusByte & IDE_STATUS_DRQ) ||
1965
     (statusByte == 0))
1966
    {
1967
     apRebuildInfo->Status = REBUILD_ERR_DISK_BUSY;
1968
     printk("IT8212Rebuild: Disk[%d] not ready. Status=0x%X\n",
1969
		     apRebuildInfo->DiskArrayId, statusByte);
1970
     return SRB_STATUS_BUSY;
1971
    }
1972
1973
 /*
1974
  * Disable interrupt to avoid the unexpected interrupt.
1975
  */
1976
 outb(IDE_DC_DISABLE_INTERRUPTS, pChan->io_ports[IDE_CONTROL_OFFSET]);
1977
1978
 /*
1979
  * Give a direction.
1980
  */
1981
 outb(rebuildDirection, pChan->io_ports[IDE_FEATURE_OFFSET]);
1982
1983
 /*
1984
  * Issue a REBUILD commmand.
1985
  */
1986
 outb(IDE_COMMAND_REBUILD, pChan->io_ports[IDE_COMMAND_OFFSET]);
1987
1988
 /*
1989
  * Check for errors.
1990
  */
1991
 WaitForCommandComplete(pChan, statusByte);
1992
1993
 /*
1994
  * Reenable interrupt after command complete.
1995
  */
1996
 outb(IDE_DC_REENABLE_CONTROLLER, pChan->io_ports[IDE_CONTROL_OFFSET]);
1997
1998
 if (statusByte != IDE_STATUS_IDLE)
1999
    {
2000
     if (statusByte & IDE_STATUS_ERROR)
2001
	{
2002
	 apRebuildInfo->Status = inb(pChan->io_ports[IDE_NSECTOR_OFFSET]);
2003
	 printk("IT8212Rebuild:Rebuild error. reason=0x%X\n", apRebuildInfo->Status);
2004
	}
2005
     return SRB_STATUS_ERROR;
2006
    }
2007
2008
 return SRB_STATUS_SUCCESS;
2009
2010
} /* end IT8212Rebuild */
2011
2012
/************************************************************************
2013
 * Switch to DMA mode if necessary.
2014
 *
2015
 * offset 0x50 = PCI Mode Control Register
2016
 *
2017
 * Bit 0 = PCI Mode Select (1=firmware mode, 0=transparent mode)
2018
 * Bit 1 = Primary Channel IDE Clock Frequency Select (1=50, 0=66)
2019
 * Bit 2 = Secondary Channel IDE Clock Freq Select (1=50, 0=66)
2020
 * Bit 3 = Primary   Channel Dev 0 Transfer Mode (1=MultiWord DMA, 0=Ultra DMA)
2021
 * Bit 4 = Primary   Channel Dev 1 Transfer Mode (1=MultiWord DMA, 0=Ultra DMA)
2022
 * Bit 5 = Secondary Channel Dev 0 Transfer Mode (1=MultiWord DMA, 0=Ultra DMA)
2023
 * Bit 6 = Secondary Channel Dev 1 Transfer Mode (1=MultiWord DMA, 0=Ultra DMA)
2024
 * Bit 7 = PCI Mode Reset
2025
 ************************************************************************/
2026
VOID
2027
IT8212SwitchDmaMode(PChannel pChan, UCHAR DeviceId)
2028
{
2029
2030
 UCHAR			pciControl;
2031
 UCHAR			channel;
2032
 UCHAR			device;
2033
 UCHAR			configByte = 0;
2034
 UCHAR			RevisionID;
2035
 struct pci_dev *	pPciDev = pChan->pPciDev;
2036
2037
 /*
2038
  * These tables are for performance issue. Better formance than lots
2039
  * of "Shifts".
2040
  */
2041
 UCHAR		dmaModeV10[4]  = {0x18, 0x18, 0x60, 0x60};
2042
 UCHAR		udmaModeV10[4] = {0xE7, 0xE7, 0x9F, 0x9F};
2043
 UCHAR		ideClock[4]    = {0xFD, 0xFD, 0xFB, 0xFB};
2044
2045
#if (0)
2046
 UCHAR		dmaMode[4]     = {0x08, 0x10, 0x20, 0x40};
2047
 UCHAR		udmaMode[4]    = {0xF7, 0xEF, 0xDF, 0xBF};
2048
#endif
2049
2050
 /*
2051
  * channel --> 0-1; device --> 0-1; DeviceId --> 0-3;
2052
  */
2053
 channel = DeviceId >> 1;
2054
 device  = DeviceId & 1;
2055
2056
 /*
2057
  * Do nothing if the mode switch is unnecessary.
2058
  */
2059
 if (!pChan->DoSwitch || pChan->ActiveDevice == DeviceId)
2060
    {
2061
     dprintk("Do not need to switch mode!\n");
2062
     return;
2063
    }
2064
2065
 printk("IT8212SwitchDmaMode: Switch DMA mode for dev (%x)\n",
2066
	DeviceId);
2067
2068
 pci_read_config_byte(pPciDev, 0x50, &pciControl);
2069
 pci_read_config_byte(pPciDev, 0x08, &RevisionID);
2070
2071
 /*
2072
  * Running on MULTIWORD_DMA mode.
2073
  */
2074
 if (pChan->DmaType[device] == USE_MULTIWORD_DMA)
2075
    {
2076
     /*
2077
      * Switch to DMA mode.
2078
      */
2079
     if (RevisionID == 0x10)
2080
	{
2081
	 configByte = pciControl | dmaModeV10[DeviceId];
2082
	}
2083
     pci_write_config_byte(pPciDev, 0x50, configByte);
2084
    }
2085
 /*
2086
  * Running on ULTRA DMA mode.
2087
  */
2088
 else
2089
    {
2090
     /*
2091
      * Select UDMA mode.
2092
      */
2093
     configByte = pciControl;
2094
     if (RevisionID == 0x10)
2095
	{
2096
	 configByte &= udmaModeV10[DeviceId];
2097
	}
2098
2099
     /*
2100
      * Select IDE clock.
2101
      */
2102
     configByte = (configByte & ideClock[DeviceId]) |
2103
		  (pChan->IdeClock[device] << (channel + 1));
2104
2105
     pci_write_config_byte(pPciDev, 0x50, configByte);
2106
2107
     /*
2108
      * Set UDMA timing.
2109
      *
2110
      * offset 0x56 = PCI Mode Primary Device 0 Ultra DMA Timing Registers
2111
      * offset 0x57 = PCI Mode Primary Device 1 Ultra DMA Timing Registers
2112
      * offset 0x5A = PCI Mode Secondary Device 0 Ultra DMA Timing Registers
2113
      * offset 0x5B = PCI Mode Secondary Device 1 Ultra DMA Timing Registers
2114
      */
2115
     if (RevisionID == 0x10)
2116
	{
2117
	 configByte = pChan->UdmaTiming[device];
2118
	 pci_write_config_byte(pPciDev, (UCHAR) (0x56 + (channel * 4)), configByte);
2119
	 pci_write_config_byte(pPciDev, (UCHAR) (0x56 + (channel * 4) + 1), configByte);
2120
	}
2121
2122
     /*
2123
      * Set PIO/DMA timing (Becasuse maybe the IDE clock is changed.)
2124
      */
2125
     configByte = pChan->PioDmaTiming[pChan->IdeClock[device]];
2126
     pci_write_config_byte(pPciDev, (UCHAR) (0x54 + (channel * 4)), configByte);
2127
    }
2128
2129
 /*
2130
  * Record the Active device on this channel
2131
  */
2132
 pChan->ActiveDevice = device;
2133
2134
} /* end IT8212SwitchDmaMode */
2135
2136
/************************************************************************
2137
 * IT8212 read/write routines
2138
 ************************************************************************/
2139
ULONG
2140
IT8212ReadWrite(PChannel pChan, PSCSI_REQUEST_BLOCK Srb)
2141
{
2142
2143
 UCHAR		statusByte = 0;
2144
 ULONG		startingSector;
2145
 ULONG		sectorNumber;
2146
 ULONG		capacity;
2147
 PITE_ADAPTER	pAdap = ite_adapters[0];
2148
2149
 /*
2150
  * Return error if overrun.
2151
  */
2152
 startingSector = ((PCDB)Srb->Cdb)->CDB10.LogicalBlockByte3		|
2153
		  ((PCDB)Srb->Cdb)->CDB10.LogicalBlockByte2 << 8	|
2154
		  ((PCDB)Srb->Cdb)->CDB10.LogicalBlockByte1 << 16	|
2155
		  ((PCDB)Srb->Cdb)->CDB10.LogicalBlockByte0 << 24;
2156
2157
 sectorNumber = (USHORT) ((Srb->DataTransferLength + 0x1FF) / 0x200);
2158
2159
 capacity = pChan->IdentifyData[Srb->TargetId & 0x1].UserAddressableSectors;
2160
 if (capacity == 0x0FFFFFFF)
2161
    {
2162
     capacity = pChan->IdentifyData[Srb->TargetId].Capacity_48bit_LOW;
2163
    }
2164
2165
 if ((startingSector + sectorNumber - 1) > capacity)
2166
    {
2167
     printk("IT8212ReadWrite: Disk[%d] over disk size.\n", Srb->TargetId);
2168
     printk("Capacity: %ld. Starting sector: %ld. Sector number: %ld\n",
2169
		     capacity, startingSector, sectorNumber);
2170
     return SRB_STATUS_ERROR;
2171
    }
2172
2173
 /*
2174
  * Select device.
2175
  */
2176
 outb((UCHAR)((Srb->TargetId & 0x1) << 4 | 0xA0), pChan->io_ports[IDE_SELECT_OFFSET]);
2177
2178
 /*
2179
  * Wait for device ready (Not Busy and Not DRQ).
2180
  */
2181
 WaitForDeviceReady(pChan, statusByte);
2182
 if ((statusByte & IDE_STATUS_BUSY) || (statusByte & IDE_STATUS_DRQ) ||
2183
     (statusByte == 0))
2184
    {
2185
     printk("IT8212ReadWrite: Disk[%d] not ready. Status=0x%x\n",
2186
	Srb->TargetId, statusByte);
2187
     return SRB_STATUS_BUSY;
2188
    }
2189
2190
 /*
2191
  * First, switch to DMA or UDMA mode if running on bypass mode.
2192
  */
2193
 if (pAdap->bypass_mode)
2194
    {
2195
     IT8212SwitchDmaMode(pChan,	Srb->TargetId);
2196
    }
2197
2198
 /*
2199
  * Check the SCATTER/GATHER count. The upper will give the different
2200
  * memory address depend on whether use_sg is used or not.
2201
  */
2202
 if (Srb->UseSg == 0)
2203
    {
2204
     IdeBuildDmaTable(pChan, Srb);
2205
    }
2206
 else
2207
    {
2208
     IdeBuildDmaSgTable(pChan, Srb);
2209
    }
2210
2211
 /*
2212
  * Start transfer the data.
2213
  */
2214
 IdeStartTransfer(pChan, Srb, startingSector, sectorNumber);
2215
2216
 /*
2217
  * Wait for interrupt.
2218
  */
2219
 return SRB_STATUS_PENDING;
2220
2221
} /* end IT8212ReadWrite */
2222
2223
/************************************************************************
2224
 * Setup the transfer mode.
2225
 ************************************************************************/
2226
VOID
2227
IT8212SetTransferMode
2228
(
2229
 PChannel	pChan,
2230
 ULONG		DiskId,
2231
 UCHAR		TransferMode,
2232
 UCHAR	 	ModeNumber
2233
)
2234
{
2235
2236
 UCHAR		statusByte = 0;
2237
2238
 /*
2239
  * Select device.
2240
  */
2241
 outb((UCHAR) ((DiskId & 0x1) << 4 | 0xA0), pChan->io_ports[IDE_SELECT_OFFSET]);
2242
2243
 /*
2244
  * Wait for device ready (Not Busy and Not DRQ).
2245
  */
2246
 WaitForDeviceReady(pChan, statusByte);
2247
 if ((statusByte & IDE_STATUS_BUSY) || (statusByte & IDE_STATUS_DRQ))
2248
    {
2249
     printk("IT8212SetTransferMode: Disk[%ld] not ready. Status=0x%x\n",
2250
     DiskId, statusByte);
2251
     return;
2252
    }
2253
2254
 /*
2255
  * Feature number ==> 03
2256
  *
2257
  * Mode contained in Sector Count Register.
2258
  *
2259
  * Bits(7:3)	Bits(2:0)	Mode
2260
  *
2261
  * 00000	000		PIO default mode
2262
  * 00000	001		PIO default mode, disable IORDY
2263
  * 00001	mode		PIO flow control transfer mode
2264
  * 00010	mode		Single Word DMA mode
2265
  * 00100	mode		Multi-word DMA mode
2266
  * 01000	mode		Ultra DMA mode
2267
  */
2268
 TransferMode |= ModeNumber;
2269
2270
 outb(0x03,			pChan->io_ports[IDE_FEATURE_OFFSET]);
2271
 outb(TransferMode,		pChan->io_ports[IDE_NSECTOR_OFFSET]);
2272
 outb(0,			pChan->io_ports[IDE_HCYL_OFFSET]);
2273
 outb(0,			pChan->io_ports[IDE_MIDCYL_OFFSET]);
2274
 outb(IDE_COMMAND_SET_FEATURE,	pChan->io_ports[IDE_COMMAND_OFFSET]);
2275
2276
 /*
2277
  * Check error.
2278
  */
2279
 WaitForBaseCommandComplete(pChan, statusByte);
2280
2281
 if ((statusByte != IDE_STATUS_IDLE) && (statusByte != 0))
2282
    {
2283
     printk("IT8212SetTransferMode: Disk[%ld]", DiskId);
2284
     printk("return unexpected status after issue command. 0x%x\n",
2285
	statusByte);
2286
    }
2287
2288
} /* end IT8212SetTransferMode */
2289
2290
/************************************************************************
2291
 * Set the best transfer mode for device
2292
 ************************************************************************/
2293
VOID
2294
IT8212SetBestTransferMode(PITE_ADAPTER pAdap, PChannel pChan, UCHAR channel)
2295
{
2296
2297
 UCHAR			i;
2298
 UCHAR			k;
2299
 UCHAR			transferMode;
2300
 UCHAR			modeNumber;
2301
 UCHAR			pciControl;
2302
 UCHAR			device;
2303
 UCHAR			configByte;
2304
 UCHAR			cableStatus[2] = {CABLE_40_PIN, CABLE_40_PIN};
2305
 UCHAR			RevisionID;
2306
 struct pci_dev *	pPciDev = pChan->pPciDev;
2307
 PIDENTIFY_DATA2	ideIdentifyData;
2308
2309
 /*
2310
  * UDMA timing table for 66MHz clock.
2311
  * UDMA timing table for 50MHz clock.
2312
  * Best of IDE clock in this mode.
2313
  */
2314
 UCHAR	udmaTiming[3][7] =
2315
 {
2316
  {0x44, 0x42, 0x31, 0x21, 0x11, 0x22, 0x11},
2317
  {0x33, 0x31, 0x21, 0x21, 0x11, 0x11, 0x11},
2318
  {IDE_CLOCK_66, IDE_CLOCK_50, IDE_CLOCK_66, IDE_CLOCK_66, IDE_CLOCK_66, IDE_CLOCK_50, IDE_CLOCK_66}
2319
 };
2320
2321
 /*
2322
  * DMA timing table for 66 MHz clock.
2323
  * DMA timing table for 50 MHz clock.
2324
  */
2325
 UCHAR	dmaTiming[2][3] =
2326
 {
2327
  {0x88, 0x32, 0x31},
2328
  {0x66, 0x22, 0x21}
2329
 };
2330
2331
 /*
2332
  * PIO timing table for 66 MHz clock.
2333
  * PIO timing table for 50 MHz clock.
2334
  */
2335
 UCHAR	pioTiming[2][5] =
2336
 {
2337
  {0xAA, 0xA3, 0xA1, 0x33, 0x31},
2338
  {0x88, 0x82, 0x81, 0x32, 0x21}
2339
 };
2340
2341
 UCHAR	pio_dma_timing[2][2][4] =
2342
 {
2343
  {{0, 0, 0, 0}, {0, 0, 0, 0}},
2344
  {{0, 0, 0, 0}, {0, 0, 0, 0}}
2345
 };
2346
2347
 /*
2348
  * These tables are for performance issue. Better formance than lots
2349
  * of "Shifts".
2350
  */
2351
 //UCHAR dmaModeV10[4] = {0x18, 0x18, 0x60, 0x60};
2352
 UCHAR	udmaModeV10[4] = {0xE7, 0xE7, 0x9F, 0x9F};
2353
 UCHAR	dmaMode[4]     = {0x08, 0x10, 0x20, 0x40};
2354
 UCHAR	udmaMode[4]    = {0xF7, 0xEF, 0xDF, 0xBF};
2355
 UCHAR	ideClock[4]    = {0xFD, 0xFD, 0xFB, 0xFB};
2356
2357
 for (i = 0; i < 2; i++)
2358
    {
2359
     /*
2360
      * The dafault of cable status is in PCI configuration 0x40.
2361
      */
2362
     cableStatus[i] = pChan->Cable80[i];
2363
2364
     /*
2365
      * channel -->0 to 1.
2366
      * device  -->0 or 1.
2367
      */
2368
     pChan->UseDma[i] = TRUE;
2369
     device	      = i & 1;
2370
2371
     if (!(pChan->DeviceFlags[i] & DFLAGS_DEVICE_PRESENT) ||
2372
	  (pChan->DeviceFlags[i] & DFLAGS_CONFIG_CHANGED))
2373
	{
2374
	 pio_dma_timing[0][channel][device] = pio_dma_timing[0][channel][device + 2] = 0;
2375
	 pio_dma_timing[1][channel][device] = pio_dma_timing[1][channel][device + 2] = 0;
2376
	 continue;
2377
	}
2378
2379
     /*
2380
      * Set PIO Mode.
2381
      */
2382
     ideIdentifyData = &pChan->IdentifyData[i];
2383
     if ((!(ideIdentifyData->ValidFieldIndicator & 0x02)) || (ideIdentifyData->AdvancedPIOModes == 0))
2384
	{
2385
	 transferMode = PIO_FLOW_CONTROL;
2386
	 modeNumber = 2;
2387
	}
2388
     else
2389
	{
2390
	 transferMode = PIO_FLOW_CONTROL;
2391
	 modeNumber = RaidGetHighestBit((UCHAR) ideIdentifyData->AdvancedPIOModes) + 3;
2392
	}
2393
2394
     IT8212SetTransferMode(pChan, i, transferMode, modeNumber);
2395
2396
     /*
2397
      * Record the PIO timing for later use.(0 to 4)
2398
      */
2399
     pio_dma_timing[0][channel][device] = pioTiming[0][modeNumber];
2400
     pio_dma_timing[1][channel][device] = pioTiming[1][modeNumber];
2401
2402
     /*
2403
      * Get the best transfer mode (maybe Ultra DMA or Multi-Word DMA).
2404
      */
2405
     ideIdentifyData = &pChan->IdentifyData[i];
2406
     if ((!(ideIdentifyData->ValidFieldIndicator & 0x04)) || (ideIdentifyData->UltraDMASupport == 0))
2407
	{
2408
	 /*
2409
	  * UltraDMA is not valid.
2410
	  */
2411
	 transferMode = MULTIWORD_DMA;
2412
	 modeNumber = RaidGetHighestBit(ideIdentifyData->MultiWordDMASupport);
2413
	 printk("The best transfer mode of Device[%d] is DMA-%d\n", i, modeNumber);
2414
	}
2415
     else
2416
	{
2417
	 transferMode = ULTRA_DMA;
2418
	 modeNumber = RaidGetHighestBit(ideIdentifyData->UltraDMASupport);
2419
	 printk("The best transfer mode of Device[%d] is Ultra-%d\n", i, modeNumber);
2420
2421
	 /*
2422
	  * If this is 40-pin cable. Limit to Ultra DMA mode 2.
2423
	  */
2424
#	 if (0)
2425
	 if ((cableStatus[i] == CABLE_40_PIN) && (modeNumber > 2))
2426
	    {
2427
	     printk("Reduce trans mode of Device[%d] to Ultra-2 for cable issue.\n", i);
2428
	     modeNumber = 0x02;
2429
	    }
2430
#	 endif
2431
	}
2432
2433
     IT8212SetTransferMode(pChan, i, transferMode, modeNumber);
2434
2435
     /*
2436
      * If running on ByPass mode, driver must take the responsibility to
2437
      * set the PIO/DMA/UDMA timing.
2438
      */
2439
     if (pAdap->bypass_mode)
2440
	{
2441
	 pci_read_config_byte(pPciDev, 0x50, &pciControl);
2442
	 pci_read_config_byte(pPciDev, 0x08, &RevisionID);
2443
2444
	 if (transferMode == ULTRA_DMA)
2445
	    {
2446
	     /*
2447
	      * Set this channel to UDMA mode (not only the device).
2448
	      */
2449
	     if (RevisionID == 0x10)
2450
		{
2451
		 configByte = pciControl & udmaModeV10[i + channel * 2];
2452
		}
2453
	     else
2454
		{
2455
		 configByte = pciControl & udmaMode[i + channel * 2];
2456
		}
2457
2458
	     /*
2459
	      * Select IDE clock (50MHz or 66MHz).
2460
	      */
2461
	     configByte &= ideClock[i + channel * 2];
2462
	     configByte |= (udmaTiming[2][modeNumber] << (channel + 1));
2463
2464
	     pci_write_config_byte(pPciDev, 0x50, configByte);
2465
2466
	     /*
2467
	      * Set UDMA timing.
2468
	      */
2469
	     configByte = udmaTiming[udmaTiming[2][modeNumber]][modeNumber];
2470
	     if (modeNumber == 5 || modeNumber == 6)
2471
		{
2472
		 /*
2473
		  * Enable UDMA mode 5/6
2474
		  */
2475
		 configByte |= UDMA_MODE_5_6;
2476
		}
2477
2478
	     /*
2479
	      * Bug Bug. Fill these two fields into the same value.
2480
	      */
2481
	     if (RevisionID == 0x10)
2482
		{
2483
		 pci_write_config_byte(pPciDev, (UCHAR) (0x56 + (channel * 4)), configByte);
2484
		 pci_write_config_byte(pPciDev, (UCHAR) (0x56 + (channel * 4) + 1), configByte);
2485
		}
2486
	     else
2487
		{
2488
		 pci_write_config_byte(pPciDev, (UCHAR) (0x56 + (channel * 4) + device), configByte);
2489
		}
2490
2491
	     /*
2492
	      * Record the best UDMA mode for this device.
2493
	      */
2494
	     pChan->DmaType[i]	  = ULTRA_DMA;
2495
	     pChan->IdeClock[i]	  = udmaTiming[2][modeNumber];
2496
	     pChan->UdmaTiming[i] = configByte;
2497
	    }
2498
	else if (transferMode == MULTIWORD_DMA)
2499
	    {
2500
	     /*
2501
	      * If an ATAPI device with DMA mode, force it to run in PIO mode.
2502
	      */
2503
	     if (RevisionID == 0x10 && pChan->DeviceFlags[i] & DFLAGS_ATAPI_DEVICE)
2504
		{
2505
		 pChan->UseDma[i] = FALSE;
2506
		}
2507
	     else
2508
		{
2509
		 /*
2510
		  * Set this device to DMA mode.
2511
		  */
2512
		 configByte = pciControl | dmaMode[i + channel * 2];
2513
		 pci_write_config_byte(pPciDev, 0x50, configByte);
2514
2515
		 /*
2516
		  * Record DMA timing (for later use).
2517
		  */
2518
		 pio_dma_timing[0][channel][device + 2] = dmaTiming[0][modeNumber];
2519
		 pio_dma_timing[1][channel][device + 2] = dmaTiming[1][modeNumber];
2520
		}
2521
	     pChan->DmaType[i] = USE_MULTIWORD_DMA;
2522
	    }
2523
	 pChan->ActiveDevice = device;
2524
	}
2525
    }
2526
2527
 /*
2528
  * Because each channel owns one PIO/DMA timimg register only, so we
2529
  * must set the timing to the slowest one to fit all. Really stupid H/W! :(
2530
  */
2531
 if (pAdap->bypass_mode)
2532
    {
2533
     /*
2534
      * Loop for the two IDE clocks (50 MHz and 66 MHz).
2535
      */
2536
     for (i = 0; i < 2; i++)
2537
	{
2538
	 configByte = 0;
2539
	 for (k = 0; k < 4; k++)
2540
	    {
2541
	     /*
2542
	      * High part.
2543
	      */
2544
	     if ((pio_dma_timing[i][channel][k] & 0xF0) > (configByte & 0xF0))
2545
		{
2546
		 configByte = (configByte & 0xF) | (pio_dma_timing[i][channel][k] & 0xF0);
2547
		}
2548
2549
	     /*
2550
	      * Low part.
2551
	      */
2552
	     if ((pio_dma_timing[i][channel][k] & 0xF) > (configByte & 0xF))
2553
		{
2554
		 configByte = (configByte & 0xF0) | (pio_dma_timing[i][channel][k] & 0xF);
2555
		}
2556
	    }
2557
2558
	 /*
2559
	  * Record the PIO/DMA timing for this channel.
2560
	  */
2561
	 pChan->PioDmaTiming[i] = configByte;
2562
	}
2563
2564
     /*
2565
      * Set PIO/DMA timing register for each channel.
2566
      */
2567
     configByte = pChan->PioDmaTiming[(pciControl >> (channel + 1)) & 1];
2568
     if (configByte != 0)
2569
	{
2570
	 pci_write_config_byte(pPciDev, (UCHAR) (0x54 + (channel * 4)), configByte);
2571
	}
2572
2573
     /*
2574
      * Check shall we do switch between the two devices
2575
      */
2576
     for (i = 0; i < 2; i++)
2577
 	{
2578
	 pChan->DoSwitch = TRUE;
2579
2580
	 /*
2581
	  * Master is not present
2582
	  */
2583
	 if (!(pChan->DeviceFlags[i] & DFLAGS_DEVICE_PRESENT) || (pChan->DeviceFlags[i] & DFLAGS_CONFIG_CHANGED))
2584
	    {
2585
	     printk("Channel %x: Master is not present. No switch mode.\n", channel);
2586
	     pChan->DoSwitch = FALSE;
2587
	     continue;
2588
	    }
2589
2590
	 /*
2591
	  * Slave is not present
2592
	  */
2593
	 if (!(pChan->DeviceFlags[i + 1] & DFLAGS_DEVICE_PRESENT) || (pChan->DeviceFlags[i + 1] & DFLAGS_CONFIG_CHANGED))
2594
	    {
2595
	     printk("Channel %x: Slave is not present. No switch mode.\n", channel);
2596
	     pChan->DoSwitch = FALSE;
2597
	     continue;
2598
	    }
2599
2600
	 /*
2601
	  * If both devices are running on DMA mode, no switch.
2602
	  */
2603
	 if (pChan->DmaType[i] == USE_MULTIWORD_DMA && pChan->DmaType[i + 1] == USE_MULTIWORD_DMA)
2604
	    {
2605
	     printk("Channel %x: Run on DMA mode only. No switch mode.\n", channel);
2606
	     pChan->DoSwitch = FALSE;
2607
	     continue;
2608
	    }
2609
2610
	 /*
2611
	  * No switch if the two devices are running on the same mode.
2612
	  */
2613
	 if ((pChan->DmaType[i]    == pChan->DmaType[i + 1]   ) &&
2614
	     (pChan->UdmaTiming[i] == pChan->UdmaTiming[i + 1]) &&
2615
	     (pChan->IdeClock[i]   == pChan->IdeClock[i + 1]  ))
2616
	    {
2617
	     printk("Channel %x: Two dev run on the same mode. No switch mode.\n", channel);
2618
	     pChan->DoSwitch = FALSE;
2619
	     continue;
2620
	    }
2621
2622
	 printk("Channel %x: Switch mode if needed.\n", channel);
2623
	}
2624
    }
2625
2626
} /* end IT8212SetBestTransferMode */
2627
2628
/************************************************************************
2629
 * Initialize bypass(transparent) mode if BIOS is not ready
2630
 ************************************************************************/
2631
BOOLEAN
2632
IT8212InitBypassMode(struct pci_dev *pPciDev)
2633
{
2634
2635
 USHORT			configWord = 0;
2636
2637
 /*
2638
  * 1.2. Reset local CPU, and set BIOS not ready.
2639
  */
2640
 pci_write_config_byte(pPciDev, 0x5E, 0x01);
2641
2642
 /*
2643
  * 3.4. Set to bypass mode, and reset PCI bus.
2644
  */
2645
 pci_write_config_byte(pPciDev, 0x50, 0x80);
2646
 pci_write_config_byte(pPciDev, 0x50, 0x00);
2647
2648
 /*
2649
  * 5. Set PIO timing register of primary register.
2650
  */
2651
 pci_write_config_byte(pPciDev, 0x54, 0xA3);
2652
2653
 /*
2654
  * 6. Set PIO timing register of secondary register.
2655
  */
2656
 pci_write_config_byte(pPciDev, 0x58, 0xA3);
2657
2658
 /*
2659
  * Testing.
2660
  */
2661
 pci_write_config_word(pPciDev, 0x4, 0x7);
2662
2663
 pci_read_config_word(pPciDev,  0x50, &configWord);
2664
 configWord = configWord | 0xA000;
2665
 pci_write_config_word(pPciDev, 0x40, configWord);
2666
2667
 pci_write_config_byte(pPciDev, 0x0D, 0x00);
2668
 pci_write_config_dword(pPciDev,0x4C, 0x02040204);
2669
 pci_write_config_byte(pPciDev, 0x42, 0x36);
2670
2671
 return TRUE;
2672
2673
} /* end IT8212InitBypassMode */
2674
2675
/************************************************************************
2676
 * This is the interrupt service routine for ATAPI IDE miniport driver.
2677
 * TURE if expecting an interrupt.
2678
 ************************************************************************/
2679
BOOLEAN
2680
IT8212Interrupt(PChannel pChan)
2681
{
2682
2683
 UCHAR			statusByte = 0;
2684
 UCHAR			bmstatus = 0;
2685
 ULONG			i = 0;
2686
 ULONG			bmbase = pChan->dma_base;
2687
 PSCSI_REQUEST_BLOCK	Srb = pChan->CurrentSrb;
2688
 PITE_ADAPTER		pAdap = ite_adapters[0];
2689
2690
 if (!Srb || !pChan->ExpectingInterrupt)
2691
    {
2692
     dprintk("!Suspicious Interrupt\n");
2693
2694
     /*
2695
      * Clear interrupt by reading status register.
2696
      */
2697
     outb((UCHAR) 0xA0, pChan->io_ports[IDE_SELECT_OFFSET]);
2698
     GetBaseStatus(pChan, statusByte);
2699
     outb((UCHAR) 0xB0, pChan->io_ports[IDE_SELECT_OFFSET]);
2700
     GetBaseStatus(pChan, statusByte);
2701
     outb(bmbase + 2, (UCHAR) (inb(bmbase + 2) | BM_STAT_FLG_INT));
2702
2703
     return FALSE;
2704
    }
2705
2706
 /*
2707
  * To handle share IRQ condition.
2708
  * If the interrupt is not ours, return FALSE.
2709
  */
2710
 bmstatus = inb(bmbase + 2);
2711
 if ((bmstatus & BM_STAT_FLG_INT) == 0)
2712
    {
2713
     printk("IT8212Interrupt: Spurious interrupt (Interrupt bit is not on)\n");
2714
     return FALSE;
2715
    }
2716
2717
 /*
2718
  * Bug Fix: All PIO access are blocked during bus master operation, so
2719
  * stop bus master operation before we try to access IDE registers.
2720
  */
2721
 if (pAdap->bypass_mode)
2722
    {
2723
     outb(bmbase, 0);
2724
    }
2725
2726
 /*
2727
  * Clear interrupt by reading status.
2728
  */
2729
 GetBaseStatus(pChan, statusByte);
2730
 outb(bmbase + 2, (UCHAR) (bmstatus | BM_STAT_FLG_INT));
2731
2732
 /*
2733
  * Handle ATAPI interrupt.
2734
  */
2735
 if (pChan->DeviceFlags[Srb->TargetId & 1] & DFLAGS_ATAPI_DEVICE)
2736
    {
2737
     return AtapiInterrupt(pChan);
2738
    }
2739
2740
 pChan->ExpectingInterrupt = FALSE;
2741
2742
 if ((statusByte & IDE_STATUS_BUSY) || (statusByte & IDE_STATUS_DRQ))
2743
    {
2744
     /*
2745
      * Ensure BUSY and DRQ is non-asserted.
2746
      */
2747
     for (i = 0; i < 100; i++)
2748
	{
2749
	 GetBaseStatus(pChan, statusByte);
2750
	 if (!(statusByte & IDE_STATUS_BUSY) && !(statusByte & IDE_STATUS_DRQ))
2751
	    {
2752
	     break;
2753
	    }
2754
	 mdelay(5);
2755
	}
2756
2757
     if (i == 100)
2758
	{
2759
	 printk("IT8212Interrupt: Disk[%x] return busy or drq status. Status = 0x%x\n",
2760
		Srb->TargetId, statusByte);
2761
	 return FALSE;
2762
	}
2763
     }
2764
2765
 if (statusByte & IDE_STATUS_ERROR)
2766
    {
2767
     /*
2768
      * Stop bus master operation.
2769
      */
2770
     outb(bmbase, 0);
2771
2772
     printk("IT8212Interrupt: ERROR!\n");
2773
2774
     /*
2775
      * Map error to specific SRB status and handle request sense.
2776
      */
2777
     Srb->SrbStatus = MapError(pChan, Srb);
2778
    }
2779
 else
2780
    {
2781
     Srb->SrbStatus = SRB_STATUS_SUCCESS;
2782
    }
2783
2784
 pChan->CurrentSrb = NULL;
2785
2786
 TaskDone(pChan, Srb);
2787
2788
 return TRUE;
2789
2790
} /* end IT8212Interrupt */
2791
2792
/************************************************************************
2793
 * This is the interrupt service routine for ATAPI IDE miniport driver.
2794
 * TRUE if expecting an interrupt. Remember the ATAPI io registers are
2795
 * different from IDE io registers and this is for each channel not for
2796
 * entire controller.
2797
 ************************************************************************/
2798
BOOLEAN
2799
AtapiInterrupt(PChannel pChan)
2800
{
2801
2802
 ULONG			wordCount = 0;
2803
 ULONG			wordsThisInterrupt = 256;
2804
 ULONG			status;
2805
 ULONG			i;
2806
 UCHAR			statusByte;
2807
 UCHAR			interruptReason;
2808
 PSCSI_REQUEST_BLOCK	srb = pChan->CurrentSrb;
2809
2810
 /*
2811
  * Clear interrupt by reading status.
2812
  */
2813
 GetBaseStatus(pChan, statusByte);
2814
2815
 dprintk("AtapiInterrupt: Entered with status (%x)\n", statusByte);
2816
2817
 if (statusByte & IDE_STATUS_BUSY)
2818
    {
2819
     /*
2820
      * Ensure BUSY is non-asserted.
2821
      */
2822
     for (i = 0; i < 10; i++)
2823
	{
2824
	 GetBaseStatus(pChan, statusByte);
2825
	 if (!(statusByte & IDE_STATUS_BUSY))
2826
	    {
2827
	     break;
2828
	    }
2829
	 mdelay(5);
2830
	}
2831
2832
     if (i == 10)
2833
	{
2834
	 printk("AtapiInterrupt: BUSY on entry. Status %x\n", statusByte);
2835
	 return FALSE;
2836
	}
2837
    }
2838
2839
 /*
2840
  * Check for error conditions.
2841
  */
2842
 if (statusByte & IDE_STATUS_ERROR)
2843
    {
2844
     if (srb->Cdb[0] != SCSIOP_REQUEST_SENSE)
2845
	{
2846
	 /*
2847
	  * Fail this request.
2848
	  */
2849
	 status = SRB_STATUS_ERROR;
2850
	 goto CompleteRequest;
2851
	}
2852
    }
2853
2854
 /*
2855
  * Check reason for this interrupt.
2856
  */
2857
 interruptReason = (inb(pChan->io_ports[ATAPI_INTREASON_OFFSET]) & 0x3);
2858
 wordsThisInterrupt = 256;
2859
2860
 if (interruptReason == 0x1 && (statusByte & IDE_STATUS_DRQ))
2861
    {
2862
     /*
2863
      * Write the packet.
2864
      */
2865
     printk("AtapiInterrupt: Writing Atapi packet.\n");
2866
2867
     /*
2868
      * Send CDB to device.
2869
      */
2870
     WriteBuffer(pChan, (PUSHORT)srb->Cdb, 6);
2871
2872
     return TRUE;
2873
    }
2874
 else if (interruptReason == 0x0 && (statusByte & IDE_STATUS_DRQ))
2875
    {
2876
     /*
2877
      * Write the data.
2878
      */
2879
2880
     /*
2881
      * Pick up bytes to transfer and convert to words.
2882
      */
2883
     wordCount = inb(pChan->io_ports[ATAPI_LCYL_OFFSET]);
2884
     wordCount |= inb(pChan->io_ports[ATAPI_HCYL_OFFSET]) << 8;
2885
2886
     /*
2887
      * Covert bytes to words.
2888
      */
2889
     wordCount >>= 1;
2890
2891
     if (wordCount != pChan->WordsLeft)
2892
	{
2893
	 printk( "AtapiInterrupt: %ld words requested; %ld words xferred\n",
2894
		pChan->WordsLeft, wordCount);
2895
	}
2896
2897
     /*
2898
      * Verify this makes sense.
2899
      */
2900
     if (wordCount > pChan->WordsLeft)
2901
	{
2902
	 wordCount = pChan->WordsLeft;
2903
	}
2904
2905
     /*
2906
      * Ensure that this is a write command.
2907
      */
2908
     if (srb->SrbFlags & SRB_FLAGS_DATA_OUT)
2909
	{
2910
	 dprintk("AtapiInterrupt: Write interrupt\n");
2911
	 WaitOnBusy(pChan, statusByte);
2912
	 WriteBuffer(pChan, pChan->DataBuffer, wordCount);
2913
2914
#	 if (0)
2915
	 /*
2916
	  * Translate ATAPI data back to SCSI data if needed (don't
2917
	  * convert if the original command is SCSIOP_MODE_SELECT10)
2918
	  */
2919
	 if (srb->Cdb[0] == ATAPI_MODE_SELECT && pchan->ConvertCdb)
2920
	    {
2921
	     Atapi2Scsi(pChan, srb, (char *)pChan->DataBuffer, wordCount << 1);
2922
	    }
2923
#	 endif
2924
	}
2925
     else
2926
	{
2927
	 printk("AtapiInterrupt: Int reason %x, but srb is for a write %p.\n",
2928
	 interruptReason, srb);
2929
2930
	 /*
2931
	  * Fail this request.
2932
	  */
2933
	 status = SRB_STATUS_ERROR;
2934
	 goto CompleteRequest;
2935
	}
2936
2937
     /*
2938
      * Advance data buffer pointer and bytes left.
2939
      */
2940
     pChan->DataBuffer += wordCount;
2941
     pChan->WordsLeft -= wordCount;
2942
2943
     return TRUE;
2944
    }
2945
 else if (interruptReason == 0x2 && (statusByte & IDE_STATUS_DRQ))
2946
    {
2947
     /*
2948
      * Pick up bytes to transfer and convert to words.
2949
      */
2950
     wordCount = inb(pChan->io_ports[ATAPI_LCYL_OFFSET]);
2951
     wordCount |= inb(pChan->io_ports[ATAPI_HCYL_OFFSET]) << 8;
2952
2953
     /*
2954
      * Covert bytes to words.
2955
      */
2956
     wordCount >>= 1;
2957
     if (wordCount != pChan->WordsLeft)
2958
	{
2959
	 printk("AtapiInterrupt: %ld words requested; %ld words xferred\n",
2960
		pChan->WordsLeft, wordCount);
2961
	}
2962
2963
     /*
2964
      * Verify this makes sense.
2965
      */
2966
     if (wordCount > pChan->WordsLeft)
2967
	{
2968
	 wordCount = pChan->WordsLeft;
2969
	}
2970
2971
     /*
2972
      * Ensure that this is a read command.
2973
      */
2974
     if (srb->SrbFlags & SRB_FLAGS_DATA_IN)
2975
	{
2976
	 dprintk("AtapiInterrupt: Read interrupt\n");
2977
	 WaitOnBusy(pChan, statusByte);
2978
	 ReadBuffer(pChan, pChan->DataBuffer, wordCount);
2979
2980
	 /*
2981
	  * From Windows DDK
2982
	  * You should typically set the ANSI-approved Version field,
2983
	  * in the INQUIRY response, to at least 2.
2984
	  */
2985
	 if (srb->Cdb[0] == SCSIOP_INQUIRY)
2986
	    {
2987
	     /*
2988
	      * Maybe it's not necessary in Linux driver.
2989
	      */
2990
	     *((PUCHAR)pChan->DataBuffer + 2) = 2;
2991
	    }
2992
	}
2993
     else
2994
	{
2995
	 printk("AtapiInterrupt: Int reason %x, but srb is for a read %p.\n",
2996
		interruptReason, srb);
2997
2998
	 /*
2999
	  * Fail this request.
3000
	  */
3001
	 status = SRB_STATUS_ERROR;
3002
	 goto CompleteRequest;
3003
	}
3004
3005
     /*
3006
      * Advance data buffer pointer and bytes left.
3007
      */
3008
     pChan->DataBuffer += wordCount;
3009
     pChan->WordsLeft -= wordCount;
3010
3011
     /*
3012
      * Check for read command complete.
3013
      */
3014
     if (pChan->WordsLeft == 0)
3015
	{
3016
	 /*
3017
	  * Work around to make many atapi devices return correct sector size
3018
	  * of 2048. Also certain devices will have sector count == 0x00, check
3019
	  * for that also.
3020
	  */
3021
	 if ((srb->Cdb[0] == 0x25) &&
3022
	    ((pChan->IdentifyData[srb->TargetId & 1].GeneralConfiguration >> 8) & 0x1F) == 0x05)
3023
	    {
3024
	     pChan->DataBuffer -= wordCount;
3025
	     if (pChan->DataBuffer[0] == 0x00)
3026
		{
3027
		 *((ULONG *) &(pChan->DataBuffer[0])) = 0xFFFFFF7F;
3028
		}
3029
3030
	     *((ULONG *) &(pChan->DataBuffer[2])) = 0x00080000;
3031
	     pChan->DataBuffer += wordCount;
3032
	    }
3033
	}
3034
     return TRUE;
3035
    }
3036
 else if (interruptReason == 0x3 && !(statusByte & IDE_STATUS_DRQ))
3037
    {
3038
     dprintk("Command Complete!\n");
3039
     /*
3040
      * Command complete.
3041
      */
3042
     if (pChan->WordsLeft)
3043
	{
3044
	 status = SRB_STATUS_DATA_OVERRUN;
3045
	}
3046
     else
3047
	{
3048
	 status = SRB_STATUS_SUCCESS;
3049
	}
3050
3051
CompleteRequest:
3052
3053
     if (status == SRB_STATUS_ERROR)
3054
	{
3055
	 /*
3056
	  * Map error to specific SRB status and handle request sense.
3057
	  */
3058
	 printk("AtapiInterrupt error\n");
3059
3060
	 status = MapError(pChan, srb);
3061
3062
	 /*
3063
	  * Try to recover it.... 2003/02/27
3064
	  */
3065
3066
3067
	 pChan->RDP = FALSE;
3068
	}
3069
     else
3070
	{
3071
	 /*
3072
	  * Wait for busy to drop.
3073
	  */
3074
	 for (i = 0; i < 30; i++)
3075
	    {
3076
	     GetStatus(pChan, statusByte);
3077
	     if (!(statusByte & IDE_STATUS_BUSY))
3078
		{
3079
		 break;
3080
		}
3081
	     udelay(500);
3082
	    }
3083
3084
	 if (i == 30)
3085
	    {
3086
	     /*
3087
	      * Reset the controller.
3088
	      */
3089
	     printk("AtapiInterrupt: Resetting due to BSY still up - %x.\n", statusByte);
3090
	     AtapiResetController(pChan);
3091
	     return TRUE;
3092
	    }
3093
3094
	 /*
3095
	  * Check to see if DRQ is still up.
3096
	  */
3097
	 if (statusByte & IDE_STATUS_DRQ)
3098
	    {
3099
	     for (i = 0; i < 500; i++)
3100
		{
3101
		 GetStatus(pChan, statusByte);
3102
		 if (!(statusByte & IDE_STATUS_DRQ))
3103
		    {
3104
		     break;
3105
		    }
3106
		 udelay(100);
3107
		}
3108
3109
	     if (i == 500)
3110
		{
3111
		 /*
3112
		  * Reset the controller.
3113
		  */
3114
		 printk("AtapiInterrupt: Resetting due to DRQ still up - %x\n",
3115
			statusByte);
3116
		 AtapiResetController(pChan);
3117
		 return TRUE;
3118
		}
3119
	    }
3120
	}
3121
3122
     /*
3123
      * Clear interrupt expecting flag.
3124
      */
3125
     pChan->ExpectingInterrupt = FALSE;
3126
3127
     /*
3128
      * Sanity check that there is a current request.
3129
      */
3130
     if (srb != NULL)
3131
	{
3132
	 /*
3133
	  * Set status in SRB.
3134
	  */
3135
	 srb->SrbStatus = (UCHAR)status;
3136
3137
	 /*
3138
	  * Check for underflow.
3139
	  */
3140
	 if (pChan->WordsLeft)
3141
	    {
3142
	     /*
3143
	      * Subtract out residual words and update if filemark hit,
3144
	      * setmark hit , end of data, end of media...
3145
	      */
3146
	     if (!(pChan->DeviceFlags[srb->TargetId & 1] & DFLAGS_TAPE_DEVICE))
3147
		{
3148
		 if (status == SRB_STATUS_DATA_OVERRUN)
3149
		    {
3150
		     srb->DataTransferLength -= pChan->WordsLeft * 2;
3151
		    }
3152
		 else
3153
		    {
3154
		     srb->DataTransferLength = 0;
3155
		    }
3156
		}
3157
	     else
3158
		{
3159
		 srb->DataTransferLength -= pChan->WordsLeft * 2;
3160
		}
3161
	    }
3162
3163
	 GetBaseStatus(pChan, statusByte);
3164
	 if (pChan->RDP && !(statusByte & IDE_STATUS_DSC))
3165
	    {
3166
	     printk("-@@-\n");
3167
	    }
3168
	 else
3169
	    {
3170
	     /*
3171
	      * Clear current SRB. Indicate ready for next request.
3172
	      */
3173
	     pChan->CurrentSrb = NULL;
3174
	     TaskDone(pChan, srb);
3175
	    }
3176
	}
3177
     else
3178
	{
3179
	 printk( "AtapiInterrupt: No SRB!\n");
3180
	}
3181
3182
     return TRUE;
3183
    }
3184
 else
3185
    {
3186
     /*
3187
      * Unexpected int.
3188
      */
3189
     printk("AtapiInterrupt: Unexpected interrupt. InterruptReason %x. Status %x.\n",
3190
     interruptReason, statusByte);
3191
     return FALSE;
3192
    }
3193
3194
 return TRUE;
3195
3196
} /* end AtapiInterrupt */
3197
3198
/************************************************************************
3199
 * IRQ handler
3200
 ************************************************************************/
3201
static void
3202
Irq_Handler(int irq, void *dev_id, struct pt_regs *regs)
3203
{
3204
3205
 UCHAR			i;
3206
 UCHAR			j;
3207
 ULONG			flags;
3208
3209
 dprintk("Irq_Handler enter\n");
3210
3211
 spin_lock_irqsave(&io_request_lock, flags);
3212
3213
 /*
3214
  * Scan for interrupt to process.
3215
  */
3216
 for (i = 0; i < NumAdapters; i++)
3217
    {
3218
     PITE_ADAPTER pAdap = ite_adapters[i];
3219
     if (pAdap->irq != irq) continue;
3220
3221
     for (j = 0; j < pAdap->num_channels; j++)
3222
	{
3223
	 IT8212Interrupt(&pAdap->IDEChannel[j]);
3224
	}
3225
    }
3226
3227
 spin_unlock_irqrestore(&io_request_lock, flags);
3228
3229
 dprintk("Irq_Handler exit\n");
3230
3231
} /* end Irq_Handler */
3232
3233
/************************************************************************
3234
 * This routine handles IDE Verify.
3235
 ************************************************************************/
3236
UCHAR
3237
IdeVerify(PChannel pChan, PSCSI_REQUEST_BLOCK Srb)
3238
{
3239
3240
 ULONG		startingSector;
3241
 ULONG		sectors;
3242
 ULONG		endSector;
3243
 USHORT		sectorCount;
3244
 UCHAR		drvSelect;
3245
 UCHAR		statusByte = 0;
3246
3247
 /*
3248
  * Select device
3249
  */
3250
 outb((UCHAR)((Srb->TargetId & 0x1) << 4 | 0xA0), pChan->io_ports[IDE_SELECT_OFFSET]);
3251
3252
 /*
3253
  * Wait for device ready (Not BUSY and Not DRQ)
3254
  */
3255
 WaitForDeviceReady(pChan, statusByte);
3256
 if ((statusByte & IDE_STATUS_BUSY) || (statusByte & IDE_STATUS_DRQ) ||
3257
     (statusByte == 0))
3258
    {
3259
     printk("IdeVerify: Disk[%d] not ready. Status=0x%x\n",
3260
		Srb->TargetId, statusByte);
3261
     return SRB_STATUS_BUSY;
3262
    }
3263
3264
 /*
3265
  * Get the starting sector number from CDB.
3266
  */
3267
 startingSector = ((PCDB)Srb->Cdb)->CDB10.LogicalBlockByte3 |
3268
		  ((PCDB)Srb->Cdb)->CDB10.LogicalBlockByte2 << 8 |
3269
		  ((PCDB)Srb->Cdb)->CDB10.LogicalBlockByte1 << 16 |
3270
		  ((PCDB)Srb->Cdb)->CDB10.LogicalBlockByte0 << 24;
3271
3272
 sectorCount = (USHORT)(((PCDB)Srb->Cdb)->CDB10.TransferBlocksMsb << 8 |
3273
			((PCDB)Srb->Cdb)->CDB10.TransferBlocksLsb );
3274
3275
 endSector = startingSector + sectorCount;
3276
3277
 /*
3278
  * Drive has these number sectors.
3279
  *
3280
  * 48-bit addressing.
3281
  */
3282
 if (endSector > 0x0FFFFFFF)
3283
    {
3284
     sectors = pChan->IdentifyData[Srb->TargetId & 0x01].Capacity_48bit_LOW;
3285
3286
     printk("IdeVerify (48-bit): Starting sector %ld, Ending sector %ld\n",
3287
		startingSector, endSector);
3288
3289
     if (endSector > sectors)
3290
	{
3291
	 /*
3292
	  * Too big, round down.
3293
	  */
3294
	 printk("IdeVerify: Truncating request to %lx blocks\n", sectors - startingSector - 1);
3295
3296
	 outb((UCHAR)((sectors - startingSector - 1) >> 8), pChan->io_ports[IDE_NSECTOR_OFFSET]);
3297
	 outb((UCHAR)(sectors - startingSector - 1), pChan->io_ports[IDE_NSECTOR_OFFSET]);
3298
	}
3299
     else
3300
	{
3301
	 /*
3302
	  * Set up sector count register. Round up to next block.
3303
	  */
3304
	 if (sectorCount > 0xFFFF)
3305
	    {
3306
	     sectorCount = (USHORT)0xFFFF;
3307
	    }
3308
3309
	 outb((UCHAR)(sectorCount >> 8), pChan->io_ports[IDE_NSECTOR_OFFSET]);
3310
	 outb((UCHAR) sectorCount, pChan->io_ports[IDE_NSECTOR_OFFSET]);
3311
	}
3312
3313
     /*
3314
      * Indicate expecting an interrupt.
3315
      */
3316
     pChan->ExpectingInterrupt = TRUE;
3317
3318
     /*
3319
      * Set up LBA address
3320
      */
3321
     outb((UCHAR) (startingSector >> 24), pChan->io_ports[IDE_LOCYL_OFFSET]);
3322
     outb((UCHAR) startingSector,	  pChan->io_ports[IDE_LOCYL_OFFSET]);
3323
     outb((UCHAR) 0,			  pChan->io_ports[IDE_MIDCYL_OFFSET]);
3324
     outb((UCHAR) (startingSector >> 8),  pChan->io_ports[IDE_MIDCYL_OFFSET]);
3325
     outb((UCHAR) 0,			  pChan->io_ports[IDE_HCYL_OFFSET]);
3326
     outb((UCHAR) (startingSector >> 16), pChan->io_ports[IDE_HCYL_OFFSET]);
3327
3328
     /*
3329
      * Send verify command.
3330
      */
3331
     outb(IDE_COMMAND_READ_VERIFY_EXT, pChan->io_ports[IDE_COMMAND_OFFSET]);
3332
    }
3333
 /*
3334
  * 28-bit addressing
3335
  */
3336
 else
3337
    {
3338
     sectors = pChan->IdentifyData[Srb->TargetId & 0x01].UserAddressableSectors;
3339
3340
     printk("IdeVerify: Starting sector %ld, Ending sector %ld\n",
3341
		startingSector, endSector);
3342
3343
     if (endSector > sectors)
3344
	{
3345
	 /*
3346
	  * Too big, round down.
3347
	  */
3348
	 printk("IdeVerify: Truncating request to %ld blocks\n", sectors - startingSector - 1);
3349
	 outb((UCHAR)(sectors - startingSector - 1), pChan->io_ports[IDE_NSECTOR_OFFSET]);
3350
	}
3351
     else
3352
	{
3353
	 /*
3354
	  * Set up sector count register. Round up to next block.
3355
	  */
3356
	 if (sectorCount > 0xFF)
3357
	    {
3358
	     sectorCount = (USHORT)0xFF;
3359
	    }
3360
	 outb((UCHAR)sectorCount, pChan->io_ports[IDE_NSECTOR_OFFSET]);
3361
	}
3362
3363
     /*
3364
      * Indicate expecting an interrupt.
3365
      */
3366
     pChan->ExpectingInterrupt = TRUE;
3367
3368
     /*
3369
      * Set up LBA address
3370
      */
3371
     outb((UCHAR) startingSector,	 pChan->io_ports[IDE_LOCYL_OFFSET]);
3372
     outb((UCHAR) (startingSector >> 8), pChan->io_ports[IDE_MIDCYL_OFFSET]);
3373
     outb((UCHAR) (startingSector >> 16),pChan->io_ports[IDE_HCYL_OFFSET]);
3374
3375
     /*
3376
      * Select driver, set LBA mode, set LBA (27:27)
3377
      */
3378
     drvSelect = (UCHAR) (startingSector >> 24);
3379
     drvSelect = drvSelect | (((UCHAR) Srb->TargetId & 0x1) << 4) | 0xA0 | 0x40;
3380
     outb(drvSelect, pChan->io_ports[IDE_SELECT_OFFSET]);
3381
3382
     /*
3383
      * Send verify command.
3384
      */
3385
     outb(IDE_COMMAND_READ_VERIFY, pChan->io_ports[IDE_COMMAND_OFFSET]);
3386
    }
3387
3388
 /*
3389
  * Wait for interrupt.
3390
  */
3391
 return SRB_STATUS_PENDING;
3392
3393
} /* end IdeVerify */
3394
3395
/************************************************************************
3396
 * This function is used to copy memory with overlapped destination and
3397
 * source. I guess ScsiPortMoveMemory cannot handle this well. Can it?
3398
 ************************************************************************/
3399
void
3400
IT8212MoveMemory(PUCHAR DestAddr, PUCHAR SrcAddr, LONG ByteCount)
3401
{
3402
3403
 long		i;
3404
3405
 dprintk("IT8212MoveMemory: DestAddr=0x%x, SrcAddr=0x%x, ByteCount=0x%x\n",
3406
	DestAddr, SrcAddr, ByteCount);
3407
3408
 if (DestAddr > SrcAddr)
3409
    {
3410
     /*
3411
      * If Destination Area is in the back of the Source Area, copy from
3412
      * the end of the requested area.
3413
      */
3414
     for (i = (ByteCount - 1); i >= 0; i--)
3415
	{
3416
	 *(DestAddr + i) = *(SrcAddr + i);
3417
	}
3418
    }
3419
 else if (DestAddr < SrcAddr)
3420
    {
3421
     /*
3422
      * If Destination Area is in the front of the Source Area, copy from
3423
      * the begin of the requested area.
3424
      */
3425
     for (i = 0; i < ByteCount; i++)
3426
	{
3427
	 *(DestAddr + i) = *(SrcAddr + i);
3428
	}
3429
    }
3430
3431
} /* end IT8212MoveMemory */
3432
3433
/************************************************************************
3434
 * Convert SCSI packet command to Atapi packet command.
3435
 ************************************************************************/
3436
void
3437
Scsi2Atapi(PChannel pChan, PSCSI_REQUEST_BLOCK Srb)
3438
{
3439
3440
 dprintk("++++++++++ Scsi2Atapi (0x%X) ++++++++++\n", Srb->Cdb[0]);
3441
 dprintk("Srb->DataBuffer=0x%x\n", Srb->DataBuffer);
3442
 dprintk("Srb->DataTransferLength=0x%x\n", Srb->DataTransferLength);
3443
 dprintk("CDB=0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
3444
	Srb->Cdb[0], Srb->Cdb[1], Srb->Cdb[2], Srb->Cdb[3], Srb->Cdb[4], Srb->Cdb[5],
3445
	Srb->Cdb[6], Srb->Cdb[7], Srb->Cdb[8], Srb->Cdb[9], Srb->Cdb[10], Srb->Cdb[11]);
3446
3447
 /*
3448
  * Change the cdb length.
3449
  */
3450
 Srb->CdbLength = 12;
3451
3452
 /*
3453
  * Because the block descripter and the header translation, we must
3454
  * adjust the requested length.
3455
  */
3456
 Srb->DataTransferLength -= 4;
3457
3458
 /*
3459
  * Record the original CDB for later restore.
3460
  */
3461
 memcpy(pChan->TempCdb, Srb->Cdb, MAXIMUM_CDB_SIZE);
3462
3463
 /*
3464
  * Indicate that we have performed Scsi2Atapi function. And we must
3465
  * restore the CDB back once the command complete.
3466
  */
3467
 pChan->ConvertCdb = TRUE;
3468
3469
 switch (Srb->Cdb[0])
3470
    {
3471
     /*
3472
      * Convert the command from SCSIOP_MODE_SENSE (0x1A) to
3473
      * SCSIOP_MODE_SENSE10 (0x5A).
3474
      */
3475
     case SCSIOP_MODE_SENSE:
3476
	{
3477
	 PSCSI_MODE_SENSE10 modeSense10 = (PSCSI_MODE_SENSE10) Srb->Cdb;
3478
	 PSCSI_MODE_SENSE6  modeSense6 = (PSCSI_MODE_SENSE6) pChan->TempCdb;
3479
3480
	 /*
3481
	  * 1. Zero out the whole CDB.
3482
	  */
3483
	 memset((PUCHAR)modeSense10, 0, MAXIMUM_CDB_SIZE);
3484
3485
	 /*
3486
	  * 2. Fill in command code (SCSI_MODE_SENSE10).
3487
	  */
3488
	 modeSense10->OperationCode = ATAPI_MODE_SENSE;
3489
	 modeSense10->Dbd = modeSense6->Dbd;
3490
	 modeSense10->PageCode = modeSense6->PageCode;
3491
	 modeSense10->Pc = modeSense6->Pc;
3492
	 modeSense10->SubpageCode = modeSense6->SubpageCode;
3493
	 modeSense10->AllocationLengthLsb = modeSense6->AllocationLength;
3494
	 modeSense10->Control = modeSense6->Control;
3495
3496
	 /*
3497
	  * 3. Becasuse we will fake a block descripter (-8), and
3498
	  * translate the header (+4), so the requested length
3499
	  * should be modified. That is, -8+4=-4 bytes.
3500
	  */
3501
	 modeSense10->AllocationLengthLsb -= 4;
3502
3503
	 break;
3504
	}
3505
3506
     /*
3507
      * Convert the command from SCSIOP_MODE_SELECT (0x15) to
3508
      * SCSIOP_MODE_SELECT10 (0x5A).
3509
      */
3510
     case SCSIOP_MODE_SELECT:
3511
	{
3512
	 UCHAR		tempHeader[sizeof(PSCSI_MODE_PARAMETER_HEADER6)];
3513
	 USHORT		byteCount;
3514
	 PSCSI_MODE_PARAMETER_HEADER10 header10 = (PSCSI_MODE_PARAMETER_HEADER10)Srb->DataBuffer;
3515
	 PSCSI_MODE_PARAMETER_HEADER6 header6 = (PSCSI_MODE_PARAMETER_HEADER6)tempHeader;
3516
	 PSCSI_MODE_SELECT10 modeSelect10 = (PSCSI_MODE_SELECT10)Srb->Cdb;
3517
	 PSCSI_MODE_SELECT6 modeSelect6 = (PSCSI_MODE_SELECT6)pChan->TempCdb;
3518
3519
	 /*
3520
	  * First, convert the command block.
3521
	  */
3522
3523
	 /*
3524
	  * 1. Zero out the whole CDB.
3525
	  */
3526
	 memset((PUCHAR)modeSelect10, 0, MAXIMUM_CDB_SIZE);
3527
3528
	 /*
3529
	  * 2. Fill in command code (SCSI_MODE_SENSE10).
3530
	  */
3531
	 modeSelect10->OperationCode = ATAPI_MODE_SELECT;
3532
	 modeSelect10->SPBit = modeSelect6->SPBit;
3533
	 modeSelect10->PFBit = modeSelect6->PFBit;
3534
	 modeSelect10->ParameterListLengthLsb = modeSelect6->ParameterListLength;
3535
	 modeSelect10->Control = modeSelect6->Control;
3536
3537
	 /*
3538
	  * 3. Becasuse we will remove the block descripter (-8), and
3539
	  * translate the header (+4), so the requested length should
3540
	  * be modified. That is, -8+4=-4 bytes.
3541
	  */
3542
	 modeSelect10->ParameterListLengthLsb -= 4;
3543
3544
	 /*
3545
	  * Second, convert the parameter page format from SCSI to ATAPI.
3546
	  */
3547
3548
	 /*
3549
	  * Remove the mode parameter data (except the header and the
3550
	  * block descripter).
3551
	  */
3552
	 byteCount = modeSelect6->ParameterListLength - sizeof(SCSI_MODE_PARAMETER_HEADER6) - sizeof(SCSI_MODE_PARAMTER_BLOCK_DESCRIPTER);
3553
	 if (byteCount > 0)
3554
	    {
3555
	     IT8212MoveMemory(
3556
		(PUCHAR) header10 + sizeof(SCSI_MODE_PARAMETER_HEADER10),
3557
		(PUCHAR) header10 + sizeof(SCSI_MODE_PARAMETER_HEADER6) + sizeof(SCSI_MODE_PARAMTER_BLOCK_DESCRIPTER),
3558
		byteCount
3559
			);
3560
	    }
3561
3562
	 /*
3563
	  * Keep the original header6 (4 bytes) in tempHeader for later use
3564
	  */
3565
	 memcpy(tempHeader, header10, sizeof(SCSI_MODE_PARAMETER_HEADER6));
3566
3567
	 /*
3568
	  * Change the "mode parameter header(6)" to "mode parameter header(10)"
3569
	  * Notice: Remove the block descripter in SCSI-2 command out. It
3570
	  * won't be used in MMC.
3571
	  */
3572
	 memset((PUCHAR)header10, 0, sizeof(SCSI_MODE_PARAMETER_HEADER10));
3573
	 header10->ModeDataLengthLsb = header6->ModeDataLength;
3574
	 header10->MediumType = header6->MediumType;
3575
	 header10->DeviceSpecificParameter = header6->DeviceSpecificParameter;
3576
	 header10->BlockDescriptorLengthLsb = header6->BlockDescriptorLength;
3577
3578
	 /*
3579
	  * ATAPI doesn't support block descripter, so remove it from the
3580
	  * mode paramter.
3581
	  */
3582
	 header10->BlockDescriptorLengthLsb = 0;
3583
3584
	 break;
3585
	}
3586
    }
3587
3588
 dprintk("Srb->DataTransferLength=0x%x\n", Srb->DataTransferLength);
3589
 dprintk("Final CDB=0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
3590
	Srb->Cdb[0], Srb->Cdb[1], Srb->Cdb[2], Srb->Cdb[3], Srb->Cdb[4], Srb->Cdb[5],
3591
	Srb->Cdb[6], Srb->Cdb[7], Srb->Cdb[8], Srb->Cdb[9], Srb->Cdb[10], Srb->Cdb[11]);
3592
 dprintk("---------- Scsi2Atapi (0x%X) ----------\n", Srb->Cdb[0]);
3593
3594
} /* end Scsi2Atapi */
3595
3596
/************************************************************************
3597
 * Send ATAPI packet command to device.
3598
 ************************************************************************/
3599
UCHAR
3600
AtapiSendCommand(PChannel pChan, PSCSI_REQUEST_BLOCK Srb)
3601
{
3602
3603
 ULONG			bmAddress = pChan->dma_base;
3604
 ULONG			i;
3605
 ULONG			flags;
3606
 UCHAR			statusByte;
3607
 UCHAR			byteCountLow;
3608
 UCHAR			byteCountHigh;
3609
 UCHAR			useDMA;
3610
 UCHAR			RevisionID = 0;
3611
 UCHAR			bmClearStat;
3612
 PITE_ADAPTER		pAdap = ite_adapters[0];
3613
3614
 dprintk("AtapiSendCommand: Command 0x%X to Device %d\n",
3615
	Srb->Cdb[0], Srb->TargetId);
3616
3617
 /*
3618
  * Default use PIO mode.
3619
  */
3620
 useDMA	= 0;
3621
 pChan->ConvertCdb = FALSE;
3622
3623
 /*
3624
  * Make sure command is to ATAPI device.
3625
  */
3626
 flags = pChan->DeviceFlags[Srb->TargetId & 1];
3627
 if (flags & (DFLAGS_SANYO_ATAPI_CHANGER | DFLAGS_ATAPI_CHANGER))
3628
    {
3629
     if ((Srb->Lun) > (pChan->DiscsPresent[Srb->TargetId & 1] - 1))
3630
	{
3631
	 /*
3632
	  * Indicate no device found at this address.
3633
	  */
3634
	 return SRB_STATUS_SELECTION_TIMEOUT;
3635
	}
3636
    }
3637
 else if (Srb->Lun > 0)
3638
    {
3639
     return SRB_STATUS_SELECTION_TIMEOUT;
3640
    }
3641
3642
 if (!(flags & DFLAGS_ATAPI_DEVICE))
3643
    {
3644
     return SRB_STATUS_SELECTION_TIMEOUT;
3645
    }
3646
3647
 /*
3648
  * Select device 0 or 1.
3649
  */
3650
 outb((UCHAR)(((Srb->TargetId & 0x1) << 4) | 0xA0),
3651
	pChan->io_ports[ATAPI_SELECT_OFFSET]);
3652
3653
 /*
3654
  * Try to enable interrupt again. (2003/02/25)
3655
  */
3656
#if (0)
3657
 outb(0x00, pChan->io_ports[ATAPI_CONTROL_OFFSET]);
3658
#endif
3659
3660
 /*
3661
  * Verify that controller is ready for next command.
3662
  */
3663
 GetStatus(pChan, statusByte);
3664
3665
 dprintk("AtapiSendCommand: Entered with status %x\n", statusByte);
3666
3667
 if (statusByte & IDE_STATUS_BUSY)
3668
    {
3669
     printk("AtapiSendCommand: Device busy (%x)\n", statusByte);
3670
     return SRB_STATUS_BUSY;
3671
    }
3672
3673
 if (statusByte & IDE_STATUS_ERROR)
3674
    {
3675
     if (Srb->Cdb[0] != SCSIOP_REQUEST_SENSE)
3676
	{
3677
	 printk("AtapiSendCommand: Error on entry: (%x)\n", statusByte);
3678
3679
	 /*
3680
	  * Read the error reg. to clear it and fail this request.
3681
	  */
3682
	 return MapError(pChan, Srb);
3683
	}
3684
    }
3685
3686
 /*
3687
  * If a tape drive doesn't have DSC set and the last command is
3688
  * restrictive, don't send the next command. See discussion of
3689
  * Restrictive Delayed Process commands in QIC-157.
3690
  */
3691
 if ((!(statusByte & IDE_STATUS_DSC)) && (flags & DFLAGS_TAPE_DEVICE) && pChan->RDP)
3692
    {
3693
     mdelay(1);
3694
     printk("AtapiSendCommand: DSC not set. %x\n", statusByte);
3695
     return SRB_STATUS_BUSY;
3696
    }
3697
3698
 if (statusByte & IDE_STATUS_DRQ)
3699
    {
3700
     printk("AtapiSendCommand: Enter with status (%x). Attempt to recover.\n",
3701
	statusByte);
3702
3703
     /*
3704
      * Try to drain the data that one preliminary device thinks that it has
3705
      * to transfer. Hopefully this random assertion of DRQ will not be present
3706
      * in production devices.
3707
      */
3708
     for (i = 0; i < 0x10000; i++)
3709
	{
3710
	 GetStatus(pChan, statusByte);
3711
3712
	 if (statusByte & IDE_STATUS_DRQ)
3713
	    {
3714
	     /*
3715
	      * Note: The data register is always referenced as a 16-bit word.
3716
	      */
3717
	     inw(pChan->io_ports[ATAPI_DATA_OFFSET]);
3718
	    }
3719
	 else
3720
	    {
3721
	     break;
3722
	    }
3723
	}
3724
3725
     if (i == 0x10000)
3726
	{
3727
	 printk("AtapiSendCommand: DRQ still asserted.Status (%x)\n", statusByte);
3728
	 printk("AtapiSendCommand: Issued soft reset to Atapi device. \n");
3729
3730
	 AtapiSoftReset(pChan, Srb->TargetId);
3731
3732
	 /*
3733
	  * Re-initialize Atapi device.
3734
	  */
3735
	 IssueIdentify(pChan, (Srb->TargetId & 1), IDE_COMMAND_ATAPI_IDENTIFY);
3736
3737
	 /*
3738
	  * Inform the port driver that the bus has been reset.
3739
	  */
3740
3741
	 /*
3742
	  * Clean up device extension fields that AtapiStartIo won't.
3743
	  */
3744
	 pChan->ExpectingInterrupt = FALSE;
3745
	 pChan->RDP = FALSE;
3746
3747
	 return SRB_STATUS_BUS_RESET;
3748
	}
3749
    }
3750
3751
 if (flags & (DFLAGS_SANYO_ATAPI_CHANGER | DFLAGS_ATAPI_CHANGER))
3752
    {
3753
     /*
3754
      * As the cdrom driver sets the LUN field in the cdb, it must be removed.
3755
      */
3756
     Srb->Cdb[1] &= ~0xE0;
3757
3758
     if ((Srb->Cdb[0] == SCSIOP_TEST_UNIT_READY) && (flags & DFLAGS_SANYO_ATAPI_CHANGER))
3759
	{
3760
	 /*
3761
	  * Torisan changer. TUR's are overloaded to be platter switches.
3762
	  */
3763
	 Srb->Cdb[7] = Srb->Lun;
3764
	}
3765
    }
3766
3767
 /*
3768
  * Convert SCSI to ATAPI commands if needed
3769
  */
3770
 switch (Srb->Cdb[0])
3771
    {
3772
     case SCSIOP_MODE_SENSE:
3773
     case SCSIOP_MODE_SELECT:
3774
	 if (flags & DFLAGS_ATAPI_DEVICE)
3775
	    {
3776
	     Scsi2Atapi(pChan, Srb);
3777
	    }
3778
	 break;
3779
    }
3780
3781
 if (pChan->UseDma[Srb->TargetId & 1])
3782
    {
3783
     switch (Srb->Cdb[0])
3784
	{
3785
	 case SCSIOP_READ:			/* (0x28)		*/
3786
	 case 0xA8:				/* READ(12)		*/
3787
	 case SCSIOP_READ_CD:
3788
3789
	     if (Srb->DataTransferLength == 0)
3790
		{
3791
		 break;
3792
		}
3793
3794
	     /*
3795
	      * First, switch to DMA or UDMA mode if running on Bypass mode.
3796
	      */
3797
	     if (pAdap->bypass_mode)
3798
		{
3799
		 IT8212SwitchDmaMode(pChan, Srb->TargetId);
3800
		}
3801
3802
	     /*
3803
	      * Check the SCATTER/GATHER count. The upper will give the
3804
	      * different memory address depend on whether use_sg is used
3805
	      * or not.
3806
	      */
3807
	     if (Srb->UseSg == 0)
3808
		{
3809
		 IdeBuildDmaTable(pChan, Srb);
3810
		}
3811
	     else
3812
		{
3813
		 IdeBuildDmaSgTable(pChan, Srb);
3814
		}
3815
3816
	     bmClearStat = inb(bmAddress + 2);
3817
	     if (Srb->TargetId & 0x01)
3818
		{
3819
		 bmClearStat = bmClearStat | BM_DRV1_DMA_CAPABLE |
3820
			   BM_STAT_FLG_INT | BM_STAT_FLG_ERR;
3821
		}
3822
	     else
3823
		{
3824
		 bmClearStat = bmClearStat | BM_DRV0_DMA_CAPABLE |
3825
			   BM_STAT_FLG_INT | BM_STAT_FLG_ERR;
3826
		}
3827
3828
	     useDMA = 1;
3829
3830
	     outb(0, bmAddress);
3831
3832
	     /*
3833
	      * Setup PRD table physical address.
3834
	      */
3835
	     outl(pChan->dmatable_dma, bmAddress + 4);
3836
3837
	     /*
3838
	      * Clear the status.
3839
	      */
3840
	     outb(bmClearStat, bmAddress + 2);
3841
3842
	     break;
3843
	} /* end switch (Srb->Cdb[0]) */
3844
    }
3845
3846
 /*
3847
  * Set data buffer pointer and words left.
3848
  */
3849
 pChan->DataBuffer = (PUSHORT)Srb->DataBuffer;
3850
3851
 if (useDMA)
3852
    {
3853
     pChan->WordsLeft = 0;
3854
    }
3855
 else
3856
    {
3857
     pChan->WordsLeft = Srb->DataTransferLength / 2;
3858
    }
3859
3860
 outb((UCHAR)(((Srb->TargetId & 0x1) << 4) | 0xA0),
3861
	pChan->io_ports[ATAPI_SELECT_OFFSET]);
3862
3863
 WaitOnBusy(pChan, statusByte);
3864
3865
 /*
3866
  * Write transfer byte count to registers.
3867
  */
3868
 byteCountLow  = (UCHAR)(Srb->DataTransferLength & 0xFF);
3869
 byteCountHigh = (UCHAR)(Srb->DataTransferLength >> 8);
3870
3871
 if (Srb->DataTransferLength >= 0x10000)
3872
    {
3873
     byteCountLow = byteCountHigh = 0xFF;
3874
    }
3875
3876
 outb(byteCountLow,  pChan->io_ports[ATAPI_LCYL_OFFSET]);
3877
 outb(byteCountHigh, pChan->io_ports[ATAPI_HCYL_OFFSET]);
3878
 outb(0,	     pChan->io_ports[ATAPI_INTREASON_OFFSET]);
3879
 outb(0,	     pChan->io_ports[ATAPI_UNUSED1_OFFSET]);
3880
 outb(useDMA,	     pChan->io_ports[ATAPI_FEATURE_OFFSET]);
3881
3882
 WaitOnBusy(pChan, statusByte);
3883
3884
 if (flags & DFLAGS_INT_DRQ)
3885
    {
3886
     /*
3887
      * This device interrupts when ready to receive the packet.
3888
      *
3889
      * Write ATAPI packet command.
3890
      */
3891
     outb(IDE_COMMAND_ATAPI_PACKET, pChan->io_ports[IDE_COMMAND_OFFSET]);
3892
3893
     printk("AtapiSendCommand: Wait for int. to send packet. Status (%x)\n",
3894
	statusByte);
3895
3896
     pChan->ExpectingInterrupt = TRUE;
3897
3898
     return SRB_STATUS_PENDING;
3899
    }
3900
 else
3901
    {
3902
     /*
3903
      * Write ATAPI packet command.
3904
      */
3905
     outb(IDE_COMMAND_ATAPI_PACKET, pChan->io_ports[IDE_COMMAND_OFFSET]);
3906
3907
     /*
3908
      * Wait for DRQ.
3909
      */
3910
     WaitOnBusy(pChan, statusByte);
3911
     WaitForDrq(pChan, statusByte);
3912
3913
     if (!(statusByte & IDE_STATUS_DRQ))
3914
	{
3915
	 printk("AtapiSendCommand: DRQ never asserted (%x)\n", statusByte);
3916
	 return SRB_STATUS_ERROR;
3917
	}
3918
    }
3919
3920
 /*
3921
  * Need to read status register.
3922
  */
3923
 GetBaseStatus(pChan, statusByte);
3924
3925
 /*
3926
  * Send CDB to device.
3927
  * After detecting DRQ, the host writes the 12 bytes(6 words) of Command
3928
  * to the Data Register.
3929
  */
3930
 WaitOnBusy(pChan, statusByte);
3931
 WriteBuffer(pChan, (PUSHORT)Srb->Cdb, 6);
3932
3933
 /*
3934
  * If running on DMA mode, start BUS MASTER operation.
3935
  */
3936
 if (useDMA)
3937
    {
3938
     /*
3939
      * If SCSIOP_READ command is sent to an Audio CD, error will be
3940
      * returned. But the error will be blocked by our controller if bus
3941
      * master operation started. So wait for a short period to check if
3942
      * error occurs. If error occurs, don't start bus master operation.
3943
      */
3944
     if (RevisionID == 0x10)
3945
	{
3946
	 for (i = 0; i < 500; i++)
3947
	    {
3948
	     udelay(1);
3949
	     statusByte = inb(bmAddress + 2);
3950
	     if (statusByte & BM_STAT_FLG_INT)
3951
		{
3952
		 /*
3953
		  * If error occurs, give up this round.
3954
		  */
3955
		 printk("AtapiSendCommand: Command failed. Don't start bus master.");
3956
		 printk("status=%x, i=%ld\n", statusByte, i);
3957
3958
		 pChan->ExpectingInterrupt = TRUE;
3959
		 return SRB_STATUS_PENDING;
3960
		}
3961
	    }
3962
	}
3963
3964
     if (Srb->SrbFlags & SRB_FLAGS_DATA_IN)
3965
	{
3966
	 outb(BM_CMD_FLG_START | BM_CMD_FLG_WRTTOMEM, bmAddress);
3967
	}
3968
     else if (Srb->SrbFlags & SRB_FLAGS_DATA_OUT)
3969
	{
3970
	 outb(BM_CMD_FLG_START | BM_CMD_FLG_WRTTODSK, bmAddress);
3971
	}
3972
    } /* end if (useDMA) */
3973
3974
 /*
3975
  * Indicate expecting an interrupt and wait for it.
3976
  */
3977
 pChan->ExpectingInterrupt = TRUE;
3978
3979
 return SRB_STATUS_PENDING;
3980
3981
} /* end AtapiSendCommand */
3982
3983
/************************************************************************
3984
 * Program ATA registers for IDE disk transfer.
3985
 ************************************************************************/
3986
ULONG
3987
IdeSendCommand(PChannel pChan, PSCSI_REQUEST_BLOCK Srb)
3988
{
3989
3990
 UCHAR			statusByte;
3991
 ULONG			status;
3992
 ULONG			i;
3993
 Scsi_Cmnd *		pREQ;
3994
 unsigned char *	request_buffer;
3995
 PINQUIRYDATA		inquiryData;
3996
3997
 pREQ		= Srb->pREQ;
3998
 status		= SRB_STATUS_SUCCESS;
3999
 statusByte	= 0;
4000
4001
 switch (Srb->Cdb[0])
4002
    {
4003
     case SCSIOP_INQUIRY:
4004
	 dprintk("SCSIOP_INQUIRY\n");
4005
4006
	 /*
4007
	  * Filter out all TIDs but 0 and 1 since this is an IDE interface
4008
	  * which support up to two devices.
4009
	  */
4010
	 if ((pREQ->lun != 0) ||
4011
            (!pChan->DeviceFlags[pREQ->target & 1] & DFLAGS_DEVICE_PRESENT))
4012
	    {
4013
	     /*
4014
	      * Indicate no device found at this address.
4015
	      */
4016
	     status = SRB_STATUS_INVALID_TARGET_ID;
4017
	     break;
4018
	    }
4019
	 else
4020
	    {
4021
	     request_buffer = Srb->DataBuffer;
4022
	     inquiryData    = Srb->DataBuffer;
4023
4024
	     /*
4025
	      * Zero INQUIRY data structure.
4026
	      */
4027
	     memset(request_buffer, 0, Srb->DataTransferLength);
4028
4029
	     /*
4030
	      * Standard IDE interface only supports disks.
4031
	      */
4032
	     inquiryData->DeviceType = DIRECT_ACCESS_DEVICE;
4033
4034
	     /*
4035
	      * Device type modifer.
4036
	      */
4037
	     request_buffer[1] = 0;
4038
4039
	     /*
4040
	      * No ANSI/ISO compliance.
4041
	      */
4042
	     request_buffer[2] = 0;
4043
4044
	     /*
4045
	      * Additional length.
4046
	      */
4047
	     request_buffer[4] = 31;
4048
	     memcpy(&request_buffer[8], "ITE     ", 8);
4049
	     memcpy(&request_buffer[16], "IT8212X         ", 16);
4050
	     memcpy(&request_buffer[32], "1.3", 3);
4051
4052
	     /*
4053
 	      * Set the removable bit, if applicable.
4054
	      */
4055
	     if (pChan->DeviceFlags[pREQ->target & 1] & DFLAGS_REMOVABLE_DRIVE)
4056
		{
4057
		 inquiryData->RemovableMedia = 1;
4058
		}
4059
4060
	     status = SRB_STATUS_SUCCESS;
4061
	    }
4062
4063
	 break;
4064
4065
     case SCSIOP_MODE_SENSE:
4066
	 status = SRB_STATUS_INVALID_REQUEST;
4067
	 break;
4068
4069
     case SCSIOP_TEST_UNIT_READY:
4070
	 status = SRB_STATUS_SUCCESS;
4071
	 break;
4072
4073
     case SCSIOP_READ_CAPACITY:
4074
	 /*
4075
	  * Claim 512 byte blocks (big-endian).
4076
	  */
4077
	 ((PREAD_CAPACITY_DATA)Srb->DataBuffer)->BytesPerBlock = 0x20000;
4078
4079
	 /*
4080
	  * Calculate last sector.
4081
	  */
4082
	 if (pChan->IdentifyData[pREQ->target & 0x01].UserAddressableSectors == 0x0FFFFFFF)
4083
	    {
4084
	     i = pChan->IdentifyData[pREQ->target & 0x01].Capacity_48bit_LOW - 1;
4085
	    }
4086
	 else
4087
	    {
4088
	     i = pChan->IdentifyData[pREQ->target & 0x01].UserAddressableSectors - 1;
4089
	    }
4090
4091
	 ((PREAD_CAPACITY_DATA)Srb->DataBuffer)->LogicalBlockAddress =
4092
		(((PUCHAR)&i)[0] << 24) | (((PUCHAR)&i)[1] << 16) |
4093
		(((PUCHAR)&i)[2] << 8)  | ((PUCHAR)&i)[3];
4094
4095
	 status = SRB_STATUS_SUCCESS;
4096
	 break;
4097
4098
     case SCSIOP_VERIFY:
4099
	 status = IdeVerify(pChan, Srb);
4100
	 break;
4101
4102
     case SCSIOP_READ:
4103
     case SCSIOP_WRITE:
4104
	 status = IT8212ReadWrite(pChan, Srb);
4105
	 break;
4106
4107
     case SCSIOP_START_STOP_UNIT:
4108
	 /*
4109
	  * Determine what type of operation we should perform
4110
	  */
4111
	 status = SRB_STATUS_SUCCESS;
4112
	 break;
4113
4114
     case SCSIOP_REQUEST_SENSE:
4115
	 /*
4116
	  * This function makes sense buffers to report the results
4117
	  * of the original GET_MEDIA_STATUS command
4118
	  */
4119
	 status = SRB_STATUS_INVALID_REQUEST;
4120
	 break;
4121
4122
     default:
4123
	 printk("IdeSendCommand: Unsupported command %x\n", Srb->Cdb[0]);
4124
	 status = SRB_STATUS_INVALID_REQUEST;
4125
    } /* end switch */
4126
4127
 return status;
4128
4129
} /* end IdeSendCommand */
4130
4131
/************************************************************************
4132
 * This routine is called from the SCSI port driver synchronized with
4133
 * the kernel to start an IO request. If the current SRB is busy, return
4134
 * FALSE, else return TURE.
4135
 ************************************************************************/
4136
BOOLEAN
4137
AtapiStartIo(PChannel pChan, PSCSI_REQUEST_BLOCK Srb)
4138
{
4139
4140
 ULONG			status = 0;
4141
4142
 /*
4143
  * Determine which function.
4144
  */
4145
 switch (Srb->Function)
4146
    {
4147
     case SRB_FUNCTION_EXECUTE_SCSI:
4148
	 /*
4149
	  * Sanity check. Only one request can be outstanding on a
4150
	  * controller.
4151
 	  */
4152
 	 if (pChan->CurrentSrb)
4153
	    {
4154
	     printk("AtapiStartIo:Already have a request!\n");
4155
	     status = SRB_STATUS_BUSY;
4156
	     Srb->SrbStatus = SRB_STATUS_BUSY;
4157
	     goto busy;
4158
	    }
4159
4160
	 /*
4161
	  * Indicate that a request is active on the controller.
4162
	  */
4163
	 pChan->CurrentSrb  = Srb;
4164
	 Srb->SrbStatus	    = SRB_STATUS_PENDING;
4165
4166
	 /*
4167
 	  * Send command to device.
4168
	  */
4169
	 if (pChan->DeviceFlags[Srb->TargetId & 1] & DFLAGS_ATAPI_DEVICE)
4170
	    {
4171
	     /*
4172
	      * If this is ATAPI device.
4173
	      */
4174
	     status = AtapiSendCommand(pChan, Srb);
4175
	    }
4176
	 else if (pChan->DeviceFlags[Srb->TargetId & 1] & DFLAGS_DEVICE_PRESENT)
4177
	    {
4178
	     /*
4179
	      * If this is IDE device.
4180
	      */
4181
	     status = IdeSendCommand(pChan, Srb);
4182
	    }
4183
	 else
4184
	    {
4185
	     /*
4186
	      * Nothing else.
4187
	      */
4188
	     status = SRB_STATUS_SELECTION_TIMEOUT;
4189
	    }
4190
4191
	 break;
4192
4193
     case SRB_FUNCTION_IO_CONTROL:
4194
	 /*
4195
	  * IO control function.
4196
	  */
4197
	 printk("AtapiStartIo: IO control\n");
4198
	 break;
4199
4200
    default:
4201
	 /*
4202
	  * Indicate unsupported command.
4203
	  */
4204
	 status = SRB_STATUS_INVALID_REQUEST;
4205
	 break;
4206
4207
    } /* end switch */
4208
4209
 busy:
4210
 if (status != SRB_STATUS_PENDING)
4211
    {
4212
     /*
4213
      * Set status in SRB.
4214
      */
4215
     Srb->SrbStatus = (UCHAR)status;
4216
     dprintk("AtapiStartIo: status=%lx\n", status);
4217
     TaskDone(pChan, Srb);
4218
    }
4219
4220
 return TRUE;
4221
4222
} /* end AtapiStartIo */
4223
4224
/************************************************************************
4225
 * Convert Scsi_Cmnd structure to SCSI_REQUEST_BLOCK
4226
 ************************************************************************/
4227
void
4228
MapRequest(Scsi_Cmnd * pREQ, PSCSI_REQUEST_BLOCK Srb)
4229
{
4230
4231
 Srb->Length 	= sizeof(SCSI_REQUEST_BLOCK);
4232
 Srb->CdbLength	= pREQ->cmd_len;
4233
 Srb->TargetId	= pREQ->target;
4234
 Srb->Lun	= pREQ->lun;
4235
 Srb->UseSg	= pREQ->use_sg;
4236
4237
 /*
4238
  * Copy the actual command from Scsi_Cmnd to CDB.
4239
  */
4240
 memcpy(Srb->Cdb, pREQ->cmnd, Srb->CdbLength);
4241
4242
 /*
4243
  * Always the SCSI_FUNCTION_EXECUTE_SCSI now.
4244
  */
4245
 Srb->Function = SRB_FUNCTION_EXECUTE_SCSI;
4246
4247
 Srb->SrbStatus			= 0;
4248
 Srb->ScsiStatus		= 0;
4249
 Srb->SenseInfoBufferLength	= 16;
4250
4251
 /*
4252
  * The CDB's first byte is operation code.
4253
  */
4254
 if ((Srb->Cdb[0] == SCSIOP_WRITE6) || (Srb->Cdb[0] == SCSIOP_WRITE) ||
4255
     (Srb->Cdb[0] == SCSIOP_MODE_SELECT10))
4256
    {
4257
     Srb->SrbFlags = SRB_FLAGS_DATA_OUT;
4258
    }
4259
 else
4260
    {
4261
     Srb->SrbFlags = SRB_FLAGS_DATA_IN;
4262
    }
4263
4264
 Srb->TimeOutValue	 = 0;
4265
 Srb->SenseInfoBuffer	 = pREQ->sense_buffer;
4266
4267
 if (Srb->Cdb[0] == SCSIOP_REQUEST_SENSE)
4268
    {
4269
     Srb->DataTransferLength = 0x40;
4270
     Srb->DataBuffer	     = pREQ->sense_buffer;
4271
    }
4272
 else
4273
    {
4274
     Srb->DataTransferLength = pREQ->request_bufflen;
4275
     Srb->DataBuffer	     = pREQ->request_buffer;
4276
    }
4277
4278
 if (pREQ->use_sg)
4279
    {
4280
     Srb->WorkingFlags |= SRB_WFLAGS_USE_SG;
4281
    }
4282
4283
 Srb->pREQ = pREQ;
4284
4285
} /* end MapRequest */
4286
4287
/************************************************************************
4288
 * Function:    void ite_watch_timer(PChannel pChan)
4289
 * Description: WatchDog timer
4290
 * Returns:     None
4291
 ************************************************************************/
4292
void
4293
ite_watch_timer(PChannel pChan)
4294
{
4295
4296
 unsigned long		flags;
4297
 Scsi_Cmnd *		pREQ;
4298
 PSCSI_REQUEST_BLOCK	Srb;
4299
4300
 printk("ite_watch_timer enter\n");
4301
4302
 spin_lock_irqsave (&io_request_lock, flags);
4303
4304
 Srb = pChan->CurrentSrb;
4305
 if (Srb) { pREQ = Srb->pREQ; }
4306
 else	  { pREQ = NULL;      }
4307
4308
 spin_unlock_irqrestore (&io_request_lock, flags);
4309
4310
 printk("ite_watch_timer exit\n");
4311
4312
} /* end ite_watch_timer */
4313
4314
/************************************************************************
4315
 * Function:    void iteretry(PChannel pChan)
4316
 * Description: This function will be call after waitting 1 ticks (1 tick
4317
 *              = 1/Hz second). It will execute retry operation.
4318
 * Returns:     None
4319
 ************************************************************************/
4320
void
4321
iteretry(PChannel pChan)
4322
{
4323
4324
 Scsi_Cmnd *		pREQ;
4325
 unsigned long		flags;
4326
4327
 spin_lock_irqsave(&io_request_lock, flags);
4328
4329
 if (pChan->CurrentSrb)
4330
    {
4331
     pREQ = pChan->CurrentSrb->pREQ;
4332
     printk("##iteretry##\n");
4333
     TaskStart(pChan, pREQ);
4334
    }
4335
4336
 spin_unlock_irqrestore(&io_request_lock, flags);
4337
4338
} /* end iteretry */
4339
4340
/************************************************************************
4341
 * A task execution has been done. For OS request, we need to Notify OS
4342
 * and invoke next take which wait at queue
4343
 ************************************************************************/
4344
void
4345
TaskDone(PChannel pChan, PSCSI_REQUEST_BLOCK Srb)
4346
{
4347
4348
 Scsi_Cmnd * pREQ = Srb->pREQ;
4349
4350
 pChan->CurrentSrb = NULL;
4351
 pChan->RetryCount = 0;
4352
4353
 switch (SRB_STATUS(Srb->SrbStatus))
4354
    {
4355
     case SRB_STATUS_SUCCESS:
4356
	 pREQ->result = (DID_OK << 16);
4357
	 break;
4358
4359
     case SRB_STATUS_SELECTION_TIMEOUT:
4360
	 pREQ->result = (DID_NO_CONNECT << 16);
4361
	 break;
4362
4363
     case SRB_STATUS_BUSY:
4364
	 pREQ->result =	(DID_BUS_BUSY << 16);
4365
	 break;
4366
4367
     case SRB_STATUS_BUS_RESET:
4368
	 pREQ->result =	(DID_RESET << 16);
4369
	 break;
4370
4371
     case SRB_STATUS_INVALID_TARGET_ID:
4372
     case SRB_STATUS_INVALID_PATH_ID:
4373
     case SRB_STATUS_INVALID_LUN:
4374
     case SRB_STATUS_NO_HBA:
4375
	 pREQ->result = (DID_BAD_TARGET << 16);
4376
	 break;
4377
4378
     case SRB_STATUS_NO_DEVICE:
4379
	 pREQ->result = (DID_BAD_TARGET << 16);
4380
	 break;
4381
4382
     case SRB_STATUS_ERROR:
4383
	 pREQ->result = (DRIVER_SENSE << 24) | (DID_OK << 16) |
4384
		 	(CHECK_CONDITION << 1);
4385
	 break;
4386
    }
4387
4388
 dprintk("TaskDone(pChan=%p, pREQ=%p, result=%x)\n",
4389
	pChan, pREQ, pREQ->result);
4390
4391
 /*
4392
  * Notify OS that this OS request has been done.
4393
  */
4394
 pREQ->scsi_done(pREQ);
4395
4396
 /*
4397
  * Check the queue again.
4398
  */
4399
 TaskQueue();
4400
4401
} /* end TaskDone */
4402
4403
/************************************************************************
4404
 * Start a command, doing convert first.
4405
 ************************************************************************/
4406
void
4407
TaskStart(PChannel pChan, Scsi_Cmnd *pREQ)
4408
{
4409
4410
 PSCSI_REQUEST_BLOCK	Srb;
4411
4412
 dprintk("TaskStart(pChan=%p, pREQ=%p)\n", pChan, pREQ);
4413
4414
 Srb = &pChan->_Srb;
4415
4416
 /*
4417
  * Clear the SRB structure.
4418
  */
4419
 memset(Srb, 0, sizeof(SCSI_REQUEST_BLOCK));
4420
4421
 /*
4422
  * Convert Scsi_Cmnd structure to SCSI_REQUEST_BLOCK.
4423
  */
4424
 MapRequest(pREQ, Srb);
4425
4426
 /*
4427
  * Start IDE I/O command.
4428
  */
4429
 AtapiStartIo(pChan, Srb);
4430
4431
} /* end TaskStart */
4432
4433
/************************************************************************
4434
 * Check if queue is empty. If there are request in queue, transfer the
4435
 * request to HIM's request and execute the request.
4436
 ************************************************************************/
4437
void
4438
TaskQueue(void)
4439
{
4440
4441
 unsigned long		flags;
4442
 Scsi_Cmnd *		SCpnt;
4443
 PChannel		pChan;
4444
 PITE_ADAPTER		pAdap;
4445
4446
 check_next:
4447
4448
 if (it8212_req_last != NULL)
4449
    {
4450
     spin_lock_irqsave(&queue_request_lock, flags);
4451
     SCpnt = (Scsi_Cmnd *)it8212_req_last->SCp.ptr;
4452
4453
     if (it8212_req_last == SCpnt)
4454
	{
4455
	 it8212_req_last = NULL;
4456
	}
4457
     else
4458
	{
4459
	 it8212_req_last->SCp.ptr = (char *)SCpnt->SCp.ptr;
4460
	}
4461
     spin_unlock_irqrestore(&queue_request_lock, flags);
4462
4463
     pAdap = ite_adapters[0];
4464
4465
     if (SCpnt->target < 2)	 pChan = &pAdap->IDEChannel[0];
4466
     else			 pChan = &pAdap->IDEChannel[1];
4467
4468
     /*
4469
      * Check the command.
4470
      */
4471
     if (SCpnt->host)
4472
	{
4473
	 if (SCpnt->channel != 0 || SCpnt->target >= 4)
4474
	    {
4475
	     /*
4476
	      * Returns that we have a bad target.
4477
	      */
4478
	     dprintk("BAD TARGET\n");
4479
	     SCpnt->result = (DID_BAD_TARGET << 16);
4480
	     SCpnt->scsi_done(SCpnt);
4481
	     goto check_next;
4482
	    }
4483
	}
4484
4485
     TaskStart(pChan, SCpnt);
4486
     return;
4487
    }
4488
4489
} /* end TaskQueue */
4490
4491
/****************************************************************
4492
 * Name:	iteraid_queuecommand
4493
 * Description:	Process a queued command from the SCSI manager.
4494
 * Parameters:	SCpnt - Pointer to SCSI command structure.
4495
 *		done  - Pointer to done function to call.
4496
 * Returns:	Status code.
4497
 ****************************************************************/
4498
int
4499
iteraid_queuecommand(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
4500
{
4501
4502
 unsigned long		flags;
4503
4504
 dprintk("##Queuecommand enter##\n");
4505
4506
 /*
4507
  * Hooks the done routine.
4508
  */
4509
 SCpnt->scsi_done = (void *)done;
4510
4511
 spin_lock_irqsave(&queue_request_lock, flags);
4512
 if (it8212_req_last == NULL)
4513
    {
4514
     SCpnt->SCp.ptr = (char *)SCpnt;
4515
    }
4516
 else
4517
    {
4518
     SCpnt->SCp.ptr = it8212_req_last->SCp.ptr;
4519
     it8212_req_last->SCp.ptr = (char *)SCpnt;
4520
    }
4521
 it8212_req_last = SCpnt;
4522
 spin_unlock_irqrestore(&queue_request_lock, flags);
4523
4524
 spin_lock_irqsave(&gio_request_lock, flags);
4525
 TaskQueue();
4526
 spin_unlock_irqrestore(&gio_request_lock, flags);
4527
4528
 dprintk("@@Queuecommand exit@@\n");
4529
4530
 return 0;
4531
4532
} /* end iteraid_queuecommand */
4533
4534
/****************************************************************
4535
 * Name:	internal_done :LOCAL
4536
 * Description:	Done handler for non-queued commands
4537
 * Parameters:	SCpnt - Pointer to SCSI command structure.
4538
 * Returns:	Nothing.
4539
 ****************************************************************/
4540
static void
4541
internal_done(Scsi_Cmnd * SCpnt)
4542
{
4543
4544
 SCpnt->SCp.Status++;
4545
4546
} /* end internal_done */
4547
4548
/****************************************************************
4549
 * Name:	iteraid_command
4550
 * Description:	Process a command from the SCSI manager.
4551
 * Parameters:	SCpnt - Pointer to SCSI command structure.
4552
 * Returns:	Status code.
4553
 ****************************************************************/
4554
int
4555
iteraid_command(Scsi_Cmnd *SCpnt)
4556
{
4557
4558
 DWORD		timeout;
4559
4560
 SCpnt->SCp.Status = 0;
4561
 iteraid_queuecommand(SCpnt, internal_done);
4562
4563
 /*
4564
  * Should be longer than hard-reset time.
4565
  */
4566
 timeout = jiffies + 60 * HZ;
4567
 while (!SCpnt->SCp.Status && time_before(jiffies, timeout))
4568
    {
4569
     barrier();
4570
    }
4571
4572
 if (!SCpnt->SCp.Status) SCpnt->result = (DID_ERROR << 16);
4573
4574
 return SCpnt->result;
4575
4576
} /* end iteraid_command */
4577
4578
/************************************************************************
4579
 * Enables disables media status notification
4580
 ************************************************************************/
4581
void
4582
IdeMediaStatus(BOOLEAN	EnableMSN, PChannel pChan, ULONG Device)
4583
{
4584
4585
 UCHAR			statusByte;
4586
 UCHAR			errorByte;
4587
4588
 statusByte = 0;
4589
4590
 if (EnableMSN == TRUE)
4591
    {
4592
     /*
4593
      * If supported enable Media Status Notification support.
4594
      */
4595
     if ((pChan->DeviceFlags[Device] & DFLAGS_REMOVABLE_DRIVE))
4596
	{
4597
	 outb((UCHAR) (0x95), pChan->io_ports[IDE_FEATURE_OFFSET]);
4598
	 outb(IDE_COMMAND_ENABLE_MEDIA_STATUS, pChan->io_ports[IDE_COMMAND_OFFSET]);
4599
4600
	 WaitOnBaseBusy(pChan, statusByte);
4601
4602
	 if (statusByte & IDE_STATUS_ERROR)
4603
	    {
4604
	     /*
4605
	      * Read the error register.
4606
	      */
4607
	     errorByte = inb(pChan->io_ports[IDE_ERROR_OFFSET]);
4608
4609
	     printk("Error enabling media status. Status %u, error byte %u\n",
4610
		statusByte, errorByte);
4611
	    }
4612
	 else
4613
	    {
4614
	     pChan->DeviceFlags[Device] |= DFLAGS_MEDIA_STATUS_ENABLED;
4615
	     printk("Media Status Notification Supported\n");
4616
	     pChan->ReturningMediaStatus = 0;
4617
	    }
4618
	}
4619
    }
4620
 else /* end if EnableMSN == TRUE */
4621
    {
4622
     /*
4623
      * Disable if previously enabled.
4624
      */
4625
     if ((pChan->DeviceFlags[Device] & DFLAGS_MEDIA_STATUS_ENABLED))
4626
	{
4627
	 outb((UCHAR)(0x31), pChan->io_ports[IDE_FEATURE_OFFSET]);
4628
	 outb(IDE_COMMAND_ENABLE_MEDIA_STATUS,
4629
			pChan->io_ports[IDE_COMMAND_OFFSET]);
4630
4631
	 WaitOnBaseBusy(pChan, statusByte);
4632
	 pChan->DeviceFlags[Device] &= ~DFLAGS_MEDIA_STATUS_ENABLED;
4633
	}
4634
    }
4635
4636
} /* end IdeMediaStatus */
4637
4638
/************************************************************************
4639
 * Add watchdog timer.
4640
 ************************************************************************/
4641
void
4642
SetWatchDogTimer(PChannel pChan)
4643
{
4644
4645
 pChan->watch_timer->expires = jiffies + 3 * HZ;
4646
 add_timer(pChan->watch_timer);
4647
4648
} /* end SetWatchDogTimer */
4649
4650
/************************************************************************
4651
 * Cancel the watchdog timer.
4652
 ************************************************************************/
4653
void
4654
CancelWatchDogTimer(PChannel pChan)
4655
{
4656
4657
 del_timer(pChan->watch_timer);
4658
4659
} /* end CancelWatchDogTimer */
4660
4661
/************************************************************************
4662
 * Issue IDENTIFY command to a device
4663
 * Either the standard (EC) or the ATAPI packet (A1) IDENTIFY.
4664
 ************************************************************************/
4665
BOOLEAN
4666
IssueIdentify(PChannel pChan, UCHAR DeviceNumber, UCHAR Command)
4667
{
4668
4669
 ULONG			i, j;
4670
 UCHAR			statusByte = 0;
4671
4672
 /*
4673
  * Check that the status register makes sense.
4674
  */
4675
 GetBaseStatus(pChan, statusByte);
4676
4677
 if (Command == IDE_COMMAND_IDENTIFY)
4678
    {
4679
     /*
4680
      * Mask status byte ERROR bits.
4681
      */
4682
     statusByte &= ~(IDE_STATUS_ERROR | IDE_STATUS_INDEX);
4683
4684
     printk("IssueIdentify: Checking for IDE. Status (%x)\n", statusByte);
4685
4686
     /*
4687
      * Check if register value is reasonable.
4688
      */
4689
     if (statusByte != IDE_STATUS_IDLE)
4690
	{
4691
	 /*
4692
	  * Reset the channel.
4693
	  */
4694
	 printk("IssueIdentify: Resetting channel.\n");
4695
	 IdeHardReset(pChan, statusByte);
4696
4697
	 outb((UCHAR)((DeviceNumber << 4) | 0xA0), pChan->io_ports[IDE_SELECT_OFFSET]);
4698
	 GetBaseStatus(pChan, statusByte);
4699
	 statusByte &= ~IDE_STATUS_INDEX;
4700
4701
	 if (statusByte != IDE_STATUS_IDLE)
4702
	    {
4703
	     /*
4704
	      * Give up on this.
4705
	      */
4706
	     printk("IssueIdentify (IDE): Disk[%d] not ready. Status=0x%x\n",
4707
		     DeviceNumber, statusByte);
4708
	     return FALSE;
4709
	    }
4710
	}
4711
    }
4712
 else
4713
    {
4714
     dprintk("IssueIdentify: Checking for ATAPI. Status (%x)\n", statusByte);
4715
     if ((statusByte & IDE_STATUS_BUSY) || (statusByte & IDE_STATUS_DRQ))
4716
	{
4717
	 /*
4718
	  * Reset the device.
4719
	  */
4720
	 dprintk("IssueIdentify: Resetting device.\n");
4721
	 AtapiSoftReset(pChan, DeviceNumber);
4722
4723
	 outb((UCHAR)((DeviceNumber << 4) | 0xA0), pChan->io_ports[IDE_SELECT_OFFSET]);
4724
	 GetBaseStatus(pChan, statusByte);
4725
	 if (statusByte != 0)
4726
	    {
4727
	     /*
4728
	      * Give up on this.
4729
	      */
4730
	     printk("IssueIdentify (ATAPI): Disk[%d] not ready. Status=0x%x\n", DeviceNumber, statusByte);
4731
	     return FALSE;
4732
	    }
4733
	}
4734
    }
4735
4736
 for (j = 0; j < 2; j++)
4737
    {
4738
     /*
4739
      * Wait for device ready (Not Busy and Not DRQ).
4740
      */
4741
     outb((UCHAR)((DeviceNumber << 4) | 0xA0), pChan->io_ports[IDE_SELECT_OFFSET]);
4742
     WaitForDeviceReady(pChan, statusByte);
4743
     if ((statusByte & IDE_STATUS_BUSY) || (statusByte & IDE_STATUS_DRQ))
4744
	{
4745
	 printk("IssueIdentify: Disk[%d] not ready. Status=0x%x\n", DeviceNumber, statusByte);
4746
	 continue;
4747
	}
4748
4749
     /*
4750
      * Send IDENTIFY command.
4751
      */
4752
     outb(Command, pChan->io_ports[IDE_COMMAND_OFFSET]);
4753
4754
     /*
4755
      * Wait for DRQ.
4756
      */
4757
     WaitForBaseDrq(pChan, statusByte);
4758
     if (!(statusByte & IDE_STATUS_DRQ))
4759
	{
4760
	 printk("IssueIdentify: Disk[%d] DRQ never asserted. Status=%x\n", DeviceNumber, statusByte);
4761
4762
	 /*
4763
	  * Give one more chance.
4764
	  */
4765
	 if (Command == IDE_COMMAND_IDENTIFY)
4766
	    {
4767
	     IdeHardReset(pChan, statusByte);
4768
	    }
4769
	 else
4770
	    {
4771
	     AtapiSoftReset(pChan, DeviceNumber);
4772
	    }
4773
	}
4774
     else
4775
	{
4776
	 break;
4777
	}
4778
    }
4779
4780
 /*
4781
  * Check for error on really stupid master devices that assert random
4782
  * patterns of bits in the status register at the slave address.
4783
  */
4784
 outb((UCHAR)((DeviceNumber << 4) | 0xA0), pChan->io_ports[IDE_SELECT_OFFSET]);
4785
 GetBaseStatus(pChan, statusByte);
4786
 if (statusByte & IDE_STATUS_ERROR)
4787
    {
4788
     printk("IssueIdentify: Disk[%d] returns error status\n", DeviceNumber);
4789
     return FALSE;
4790
    }
4791
4792
 dprintk("IssueIdentify: Status before read words %x\n", statusByte);
4793
4794
 /*
4795
  * Suck out 256 words. After waiting for one model that asserts busy
4796
  * after receiving the Packet Identify command.
4797
  */
4798
 WaitOnBusy(pChan, statusByte);
4799
4800
 if (!(statusByte & IDE_STATUS_DRQ)) { return FALSE; }
4801
4802
 ReadBuffer(pChan, (PUSHORT)&pChan->FullIdentifyData, 256);
4803
4804
 /*
4805
  * Check out a few capabilities / limitations of the device.
4806
  * 01/29/2003
4807
  */
4808
 if (pChan->FullIdentifyData.SpecialFunctionsEnabled & 1)
4809
    {
4810
     /*
4811
      * Determine if this drive supports the MSN functions.
4812
      */
4813
     printk("Marking drive %x as removable. SFE = %x\n",
4814
	DeviceNumber, pChan->FullIdentifyData.SpecialFunctionsEnabled);
4815
     pChan->DeviceFlags[DeviceNumber] |= DFLAGS_REMOVABLE_DRIVE;
4816
    }
4817
4818
 memcpy(&pChan->IdentifyData[DeviceNumber], &pChan->FullIdentifyData,
4819
		sizeof(IDENTIFY_DATA2));
4820
4821
 if (pChan->IdentifyData[DeviceNumber].GeneralConfiguration & 0x20 &&
4822
        Command != IDE_COMMAND_IDENTIFY)
4823
    {
4824
     /*
4825
      * This device interrupts with the assertion of DRQ after receiving
4826
      * Atapi Packet Command.
4827
      */
4828
     pChan->DeviceFlags[DeviceNumber] |= DFLAGS_INT_DRQ;
4829
4830
     dprintk(KERN_NOTICE "Device interrupts on assertion of DRQ.\n");
4831
    }
4832
 else
4833
    {
4834
     dprintk(KERN_NOTICE "Device does't interrupt on assertion of DRQ.\n");
4835
    }
4836
4837
 if (((pChan->IdentifyData[DeviceNumber].GeneralConfiguration & 0xF00)
4838
	== 0x100) && Command != IDE_COMMAND_IDENTIFY)
4839
    {
4840
     /*
4841
      * This is a tape.
4842
      */
4843
     pChan->DeviceFlags[DeviceNumber] |= DFLAGS_TAPE_DEVICE;
4844
     printk(KERN_NOTICE "IssueIdentify: Device is a tape drive.\n");
4845
    }
4846
 else
4847
    {
4848
     dprintk(KERN_NOTICE "IssueIdentify: Device is not a tape drive.\n");
4849
    }
4850
4851
 /*
4852
  * Work around for some IDE and one model Atapi that will present more
4853
  * then 256 bytes for the Identify data.
4854
  */
4855
 WaitOnBaseBusy(pChan, statusByte);
4856
4857
 for (i = 0; i < 0x10000; i++)
4858
    {
4859
     GetStatus(pChan, statusByte);
4860
4861
     if (statusByte & IDE_STATUS_DRQ)
4862
	{
4863
         /*
4864
          * Suck out any remaining bytes and throw away.
4865
          */
4866
	 inw(pChan->io_ports[IDE_DATA_OFFSET]);
4867
	}
4868
     else
4869
	{
4870
	 break;
4871
	}
4872
    }
4873
4874
 return TRUE;
4875
4876
} /* end IssueIdentify() */
4877
4878
/************************************************************************
4879
 * Check this is the IDE or ATAPI disk then identify it
4880
 ************************************************************************/
4881
static BOOLEAN
4882
iteraid_find_device(PChannel pChan, UCHAR channel)
4883
{
4884
4885
 BOOLEAN		deviceResponded = FALSE;
4886
 UCHAR			deviceNumber;
4887
 UCHAR			signatureLow;
4888
 UCHAR			signatureHigh;
4889
 UCHAR			statusByte = 0;
4890
4891
4892
 /*
4893
  * Clear expecting interrupt flag and current SRB field.
4894
  */
4895
 pChan->ExpectingInterrupt = FALSE;
4896
 pChan->CurrentSrb	   = NULL;
4897
4898
 /*
4899
  * Search for devices in each channel.
4900
  */
4901
 for (deviceNumber = 0; deviceNumber < 2; deviceNumber++)
4902
    {
4903
     /*
4904
      * Select the device.
4905
      */
4906
     outb((UCHAR)((deviceNumber << 4) | 0xA0), pChan->io_ports[IDE_SELECT_OFFSET]);
4907
4908
     /*
4909
      * Disable interrupts during initialization.
4910
      */
4911
     outb(IDE_DC_DISABLE_INTERRUPTS, pChan->io_ports[IDE_CONTROL_OFFSET]);
4912
4913
     /*
4914
      * Check here for some SCSI adapters that incorporate IDE emulation.
4915
      */
4916
     statusByte = inb(pChan->io_ports[IDE_CONTROL_OFFSET]);
4917
4918
     /*
4919
      * Do soft reset on selected device. (AtapiSoftReset)
4920
      */
4921
     AtapiSoftReset(pChan, deviceNumber);
4922
     WaitOnBusy(pChan, statusByte);
4923
4924
     signatureLow  = inb(pChan->io_ports[IDE_MIDCYL_OFFSET]);
4925
     signatureHigh = inb(pChan->io_ports[IDE_HCYL_OFFSET]);
4926
4927
     if (signatureLow == 0x14 && signatureHigh == 0xEB)
4928
	{
4929
	 /*
4930
	  * ATAPI signature found. Issue ATAPI packet identify command.
4931
	  */
4932
	 if (IssueIdentify(pChan, deviceNumber, IDE_COMMAND_ATAPI_IDENTIFY))
4933
	    {
4934
	     /*
4935
	      * Indicate ATAPI device.
4936
	      */
4937
	     printk("iteraid_find_device: Channel %x Device %x is ATAPI.\n",
4938
		channel, deviceNumber);
4939
4940
	     pChan->DeviceFlags[deviceNumber] |= DFLAGS_ATAPI_DEVICE;
4941
	     pChan->DeviceFlags[deviceNumber] |= DFLAGS_DEVICE_PRESENT;
4942
	     pChan->DeviceFlags[deviceNumber] &= ~DFLAGS_CONFIG_CHANGED;
4943
4944
	     deviceResponded = TRUE;
4945
4946
	     GetStatus(pChan, statusByte);
4947
	     if (statusByte & IDE_STATUS_ERROR)
4948
		{
4949
		 AtapiSoftReset(pChan, deviceNumber);
4950
		}
4951
	    }
4952
	 else
4953
	    {
4954
	     /*
4955
	      * Indicate no working device.
4956
	      */
4957
	     printk("iteraid_find_device: Channel %x Device %x not respond.\n",
4958
		channel, deviceNumber);
4959
	     pChan->DeviceFlags[deviceNumber] &= ~DFLAGS_DEVICE_PRESENT;
4960
	    }
4961
	}
4962
     else
4963
	{
4964
	 /*
4965
	  * Select the device.
4966
	  */
4967
	 outb((UCHAR)((deviceNumber << 4) | 0xA0), pChan->io_ports[IDE_SELECT_OFFSET]);
4968
4969
	 /*
4970
	  * Check here for some SCSI adapters that incorporate IDE emulation.
4971
	  */
4972
	 GetStatus(pChan, statusByte);
4973
4974
	 /*
4975
	  * No Disk.
4976
	  */
4977
	 if (statusByte == 0xFF || statusByte == 0x7F || statusByte == 0x0)
4978
	    {
4979
	     dprintk("FindDevices: Cannot find IDE device. status = %x\n", statusByte);
4980
	     continue;
4981
	    }
4982
4983
	 /*
4984
	  * Issue IDE Identify. If an ATAPI device is actually present,
4985
	  * the signature will be asserted, and the drive will be
4986
	  * recognized as such.
4987
	  */
4988
	 if (IssueIdentify(pChan, deviceNumber, IDE_COMMAND_IDENTIFY))
4989
	    {
4990
	     /*
4991
	      * IDE drive found.
4992
	      */
4993
	     dprintk(KERN_WARNING "FindDevices: Device %u is IDE\n",
4994
		(channel * 2) + deviceNumber);
4995
4996
	     pChan->DeviceFlags[deviceNumber] |= DFLAGS_DEVICE_PRESENT;
4997
	     pChan->DeviceFlags[deviceNumber] &= ~DFLAGS_ATAPI_DEVICE;
4998
	     pChan->DeviceFlags[deviceNumber] &= ~DFLAGS_CONFIG_CHANGED;
4999
5000
	     deviceResponded = TRUE;
5001
	    }
5002
	 else
5003
	    {
5004
	     dprintk(KERN_WARNING "FindDevices: Device %u is not present\n",
5005
		(channel * 2) + deviceNumber);
5006
	     pChan->DeviceFlags[deviceNumber] &= ~DFLAGS_DEVICE_PRESENT;
5007
	    }
5008
	}
5009
    }
5010
5011
 return deviceResponded;
5012
5013
} /* end iteraid_find_device */
5014
5015
/************************************************************************
5016
 * IDE disk hardware initialize
5017
 ************************************************************************/
5018
BOOLEAN
5019
AtapiHwInitialize(PITE_ADAPTER pAdap, PChannel pChan, UCHAR channel)
5020
{
5021
5022
 UCHAR			i;
5023
 UCHAR			statusByte = 0;
5024
5025
 /*
5026
  * For two devices in this channel.
5027
  */
5028
 for (i = 0; i < 2; i++)
5029
    {
5030
     /*
5031
      * only check in Fireware mode.
5032
      */
5033
     if (pAdap->bypass_mode == FALSE)
5034
	{
5035
	 outb((UCHAR) (0xA0 | ((UCHAR) i << 4)), pChan->io_ports[IDE_SELECT_OFFSET]);
5036
5037
	 /*
5038
	  * Check if card at this address.
5039
	  */
5040
	 outb(0xAA, pChan->io_ports[IDE_MIDCYL_OFFSET]);
5041
5042
	 /*
5043
	  * Check if indentifier can be read back.
5044
	  */
5045
	if ((statusByte = inb(pChan->io_ports[IDE_MIDCYL_OFFSET])) != 0xAA)
5046
	   {
5047
	     printk("AtapiHwInitialize: Identifier read back from (%x, %x) = %x\n",
5048
	     channel, i, statusByte);
5049
5050
	     /*
5051
	      * ***** Dont free it....For later use *****
5052
	      * ScsiPortFreeDeviceBase(HwDeviceExtension, ioSpace1);
5053
	      */
5054
	     continue;
5055
	    }
5056
5057
	 printk("AtapiHwInitialize: Found ATA device (%x, %x)n", channel, i);
5058
	}
5059
5060
    }
5061
5062
 return TRUE;
5063
5064
} /* end AtapiHwInitialize */
5065
5066
/************************************************************************
5067
 * Initialize a adapter, return 0 means success
5068
 ************************************************************************/
5069
static int
5070
iteraid_init(PITE_ADAPTER pAdap, struct pci_dev *pPciDev)
5071
{
5072
5073
 unsigned char		z;
5074
 unsigned char		i;
5075
 unsigned char		j;
5076
 unsigned int		set_irq;
5077
 USHORT			control_addr;	/* Control reg base address	*/
5078
 USHORT			base_addr;	/* IDE I/O port base address	*/
5079
 USHORT			bm_base_addr;	/* Bus Master base address	*/
5080
 PChannel		pChan;		/* Use for each channel		*/
5081
5082
 /*
5083
  * Common settings.
5084
  */
5085
 pAdap->pci_bus	 = pPciDev->bus->number;
5086
 pAdap->devfn	 = pPciDev->devfn;
5087
 pAdap->irq	 = pPciDev->irq;
5088
 pAdap->irqOwned = 0;
5089
5090
 printk(KERN_NOTICE "iteraid_init: Found Controller: %s\n", pAdap->name);
5091
5092
 /*
5093
  * Allocate buffer for IDE channles (One IT8212 supports two channels)
5094
  */
5095
 pAdap->IDEChannel =
5096
 (PChannel)kmalloc(sizeof(Channel) * pAdap->num_channels, GFP_ATOMIC);
5097
5098
 if (pAdap->IDEChannel == 0)
5099
    {
5100
     printk("iteraid_init: kmalloc() failed for channels.\n");
5101
     return -1;
5102
    }
5103
5104
 memset(pAdap->IDEChannel, 0, sizeof(Channel) * pAdap->num_channels);
5105
5106
 set_irq = 1;
5107
 for (i = 0; i < NumAdapters; i++)
5108
    {
5109
     if (ite_adapters[i]->irqOwned == pAdap->irq) set_irq = 0;
5110
    }
5111
5112
 /*
5113
  * Request the irq (share irq) and hook the interrupt service routine.
5114
  */
5115
 if (set_irq)
5116
    {
5117
     if (request_irq(pAdap->irq, Irq_Handler, SA_SHIRQ, PROC_DIR_NAME, pAdap) < 0)
5118
	{
5119
	 printk("Unable to allocate IRQ for %s\n", pAdap->name);
5120
	 return -1;
5121
	}
5122
     pAdap->irqOwned = pAdap->irq;
5123
    }
5124
5125
 /*
5126
  * Get the IDE port and DMA registers.
5127
  */
5128
 for (i = 0; i < pAdap->num_channels; i++)
5129
    {
5130
     /*
5131
      * Reference the book "LINUX DEVICE DRIVER 2nd", Page 484
5132
      * unsigned long pci_resource_start(struct pci_dev *dev, int bar);
5133
      *
5134
      * This function returns the first address (memory address or I/O
5135
      * port number) associated with one of the six PCI I/O regions. The
5136
      * region is selected by the integer bar (the base address register)
5137
      * ranging from 0 to 5, inclusive.
5138
      */
5139
     base_addr = pci_resource_start(pPciDev, i * 2);
5140
     bm_base_addr = pci_resource_start(pPciDev, 4);
5141
5142
     pChan = &pAdap->IDEChannel[i];
5143
5144
     if (i == 0)
5145
 	{
5146
	 control_addr = pci_resource_start(pPciDev, 1);
5147
	 pChan->dma_base = bm_base_addr;
5148
   	}
5149
     else
5150
  	{
5151
	 control_addr = pci_resource_start(pPciDev, 3);
5152
	 pChan->dma_base = bm_base_addr + 8;
5153
	}
5154
5155
     for (j = 0; j <= IDE_STATUS_OFFSET; j++)
5156
	{
5157
	 pChan->io_ports[j] = base_addr;
5158
	 base_addr += 1;
5159
	}
5160
5161
     pChan->io_ports[IDE_CONTROL_OFFSET] = control_addr + 2;
5162
    }
5163
5164
 /*
5165
  * Initialize channels.
5166
  */
5167
 for (z = 0; z < pAdap->num_channels; z++)
5168
    {
5169
     pChan	    = &pAdap->IDEChannel[z];
5170
     pChan->pPciDev = pPciDev;
5171
     pChan->channel = z;
5172
5173
     /*
5174
      * This section should be masked off if BIOS is ready.
5175
      */
5176
#    if (MARK_DEBUG_BYPASS_MODE)
5177
     /*
5178
      * BIOS is not ready, so I change to ByPass Mode by myself.
5179
      */
5180
     pAdap->bypass_mode = TRUE;
5181
5182
     /*
5183
      * Change to bypass mode.
5184
      */
5185
     IT8212InitBypassMode(pPciDev);
5186
#    endif
5187
5188
     /*
5189
      * Hardware initialize.
5190
      */
5191
#    if (0)
5192
     AtapiHwInitialize(pAdap, pChan, z);
5193
#    endif
5194
5195
     /*
5196
      * Find and identify the IDE or ATAPI device.
5197
      */
5198
     iteraid_find_device(pChan, z);
5199
5200
     /*
5201
      * Set the best transfer mode.
5202
      */
5203
     IT8212SetBestTransferMode(pAdap, pChan, z);
5204
5205
     /*
5206
      * Set Scatter/Gather List buffer for the channel.
5207
      */
5208
     IdeSetupDma(pChan, pChan->dma_base, 8);
5209
5210
     /*
5211
      * Setup retry timer.
5212
      */
5213
     pChan->retry_timer =
5214
	(struct timer_list *)kmalloc(sizeof(struct timer_list) * 2, GFP_ATOMIC);
5215
5216
     if (pChan->retry_timer == NULL)
5217
	{
5218
	 printk("Allocate retry_timer failed\n");
5219
	 return -1;
5220
	}
5221
5222
     init_timer(pChan->retry_timer);
5223
     pChan->retry_timer->function = (void (*)(ULONG))iteretry;
5224
     pChan->retry_timer->data = (ULONG)pChan;
5225
5226
     pChan->watch_timer = pChan->retry_timer + 1;
5227
     init_timer(pChan->watch_timer);
5228
     pChan->watch_timer->function = (void (*)(ULONG))ite_watch_timer;
5229
     pChan->watch_timer->data = (ULONG)pChan;
5230
    }
5231
5232
 return 0;
5233
5234
} /* end iteraid_init */
5235
5236
/************************************************************************
5237
 * This function will find and initialize any cards
5238
 ************************************************************************/
5239
int
5240
iteraid_detect(Scsi_Host_Template *tpnt)
5241
{
5242
5243
 int			major = 0;
5244
 UCHAR			i;
5245
 UCHAR			j;
5246
 UCHAR			mode = 0;
5247
 PChannel		pChan;
5248
 PITE_ADAPTER		pAdap;
5249
 struct pci_dev *	pPciDev;
5250
5251
 /*
5252
  * Check if the kernel has PCI interface compiled in.
5253
  */
5254
 if (!pci_present())
5255
    {
5256
     printk("iteraid_detect: No pci interface present.\n");
5257
     return 0;
5258
    }
5259
5260
 /*
5261
  * Search ITE IT8212 chip.
5262
  */
5263
 pPciDev = NULL;
5264
 while ((pPciDev = pci_find_device(ITE_VENDOR_ID, ITE_DEVICE_ID, pPciDev)))
5265
    {
5266
     if (PCI_FUNC(pPciDev->devfn)) continue;
5267
5268
     /*
5269
      * Allocate memory for Adapter.
5270
      */
5271
     pAdap = (PITE_ADAPTER)kmalloc(sizeof(ITE_ADAPTER), GFP_ATOMIC);
5272
5273
     if (pAdap == NULL)
5274
	{
5275
	 printk("iteraid_detect: kmalloc() failed for card.\n");
5276
	 continue;
5277
	}
5278
5279
     memset(pAdap, 0, sizeof(ITE_ADAPTER));
5280
5281
     pAdap->name	 = CONTROLLER_NAME_IT8212;
5282
     pAdap->num_channels = 2;
5283
     pAdap->pci_dev	 = pPciDev;
5284
5285
     /*
5286
      * Check if we are in bypass(transparent) or firmware mode.
5287
      */
5288
     pci_read_config_byte(pPciDev, 0x50, &mode);
5289
     if (mode & 1) { dprintk("Firmware  mode\n"); pAdap->bypass_mode = FALSE; }
5290
     else	   { dprintk("Bypass    mode\n"); pAdap->bypass_mode = TRUE;  }
5291
5292
     if (iteraid_init(pAdap, pPciDev) == 0)
5293
	{
5294
	 ite_adapters[NumAdapters++] = pAdap;
5295
	}
5296
    }
5297
5298
 /*
5299
  * Reenable interrupt after initialization. 2003/04/28
5300
  */
5301
 for (i = 0; i < NumAdapters; i++)
5302
    {
5303
     pAdap = ite_adapters[i];
5304
     for (j = 0; j < pAdap->num_channels; j++)
5305
	{
5306
	 pChan = &pAdap->IDEChannel[j];
5307
	 outb(IDE_DC_REENABLE_CONTROLLER, pChan->io_ports[IDE_CONTROL_OFFSET]);
5308
	}
5309
    }
5310
5311
 if (NumAdapters)
5312
    {
5313
     /*
5314
      * Register a virtual host.
5315
      */
5316
     ite_vhost = scsi_register(tpnt, 0);
5317
5318
     ite_vhost->io_port		= 0;
5319
     ite_vhost->n_io_port	= 0;
5320
     ite_vhost->max_channel	= 0;
5321
     ite_vhost->max_id		= MAX_DEVICES;
5322
     ite_vhost->max_lun		= 1;
5323
     scsi_set_pci_device(ite_vhost, pPciDev);
5324
5325
     /*
5326
      * Register the driver as a character device, for applications to
5327
      * acess it for ioctls. Ideally, this should go in the init_module()
5328
      * routine, but since it is hidden in the file "scsi_module.c" (
5329
      * included in the end), we define it here. First argument (major)
5330
      * to register_chrdev implies a dynamic major number allocation.
5331
      */
5332
     major = register_chrdev(0, "itedev", &itedev_fops);
5333
5334
     /*
5335
      * Register the Shutdown Notification hook in the kernel.
5336
      */
5337
5338
     /*
5339
      * Initialize ioctl semphore.
5340
      */
5341
     init_MUTEX(&mimd_entry_mtx);
5342
    }
5343
5344
 return 1;
5345
5346
} /* end iteraid_detect() */
5347
5348
/************************************************************************
5349
 * Name:	iteraid_release
5350
 * Description:	Release resources allocated for a single each adapter.
5351
 * Parameters:	pshost - Pointer to SCSI command structure.
5352
 * Returns:	zero.
5353
 ************************************************************************/
5354
int
5355
iteraid_release(struct Scsi_Host *pshost)
5356
{
5357
5358
 int		i;
5359
 int		j;
5360
 PITE_ADAPTER	pAdap;
5361
5362
 for (i = 0; i < NumAdapters; i++)
5363
    {
5364
     pAdap = ite_adapters[i];
5365
5366
     if (pAdap->irqOwned) free_irq(pAdap->irq, pAdap);
5367
5368
     for (j = 0; j < pAdap->num_channels; j++)
5369
	{
5370
	 kfree(pAdap->IDEChannel[j].retry_timer);
5371
	}
5372
     if (pAdap->IDEChannel != NULL)	{ kfree(pAdap->IDEChannel); }
5373
     if (pAdap != NULL)			{ kfree(pAdap); }
5374
    }
5375
5376
 scsi_unregister(pshost);
5377
5378
 return 0;
5379
5380
} /* end iteraid_Release */
5381
5382
/************************************************************************
5383
 * Name:	iteraid_old_abort
5384
 * Description:	Process the Abort command from the SCSI manager.
5385
 * Parameters:	SCpnt - Pointer to SCSI command structure.
5386
 * Returns:	Always snooze(A short sleep).
5387
 ************************************************************************/
5388
int
5389
iteraid_old_abort(Scsi_Cmnd *SCpnt)
5390
{
5391
5392
 return	SCSI_ABORT_SNOOZE;
5393
5394
} /* end iteraid_old_abort */
5395
5396
/************************************************************************
5397
 * Name:	iteraid_old_reset
5398
 * Description:	Process the Reset command from the SCSI manager.
5399
 * Parameters:	SCpnt - Pointer to SCSI command structure.
5400
 *		flags - Flags about the reset command
5401
 * Returns:	No active command at this time, so this means
5402
 *		that each time we got some kind of response the
5403
 *		last time through.  Tell the mid-level code to
5404
 *		request sense information in order to decide what
5405
 *		to do next.
5406
 ************************************************************************/
5407
int
5408
iteraid_old_reset(Scsi_Cmnd *SCpnt, unsigned int reset_flags)
5409
{
5410
5411
 return SCSI_RESET_SUCCESS;
5412
5413
} /* end iteraid_old_reset */
5414
5415
/************************************************************************
5416
 * Name:	iteraid_biosparam
5417
 * Description:	Process the biosparam request from the SCSI manager to
5418
 *		return C/H/S data.
5419
 * Parameters:	disk - Pointer to SCSI disk structure.
5420
 *		dev  - Major/minor number from kernel.
5421
 *		geom - Pointer to integer array to place geometry data.
5422
 * Returns:	zero.
5423
 ************************************************************************/
5424
int
5425
iteraid_biosparam(Scsi_Disk *disk, kdev_t dev, int geom[])
5426
{
5427
5428
 int		heads, sectors, cylinders;
5429
5430
 /*
5431
  * Default heads (64) & sectors (32)
5432
  * Handle extended translation size for logical drives > 1Gb
5433
  */
5434
 if (disk->capacity >= 0x200000)
5435
    {
5436
     heads   = 255;
5437
     sectors = 63;
5438
    }
5439
 else
5440
    {
5441
     heads   = 64;
5442
     sectors = 32;
5443
    }
5444
5445
 cylinders = disk->capacity / (heads * sectors);
5446
5447
 /*
5448
  * Return result
5449
  */
5450
 geom[0] = heads;
5451
 geom[1] = sectors;
5452
 geom[2] = cylinders;
5453
5454
 return 0;
5455
5456
} /* end iteraid_biosparam */
5457
5458
/************************************************************************
5459
 *
5460
 ************************************************************************/
5461
int
5462
iteraid_proc_info
5463
(
5464
char *		buffer,
5465
char **		start,
5466
off_t		offset,
5467
int		length,
5468
int		host_no,
5469
int		inout
5470
)
5471
{
5472
5473
 return 0;
5474
5475
} /* end iteraid_proc_info */
5476
5477
/************************************************************************
5478
 *
5479
 ************************************************************************/
5480
static int
5481
itedev_open(struct inode *inode,  struct file *filep)
5482
{
5483
5484
 MOD_INC_USE_COUNT;
5485
5486
 return 0;
5487
5488
} /* end itedev_open */
5489
5490
/************************************************************************
5491
 * IOCTL code entry.
5492
 ************************************************************************/
5493
static int
5494
itedev_ioctl_entry
5495
(
5496
 struct inode *	inode,
5497
 struct	file *	filep,
5498
 unsigned int	cmd,
5499
 unsigned long	arg
5500
)
5501
{
5502
5503
 int		ret = -1;
5504
5505
 /*
5506
  * We do not allow parallel ioctls to the driver as of now.
5507
  */
5508
 down(&mimd_entry_mtx);
5509
 ret = itedev_ioctl(inode, filep, cmd, arg);
5510
 up(&mimd_entry_mtx);
5511
5512
 return ret;
5513
5514
} /* end itedev_ioctl_entry */
5515
5516
/************************************************************************
5517
 * Real IOCTL code.
5518
 ************************************************************************/
5519
static int
5520
itedev_ioctl
5521
(
5522
 struct inode *	inode,
5523
 struct	file *	filep,
5524
 unsigned int	cmd,
5525
 unsigned long	arg
5526
)
5527
{
5528
5529
 UCHAR		diskArrayId;
5530
 UCHAR		statusByte = 0;
5531
 UCHAR		srbStatus;
5532
 UCHAR		progress;
5533
 UCHAR		status;
5534
 uioctl_t *	pioc;
5535
 PITE_ADAPTER	pAdap;
5536
 PChannel	pChan;
5537
5538
 printk("itedev_ioctl enter\n");
5539
5540
 /*
5541
  * Extract the type and number bitfield.
5542
  */
5543
 if (_IOC_TYPE(cmd) != ITE_IOCMAGIC)
5544
	 return -EINVAL;
5545
5546
 /*
5547
  * Allocate space for ioctl data structure.
5548
  */
5549
 if ((pioc = kmalloc(sizeof(uioctl_t), GFP_KERNEL)) == NULL)
5550
    {
5551
     printk("itedev_ioctl: error kmalloc on ioctl\n");
5552
     return -ENOMEM;
5553
    }
5554
5555
 /*
5556
  * Get the user ioctl structure.
5557
  */
5558
 if (copy_from_user(pioc, (uioctl_t *)arg, sizeof(uioctl_t)))
5559
    {
5560
     kfree(pioc);
5561
     return -EFAULT;
5562
    }
5563
5564
 /*
5565
  * Check which command to do.
5566
  */
5567
 switch (cmd)
5568
    {
5569
     case ITE_IOC_GET_IDENTIFY_DATA:
5570
	 printk("ITE_IOCGIDENTIFY\n");
5571
5572
	 /*
5573
	  * Get the virtual device identify data.
5574
	  */
5575
	 status = IT8212IssueIdentify(pioc);
5576
5577
	 return 0;
5578
5579
     case ITE_IOC_GET_PHY_DISK_STATUS:
5580
	 printk("ITE_IOC_GET_PHY_DISK_STATUS\n");
5581
5582
	 /*
5583
	  * Get the physical disk status.
5584
	  */
5585
	 status = IT8212GetChipStatus(pioc);
5586
5587
	 return 0;
5588
5589
     case ITE_IOC_CREATE_DISK_ARRAY:
5590
	 printk("ITE_IOC_CREATE_DISK_ARRAY\n");
5591
5592
	 /*
5593
	  * Create disk array.
5594
	  */
5595
	 status = IT8212CreateDiskArray(pioc);
5596
5597
	 if (status != SRB_STATUS_SUCCESS) return status;
5598
5599
	 status = IT8212ErasePartition(pioc);
5600
5601
	 return 0;
5602
5603
     case ITE_IOC_REBUILD_START:
5604
	 printk("ITE_IOC_REBUILD_START\n");
5605
5606
	 /*
5607
	  * Rebuild array.
5608
	  */
5609
	 status = IT8212Rebuild(pioc);
5610
5611
	 return 0;
5612
5613
     case ITE_IOC_GET_REBUILD_STATUS:
5614
	 printk("ITE_IOC_GET_REBUILD_STATUS\n");
5615
5616
	 pAdap = ite_adapters[pioc->AdapterNo];
5617
5618
	 /*
5619
	  * Get the rebuild disk ID.
5620
	  */
5621
	 diskArrayId = pioc->DiskNo;
5622
5623
	 /*
5624
	  * Select channel.
5625
	  */
5626
	 if (diskArrayId < 2)
5627
	    {
5628
	     pChan = &pAdap->IDEChannel[0];
5629
	    }
5630
	 else
5631
	    {
5632
	     pChan = &pAdap->IDEChannel[1];
5633
	    }
5634
5635
	 /*
5636
	  * Select device.
5637
	  */
5638
	 outb(((UCHAR) (diskArrayId << 4) | 0xA0),
5639
		pChan->io_ports[IDE_SELECT_OFFSET]);
5640
5641
	 /*
5642
	  * Wait for device ready (not BUSY and not DRQ).
5643
	  */
5644
	 WaitForDeviceReady(pChan, statusByte);
5645
	 if ((statusByte & IDE_STATUS_BUSY) || (statusByte & IDE_STATUS_DRQ) ||
5646
	     (statusByte == 0))
5647
	    {
5648
	     printk("IT8212GetRebuildStatus: Disk[%d] busy. Status=0x%X\n",
5649
		     diskArrayId, statusByte);
5650
	     srbStatus = SRB_STATUS_BUSY;
5651
	     goto exit;
5652
	    }
5653
5654
	 /*
5655
	  * Disable interrupt to avoid the unexpected interrupt.
5656
	  */
5657
	 outb(IDE_DC_DISABLE_INTERRUPTS, pChan->io_ports[IDE_CONTROL_OFFSET]);
5658
5659
	 /*
5660
	  * Issue command.
5661
	  */
5662
	 outb(IDE_COMMAND_REBUILD_STATUS, pChan->io_ports[IDE_COMMAND_OFFSET]);
5663
5664
	 /*
5665
	  * Check error.
5666
	  */
5667
	 WaitForCommandComplete(pChan, statusByte);
5668
5669
	 /*
5670
	  * Reenable interrupt after command complete.
5671
	  */
5672
	 outb(IDE_DC_REENABLE_CONTROLLER, pChan->io_ports[IDE_CONTROL_OFFSET]);
5673
5674
	 if (statusByte != IDE_STATUS_IDLE)
5675
	    {
5676
	     srbStatus = SRB_STATUS_ERROR;
5677
	     goto exit;
5678
	    }
5679
5680
	 progress = inb(pChan->io_ports[IDE_NSECTOR_OFFSET]);
5681
	 srbStatus = SRB_STATUS_SUCCESS;
5682
5683
	 /*
5684
	  * Put the rebuld status to user space.
5685
	  */
5686
	 put_user(progress, (UCHAR *)arg);
5687
5688
exit:
5689
5690
	 return 0;
5691
5692
     case ITE_IOC_RESET_ADAPTER:
5693
	 printk("ITE_IOC_RESET_ADAPTER\n");
5694
5695
	 /*
5696
	  * Reset the adapter.
5697
	  */
5698
	 status = IT8212ResetAdapter();
5699
5700
	 /*
5701
	  * Return TURE or FALSE to user space.
5702
	  */
5703
	 put_user(status, (UCHAR *)arg);
5704
5705
	 return 0;
5706
5707
     case ITE_IOC_GET_DRIVER_VERSION:
5708
	 printk("ITE_IOC_GET_DRIVER_VERSION\n");
5709
5710
	 /*
5711
	  * Get the current driver version.
5712
	  */
5713
	 put_user(driver_ver, (int *)arg);
5714
5715
	 return 0;
5716
5717
     default:
5718
	 return -EINVAL;
5719
5720
    } /* end switch */
5721
5722
5723
} /* end itedev_ioctl */
5724
5725
/************************************************************************
5726
 *
5727
 ************************************************************************/
5728
static int
5729
itedev_close(struct inode *inode,  struct file *filep)
5730
{
5731
5732
#ifdef MODULE
5733
 MOD_DEC_USE_COUNT;
5734
#endif
5735
5736
 return 0;
5737
5738
} /* end itedev_close */
5739
5740
/*
5741
 * To make our driver a SCSI module, we have to include some magic at the
5742
 * end of the file. This generates the init_module and clean_module code
5743
 * nedded for a SCSI device, rather than having the author replicate it
5744
 * each time a new driver is written.
5745
 */
5746
static Scsi_Host_Template driver_template = ITERAID;
5747
#include "scsi_module.c"
(-)linux-2.4.20/drivers/scsi/iteraid.h (+1574 lines)
Line 0 Link Here
1
/*
2
 * linux/drivers/scsi/iteraid.h
3
 *
4
 * (C) Copyright 2002 ITE, inc.
5
 *
6
 * Nov 11, 2002-2003	Mark Lu	file created.
7
 *
8
 * ITE IT8212 RAID Controller device driver header for Linux
9
 */
10
11
#ifndef _ITERAID_H_
12
#define _ITERAID_H_
13
14
#include <linux/version.h>
15
#include <linux/types.h>
16
#include <linux/kdev_t.h>
17
18
#define ITE_VENDOR_ID	0x1283			/* Vendor ID (ITE)	*/
19
#define ITE_DEVICE_ID	0x8212			/* Device IF (IT8212)	*/
20
#define MAX_ADAPTERS	2			/* Max Board supported	*/
21
#define MAX_DEVICES	(MAX_ADAPTERS * 4)	/* Max Dev supported	*/
22
23
#define TRUE		1
24
#define FALSE 		0
25
26
/*
27
 * Undef macros which may conflict
28
 */
29
#undef	START_STOP
30
31
/************************************************************************
32
 * Some data types define
33
 ************************************************************************/
34
typedef long		LONG,  *PLONG;
35
typedef unsigned long	ULONG, *PULONG;
36
typedef unsigned long	DWORD, *PDWORD, BOOL;
37
typedef unsigned int	UINT,  *PUINT;
38
typedef unsigned char	BYTE,  *PBYTE;
39
typedef unsigned char	UCHAR, *PUCHAR;
40
typedef unsigned char	BOOLEAN;
41
typedef unsigned short	USHORT,*PUSHORT;
42
typedef unsigned short	WORD,  *PWORD;
43
typedef void		VOID,  *PVOID;
44
typedef unsigned char  *ADDRESS;
45
46
/************************************************************************
47
 * Debugging macro
48
 ************************************************************************/
49
#ifdef ITE_DEBUG
50
#define dprintk(msg...) printk(msg)
51
#else
52
#define dprintk(msg...) do { } while(0)
53
#endif
54
55
/************************************************************************
56
 * Raid level definitions
57
 ************************************************************************/
58
#define RAID_LEVEL_0			0
59
#define RAID_LEVEL_1			1
60
#define RAID_LEVEL_10			2
61
#define RAID_LEVEL_JBOD			3
62
#define RAID_LEVEL_NORMAL		4
63
#define RAID_LEVEL_NODISK		5
64
65
/************************************************************************
66
 * Physical disk status definitions
67
 ************************************************************************/
68
#define DISK_KEY_OFF			0
69
#define DISK_OFF_LINE			1
70
#define DISK_ON_LINE			2
71
#define DISK_REBUILDING			3
72
#define DISK_PLUGGING			4
73
#define DISK_PLUGGING_OK		5
74
75
#define MaximumLBAOf28Bit		0x10000000
76
77
#define DisableChannel			1
78
#define EnableChannel			2
79
80
#define CABLE_40_PIN			0
81
#define CABLE_80_PIN			1
82
83
#define RaidActive			0
84
#define RaidInactive			1
85
86
#define IDE_CLOCK_66			0
87
#define IDE_CLOCK_50			1
88
89
#define USE_ULTRA_DMA			0
90
#define USE_MULTIWORD_DMA		1
91
92
/************************************************************************
93
 *
94
 ************************************************************************/
95
typedef struct _PHYSICAL_DISK_STATUS
96
{
97
 UCHAR		ModelNumber[40];		/* Byte 00-39		*/
98
 ULONG		UserAddressableSectors_LOW;	/* Byte 40-43		*/
99
 ULONG		UserAddressableSectors_HIGH;	/* Byte 44-47		*/
100
 UCHAR		MultiWordDMASupport;		/* Byte 48		*/
101
 UCHAR		MultiWordDMAActive;		/* Byte 49		*/
102
 UCHAR		UltraDMASupport;		/* Byte 50		*/
103
 UCHAR		UltraDMAActive;			/* Byte 51		*/
104
 UCHAR		RaidType;			/* Byte 52		*/
105
 UCHAR		RaidNumber;			/* Byte 53		*/
106
 UCHAR		SerialNumber[20];		/* Byte 54-73		*/
107
 UCHAR		DiskStatus;			/* Byte 74		*/
108
 UCHAR		DiskOriginalLocation;		/* Byte 75		*/
109
 UCHAR		Cable80Pin;			/* Byte 76		*/
110
 UCHAR		BootableDisk;			/* Byte 77		*/
111
 UCHAR		StorageSize[8];			/* Byte 78-85		*/
112
 UCHAR		Reserved[35];			/* Byte 86-120		*/
113
 UCHAR		UpdateYear;			/* Byte 121		*/
114
 UCHAR		UpdateMonth;			/* Byte 122		*/
115
 UCHAR		UpdateDay;			/* Byte 123		*/
116
 UCHAR		FirmwareVer;			/* Byte 124		*/
117
 UCHAR		RebuildStatus;			/* Byte 125		*/
118
 UCHAR		StripeSize;			/* Byte 126		*/
119
 UCHAR		AutoRebuildEnable;		/* Byte 127		*/
120
} PHYSICAL_DISK_STATUS, *PPHYSICAL_DISK_STATUS;
121
122
/************************************************************************
123
 *
124
 ************************************************************************/
125
typedef struct _IT8212_SET_CHIP_STATUS_INFO
126
{
127
 USHORT		RaidType;			/* Word 129		*/
128
 USHORT		ContainingDisks;		/* Word 130		*/
129
 USHORT		UltraDmaTiming01;		/* Word 131		*/
130
 USHORT		UltraDmaTiming23;		/* Word 132		*/
131
 USHORT		UltraDmaTiming45;		/* Word 133		*/
132
 USHORT		UltraDmaTiming6;		/* Word 134		*/
133
 USHORT		MultiWordDmaTiming01;		/* Word 135		*/
134
 USHORT		UltraDmaTiming2;		/* Word 136		*/
135
 USHORT		PioTiming4;			/* Word 137		*/
136
 USHORT		AutoRebuildEnable;		/* Word 138		*/
137
 USHORT		IdeClkUDma01;			/* Word 139		*/
138
 USHORT		IdeClkUDma23;			/* Word 140		*/
139
 USHORT		IdeClkUDma45;			/* Word 141		*/
140
 USHORT		IdeClkUDma6;			/* Word 142		*/
141
 USHORT		IdeClkMDma01;			/* Word 143		*/
142
 USHORT		IdeClkMDma2;			/* Word 144		*/
143
 USHORT		IdeClkPio4;			/* Word 145		*/
144
 USHORT		StripeSize;			/* Word 146		*/
145
 USHORT		BootableDisk;			/* Word 147		*/
146
 USHORT		CheckHotSwapInterval;		/* Word 148		*/
147
 USHORT		TargetSourceDisk;		/* Word 149		*/
148
 USHORT		RebuildBlockSize;		/* Word 150		*/
149
 USHORT		ResetInterval1;			/* Word 151		*/
150
 USHORT		ResetInterval2;			/* Word 152		*/
151
 USHORT		RebuildRetryTimes;		/* Word 153		*/
152
 USHORT		NewlyCreated;			/* Word 154		*/
153
} IT8212_SET_CHIP_STATUS_INFO, *PIT8212_SET_CHIP_STATUS_INFO;
154
155
/************************************************************************
156
 * Serial number written to HDD (20 bytes)
157
 ************************************************************************/
158
typedef struct _RAID_SERIAL_NUMBER
159
{
160
 USHORT		Year;
161
 UCHAR		Month;
162
 UCHAR		Date;
163
 UCHAR		Day;
164
 UCHAR		Hour;
165
 UCHAR		Minute;
166
 UCHAR		Second;
167
 UCHAR		MiniSec;
168
 UCHAR		RaidType;
169
 UCHAR		ContainingDisks;
170
 UCHAR		DontCare[9];
171
} RAID_SERIAL_NUMBER, *PRAID_SERIAL_NUMBER;
172
173
/************************************************************************
174
 * Disk array create information
175
 ************************************************************************/
176
typedef struct _RAID_CREATE_INFO
177
{
178
 UCHAR			DiskArrayId;
179
 RAID_SERIAL_NUMBER	SerialNumber;
180
 UCHAR			ModelNumber[40];
181
 USHORT			RaidType;
182
 USHORT			ContainingDisks;
183
 USHORT			AutoRebuildEnable;
184
 USHORT			StripeSize;
185
 USHORT			BootableDisk;
186
 USHORT			TargetSourceDisk;
187
 UCHAR			ErasePartition;
188
 /*
189
  * Following items index definition
190
  * 0: Primary Master
191
  * 1: Secondary Master
192
  * 2: Primary Slave
193
  * 3: Secondary Slave
194
  */
195
 UCHAR			DMASupported[4];
196
 UCHAR			UDMASupported[4];
197
 ULONG			AddressableSectors[4];
198
 UCHAR			NewlyCreated;
199
} RAID_CREATE_INFO, *PRAID_CREATE_INFO;
200
201
/************************************************************************
202
 * Rebuild data structure
203
 ************************************************************************/
204
typedef struct _RAID_REBUILD_INFO
205
{
206
 UCHAR		DiskArrayId;	/* Virtual device number (0-3)		*/
207
 UCHAR		SrcDisk;	/* Source disk (0-3)			*/
208
 UCHAR		DestDisk;	/* Destination disk (0-3)		*/
209
 UCHAR		Resume;		/* 1: Resume the last time rebuild	*/
210
				/* 0: Rebuild from LBA 0		*/
211
 UCHAR		Status;		/* Indicate the status of the current	*/
212
				/* rebuild command filled by drivers	*/
213
} RAID_REBUILD_INFO, *PRAID_REBUILD_INFO;
214
215
/************************************************************************
216
 * ATA transfer modes
217
 ************************************************************************/
218
#define PIO_DEFAULT			0x00
219
#define PIO_DEFAULT_IORDY_DISABLE	0x01
220
#define PIO_FLOW_CONTROL		0x08
221
#define SINGLEWORD_DMA			0x10
222
#define MULTIWORD_DMA			0x20
223
#define ULTRA_DMA			0x40
224
225
#define ITE_DRV_SIGNATURE		"ITE RAID CONTROLLER"
226
#define ITE_DRV_BYPASS			"ITE BYPASS MODE"
227
228
/************************************************************************
229
 * Extra IDE commands supported by Accusys
230
 ************************************************************************/
231
#define IDE_COMMAND_GET_CHIP_STATUS	0xFA
232
#define IDE_COMMAND_SET_CHIP_STATUS	0xFB
233
#define IDE_COMMAND_REBUILD		0xFC
234
#define IDE_COMMAND_REBUILD_STATUS	0xFD
235
236
#define REBUILD_ERR_WRONG_ARRAY_TYPE		0x01
237
#define REBUILD_ERR_DISK_TOO_SMALL		0x02
238
#define REBUILD_ERR_SRC_DISK_LOCATION_INCORRECT	0x03
239
#define REBUILD_ERR_SRC_DISK_OFFLINE		0x04
240
#define REBUILD_ERR_DEST_DISK_OFFLINE		0x05
241
#define REBUILD_ERR_DISK_BUSY			0x10	// defined by Andy, not accusys
242
243
/************************************************************************
244
 * ATA transfer modes
245
 ************************************************************************/
246
#define PIO_DEFAULT			0x00
247
#define PIO_DEFAULT_IORDY_DISABLE	0x01
248
#define PIO_FLOW_CONTROL		0x08
249
#define SINGLEWORD_DMA			0x10
250
#define MULTIWORD_DMA			0x20
251
#define ULTRA_DMA			0x40
252
253
/************************************************************************
254
 * IDE registers offset
255
 ************************************************************************/
256
#define IDE_NR_PORTS			10
257
258
#define IDE_DATA_OFFSET			0
259
#define IDE_ERROR_OFFSET		1
260
#define IDE_NSECTOR_OFFSET		2
261
#define IDE_LOCYL_OFFSET		3
262
#define IDE_MIDCYL_OFFSET		4
263
#define IDE_HCYL_OFFSET			5
264
#define IDE_SELECT_OFFSET		6
265
#define IDE_STATUS_OFFSET		7
266
#define IDE_CONTROL_OFFSET		8
267
#define IDE_IRQ_OFFSET			9
268
269
#define	IDE_FEATURE_OFFSET		IDE_ERROR_OFFSET
270
#define IDE_COMMAND_OFFSET		IDE_STATUS_OFFSET
271
#define IDE_ALTERNATE_OFFSET		IDE_CONTROL_OFFSET
272
273
/************************************************************************
274
 * ATAPI registers offset
275
 ************************************************************************/
276
#define ATAPI_DATA_OFFSET		0
277
#define ATAPI_ERROR_OFFSET		1
278
#define ATAPI_INTREASON_OFFSET		2
279
#define ATAPI_UNUSED1_OFFSET		3
280
#define ATAPI_LCYL_OFFSET		4
281
#define ATAPI_HCYL_OFFSET		5
282
#define ATAPI_SELECT_OFFSET		6
283
#define ATAPI_STATUS_OFFSET		7
284
#define ATAPI_CONTROL_OFFSET		8
285
286
#define ATAPI_COMMAND_OFFSET		ATAPI_STATUS_OFFSET
287
#define	ATAPI_FEATURE_OFFSET		ATAPI_ERROR_OFFSET
288
289
/************************************************************************
290
 * Following structures are according to SPC-3 (by Chanel)
291
 ************************************************************************/
292
typedef struct _SCSI_MODE_SENSE6
293
{
294
 UCHAR		OperationCode;
295
 UCHAR		Reserved1 : 3;
296
 UCHAR		Dbd : 1;
297
 UCHAR		Reserved2 : 4;
298
 UCHAR		PageCode : 6;
299
 UCHAR		Pc : 2;
300
 UCHAR		SubpageCode;
301
 UCHAR		AllocationLength;
302
 UCHAR		Control;
303
} SCSI_MODE_SENSE6, *PSCSI_MODE_SENSE6;
304
305
typedef struct _SCSI_MODE_SENSE10
306
{
307
 UCHAR		OperationCode;
308
 UCHAR		Reserved1 : 3;
309
 UCHAR		Dbd : 1;
310
 UCHAR		LLBAA : 1;
311
 UCHAR		Reserved2 : 3;
312
 UCHAR		PageCode : 6;
313
 UCHAR		Pc : 2;
314
 UCHAR		SubpageCode;
315
 UCHAR		Reserved3[3];
316
 UCHAR		AllocationLengthMsb;
317
 UCHAR		AllocationLengthLsb;
318
 UCHAR		Control;
319
} SCSI_MODE_SENSE10, *PSCSI_MODE_SENSE10;
320
321
typedef struct _SCSI_MODE_SELECT6
322
{
323
 UCHAR		OperationCode;
324
 UCHAR		SPBit : 1;
325
 UCHAR		Reserved1 : 3;
326
 UCHAR		PFBit : 1;
327
 UCHAR		Reserved2 : 3;
328
 UCHAR		Reserved3[2];
329
 UCHAR		ParameterListLength;
330
 UCHAR		Control;
331
} SCSI_MODE_SELECT6, *PSCSI_MODE_SELECT6;
332
333
typedef struct _SCSI_MODE_SELECT10
334
{
335
 UCHAR		OperationCode;
336
 UCHAR		SPBit : 1;
337
 UCHAR		Reserved1 : 3;
338
 UCHAR		PFBit : 1;
339
 UCHAR		Reserved2 : 3;
340
 UCHAR		Reserved3[5];
341
 UCHAR		ParameterListLengthMsb;
342
 UCHAR		ParameterListLengthLsb;
343
 UCHAR		Control;
344
} SCSI_MODE_SELECT10, *PSCSI_MODE_SELECT10;
345
346
typedef struct _SCSI_MODE_PARAMETER_HEADER6
347
{
348
 UCHAR		ModeDataLength;
349
 UCHAR		MediumType;
350
 UCHAR		DeviceSpecificParameter;
351
 UCHAR		BlockDescriptorLength;
352
} SCSI_MODE_PARAMETER_HEADER6, *PSCSI_MODE_PARAMETER_HEADER6;
353
354
typedef struct _SCSI_MODE_PARAMETER_HEADER10
355
{
356
 UCHAR		ModeDataLengthMsb;
357
 UCHAR		ModeDataLengthLsb;
358
 UCHAR		MediumType;
359
 UCHAR		DeviceSpecificParameter;
360
 UCHAR		Reserved[2];
361
 UCHAR		BlockDescriptorLengthMsb;
362
 UCHAR		BlockDescriptorLengthLsb;
363
} SCSI_MODE_PARAMETER_HEADER10, *PSCSI_MODE_PARAMETER_HEADER10;
364
365
typedef struct _SCSI_MODE_PARAMTER_BLOCK_DESCRIPTER
366
{
367
 UCHAR		DesityCode;
368
 UCHAR		NumberOfBlocks2;
369
 UCHAR		NumberOfBlocks1;
370
 UCHAR		NumberOfBlocks0;
371
 UCHAR		Reserved;
372
 UCHAR		BlockLength2;
373
 UCHAR		BlockLength1;
374
 UCHAR		BlockLength0;
375
} SCSI_MODE_PARAMTER_BLOCK_DESCRIPTER, *PSCSI_MODE_PARAMTER_BLOCK_DESCRIPTER;
376
377
/************************************************************************
378
 * IDE command definitions
379
 ************************************************************************/
380
#define IDE_COMMAND_ATAPI_RESET			0x08
381
#define IDE_COMMAND_RECALIBRATE			0x10
382
#define IDE_COMMAND_READ_SECTOR			0x20
383
#define IDE_COMMAND_READ_SECTOR_EXT		0x24
384
#define IDE_COMMAND_READ_DMA_EXT		0x25
385
#define IDE_COMMAND_READ_MULTIPLE_EXT		0x29
386
#define IDE_COMMAND_WRITE_SECTOR		0x30
387
#define IDE_COMMAND_WRITE_SECTOR_EXT		0x34
388
#define IDE_COMMAND_WRITE_DMA_EXT		0x35
389
#define IDE_COMMAND_WRITE_MULTIPLE_EXT		0x39
390
#define IDE_COMMAND_READ_VERIFY			0x40
391
#define IDE_COMMAND_READ_VERIFY_EXT		0x42
392
#define IDE_COMMAND_SEEK			0x70
393
#define IDE_COMMAND_SET_DRIVE_PARAMETERS	0x91
394
#define IDE_COMMAND_ATAPI_PACKET		0xA0
395
#define IDE_COMMAND_ATAPI_IDENTIFY		0xA1
396
#define IDE_COMMAND_READ_MULTIPLE		0xC4
397
#define IDE_COMMAND_WRITE_MULTIPLE		0xC5
398
#define IDE_COMMAND_SET_MULTIPLE		0xC6
399
#define IDE_COMMAND_READ_DMA			0xC8
400
#define IDE_COMMAND_WRITE_DMA			0xCA
401
#define IDE_COMMAND_GET_MEDIA_STATUS		0xDA
402
#define IDE_COMMAND_ENABLE_MEDIA_STATUS		0xEF
403
#define IDE_COMMAND_SET_FEATURE			0xEF
404
#define IDE_COMMAND_IDENTIFY			0xEC
405
#define IDE_COMMAND_MEDIA_EJECT			0xED
406
407
/************************************************************************
408
 * IDE status definitions
409
 ************************************************************************/
410
#define IDE_STATUS_ERROR			0x01
411
#define IDE_STATUS_INDEX			0x02
412
#define IDE_STATUS_CORRECTED_ERROR		0x04
413
#define IDE_STATUS_DRQ				0x08
414
#define IDE_STATUS_DSC				0x10
415
#define IDE_STATUS_DRDY				0x40
416
#define IDE_STATUS_IDLE				0x50
417
#define IDE_STATUS_BUSY				0x80
418
419
/************************************************************************
420
 * IDE drive control definitions
421
 ************************************************************************/
422
#define IDE_DC_DISABLE_INTERRUPTS		0x02
423
#define IDE_DC_RESET_CONTROLLER			0x04
424
#define IDE_DC_REENABLE_CONTROLLER		0x00
425
426
/************************************************************************
427
 * IDE error definitions
428
 ************************************************************************/
429
#define IDE_ERROR_BAD_BLOCK			0x80
430
#define IDE_ERROR_DATA_ERROR			0x40
431
#define IDE_ERROR_MEDIA_CHANGE			0x20
432
#define IDE_ERROR_ID_NOT_FOUND			0x10
433
#define IDE_ERROR_MEDIA_CHANGE_REQ		0x08
434
#define IDE_ERROR_COMMAND_ABORTED		0x04
435
#define IDE_ERROR_END_OF_MEDIA			0x02
436
#define IDE_ERROR_ILLEGAL_LENGTH		0x01
437
438
/************************************************************************
439
 * IDENTIFY data
440
 ************************************************************************/
441
typedef struct _IDENTIFY_DATA
442
{
443
 USHORT		GeneralConfiguration;		// 00 00
444
 USHORT		NumberOfCylinders;		// 02  1
445
 USHORT		Reserved1;			// 04  2
446
 USHORT		NumberOfHeads;			// 06  3
447
 USHORT		UnformattedBytesPerTrack;	// 08  4
448
 USHORT		UnformattedBytesPerSector;	// 0A  5
449
 USHORT		SectorsPerTrack;		// 0C  6
450
 USHORT		VendorUnique1[3];		// 0E  7-9
451
 USHORT		SerialNumber[10];		// 14  10-19
452
 USHORT		BufferType;			// 28  20
453
 USHORT		BufferSectorSize;		// 2A  21
454
 USHORT		NumberOfEccBytes;		// 2C  22
455
 USHORT		FirmwareRevision[4];		// 2E  23-26
456
 USHORT		ModelNumber[20];		// 36  27-46
457
 UCHAR		MaximumBlockTransfer;		// 5E  47
458
 UCHAR		VendorUnique2;			// 5F
459
 USHORT		DoubleWordIo;			// 60  48
460
 USHORT		Capabilities;			// 62  49
461
 USHORT		Reserved2;			// 64  50
462
 UCHAR		VendorUnique3;			// 66  51
463
 UCHAR		PioCycleTimingMode;		// 67
464
 UCHAR		VendorUnique4;			// 68  52
465
 UCHAR		DmaCycleTimingMode;		// 69
466
 USHORT		TranslationFieldsValid : 1;	// 6A  53
467
 USHORT		Reserved3 : 15;			//
468
 USHORT		NumberOfCurrentCylinders;	// 6C  54
469
 USHORT		NumberOfCurrentHeads;		// 6E  55
470
 USHORT		CurrentSectorsPerTrack;		// 70  56
471
 ULONG		CurrentSectorCapacity;		// 72  57-58
472
 USHORT		CurrentMultiSectorSetting;	//     59
473
 ULONG		UserAddressableSectors;		//     60-61
474
 USHORT		SingleWordDMASupport : 8;	//     62
475
 USHORT		SingleWordDMAActive : 8;	//
476
 USHORT		MultiWordDMASupport : 8;	//     63
477
 USHORT		MultiWordDMAActive : 8;		//
478
 USHORT		AdvancedPIOModes : 8;		//     64
479
 USHORT		Reserved4 : 8;			//
480
 USHORT		MinimumMWXferCycleTime;		//     65
481
 USHORT		RecommendedMWXferCycleTime;	//     66
482
 USHORT		MinimumPIOCycleTime;		//     67
483
 USHORT		MinimumPIOCycleTimeIORDY;	//     68
484
 USHORT		Reserved5[2];			//     69-70
485
 USHORT		ReleaseTimeOverlapped;		//     71
486
 USHORT		ReleaseTimeServiceCommand;	//     72
487
 USHORT		MajorRevision;			//     73
488
 USHORT		MinorRevision;			//     74
489
 USHORT		Reserved6[50];			//     75-126
490
 USHORT		SpecialFunctionsEnabled;	//     127
491
 USHORT		Reserved7[128];			//     128-255
492
} IDENTIFY_DATA, *PIDENTIFY_DATA;
493
494
/************************************************************************
495
 * Identify data without the Reserved4
496
 ************************************************************************/
497
typedef struct _IDENTIFY_DATA2
498
{
499
 USHORT		GeneralConfiguration;		// 00
500
 USHORT		NumberOfCylinders;		// 01
501
 USHORT		Reserved1;			// 02
502
 USHORT		NumberOfHeads;			// 03
503
 USHORT		Reserved2[2];			// 04-05
504
 USHORT		SectorsPerTrack;		// 06
505
 USHORT		Reserved3[3];			// 07-09
506
 USHORT		SerialNumber[10];		// 10-19
507
 USHORT		Reserved4[3];			// 20-22
508
 USHORT		FirmwareRevision[4];		// 23-26
509
 USHORT		ModelNumber[20];		// 27-46
510
 USHORT		MaximumBlockTransfer;		// 47
511
 USHORT		Reserved5;			// 48
512
 USHORT		Capabilities[2];		// 49-50
513
 USHORT		Reserved6[2];			// 51-52
514
 USHORT		ValidFieldIndicator;		// 53
515
 USHORT		NumberOfCurrentCylinders;	// 54
516
 USHORT		NumberOfCurrentHeads;		// 55
517
 USHORT		CurrentSectorsPerTrack;		// 56
518
 USHORT		CurrentSectorCapacityLow;	// 57
519
 USHORT		CurrentSectorCapacityHigh;	// 58
520
 USHORT		CurrentMultiSectorSetting;	// 59
521
 ULONG		UserAddressableSectors;		// 60-61
522
 USHORT		Reserved7;			// 62
523
 UCHAR		MultiWordDMASupport;		// 63
524
 UCHAR		MultiWordDMAActive;		//
525
 USHORT		AdvancedPIOModes;		// 64
526
 USHORT		MinimumMWXferCycleTime;		// 65
527
 USHORT		RecommendedMWXferCycleTime;	// 66
528
 USHORT		MinimumPIOCycleTime;		// 67
529
 USHORT		MinimumPIOCycleTimeIORDY;	// 68
530
 USHORT		Reserved8[6];			// 69-74
531
 USHORT		QueueDepth;			// 75
532
 USHORT		Reserved9[4];			// 76-79
533
 USHORT		MajorVersionNumber;		// 80
534
 USHORT		MinorVersionNumber;		// 81
535
 ULONG		CmdSetSupported;		// 82-83
536
 USHORT		CmdSetFeatureSupportedExt;	// 84
537
 USHORT		CmdSetFeatureEnabledLow;	// 85
538
 USHORT		CmdSetFeatureEnabledHigh;	// 86
539
 USHORT		CmdSetFeatureDefault;		// 87
540
 UCHAR		UltraDMASupport;		// 88
541
 UCHAR		UltraDMAActive;			//
542
 USHORT		SecurityEraseTime;		// 89
543
 USHORT		EnhancedSecurityEraseTime;	// 90
544
 USHORT		PowerManagementValue;		// 91
545
 USHORT		MasterPasswordRevision;		// 92
546
 USHORT		HwResetResult;			// 93
547
 USHORT		Reserved11[6];			// 94-99
548
 ULONG		Capacity_48bit_LOW;		// 100-101
549
 ULONG		Capacity_48bit_HIGH;		// 102-103
550
 USHORT		Reserved12[24];			// 104-127
551
 USHORT		SecurityStatus;			// 128
552
 USHORT		Reserved13[31];			// 129-159 vendor specific
553
 USHORT		Reserved14[96];			// 160-255
554
} IDENTIFY_DATA2, *PIDENTIFY_DATA2;
555
556
#define IDENTIFY_DATA_SIZE sizeof(IDENTIFY_DATA)
557
558
/************************************************************************
559
 * IDENTIFY capability bit definitions.
560
 ************************************************************************/
561
#define IDENTIFY_CAPABILITIES_DMA_SUPPORTED	0x0100
562
#define IDENTIFY_CAPABILITIES_LBA_SUPPORTED	0x0200
563
564
/************************************************************************
565
 * IDENTIFY DMA timing cycle modes.
566
 ************************************************************************/
567
#define IDENTIFY_DMA_CYCLES_MODE_0		0x00
568
#define IDENTIFY_DMA_CYCLES_MODE_1		0x01
569
#define IDENTIFY_DMA_CYCLES_MODE_2		0x02
570
571
typedef struct _SENSE_DATA
572
{
573
 UCHAR		ErrorCode:7;
574
 UCHAR		Valid:1;
575
 UCHAR		SegmentNumber;
576
 UCHAR		SenseKey:4;
577
 UCHAR		Reserved:1;
578
 UCHAR		IncorrectLength:1;
579
 UCHAR		EndOfMedia:1;
580
 UCHAR		FileMark:1;
581
 UCHAR		Information[4];
582
 UCHAR		AdditionalSenseLength;
583
 UCHAR		CommandSpecificInformation[4];
584
 UCHAR		AdditionalSenseCode;
585
 UCHAR		AdditionalSenseCodeQualifier;
586
 UCHAR		FieldReplaceableUnitCode;
587
 UCHAR		SenseKeySpecific[3];
588
} SENSE_DATA, *PSENSE_DATA;
589
590
/************************************************************************
591
 * Sense codes
592
 ************************************************************************/
593
#define SCSI_SENSE_NO_SENSE		0x00
594
#define SCSI_SENSE_RECOVERED_ERROR	0x01
595
#define SCSI_SENSE_NOT_READY		0x02
596
#define SCSI_SENSE_MEDIUM_ERROR		0x03
597
#define SCSI_SENSE_HARDWARE_ERROR	0x04
598
#define SCSI_SENSE_ILLEGAL_REQUEST	0x05
599
#define SCSI_SENSE_UNIT_ATTENTION	0x06
600
#define SCSI_SENSE_DATA_PROTECT		0x07
601
#define SCSI_SENSE_BLANK_CHECK		0x08
602
#define SCSI_SENSE_UNIQUE		0x09
603
#define SCSI_SENSE_COPY_ABORTED		0x0A
604
#define SCSI_SENSE_ABORTED_COMMAND	0x0B
605
#define SCSI_SENSE_EQUAL		0x0C
606
#define SCSI_SENSE_VOL_OVERFLOW		0x0D
607
#define SCSI_SENSE_MISCOMPARE		0x0E
608
#define SCSI_SENSE_RESERVED		0x0F
609
610
/************************************************************************
611
 * Additional Sense codes
612
 ************************************************************************/
613
#define SCSI_ADSENSE_NO_SENSE		0x00
614
#define SCSI_ADSENSE_MAN_INTERV		0x03
615
#define SCSI_ADSENSE_LUN_NOT_READY	0x04
616
#define SCSI_ADSENSE_ILLEGAL_COMMAND	0x20
617
#define SCSI_ADSENSE_ILLEGAL_BLOCK	0x21
618
#define SCSI_ADSENSE_INVALID_LUN	0x25
619
#define SCSI_ADSENSE_SELECT_TIMEOUT	0x45
620
#define SCSI_ADSENSE_MUSIC_AREA		0xA0
621
#define SCSI_ADSENSE_DATA_AREA		0xA1
622
#define SCSI_ADSENSE_VOLUME_OVERFLOW	0xA7
623
624
#define SCSI_ADSENSE_NO_MEDIA_IN_DEVICE	0x3A
625
#define SCSI_ADWRITE_PROTECT		0x27
626
#define SCSI_ADSENSE_MEDIUM_CHANGED	0x28
627
#define SCSI_ADSENSE_BUS_RESET		0x29
628
#define SCSI_ADSENSE_TRACK_ERROR	0x14
629
#define SCSI_ADSENSE_SEEK_ERROR		0x15
630
#define SCSI_ADSENSE_REC_DATA_NOECC	0x17
631
#define SCSI_ADSENSE_REC_DATA_ECC	0x18
632
#define SCSI_ADSENSE_ILLEGAL_MODE	0x64
633
#define SCSI_ADSENSE_BAD_CDB		0x24
634
#define SCSI_ADSENSE_BAD_PARM_LIST	0x26
635
#define SCSI_ADSENSE_CANNOT_READ_MEDIUM	0x30
636
637
#define SCSISTAT_CHECK_CONDITION	0x02
638
639
/************************************************************************
640
 * Inquiry buffer structure. This is the data returned from the target
641
 * after it receives an inquiry.
642
 *
643
 * This structure may be extended by the number of bytes specified
644
 * in the field AdditionalLength. The defined size constant only
645
 * includes fields through ProductRevisionLevel.
646
 *
647
 * The NT SCSI drivers are only interested in the first 36 bytes of data.
648
 ************************************************************************/
649
650
#define INQUIRYDATABUFFERSIZE 36
651
652
typedef struct _INQUIRYDATA
653
{
654
 UCHAR		DeviceType : 5;
655
 UCHAR		DeviceTypeQualifier : 3;
656
 UCHAR		DeviceTypeModifier : 7;
657
 UCHAR		RemovableMedia : 1;
658
 UCHAR		Versions;
659
 UCHAR		ResponseDataFormat;
660
 UCHAR		AdditionalLength;
661
 UCHAR		Reserved[2];
662
 UCHAR		SoftReset : 1;
663
 UCHAR		CommandQueue : 1;
664
 UCHAR		Reserved2 : 1;
665
 UCHAR		LinkedCommands : 1;
666
 UCHAR		Synchronous : 1;
667
 UCHAR		Wide16Bit : 1;
668
 UCHAR		Wide32Bit : 1;
669
 UCHAR		RelativeAddressing : 1;
670
 UCHAR		VendorId[8];
671
 UCHAR		ProductId[16];
672
 UCHAR		ProductRevisionLevel[4];
673
 UCHAR		VendorSpecific[20];
674
 UCHAR		Reserved3[40];
675
} INQUIRYDATA, *PINQUIRYDATA;
676
677
#define DIRECT_ACCESS_DEVICE		0x00	/* Disks		*/
678
679
/************************************************************************
680
 * Read Capacity Data - returned in Big Endian format
681
 ************************************************************************/
682
typedef struct _READ_CAPACITY_DATA
683
{
684
 ULONG		LogicalBlockAddress;
685
 ULONG		BytesPerBlock;
686
} READ_CAPACITY_DATA, *PREAD_CAPACITY_DATA;
687
688
#define MAXIMUM_CDB_SIZE	12
689
690
/************************************************************************
691
 * CDB (Command Descriptor Block)
692
 ************************************************************************/
693
typedef union _CDB
694
{
695
 /*
696
  * Standard 6-byte CDB
697
  */
698
 struct _CDB6READWRITE
699
    {
700
     UCHAR	OperationCode;
701
     UCHAR	LogicalBlockMsb1 : 5;
702
     UCHAR	LogicalUnitNumber : 3;
703
     UCHAR	LogicalBlockMsb0;
704
     UCHAR	LogicalBlockLsb;
705
     UCHAR	TransferBlocks;
706
     UCHAR	Control;
707
    } CDB6READWRITE, *PCDB6READWRITE;
708
709
 /*
710
  * Standard 10-byte CDB
711
  */
712
 struct _CDB10
713
    {
714
     UCHAR	OperationCode;
715
     UCHAR	Reserved1 : 5;
716
     UCHAR	LogicalUnitNumber : 3;
717
     UCHAR	LogicalBlockByte0;
718
     UCHAR	LogicalBlockByte1;
719
     UCHAR	LogicalBlockByte2;
720
     UCHAR	LogicalBlockByte3;
721
     UCHAR	Reserved2;
722
     UCHAR	TransferBlocksMsb;
723
     UCHAR	TransferBlocksLsb;
724
     UCHAR	Control;
725
    } CDB10, *PCDB10;
726
727
 struct _START_STOP
728
    {
729
     UCHAR	OperationCode;
730
     UCHAR	Immediate: 1;
731
     UCHAR	Reserved1 : 4;
732
     UCHAR	LogicalUnitNumber : 3;
733
     UCHAR	Reserved2[2];
734
     UCHAR	Start : 1;
735
     UCHAR	LoadEject : 1;
736
     UCHAR	Reserved3 : 6;
737
     UCHAR	Control;
738
    } START_STOP, *PSTART_STOP;
739
740
} CDB, *PCDB;
741
742
/************************************************************************
743
 * SCSI CDB operation codes
744
 ************************************************************************/
745
#define SCSIOP_TEST_UNIT_READY		0x00
746
#define SCSIOP_REZERO_UNIT		0x01
747
#define SCSIOP_REWIND			0x01
748
#define SCSIOP_REQUEST_BLOCK_ADDR	0x02
749
#define SCSIOP_REQUEST_SENSE		0x03
750
#define SCSIOP_FORMAT_UNIT		0x04
751
#define SCSIOP_READ_BLOCK_LIMITS	0x05
752
#define SCSIOP_REASSIGN_BLOCKS		0x07
753
#define SCSIOP_READ6			0x08
754
#define SCSIOP_RECEIVE			0x08
755
#define SCSIOP_WRITE6			0x0A
756
#define SCSIOP_PRINT			0x0A
757
#define SCSIOP_SEND			0x0A
758
#define SCSIOP_SEEK6			0x0B
759
#define SCSIOP_TRACK_SELECT		0x0B
760
#define SCSIOP_SLEW_PRINT		0x0B
761
#define SCSIOP_SEEK_BLOCK		0x0C
762
#define SCSIOP_PARTITION		0x0D
763
#define SCSIOP_READ_REVERSE		0x0F
764
#define SCSIOP_WRITE_FILEMARKS		0x10
765
#define SCSIOP_FLUSH_BUFFER		0x10
766
#define SCSIOP_SPACE			0x11
767
#define SCSIOP_INQUIRY			0x12
768
#define SCSIOP_VERIFY6			0x13
769
#define SCSIOP_RECOVER_BUF_DATA		0x14
770
#define SCSIOP_MODE_SELECT		0x15
771
#define SCSIOP_RESERVE_UNIT		0x16
772
#define SCSIOP_RELEASE_UNIT		0x17
773
#define SCSIOP_COPY			0x18
774
#define SCSIOP_ERASE			0x19
775
#define SCSIOP_MODE_SENSE		0x1A
776
#define SCSIOP_START_STOP_UNIT		0x1B
777
#define SCSIOP_STOP_PRINT		0x1B
778
#define SCSIOP_LOAD_UNLOAD		0x1B
779
#define SCSIOP_RECEIVE_DIAGNOSTIC	0x1C
780
#define SCSIOP_SEND_DIAGNOSTIC		0x1D
781
#define SCSIOP_MEDIUM_REMOVAL		0x1E
782
#define SCSIOP_READ_CAPACITY		0x25
783
#define SCSIOP_READ			0x28
784
#define SCSIOP_WRITE			0x2A
785
#define SCSIOP_SEEK			0x2B
786
#define SCSIOP_LOCATE			0x2B
787
#define SCSIOP_WRITE_VERIFY		0x2E
788
#define SCSIOP_VERIFY			0x2F
789
#define SCSIOP_SEARCH_DATA_HIGH		0x30
790
#define SCSIOP_SEARCH_DATA_EQUAL	0x31
791
#define SCSIOP_SEARCH_DATA_LOW		0x32
792
#define SCSIOP_SET_LIMITS		0x33
793
#define SCSIOP_READ_POSITION		0x34
794
#define SCSIOP_SYNCHRONIZE_CACHE	0x35
795
#define SCSIOP_COMPARE			0x39
796
#define SCSIOP_COPY_COMPARE		0x3A
797
#define SCSIOP_WRITE_DATA_BUFF		0x3B
798
#define SCSIOP_READ_DATA_BUFF		0x3C
799
#define SCSIOP_CHANGE_DEFINITION	0x40
800
#define SCSIOP_READ_SUB_CHANNEL		0x42
801
#define SCSIOP_READ_TOC			0x43
802
#define SCSIOP_READ_HEADER		0x44
803
#define SCSIOP_PLAY_AUDIO		0x45
804
#define SCSIOP_PLAY_AUDIO_MSF		0x47
805
#define SCSIOP_PLAY_TRACK_INDEX		0x48
806
#define SCSIOP_PLAY_TRACK_RELATIVE	0x49
807
#define SCSIOP_PAUSE_RESUME		0x4B
808
#define SCSIOP_LOG_SELECT		0x4C
809
#define SCSIOP_LOG_SENSE		0x4D
810
#define SCSIOP_MODE_SELECT10		0x55
811
#define SCSIOP_MODE_SENSE10		0x5A
812
#define SCSIOP_LOAD_UNLOAD_SLOT		0xA6
813
#define SCSIOP_MECHANISM_STATUS		0xBD
814
#define SCSIOP_READ_CD			0xBE
815
816
#define DRIVER_NAME		"Device Driver for IT8212 RAID Controller"
817
#define COMPANY_NAME		"Integrated Technology Express, Inc."
818
#define CONTROLLER_NAME_IT8212	"IT8212 UDMA/ATA133 RAID Controller"
819
#define PROC_DIR_NAME		"it8212"
820
#define ITE_MAX_CMDS		124
821
822
#define PCI_IOSEN	0x01	/* Enable IO space			*/
823
#define PCI_BMEN	0x04	/* Enable IDE bus master		*/
824
825
/************************************************************************
826
 * PRD (Physical Region Descriptor) = Scatter-gather table
827
 *
828
 * |  byte3   |	 byte2	 |  byte1   |   byte0   |
829
 * +--------------------------------------------+
830
 * | Memory Region Physical Base Address[31:1]  |
831
 * +----+----------------+----------------------+
832
 * |EOT |  reserved      |   Byte count[15:1]   |
833
 * +----+----------------+----------------------+
834
 ************************************************************************/
835
typedef struct _PRD_TABLE_ENTRY
836
{
837
 ULONG		PhysicalBaseAddress;		/* Byte0 - Byte3	*/
838
 USHORT		ByteCount;			/* Byte4 - Byte5	*/
839
 USHORT		EndOfTable;			/* Byte6 - Byte7	*/
840
} PRD_TABLE_ENTRY, *PPRD_TABLE_ENTRY;
841
842
#define SG_FLAG_EOT			0x8000	/* End of PRD		*/
843
#define MAX_SG_DESCRIPTORS		17	/* 17 -- maximum 64K	*/
844
845
#define NUM_OF_PRD_TABLE_ENTRY		0x10
846
847
/************************************************************************
848
 * Bus master register bits definition
849
 ************************************************************************/
850
#define BM_CMD_FLG_START		0x01
851
#define BM_CMD_FLG_WRTTOMEM		0x08
852
#define BM_CMD_FLG_WRTTODSK		0x00
853
854
#define BM_STAT_FLG_ACTIVE		0x01
855
#define BM_STAT_FLG_ERR			0x02
856
#define BM_STAT_FLG_INT			0x04
857
#define BM_DRV0_DMA_CAPABLE		0x20
858
#define BM_DRV1_DMA_CAPABLE		0x40
859
860
#define BM_PRD_FLG_EOT  		0x8000
861
862
/************************************************************************
863
 * SRB Functions
864
 ************************************************************************/
865
#define SRB_FUNCTION_EXECUTE_SCSI	0x00
866
#define SRB_FUNCTION_IO_CONTROL		0x02
867
#define SRB_FUNCTION_SHUTDOWN		0x07
868
#define SRB_FUNCTION_FLUSH		0x08
869
870
/************************************************************************
871
 * SRB Status
872
 ************************************************************************/
873
#define SRB_STATUS_PENDING		0x00
874
#define SRB_STATUS_SUCCESS		0x01
875
#define SRB_STATUS_ABORTED		0x02
876
#define SRB_STATUS_ABORT_FAILED		0x03
877
#define SRB_STATUS_ERROR		0x04
878
#define SRB_STATUS_BUSY			0x05
879
#define SRB_STATUS_INVALID_REQUEST	0x06
880
#define SRB_STATUS_INVALID_PATH_ID	0x07
881
#define SRB_STATUS_NO_DEVICE		0x08
882
#define SRB_STATUS_TIMEOUT		0x09
883
#define SRB_STATUS_SELECTION_TIMEOUT	0x0A
884
#define SRB_STATUS_COMMAND_TIMEOUT	0x0B
885
#define SRB_STATUS_MESSAGE_REJECTED	0x0D
886
#define SRB_STATUS_BUS_RESET		0x0E
887
#define SRB_STATUS_PARITY_ERROR		0x0F
888
#define SRB_STATUS_REQUEST_SENSE_FAILED	0x10
889
#define SRB_STATUS_NO_HBA		0x11
890
#define SRB_STATUS_DATA_OVERRUN		0x12
891
#define SRB_STATUS_UNEXPECTED_BUS_FREE	0x13
892
#define SRB_STATUS_BAD_SRB_BLOCK_LENGTH	0x15
893
#define SRB_STATUS_REQUEST_FLUSHED	0x16
894
#define SRB_STATUS_INVALID_LUN		0x20
895
#define SRB_STATUS_INVALID_TARGET_ID	0x21
896
#define SRB_STATUS_BAD_FUNCTION		0x22
897
#define SRB_STATUS_ERROR_RECOVERY	0x23
898
#define SRB_STATUS_NEED_REQUEUE		0x24
899
900
/************************************************************************
901
 * SRB Status Masks
902
 ************************************************************************/
903
#define SRB_STATUS_QUEUE_FROZEN		0x40
904
#define SRB_STATUS_AUTOSENSE_VALID	0x80
905
906
#define SRB_STATUS(Status)	\
907
	(Status & ~(SRB_STATUS_AUTOSENSE_VALID | SRB_STATUS_QUEUE_FROZEN))
908
909
/************************************************************************
910
 * SRB Flag Bits
911
 ************************************************************************/
912
#define SRB_FLAGS_DATA_IN		0x00000040
913
#define SRB_FLAGS_DATA_OUT		0x00000080
914
915
/************************************************************************
916
 * SRB Working flags define area
917
 ************************************************************************/
918
#define	SRB_WFLAGS_USE_INTERNAL_BUFFER	0x00000001
919
#define	SRB_WFLAGS_IGNORE_ARRAY		0x00000002
920
#define	SRB_WFLAGS_HAS_CALL_BACK	0x00000004
921
#define	SRB_WFLAGS_MUST_DONE		0x00000008
922
#define	SRB_WFLAGS_ON_MIRROR_DISK	0x00000010
923
#define	SRB_WFLAGS_ON_SOURCE_DISK	0x00000020
924
#define SRB_WFLAGS_ARRAY_IO_STARTED	0x10000000
925
#define SRB_WFLAGS_WATCHTIMER_CALLED	0x20000000
926
#define SRB_WFLAGS_USE_SG		0x40000000
927
928
/************************************************************************
929
 * SCSI I/O Request Block
930
 ************************************************************************/
931
typedef struct _SCSI_REQUEST_BLOCK
932
{
933
 USHORT			Length;
934
 UCHAR			Function;
935
 UCHAR			SrbStatus;
936
 UCHAR			ScsiStatus;
937
 UCHAR			TargetId;
938
 UCHAR			Lun;
939
 UCHAR			CdbLength;
940
 UCHAR			SenseInfoBufferLength;
941
 UCHAR			UseSg;
942
 UCHAR			reseved[2];
943
 ULONG			WorkingFlags;
944
 ULONG			SrbFlags;
945
 ULONG			DataTransferLength;
946
 ULONG			TimeOutValue;
947
 PVOID			DataBuffer;
948
 PVOID			SenseInfoBuffer;
949
 UCHAR			Cdb[16];
950
 Scsi_Cmnd *		pREQ;
951
} SCSI_REQUEST_BLOCK, *PSCSI_REQUEST_BLOCK;
952
953
#define SCSI_REQUEST_BLOCK_SIZE sizeof(SCSI_REQUEST_BLOCK)
954
955
/************************************************************************
956
 * Second device flags
957
 ***********************************************************************/
958
#define DFLAGS_REDUCE_MODE	        0x00010000
959
#define DFLAGS_DEVICE_DISABLED		0x00020000
960
#define DFLAGS_BOOTABLE_DEVICE		0x00080000
961
#define DFLAGS_BOOT_MARK		0x00100000
962
#define DFLAGS_NEW_ADDED		0x40000000
963
#define DFLAGS_REMAINED_MEMBER		0x80000000
964
965
/************************************************************************
966
 * Device Extension Device Flags
967
 ************************************************************************/
968
/*
969
 * Indicates that some device is present.
970
 */
971
#define DFLAGS_DEVICE_PRESENT		0x0001
972
973
/*
974
 * Indicates whether ATAPI commands can be used.
975
 */
976
#define DFLAGS_ATAPI_DEVICE		0x0002
977
978
/*
979
 * Indicates whether this is a tape device.
980
 */
981
#define DFLAGS_TAPE_DEVICE		0x0004
982
983
/*
984
 * Indicates whether device interrupts as DRQ is set after
985
 * receiving Atapi Packet Command.
986
 */
987
#define DFLAGS_INT_DRQ			0x0008
988
989
/*
990
 * Indicates that the drive has the 'removable' bit set in
991
 * identify data (offset 128)
992
 */
993
#define DFLAGS_REMOVABLE_DRIVE		0x0010
994
995
/*
996
 * Media status notification enabled.
997
 */
998
#define DFLAGS_MEDIA_STATUS_ENABLED	0x0020
999
1000
/*
1001
 * Indicates atapi 2.5 changer present.
1002
 */
1003
#define DFLAGS_ATAPI_CHANGER		0x0040
1004
1005
/*
1006
 * Indicates multi-platter device, not conforming to the 2.5 spec.
1007
 */
1008
#define DFLAGS_SANYO_ATAPI_CHANGER	0x0080
1009
1010
/*
1011
 * Indicates that the init path for changers has already been done.
1012
 */
1013
#define DFLAGS_CHANGER_INITED		0x0100
1014
#define DFLAGS_CONFIG_CHANGED		0x0200
1015
1016
#define UDMA_MODE_5_6			0x80
1017
1018
/************************************************************************
1019
 * Used to disable 'advanced' features.
1020
 ************************************************************************/
1021
#define MAX_ERRORS			4
1022
1023
/************************************************************************
1024
 * ATAPI command definitions
1025
 ************************************************************************/
1026
#define ATAPI_MODE_SENSE		0x5A
1027
#define ATAPI_MODE_SELECT		0x55
1028
#define ATAPI_FORMAT_UNIT		0x24
1029
1030
/************************************************************************
1031
 * User IOCTL structure
1032
 * Notes:
1033
 * (1) Data transfers are limited to PAGE_SIZE (4k on i386, 8k for alpha)
1034
 ************************************************************************/
1035
typedef struct	_uioctl_t
1036
{
1037
 USHORT		inlen;		/* Length of data written to device	*/
1038
 USHORT		outlen;		/* Length of data read from device	*/
1039
 UCHAR		DiskNo;		/* The disk number			*/
1040
 UCHAR		AdapterNo;	/* The adapter number			*/
1041
 UCHAR		Status;		/* The ioctls status			*/
1042
 UCHAR		Reserved;	/* Reserved for alignment		*/
1043
 void *		data;		/* Data read from devic starts here	*/
1044
}		uioctl_t;
1045
1046
/************************************************************************
1047
 * IOCTL commands for RAID
1048
 ************************************************************************/
1049
#define ITE_IOCMAGIC			't'
1050
1051
#define ITE_IOC_GET_IDENTIFY_DATA	_IOWR(ITE_IOCMAGIC, 0, IDENTIFY_DATA)
1052
#define ITE_IOC_GET_PHY_DISK_STATUS	_IO(ITE_IOCMAGIC, 7)
1053
#define ITE_IOC_CREATE_DISK_ARRAY	_IO(ITE_IOCMAGIC, 2)
1054
#define ITE_IOC_REBUILD_START		_IO(ITE_IOCMAGIC, 3)
1055
#define ITE_IOC_GET_REBUILD_STATUS	_IO(ITE_IOCMAGIC, 4)
1056
#define ITE_IOC_RESET_ADAPTER		_IO(ITE_IOCMAGIC, 5)
1057
#define ITE_IOC_GET_DRIVER_VERSION	_IOW(ITE_IOCMAGIC, 6, int)
1058
1059
/************************************************************************
1060
 * _Channel
1061
 ************************************************************************/
1062
typedef struct _Channel
1063
{
1064
 /*
1065
  * IDE (ATAPI) io port address.
1066
  */
1067
 ULONG			io_ports[IDE_NR_PORTS];
1068
1069
 /*
1070
  * DMA base address.
1071
  */
1072
 ULONG			dma_base;
1073
1074
 /*
1075
  * Flags word for each possible device.
1076
  */
1077
 USHORT			DeviceFlags[2];
1078
1079
 /*
1080
  * Indicates number of platters on changer-ish devices.
1081
  */
1082
 ULONG			DiscsPresent[2];
1083
1084
 /*
1085
  * Indicates expecting an interrupt.
1086
  */
1087
 BOOLEAN		ExpectingInterrupt;
1088
1089
 /*
1090
  * Indicate last tape command was DSC Restrictive.
1091
  */
1092
 BOOLEAN		RDP;
1093
1094
 /*
1095
  * Interrupt level.
1096
  */
1097
 UCHAR			InterruptLevel;
1098
1099
 /*
1100
  * Placeholder for status register after a GET_MEDIA_STATUS command.
1101
  */
1102
 UCHAR			ReturningMediaStatus;
1103
1104
 /*
1105
  * Remember the channel number (0, 1)
1106
  */
1107
 UCHAR			channel;
1108
1109
 /*
1110
  * Indicates cable status.
1111
  */
1112
 UCHAR			Cable80[2];
1113
1114
 /*
1115
  * Data buffer pointer.
1116
  */
1117
 PUSHORT		DataBuffer;
1118
1119
 /*
1120
  * Data words left.
1121
  */
1122
 ULONG			WordsLeft;
1123
1124
 /*
1125
  * Retry count.
1126
  */
1127
 ULONG			RetryCount;
1128
1129
 /*
1130
  * Keep DMA type (MULTIWORD_DMA or ULTRA_DMA) for each device.
1131
  */
1132
 UCHAR			DmaType[2];
1133
1134
 /*
1135
  * Keep UDMA timing for each device.
1136
  */
1137
 UCHAR			UdmaTiming[2];
1138
1139
 /*
1140
  * Keep PIO/DMA timing for each channel. PioDmaTiming[clock][channel]
1141
  */
1142
 UCHAR			PioDmaTiming[2];
1143
1144
 /*
1145
  * Keep IDE clock (50 MHz or 66 MHz) for each device.
1146
  */
1147
 UCHAR			IdeClock[2];
1148
1149
 /*
1150
  * Keep the active device for each channel.
1151
  */
1152
 UCHAR			ActiveDevice;
1153
1154
 /*
1155
  * Indicate whether we should perform DMA mode switch on this channel?
1156
  */
1157
 UCHAR			DoSwitch;
1158
1159
 /*
1160
  * ???
1161
  */
1162
 BOOLEAN		ConvertCdb;
1163
1164
 /*
1165
  * Use or do not use DMA.
1166
  */
1167
 BOOLEAN		UseDma[2];
1168
1169
 /*
1170
  * Flags for initialization.
1171
  */
1172
 BOOLEAN		Initializing;
1173
1174
 /*
1175
  * Reserved for alignment.
1176
  */
1177
 UCHAR			reserved[3];
1178
1179
 /*
1180
  * Identify data for device.
1181
  */
1182
 IDENTIFY_DATA		FullIdentifyData;
1183
 IDENTIFY_DATA2		IdentifyData[2];
1184
1185
 /*
1186
  * DMA PRD table physical address.
1187
  */
1188
 dma_addr_t		dmatable_dma;
1189
1190
 /*
1191
  * DMA PRD table virtual address.
1192
  */
1193
 unsigned int *		dmatable_cpu;
1194
1195
 /*
1196
  * Point to SCATTER/GATHER data buffer.
1197
  */
1198
 struct scatterlist *	sg_table;
1199
1200
 /*
1201
  * How many SCATTER/GATHER counts.
1202
  */
1203
 int			sg_nents;
1204
1205
 /*
1206
  * DMA read or write.
1207
  */
1208
 int			sg_dma_direction;
1209
1210
 /*
1211
  * For retry timer.
1212
  */
1213
 struct timer_list *	retry_timer;
1214
1215
 /*
1216
  * For watchdog timer.
1217
  */
1218
 struct timer_list *	watch_timer;
1219
1220
 /*
1221
  * Current request on controller.
1222
  */
1223
 PSCSI_REQUEST_BLOCK	CurrentSrb;
1224
1225
 /*
1226
  * Original request on controller.
1227
  */
1228
 PSCSI_REQUEST_BLOCK	OriginalSrb;
1229
1230
 /*
1231
  * Internal SRB.
1232
  */
1233
 SCSI_REQUEST_BLOCK	_Srb;
1234
1235
 /*
1236
  * Remember the PCI device.
1237
  */
1238
 struct pci_dev *	pPciDev;
1239
1240
 /*
1241
  * Placeholder for CDB.
1242
  */
1243
 UCHAR	TempCdb[MAXIMUM_CDB_SIZE];
1244
} Channel, *PChannel;
1245
1246
/************************************************************************
1247
 * _Adapter
1248
 ************************************************************************/
1249
typedef struct _Adapter
1250
{
1251
 char *			name;		/* Adapter's name		*/
1252
 UCHAR			num_channels;	/* How many channels support	*/
1253
 UCHAR			irq;		/* irq number			*/
1254
 UCHAR			irqOwned;	/* If any irq is use		*/
1255
 UCHAR			pci_bus;	/* PCI bus number		*/
1256
 UCHAR			devfn;		/* Device and function number	*/
1257
 UCHAR			offline;	/* On line or off line		*/
1258
 BOOLEAN		bypass_mode;	/* bypass or firware mode	*/
1259
 UCHAR			reserved2[1];	/* Reserved for alignment	*/
1260
 Channel *		IDEChannel;	/* IT8212 supports two channels	*/
1261
 struct pci_dev *	pci_dev;	/* For PCI device		*/
1262
}
1263
ITE_ADAPTER, *PITE_ADAPTER;
1264
1265
/************************************************************************
1266
 * Function prototypes
1267
 ************************************************************************/
1268
const char *iteraid_info (struct Scsi_Host *);
1269
int iteraid_detect (Scsi_Host_Template *);
1270
int iteraid_release (struct Scsi_Host *);
1271
int iteraid_command (Scsi_Cmnd *);
1272
int iteraid_old_abort (Scsi_Cmnd *);
1273
int iteraid_old_reset (Scsi_Cmnd *, unsigned int);
1274
int iteraid_queuecommand (Scsi_Cmnd *, void (*done)(Scsi_Cmnd *));
1275
int iteraid_biosparam (Disk *, kdev_t, int *);
1276
int iteraid_proc_info (char *buffer, char **start, off_t offset,
1277
			int length, int hostno, int inout);
1278
1279
/************************************************************************
1280
 * Scsi_Host_Template Initializer
1281
 ************************************************************************/
1282
#define ITERAID								\
1283
{									\
1284
 next:				NULL,					\
1285
 module:			NULL,					\
1286
 proc_name:			"it8212",				\
1287
 proc_info:			iteraid_proc_info,			\
1288
 name:				"RAIDExpress133",			\
1289
 detect:			iteraid_detect,				\
1290
 release:			iteraid_release,			\
1291
 info:	     			NULL,					\
1292
 ioctl:				NULL,					\
1293
 command:			iteraid_command,			\
1294
 queuecommand:  		iteraid_queuecommand,			\
1295
 abort:	    			iteraid_old_abort,			\
1296
 reset:	    			iteraid_old_reset,			\
1297
 slave_attach:			NULL,					\
1298
 bios_param:			iteraid_biosparam,			\
1299
 can_queue:			124,					\
1300
 this_id:			-1,					\
1301
 sg_tablesize:   		16,					\
1302
 cmd_per_lun:			1,					\
1303
 present:			0,					\
1304
 unchecked_isa_dma:		0,					\
1305
 use_clustering:   		DISABLE_CLUSTERING,			\
1306
 use_new_eh_code:		0					\
1307
}
1308
1309
/************************************************************************
1310
 * Beautification macros
1311
 ************************************************************************/
1312
#define MAX_RETRY_COUNT		5
1313
1314
#define ScheduleRetryProcess(pChan) do {		\
1315
	pChan->retry_timer->expires = jiffies + 10;	\
1316
	add_timer(pChan->retry_timer);			\
1317
	} while (0)
1318
1319
#define CancelRetryProcess(pChan) del_timer(pChan->retry_timer)
1320
1321
#define GetStatus(pChan, Status)	\
1322
    Status = inb(pChan->io_ports[IDE_CONTROL_OFFSET]);
1323
1324
#define GetBaseStatus(pChan, Status)	\
1325
    Status = inb(pChan->io_ports[IDE_COMMAND_OFFSET]);
1326
1327
#define GetError(pChan, Error)		\
1328
    Error = inb(pChan->io_ports[IDE_ERROR_OFFSET]);
1329
1330
#define ReadBuffer(pChan, Buffer, Count) \
1331
    insw(pChan->io_ports[IDE_DATA_OFFSET], Buffer, Count);
1332
1333
#define WriteCommand(BaseIoAddress, Command) \
1334
    outb(pChan->io_ports[IDE_COMMAND_OFFSET], Command);
1335
1336
#define WriteBuffer(pChan, Buffer, Count) \
1337
    outsw(pChan->io_ports[IDE_DATA_OFFSET], Buffer, Count);
1338
1339
#define WaitOnBusy(pChan, Status)		\
1340
{						\
1341
 ULONG		i;				\
1342
 for (i = 0; i < 20000; i++)			\
1343
    {						\
1344
     GetStatus(pChan, Status);			\
1345
     if (Status & IDE_STATUS_BUSY)		\
1346
	{					\
1347
	 udelay(150);				\
1348
	 continue;				\
1349
	}					\
1350
     else 					\
1351
	{					\
1352
	 break;					\
1353
	}					\
1354
    }						\
1355
}
1356
1357
#define WaitOnBaseBusy(pChan, Status)		\
1358
{						\
1359
 ULONG		i;				\
1360
 for (i = 0; i < 20000; i++)			\
1361
    {						\
1362
     GetBaseStatus(pChan, Status);		\
1363
     if (Status & IDE_STATUS_BUSY)		\
1364
	{					\
1365
	 udelay(150);				\
1366
	 continue;				\
1367
	} 					\
1368
     else					\
1369
	{					\
1370
	 break;					\
1371
	}					\
1372
    }						\
1373
}
1374
1375
#define WaitForDrq(pChan, Status)		\
1376
{						\
1377
 ULONG		i;				\
1378
 for (i = 0; i < 1000; i++)			\
1379
    {						\
1380
     GetStatus(pChan, Status);			\
1381
     if (Status & IDE_STATUS_BUSY)		\
1382
	{					\
1383
	 udelay(100);				\
1384
	}					\
1385
     else if (Status & IDE_STATUS_DRQ)		\
1386
	{					\
1387
	 break;					\
1388
	}					\
1389
     else					\
1390
	{					\
1391
	 udelay(200);				\
1392
	}					\
1393
    }						\
1394
}
1395
1396
#define WaitForBaseDrq(pChan, Status)		\
1397
{						\
1398
 ULONG		i;				\
1399
 for (i = 0; i < 50000; i++)			\
1400
    {						\
1401
     GetBaseStatus(pChan, Status);		\
1402
     if (Status & IDE_STATUS_BUSY)		\
1403
	{					\
1404
	 udelay(100);				\
1405
	}					\
1406
     else if (Status & IDE_STATUS_DRQ)		\
1407
	{					\
1408
	 break;					\
1409
	}					\
1410
     else					\
1411
	{					\
1412
	 udelay(200);				\
1413
	}					\
1414
    }						\
1415
}
1416
1417
#define CheckBusyDrq(pChan, Status)		\
1418
{						\
1419
 ULONG		i;				\
1420
 for (i = 0; i < 50000; i++)			\
1421
    {						\
1422
     GetBaseStatus(pChan, Status);		\
1423
     if ((Status & IDE_STATUS_BUSY) ||		\
1424
	!(Status & IDE_STATUS_DRQ))		\
1425
	{					\
1426
	 udelay(200);				\
1427
	}					\
1428
     else					\
1429
	{					\
1430
	 break;					\
1431
	}					\
1432
    }						\
1433
}
1434
1435
#define WaitShortForDrq(pChan, Status)		\
1436
{						\
1437
 ULONG		i;				\
1438
 for (i = 0; i < 2; i++)			\
1439
    {						\
1440
     GetStatus(pChan, Status);			\
1441
     if (Status & IDE_STATUS_BUSY)		\
1442
	{					\
1443
	 udelay(100);				\
1444
	}					\
1445
     else if (Status & IDE_STATUS_DRQ)		\
1446
	{					\
1447
	 break;					\
1448
	}					\
1449
     else					\
1450
	{					\
1451
	 udelay(100);				\
1452
	}					\
1453
    }						\
1454
}
1455
1456
#define WaitForDeviceReady(pChan, Status)	\
1457
{						\
1458
 ULONG		i;				\
1459
 for (i = 0; i < 50000; i++)			\
1460
    {						\
1461
     GetStatus(pChan, Status);			\
1462
     if (Status == 0)				\
1463
	{					\
1464
	 break;					\
1465
	}					\
1466
     if ((Status & IDE_STATUS_BUSY) || (Status & IDE_STATUS_DRQ)) \
1467
	{					\
1468
	 udelay(200);				\
1469
	 continue;				\
1470
	}					\
1471
     else					\
1472
	{					\
1473
	 break;					\
1474
	}					\
1475
    }						\
1476
}
1477
1478
#define WaitForCommandComplete(pChan, Status)		\
1479
{							\
1480
 ULONG		i;					\
1481
 for (i = 0; i < 50000; i++)				\
1482
    { 							\
1483
     GetStatus(pChan, Status);				\
1484
     if ((Status == 0) || (Status & IDE_STATUS_ERROR)	\
1485
      || (Status == IDE_STATUS_IDLE))			\
1486
	{						\
1487
	 break;						\
1488
	}						\
1489
     udelay(200);					\
1490
     continue;						\
1491
    }							\
1492
}
1493
1494
#define WaitForBaseCommandComplete(pChan, Status)	\
1495
{							\
1496
 ULONG		i;					\
1497
 for (i = 0; i < 50000; i++)				\
1498
    { 							\
1499
     GetBaseStatus(pChan, Status);			\
1500
     if ((Status == 0) || (Status & IDE_STATUS_ERROR)	\
1501
      || (Status == IDE_STATUS_IDLE))			\
1502
	{						\
1503
	 break;						\
1504
	}						\
1505
     udelay(200);					\
1506
     continue;						\
1507
    }							\
1508
}
1509
1510
#define AtapiSoftReset(pChan, DevNum)		\
1511
{						\
1512
 UCHAR		statusByte;			\
1513
 outb((UCHAR)(((DevNum & 0x1) << 4) | 0xA0), pChan->io_ports[IDE_SELECT_OFFSET]); \
1514
 udelay(500);									  \
1515
 outb(IDE_COMMAND_ATAPI_RESET, pChan->io_ports[IDE_COMMAND_OFFSET]);		  \
1516
 mdelay(1000);									  \
1517
 outb((UCHAR)(((DevNum & 0x1) << 4) | 0xA0), pChan->io_ports[IDE_SELECT_OFFSET]); \
1518
 WaitOnBusy(pChan, statusByte);			\
1519
 udelay(500);					\
1520
}
1521
1522
#define IdeHardReset(pChan, result)		\
1523
{						\
1524
 UCHAR		statusByte;			\
1525
 ULONG		i;				\
1526
 outb(IDE_DC_RESET_CONTROLLER, pChan->io_ports[IDE_CONTROL_OFFSET]);	\
1527
 mdelay(50);								\
1528
 outb(IDE_DC_REENABLE_CONTROLLER, pChan->io_ports[IDE_CONTROL_OFFSET]);	\
1529
 for (i = 0; i < 1000 * 1000; i++)					\
1530
    {									\
1531
     statusByte = inb(pChan->io_ports[IDE_CONTROL_OFFSET]);		\
1532
     if (statusByte != IDE_STATUS_IDLE && statusByte != 0x0)		\
1533
	{					\
1534
	 udelay(30);				\
1535
	}					\
1536
     else					\
1537
	{					\
1538
	 break;					\
1539
	}					\
1540
    }						\
1541
 if (i == 1000 * 1000)				\
1542
    {						\
1543
     printk("IdeHardReset Fail!\n");		\
1544
     result = FALSE;				\
1545
    }						\
1546
 else						\
1547
    {						\
1548
     dprintk("IdeHardReset Success!\n");	\
1549
     result = TRUE;				\
1550
    }						\
1551
}
1552
1553
/************************************************************************
1554
 * Function prototypes
1555
 ************************************************************************/
1556
void		iteretry(PChannel);
1557
void		TaskStart(PChannel, Scsi_Cmnd *);
1558
void		TaskQueue(void);
1559
void		TaskDone(PChannel, PSCSI_REQUEST_BLOCK);
1560
ULONG		IdeSendCommand(PChannel, PSCSI_REQUEST_BLOCK);
1561
void		IdeMediaStatus(BOOLEAN, PChannel, ULONG);
1562
static void	IdeSetupDma(PChannel, ULONG, USHORT);
1563
void		MapRequest(Scsi_Cmnd *, PSCSI_REQUEST_BLOCK);
1564
BOOLEAN		IssueIdentify(PChannel, UCHAR, UCHAR);
1565
BOOLEAN		AtapiStartIo(PChannel, PSCSI_REQUEST_BLOCK);
1566
BOOLEAN		IT8212ResetAdapter(void);
1567
BOOLEAN		AtapiInterrupt(PChannel);
1568
1569
static int	itedev_open(struct inode *, struct file *);
1570
static int	itedev_ioctl_entry(struct inode *, struct file *, unsigned int, unsigned long);
1571
static int	itedev_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
1572
static int	itedev_close(struct inode *, struct file *);
1573
1574
#endif /* #ifndef _ITERAID_H_ */

Return to bug 20919