ide-dma.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:28k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *  linux/drivers/ide/ide-dma.c Version 4.10 June 9, 2000
  3.  *
  4.  *  Copyright (c) 1999-2000 Andre Hedrick <andre@linux-ide.org>
  5.  *  May be copied or modified under the terms of the GNU General Public License
  6.  */
  7. /*
  8.  *  Special Thanks to Mark for his Six years of work.
  9.  *
  10.  *  Copyright (c) 1995-1998  Mark Lord
  11.  *  May be copied or modified under the terms of the GNU General Public License
  12.  */
  13. /*
  14.  * This module provides support for the bus-master IDE DMA functions
  15.  * of various PCI chipsets, including the Intel PIIX (i82371FB for
  16.  * the 430 FX chipset), the PIIX3 (i82371SB for the 430 HX/VX and 
  17.  * 440 chipsets), and the PIIX4 (i82371AB for the 430 TX chipset)
  18.  * ("PIIX" stands for "PCI ISA IDE Xcellerator").
  19.  *
  20.  * Pretty much the same code works for other IDE PCI bus-mastering chipsets.
  21.  *
  22.  * DMA is supported for all IDE devices (disk drives, cdroms, tapes, floppies).
  23.  *
  24.  * By default, DMA support is prepared for use, but is currently enabled only
  25.  * for drives which already have DMA enabled (UltraDMA or mode 2 multi/single),
  26.  * or which are recognized as "good" (see table below).  Drives with only mode0
  27.  * or mode1 (multi/single) DMA should also work with this chipset/driver
  28.  * (eg. MC2112A) but are not enabled by default.
  29.  *
  30.  * Use "hdparm -i" to view modes supported by a given drive.
  31.  *
  32.  * The hdparm-3.5 (or later) utility can be used for manually enabling/disabling
  33.  * DMA support, but must be (re-)compiled against this kernel version or later.
  34.  *
  35.  * To enable DMA, use "hdparm -d1 /dev/hd?" on a per-drive basis after booting.
  36.  * If problems arise, ide.c will disable DMA operation after a few retries.
  37.  * This error recovery mechanism works and has been extremely well exercised.
  38.  *
  39.  * IDE drives, depending on their vintage, may support several different modes
  40.  * of DMA operation.  The boot-time modes are indicated with a "*" in
  41.  * the "hdparm -i" listing, and can be changed with *knowledgeable* use of
  42.  * the "hdparm -X" feature.  There is seldom a need to do this, as drives
  43.  * normally power-up with their "best" PIO/DMA modes enabled.
  44.  *
  45.  * Testing has been done with a rather extensive number of drives,
  46.  * with Quantum & Western Digital models generally outperforming the pack,
  47.  * and Fujitsu & Conner (and some Seagate which are really Conner) drives
  48.  * showing more lackluster throughput.
  49.  *
  50.  * Keep an eye on /var/adm/messages for "DMA disabled" messages.
  51.  *
  52.  * Some people have reported trouble with Intel Zappa motherboards.
  53.  * This can be fixed by upgrading the AMI BIOS to version 1.00.04.BS0,
  54.  * available from ftp://ftp.intel.com/pub/bios/10004bs0.exe
  55.  * (thanks to Glen Morrell <glen@spin.Stanford.edu> for researching this).
  56.  *
  57.  * Thanks to "Christopher J. Reimer" <reimer@doe.carleton.ca> for
  58.  * fixing the problem with the BIOS on some Acer motherboards.
  59.  *
  60.  * Thanks to "Benoit Poulot-Cazajous" <poulot@chorus.fr> for testing
  61.  * "TX" chipset compatibility and for providing patches for the "TX" chipset.
  62.  *
  63.  * Thanks to Christian Brunner <chb@muc.de> for taking a good first crack
  64.  * at generic DMA -- his patches were referred to when preparing this code.
  65.  *
  66.  * Most importantly, thanks to Robert Bringman <rob@mars.trion.com>
  67.  * for supplying a Promise UDMA board & WD UDMA drive for this work!
  68.  *
  69.  * And, yes, Intel Zappa boards really *do* use both PIIX IDE ports.
  70.  *
  71.  * check_drive_lists(ide_drive_t *drive, int good_bad)
  72.  *
  73.  * ATA-66/100 and recovery functions, I forgot the rest......
  74.  * SELECT_READ_WRITE(hwif,drive,func) for active tuning based on IO direction.
  75.  *
  76.  */
  77. #include <linux/config.h>
  78. #include <linux/types.h>
  79. #include <linux/kernel.h>
  80. #include <linux/timer.h>
  81. #include <linux/mm.h>
  82. #include <linux/interrupt.h>
  83. #include <linux/pci.h>
  84. #include <linux/init.h>
  85. #include <linux/ide.h>
  86. #include <asm/io.h>
  87. #include <asm/irq.h>
  88. /*
  89.  * Long lost data from 2.0.34 that is now in 2.0.39
  90.  *
  91.  * This was used in ./drivers/block/triton.c to do DMA Base address setup
  92.  * when PnP failed.  Oh the things we forget.  I believe this was part
  93.  * of SFF-8038i that has been withdrawn from public access... :-((
  94.  */
  95. #define DEFAULT_BMIBA 0xe800 /* in case BIOS did not init it */
  96. #define DEFAULT_BMCRBA 0xcc00 /* VIA's default value */
  97. #define DEFAULT_BMALIBA 0xd400 /* ALI's default value */
  98. extern char *ide_dmafunc_verbose(ide_dma_action_t dmafunc);
  99. #ifdef CONFIG_IDEDMA_NEW_DRIVE_LISTINGS
  100. struct drive_list_entry {
  101. char * id_model;
  102. char * id_firmware;
  103. };
  104. struct drive_list_entry drive_whitelist [] = {
  105. { "Micropolis 2112A" ,       "ALL" },
  106. { "CONNER CTMA 4000" ,       "ALL" },
  107. { "CONNER CTT8000-A" ,       "ALL" },
  108. { "ST34342A" , "ALL" },
  109. { 0 , 0 }
  110. };
  111. struct drive_list_entry drive_blacklist [] = {
  112. { "WDC AC11000H" , "ALL" },
  113. { "WDC AC22100H" , "ALL" },
  114. { "WDC AC31000H" , "ALL" },
  115. { "WDC AC32500H" , "ALL" },
  116. { "WDC AC33100H" , "ALL" },
  117. { "WDC AC31600H" , "ALL" },
  118. { "WDC AC32100H" , "24.09P07" },
  119. { "WDC AC23200L" , "21.10N21" },
  120. { "Compaq CRD-8241B" , "ALL" },
  121. { "CRD-8400B" , "ALL" },
  122. { "CRD-8480B", "ALL" },
  123. { "CRD-8480C", "ALL" },
  124. { "CRD-8482B", "ALL" },
  125.   { "CRD-84" , "ALL" },
  126. { "SanDisk SDP3B" , "ALL" },
  127. { "SanDisk SDP3B-64" , "ALL" },
  128. { "SANYO CD-ROM CRD" , "ALL" },
  129. { "HITACHI CDR-8" , "ALL" },
  130. { "HITACHI CDR-8335" , "ALL" },
  131. { "HITACHI CDR-8435" , "ALL" },
  132. { "Toshiba CD-ROM XM-6202B" , "ALL" },
  133. { "CD-532E-A" , "ALL" },
  134. { "E-IDE CD-ROM CR-840", "ALL" },
  135. { "CD-ROM Drive/F5A", "ALL" },
  136. { "RICOH CD-R/RW MP7083A", "ALL" },
  137. { "WPI CDD-820", "ALL" },
  138. { "SAMSUNG CD-ROM SC-148C", "ALL" },
  139. { "SAMSUNG CD-ROM SC-148F", "ALL" },
  140. { "SAMSUNG CD-ROM SC", "ALL" },
  141. { "SanDisk SDP3B-64" , "ALL" },
  142. { "SAMSUNG CD-ROM SN-124", "ALL" },
  143. { "PLEXTOR CD-R PX-W8432T", "ALL" },
  144. { "ATAPI CD-ROM DRIVE 40X MAXIMUM", "ALL" },
  145. { "_NEC DV5800A",               "ALL"           },  
  146. { 0 , 0 }
  147. };
  148. int in_drive_list(struct hd_driveid *id, struct drive_list_entry * drive_table)
  149. {
  150. for ( ; drive_table->id_model ; drive_table++)
  151. if ((!strcmp(drive_table->id_model, id->model)) &&
  152.     ((!strstr(drive_table->id_firmware, id->fw_rev)) ||
  153.      (!strcmp(drive_table->id_firmware, "ALL"))))
  154. return 1;
  155. return 0;
  156. }
  157. #else /* !CONFIG_IDEDMA_NEW_DRIVE_LISTINGS */
  158. /*
  159.  * good_dma_drives() lists the model names (from "hdparm -i")
  160.  * of drives which do not support mode2 DMA but which are
  161.  * known to work fine with this interface under Linux.
  162.  */
  163. const char *good_dma_drives[] = {"Micropolis 2112A",
  164.  "CONNER CTMA 4000",
  165.  "CONNER CTT8000-A",
  166.  "ST34342A", /* for Sun Ultra */
  167.  NULL};
  168. /*
  169.  * bad_dma_drives() lists the model names (from "hdparm -i")
  170.  * of drives which supposedly support (U)DMA but which are
  171.  * known to corrupt data with this interface under Linux.
  172.  *
  173.  * This is an empirical list. Its generated from bug reports. That means
  174.  * while it reflects actual problem distributions it doesn't answer whether
  175.  * the drive or the controller, or cabling, or software, or some combination
  176.  * thereof is the fault. If you don't happen to agree with the kernel's 
  177.  * opinion of your drive - use hdparm to turn DMA on.
  178.  */
  179. const char *bad_dma_drives[] = {"WDC AC11000H",
  180. "WDC AC22100H",
  181. "WDC AC32100H",
  182. "WDC AC32500H",
  183. "WDC AC33100H",
  184. "WDC AC31600H",
  185.   NULL};
  186. #endif /* CONFIG_IDEDMA_NEW_DRIVE_LISTINGS */
  187. /*
  188.  * Our Physical Region Descriptor (PRD) table should be large enough
  189.  * to handle the biggest I/O request we are likely to see.  Since requests
  190.  * can have no more than 256 sectors, and since the typical blocksize is
  191.  * two or more sectors, we could get by with a limit of 128 entries here for
  192.  * the usual worst case.  Most requests seem to include some contiguous blocks,
  193.  * further reducing the number of table entries required.
  194.  *
  195.  * The driver reverts to PIO mode for individual requests that exceed
  196.  * this limit (possible with 512 byte blocksizes, eg. MSDOS f/s), so handling
  197.  * 100% of all crazy scenarios here is not necessary.
  198.  *
  199.  * As it turns out though, we must allocate a full 4KB page for this,
  200.  * so the two PRD tables (ide0 & ide1) will each get half of that,
  201.  * allowing each to have about 256 entries (8 bytes each) from this.
  202.  */
  203. #define PRD_BYTES 8
  204. #define PRD_ENTRIES (PAGE_SIZE / (2 * PRD_BYTES))
  205. /*
  206.  * dma_intr() is the handler for disk read/write DMA interrupts
  207.  */
  208. ide_startstop_t ide_dma_intr (ide_drive_t *drive)
  209. {
  210. int i;
  211. byte stat, dma_stat;
  212. dma_stat = HWIF(drive)->dmaproc(ide_dma_end, drive);
  213. stat = GET_STAT(); /* get drive status */
  214. if (OK_STAT(stat,DRIVE_READY,drive->bad_wstat|DRQ_STAT)) {
  215. if (!dma_stat) {
  216. struct request *rq = HWGROUP(drive)->rq;
  217. rq = HWGROUP(drive)->rq;
  218. for (i = rq->nr_sectors; i > 0;) {
  219. i -= rq->current_nr_sectors;
  220. ide_end_request(1, HWGROUP(drive));
  221. }
  222. return ide_stopped;
  223. }
  224. printk("%s: dma_intr: bad DMA status (dma_stat=%x)n", 
  225.        drive->name, dma_stat);
  226. }
  227. return ide_error(drive, "dma_intr", stat);
  228. }
  229. static int ide_build_sglist (ide_hwif_t *hwif, struct request *rq)
  230. {
  231. struct buffer_head *bh;
  232. struct scatterlist *sg = hwif->sg_table;
  233. unsigned long lastdataend = ~0UL;
  234. int nents = 0;
  235. if (hwif->sg_dma_active)
  236. BUG();
  237. if (rq->cmd == READ)
  238. hwif->sg_dma_direction = PCI_DMA_FROMDEVICE;
  239. else
  240. hwif->sg_dma_direction = PCI_DMA_TODEVICE;
  241. bh = rq->bh;
  242. do {
  243. struct scatterlist *sge;
  244. /*
  245.  * continue segment from before?
  246.  */
  247. if (bh_phys(bh) == lastdataend) {
  248. sg[nents - 1].length += bh->b_size;
  249. lastdataend += bh->b_size;
  250. continue;
  251. }
  252. /*
  253.  * start new segment
  254.  */
  255. if (nents >= PRD_ENTRIES)
  256. return 0;
  257. sge = &sg[nents];
  258. memset(sge, 0, sizeof(*sge));
  259. if (bh->b_page) {
  260. sge->page = bh->b_page;
  261. sge->offset = bh_offset(bh);
  262. } else {
  263. if (((unsigned long) bh->b_data) < PAGE_SIZE)
  264. BUG();
  265. sge->address = bh->b_data;
  266. }
  267. sge->length = bh->b_size;
  268. lastdataend = bh_phys(bh) + bh->b_size;
  269. nents++;
  270. } while ((bh = bh->b_reqnext) != NULL);
  271. return pci_map_sg(hwif->pci_dev, sg, nents, hwif->sg_dma_direction);
  272. }
  273. static int ide_raw_build_sglist (ide_hwif_t *hwif, struct request *rq)
  274. {
  275. struct scatterlist *sg = hwif->sg_table;
  276. int nents = 0;
  277. ide_task_t *args = rq->special;
  278. unsigned char *virt_addr = rq->buffer;
  279. int sector_count = rq->nr_sectors;
  280. // if ((args->tfRegister[IDE_COMMAND_OFFSET] == WIN_WRITEDMA) ||
  281. //     (args->tfRegister[IDE_COMMAND_OFFSET] == WIN_WRITEDMA_EXT))
  282. if (args->command_type == IDE_DRIVE_TASK_RAW_WRITE)
  283. hwif->sg_dma_direction = PCI_DMA_TODEVICE;
  284. else
  285. hwif->sg_dma_direction = PCI_DMA_FROMDEVICE;
  286. if (sector_count > 128) {
  287. memset(&sg[nents], 0, sizeof(*sg));
  288. sg[nents].address = virt_addr;
  289. sg[nents].length = 128  * SECTOR_SIZE;
  290. nents++;
  291. virt_addr = virt_addr + (128 * SECTOR_SIZE);
  292. sector_count -= 128;
  293. }
  294. memset(&sg[nents], 0, sizeof(*sg));
  295. sg[nents].address = virt_addr;
  296. sg[nents].length =  sector_count  * SECTOR_SIZE;
  297. nents++;
  298.    
  299. return pci_map_sg(hwif->pci_dev, sg, nents, hwif->sg_dma_direction);
  300. }
  301. /*
  302.  * ide_build_dmatable() prepares a dma request.
  303.  * Returns 0 if all went okay, returns 1 otherwise.
  304.  * May also be invoked from trm290.c
  305.  */
  306. int ide_build_dmatable (ide_drive_t *drive, ide_dma_action_t func)
  307. {
  308. unsigned int *table = HWIF(drive)->dmatable_cpu;
  309. #ifdef CONFIG_BLK_DEV_TRM290
  310. unsigned int is_trm290_chipset = (HWIF(drive)->chipset == ide_trm290);
  311. #else
  312. const int is_trm290_chipset = 0;
  313. #endif
  314. unsigned int count = 0;
  315. int i;
  316. struct scatterlist *sg;
  317. if (HWGROUP(drive)->rq->cmd == IDE_DRIVE_TASKFILE)
  318. HWIF(drive)->sg_nents = i = ide_raw_build_sglist(HWIF(drive), HWGROUP(drive)->rq);
  319. else
  320. HWIF(drive)->sg_nents = i = ide_build_sglist(HWIF(drive), HWGROUP(drive)->rq);
  321. if (!i)
  322. return 0;
  323. sg = HWIF(drive)->sg_table;
  324. while (i) {
  325. u32 cur_addr;
  326. u32 cur_len;
  327. cur_addr = sg_dma_address(sg);
  328. cur_len = sg_dma_len(sg);
  329. /*
  330.  * Fill in the dma table, without crossing any 64kB boundaries.
  331.  * Most hardware requires 16-bit alignment of all blocks,
  332.  * but the trm290 requires 32-bit alignment.
  333.  */
  334. while (cur_len) {
  335. u32 xcount, bcount = 0x10000 - (cur_addr & 0xffff);
  336. if (count++ >= PRD_ENTRIES)
  337. BUG();
  338. if (bcount > cur_len)
  339. bcount = cur_len;
  340. *table++ = cpu_to_le32(cur_addr);
  341. xcount = bcount & 0xffff;
  342. if (is_trm290_chipset)
  343. xcount = ((xcount >> 2) - 1) << 16;
  344. if (xcount == 0x0000) {
  345. /* 
  346.  * Most chipsets correctly interpret a length
  347.  * of 0x0000 as 64KB, but at least one
  348.  * (e.g. CS5530) misinterprets it as zero (!).
  349.  * So here we break the 64KB entry into two
  350.  * 32KB entries instead.
  351.  */
  352. if (count++ >= PRD_ENTRIES)
  353. goto use_pio_instead;
  354. *table++ = cpu_to_le32(0x8000);
  355. *table++ = cpu_to_le32(cur_addr + 0x8000);
  356. xcount = 0x8000;
  357. }
  358. *table++ = cpu_to_le32(xcount);
  359. cur_addr += bcount;
  360. cur_len -= bcount;
  361. }
  362. sg++;
  363. i--;
  364. }
  365. if (count) {
  366. if (!is_trm290_chipset)
  367. *--table |= cpu_to_le32(0x80000000);
  368. return count;
  369. }
  370. printk("%s: empty DMA table?n", drive->name);
  371. use_pio_instead:
  372. pci_unmap_sg(HWIF(drive)->pci_dev,
  373.      HWIF(drive)->sg_table,
  374.      HWIF(drive)->sg_nents,
  375.      HWIF(drive)->sg_dma_direction);
  376. HWIF(drive)->sg_dma_active = 0;
  377. return 0; /* revert to PIO for this request */
  378. }
  379. /* Teardown mappings after DMA has completed.  */
  380. void ide_destroy_dmatable (ide_drive_t *drive)
  381. {
  382. struct pci_dev *dev = HWIF(drive)->pci_dev;
  383. struct scatterlist *sg = HWIF(drive)->sg_table;
  384. int nents = HWIF(drive)->sg_nents;
  385. pci_unmap_sg(dev, sg, nents, HWIF(drive)->sg_dma_direction);
  386. HWIF(drive)->sg_dma_active = 0;
  387. }
  388. /*
  389.  *  For both Blacklisted and Whitelisted drives.
  390.  *  This is setup to be called as an extern for future support
  391.  *  to other special driver code.
  392.  */
  393. int check_drive_lists (ide_drive_t *drive, int good_bad)
  394. {
  395. struct hd_driveid *id = drive->id;
  396. #ifdef CONFIG_IDEDMA_NEW_DRIVE_LISTINGS
  397. if (good_bad) {
  398. return in_drive_list(id, drive_whitelist);
  399. } else {
  400. int blacklist = in_drive_list(id, drive_blacklist);
  401. if (blacklist)
  402. printk("%s: Disabling (U)DMA for %sn", drive->name, id->model);
  403. return(blacklist);
  404. }
  405. #else /* !CONFIG_IDEDMA_NEW_DRIVE_LISTINGS */
  406. const char **list;
  407. if (good_bad) {
  408. /* Consult the list of known "good" drives */
  409. list = good_dma_drives;
  410. while (*list) {
  411. if (!strcmp(*list++,id->model))
  412. return 1;
  413. }
  414. } else {
  415. /* Consult the list of known "bad" drives */
  416. list = bad_dma_drives;
  417. while (*list) {
  418. if (!strcmp(*list++,id->model)) {
  419. printk("%s: Disabling (U)DMA for %sn",
  420. drive->name, id->model);
  421. return 1;
  422. }
  423. }
  424. }
  425. #endif /* CONFIG_IDEDMA_NEW_DRIVE_LISTINGS */
  426. return 0;
  427. }
  428. int report_drive_dmaing (ide_drive_t *drive)
  429. {
  430. struct hd_driveid *id = drive->id;
  431. if ((id->field_valid & 4) && (eighty_ninty_three(drive)) &&
  432.     (id->dma_ultra & (id->dma_ultra >> 14) & 3)) {
  433. if ((id->dma_ultra >> 15) & 1) {
  434. printk(", UDMA(mode 7)"); /* UDMA BIOS-enabled! */
  435. } else {
  436. printk(", UDMA(133)"); /* UDMA BIOS-enabled! */
  437. }
  438. } else if ((id->field_valid & 4) && (eighty_ninty_three(drive)) &&
  439.      (id->dma_ultra & (id->dma_ultra >> 11) & 7)) {
  440. if ((id->dma_ultra >> 13) & 1) {
  441. printk(", UDMA(100)"); /* UDMA BIOS-enabled! */
  442. } else if ((id->dma_ultra >> 12) & 1) {
  443. printk(", UDMA(66)"); /* UDMA BIOS-enabled! */
  444. } else {
  445. printk(", UDMA(44)"); /* UDMA BIOS-enabled! */
  446. }
  447. } else if ((id->field_valid & 4) &&
  448.    (id->dma_ultra & (id->dma_ultra >> 8) & 7)) {
  449. if ((id->dma_ultra >> 10) & 1) {
  450. printk(", UDMA(33)"); /* UDMA BIOS-enabled! */
  451. } else if ((id->dma_ultra >> 9) & 1) {
  452. printk(", UDMA(25)"); /* UDMA BIOS-enabled! */
  453. } else {
  454. printk(", UDMA(16)"); /* UDMA BIOS-enabled! */
  455. }
  456. } else if (id->field_valid & 4) {
  457. printk(", (U)DMA"); /* Can be BIOS-enabled! */
  458. } else {
  459. printk(", DMA");
  460. }
  461. return 1;
  462. }
  463. static int config_drive_for_dma (ide_drive_t *drive)
  464. {
  465. int config_allows_dma = 1;
  466. struct hd_driveid *id = drive->id;
  467. ide_hwif_t *hwif = HWIF(drive);
  468. #ifdef CONFIG_IDEDMA_ONLYDISK
  469. if (drive->media != ide_disk)
  470. config_allows_dma = 0;
  471. #endif
  472. if (id && (id->capability & 1) && hwif->autodma && config_allows_dma) {
  473. /* Consult the list of known "bad" drives */
  474. if (ide_dmaproc(ide_dma_bad_drive, drive))
  475. return hwif->dmaproc(ide_dma_off, drive);
  476. /* Enable DMA on any drive that has UltraDMA (mode 6/7/?) enabled */
  477. if ((id->field_valid & 4) && (eighty_ninty_three(drive)))
  478. if ((id->dma_ultra & (id->dma_ultra >> 14) & 2))
  479. return hwif->dmaproc(ide_dma_on, drive);
  480. /* Enable DMA on any drive that has UltraDMA (mode 3/4/5) enabled */
  481. if ((id->field_valid & 4) && (eighty_ninty_three(drive)))
  482. if ((id->dma_ultra & (id->dma_ultra >> 11) & 7))
  483. return hwif->dmaproc(ide_dma_on, drive);
  484. /* Enable DMA on any drive that has UltraDMA (mode 0/1/2) enabled */
  485. if (id->field_valid & 4) /* UltraDMA */
  486. if ((id->dma_ultra & (id->dma_ultra >> 8) & 7))
  487. return hwif->dmaproc(ide_dma_on, drive);
  488. /* Enable DMA on any drive that has mode2 DMA (multi or single) enabled */
  489. if (id->field_valid & 2) /* regular DMA */
  490. if ((id->dma_mword & 0x404) == 0x404 || (id->dma_1word & 0x404) == 0x404)
  491. return hwif->dmaproc(ide_dma_on, drive);
  492. /* Consult the list of known "good" drives */
  493. if (ide_dmaproc(ide_dma_good_drive, drive))
  494. return hwif->dmaproc(ide_dma_on, drive);
  495. }
  496. return hwif->dmaproc(ide_dma_off_quietly, drive);
  497. }
  498. #ifndef CONFIG_BLK_DEV_IDEDMA_TIMEOUT
  499. /*
  500.  * 1 dmaing, 2 error, 4 intr
  501.  */
  502. static int dma_timer_expiry (ide_drive_t *drive)
  503. {
  504. byte dma_stat = inb(HWIF(drive)->dma_base+2);
  505. #ifdef DEBUG
  506. printk("%s: dma_timer_expiry: dma status == 0x%02xn", drive->name, dma_stat);
  507. #endif /* DEBUG */
  508. #if 0
  509. HWGROUP(drive)->expiry = NULL; /* one free ride for now */
  510. #endif
  511. if (dma_stat & 2) { /* ERROR */
  512. byte stat = GET_STAT();
  513. return ide_error(drive, "dma_timer_expiry", stat);
  514. }
  515. if (dma_stat & 1) /* DMAing */
  516. return WAIT_CMD;
  517. return 0;
  518. }
  519. #else /* CONFIG_BLK_DEV_IDEDMA_TIMEOUT */
  520. static ide_startstop_t ide_dma_timeout_revovery (ide_drive_t *drive)
  521. {
  522. ide_hwgroup_t *hwgroup = HWGROUP(drive);
  523. ide_hwif_t *hwif = HWIF(drive);
  524. int enable_dma = drive->using_dma;
  525. unsigned long flags;
  526. ide_startstop_t startstop;
  527. spin_lock_irqsave(&io_request_lock, flags);
  528. hwgroup->handler = NULL;
  529. del_timer(&hwgroup->timer);
  530. spin_unlock_irqrestore(&io_request_lock, flags);
  531. drive->waiting_for_dma = 0;
  532. startstop = ide_do_reset(drive);
  533. if ((enable_dma) && !(drive->using_dma))
  534. (void) hwif->dmaproc(ide_dma_on, drive);
  535. return startstop;
  536. }
  537. #endif /* CONFIG_BLK_DEV_IDEDMA_TIMEOUT */
  538. static inline void ide_toggle_bounce(ide_drive_t *drive, int on)
  539. {
  540. dma64_addr_t addr = BLK_BOUNCE_HIGH;
  541. if (HWIF(drive)->no_highio || HWIF(drive)->pci_dev == NULL)
  542. return;
  543. if (on && drive->media == ide_disk) {
  544. if (!PCI_DMA_BUS_IS_PHYS)
  545. addr = BLK_BOUNCE_ANY;
  546. else
  547. addr = HWIF(drive)->pci_dev->dma_mask;
  548. }
  549. blk_queue_bounce_limit(&drive->queue, addr);
  550. }
  551. /*
  552.  * ide_dmaproc() initiates/aborts DMA read/write operations on a drive.
  553.  *
  554.  * The caller is assumed to have selected the drive and programmed the drive's
  555.  * sector address using CHS or LBA.  All that remains is to prepare for DMA
  556.  * and then issue the actual read/write DMA/PIO command to the drive.
  557.  *
  558.  * For ATAPI devices, we just prepare for DMA and return. The caller should
  559.  * then issue the packet command to the drive and call us again with
  560.  * ide_dma_begin afterwards.
  561.  *
  562.  * Returns 0 if all went well.
  563.  * Returns 1 if DMA read/write could not be started, in which case
  564.  * the caller should revert to PIO for the current request.
  565.  * May also be invoked from trm290.c
  566.  */
  567. int ide_dmaproc (ide_dma_action_t func, ide_drive_t *drive)
  568. {
  569. // ide_hwgroup_t *hwgroup = HWGROUP(drive);
  570. ide_hwif_t *hwif = HWIF(drive);
  571. unsigned long dma_base = hwif->dma_base;
  572. byte unit = (drive->select.b.unit & 0x01);
  573. unsigned int count, reading = 0, set_high = 1;
  574. byte dma_stat;
  575. switch (func) {
  576. case ide_dma_off:
  577. printk("%s: DMA disabledn", drive->name);
  578. case ide_dma_off_quietly:
  579. set_high = 0;
  580. outb(inb(dma_base+2) & ~(1<<(5+unit)), dma_base+2);
  581. case ide_dma_on:
  582. drive->using_dma = (func == ide_dma_on);
  583. if (drive->using_dma)
  584. outb(inb(dma_base+2)|(1<<(5+unit)), dma_base+2);
  585. ide_toggle_bounce(drive, set_high);
  586. return 0;
  587. case ide_dma_check:
  588. return config_drive_for_dma (drive);
  589. case ide_dma_read:
  590. reading = 1 << 3;
  591. case ide_dma_write:
  592. SELECT_READ_WRITE(hwif,drive,func);
  593. if (!(count = ide_build_dmatable(drive, func)))
  594. return 1; /* try PIO instead of DMA */
  595. outl(hwif->dmatable_dma, dma_base + 4); /* PRD table */
  596. outb(reading, dma_base); /* specify r/w */
  597. outb(inb(dma_base+2)|6, dma_base+2); /* clear INTR & ERROR flags */
  598. drive->waiting_for_dma = 1;
  599. if (drive->media != ide_disk)
  600. return 0;
  601. #ifdef CONFIG_BLK_DEV_IDEDMA_TIMEOUT
  602. ide_set_handler(drive, &ide_dma_intr, 2*WAIT_CMD, NULL); /* issue cmd to drive */
  603. #else /* !CONFIG_BLK_DEV_IDEDMA_TIMEOUT */
  604. ide_set_handler(drive, &ide_dma_intr, WAIT_CMD, dma_timer_expiry); /* issue cmd to drive */
  605. #endif /* CONFIG_BLK_DEV_IDEDMA_TIMEOUT */
  606. if ((HWGROUP(drive)->rq->cmd == IDE_DRIVE_TASKFILE) &&
  607.     (drive->addressing == 1)) {
  608. ide_task_t *args = HWGROUP(drive)->rq->special;
  609. OUT_BYTE(args->tfRegister[IDE_COMMAND_OFFSET], IDE_COMMAND_REG);
  610. } else if (drive->addressing) {
  611. OUT_BYTE(reading ? WIN_READDMA_EXT : WIN_WRITEDMA_EXT, IDE_COMMAND_REG);
  612. } else {
  613. OUT_BYTE(reading ? WIN_READDMA : WIN_WRITEDMA, IDE_COMMAND_REG);
  614. }
  615. return HWIF(drive)->dmaproc(ide_dma_begin, drive);
  616. case ide_dma_begin:
  617. /* Note that this is done *after* the cmd has
  618.  * been issued to the drive, as per the BM-IDE spec.
  619.  * The Promise Ultra33 doesn't work correctly when
  620.  * we do this part before issuing the drive cmd.
  621.  */
  622. outb(inb(dma_base)|1, dma_base); /* start DMA */
  623. return 0;
  624. case ide_dma_end: /* returns 1 on error, 0 otherwise */
  625. drive->waiting_for_dma = 0;
  626. outb(inb(dma_base)&~1, dma_base); /* stop DMA */
  627. dma_stat = inb(dma_base+2); /* get DMA status */
  628. outb(dma_stat|6, dma_base+2); /* clear the INTR & ERROR bits */
  629. ide_destroy_dmatable(drive); /* purge DMA mappings */
  630. return (dma_stat & 7) != 4 ? (0x10 | dma_stat) : 0; /* verify good DMA status */
  631. case ide_dma_test_irq: /* returns 1 if dma irq issued, 0 otherwise */
  632. dma_stat = inb(dma_base+2);
  633. #if 0  /* do not set unless you know what you are doing */
  634. if (dma_stat & 4) {
  635. byte stat = GET_STAT();
  636. outb(dma_base+2, dma_stat & 0xE4);
  637. }
  638. #endif
  639. return (dma_stat & 4) == 4; /* return 1 if INTR asserted */
  640. case ide_dma_bad_drive:
  641. case ide_dma_good_drive:
  642. return check_drive_lists(drive, (func == ide_dma_good_drive));
  643. case ide_dma_verbose:
  644. return report_drive_dmaing(drive);
  645. case ide_dma_timeout:
  646. // FIXME: Many IDE chipsets do not permit command file register access
  647. // FIXME: while the bus-master function is still active.
  648. // FIXME: To prevent deadlock with those chipsets, we must be extremely
  649. // FIXME: careful here (and in ide_intr() as well) to NOT access any
  650. // FIXME: registers from the 0x1Fx/0x17x sets before terminating the
  651. // FIXME: bus-master operation via the bus-master control reg.
  652. // FIXME: Otherwise, chipset deadlock will occur, and some systems will
  653. // FIXME: lock up completely!!
  654. #ifdef CONFIG_BLK_DEV_IDEDMA_TIMEOUT
  655. /*
  656.  * Have to issue an abort and requeue the request
  657.  * DMA engine got turned off by a goofy ASIC, and
  658.  * we have to clean up the mess, and here is as good
  659.  * as any.  Do it globally for all chipsets.
  660.  */
  661. outb(0x00, dma_base); /* stop DMA */
  662. dma_stat = inb(dma_base+2); /* get DMA status */
  663. outb(dma_stat|6, dma_base+2); /* clear the INTR & ERROR bits */
  664. printk("%s: %s: Lets do it again!" 
  665. "stat = 0x%02x, dma_stat = 0x%02xn",
  666. drive->name, ide_dmafunc_verbose(func),
  667. GET_STAT(), dma_stat);
  668. if (dma_stat & 0xF0)
  669. return ide_dma_timeout_revovery(drive);
  670. printk("%s: %s: (restart_request) Lets do it again!" 
  671. "stat = 0x%02x, dma_stat = 0x%02xn",
  672. drive->name, ide_dmafunc_verbose(func),
  673. GET_STAT(), dma_stat);
  674. return restart_request(drive);  // BUG: return types do not match!!
  675. //#else
  676. // return HWGROUP(drive)->handler(drive);
  677. #endif /* CONFIG_BLK_DEV_IDEDMA_TIMEOUT */
  678. case ide_dma_retune:
  679. case ide_dma_lostirq:
  680. printk("ide_dmaproc: chipset supported %s func only: %dn", ide_dmafunc_verbose(func),  func);
  681. return 1;
  682. default:
  683. printk("ide_dmaproc: unsupported %s func: %dn", ide_dmafunc_verbose(func), func);
  684. return 1;
  685. }
  686. }
  687. /*
  688.  * Needed for allowing full modular support of ide-driver
  689.  */
  690. int ide_release_dma (ide_hwif_t *hwif)
  691. {
  692. if (hwif->dmatable_cpu) {
  693. pci_free_consistent(hwif->pci_dev,
  694.     PRD_ENTRIES * PRD_BYTES,
  695.     hwif->dmatable_cpu,
  696.     hwif->dmatable_dma);
  697. hwif->dmatable_cpu = NULL;
  698. }
  699. if (hwif->sg_table) {
  700. kfree(hwif->sg_table);
  701. hwif->sg_table = NULL;
  702. }
  703. if ((hwif->dma_extra) && (hwif->channel == 0))
  704. release_region((hwif->dma_base + 16), hwif->dma_extra);
  705. release_region(hwif->dma_base, 8);
  706. return 1;
  707. }
  708. /*
  709.  * This can be called for a dynamically installed interface. Don't __init it
  710.  */
  711.  
  712. void ide_setup_dma (ide_hwif_t *hwif, unsigned long dma_base, unsigned int num_ports)
  713. {
  714. printk("    %s: BM-DMA at 0x%04lx-0x%04lx", hwif->name, dma_base, dma_base + num_ports - 1);
  715. if (check_region(dma_base, num_ports)) {
  716. printk(" -- ERROR, PORT ADDRESSES ALREADY IN USEn");
  717. return;
  718. }
  719. request_region(dma_base, num_ports, hwif->name);
  720. hwif->dma_base = dma_base;
  721. hwif->dmatable_cpu = pci_alloc_consistent(hwif->pci_dev,
  722.     PRD_ENTRIES * PRD_BYTES,
  723.     &hwif->dmatable_dma);
  724. if (hwif->dmatable_cpu == NULL)
  725. goto dma_alloc_failure;
  726. hwif->sg_table = kmalloc(sizeof(struct scatterlist) * PRD_ENTRIES,
  727.  GFP_KERNEL);
  728. if (hwif->sg_table == NULL) {
  729. pci_free_consistent(hwif->pci_dev, PRD_ENTRIES * PRD_BYTES,
  730.     hwif->dmatable_cpu, hwif->dmatable_dma);
  731. goto dma_alloc_failure;
  732. }
  733. hwif->dmaproc = &ide_dmaproc;
  734. if (hwif->chipset != ide_trm290) {
  735. byte dma_stat = inb(dma_base+2);
  736. printk(", BIOS settings: %s:%s, %s:%s",
  737.        hwif->drives[0].name, (dma_stat & 0x20) ? "DMA" : "pio",
  738.        hwif->drives[1].name, (dma_stat & 0x40) ? "DMA" : "pio");
  739. }
  740. printk("n");
  741. return;
  742. dma_alloc_failure:
  743. printk(" -- ERROR, UNABLE TO ALLOCATE DMA TABLESn");
  744. }
  745. /*
  746.  * Fetch the DMA Bus-Master-I/O-Base-Address (BMIBA) from PCI space:
  747.  */
  748. unsigned long __init ide_get_or_set_dma_base (ide_hwif_t *hwif, int extra, const char *name)
  749. {
  750. unsigned long dma_base = 0;
  751. struct pci_dev *dev = hwif->pci_dev;
  752. #ifdef CONFIG_BLK_DEV_IDEDMA_FORCED
  753. int second_chance = 0;
  754. second_chance_to_dma:
  755. #endif /* CONFIG_BLK_DEV_IDEDMA_FORCED */
  756. if (hwif->mate && hwif->mate->dma_base) {
  757. dma_base = hwif->mate->dma_base - (hwif->channel ? 0 : 8);
  758. } else {
  759. dma_base = pci_resource_start(dev, 4);
  760. if (!dma_base) {
  761. printk("%s: dma_base is invalid (0x%04lx)n", name, dma_base);
  762. dma_base = 0;
  763. }
  764. }
  765. #ifdef CONFIG_BLK_DEV_IDEDMA_FORCED
  766. if ((!dma_base) && (!second_chance)) {
  767. unsigned long set_bmiba = 0;
  768. second_chance++;
  769. switch(dev->vendor) {
  770. case PCI_VENDOR_ID_AL:
  771. set_bmiba = DEFAULT_BMALIBA; break;
  772. case PCI_VENDOR_ID_VIA:
  773. set_bmiba = DEFAULT_BMCRBA; break;
  774. case PCI_VENDOR_ID_INTEL:
  775. set_bmiba = DEFAULT_BMIBA; break;
  776. default:
  777. return dma_base;
  778. }
  779. pci_write_config_dword(dev, 0x20, set_bmiba|1);
  780. goto second_chance_to_dma;
  781. }
  782. #endif /* CONFIG_BLK_DEV_IDEDMA_FORCED */
  783. if (dma_base) {
  784. if (extra) /* PDC20246, PDC20262, HPT343, & HPT366 */
  785. request_region(dma_base+16, extra, name);
  786. dma_base += hwif->channel ? 8 : 0;
  787. hwif->dma_extra = extra;
  788. switch(dev->device) {
  789. case PCI_DEVICE_ID_AL_M5219:
  790. case PCI_DEVICE_ID_AMD_VIPER_7409:
  791. case PCI_DEVICE_ID_CMD_643:
  792. outb(inb(dma_base+2) & 0x60, dma_base+2);
  793. if (inb(dma_base+2) & 0x80) {
  794. printk("%s: simplex device: DMA forcedn", name);
  795. }
  796. break;
  797. default:
  798. /*
  799.  * If the device claims "simplex" DMA,
  800.  * this means only one of the two interfaces
  801.  * can be trusted with DMA at any point in time.
  802.  * So we should enable DMA only on one of the
  803.  * two interfaces.
  804.  */
  805. if ((inb(dma_base+2) & 0x80)) { /* simplex device? */
  806. if ((!hwif->drives[0].present && !hwif->drives[1].present) ||
  807.     (hwif->mate && hwif->mate->dma_base)) {
  808. printk("%s: simplex device:  DMA disabledn", name);
  809. dma_base = 0;
  810. }
  811. }
  812. }
  813. }
  814. return dma_base;
  815. }