ide-taskfile.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. /*
  2. * linux/drivers/ide/ide-taskfile.c Version 0.38 March 05, 2003
  3. *
  4. * Copyright (C) 2000-2002 Michael Cornwell <cornwell@acm.org>
  5. * Copyright (C) 2000-2002 Andre Hedrick <andre@linux-ide.org>
  6. * Copyright (C) 2001-2002 Klaus Smolin
  7. * IBM Storage Technology Division
  8. * Copyright (C) 2003-2004 Bartlomiej Zolnierkiewicz
  9. *
  10. * The big the bad and the ugly.
  11. *
  12. * Problems to be fixed because of BH interface or the lack therefore.
  13. *
  14. * Fill me in stupid !!!
  15. *
  16. * HOST:
  17. * General refers to the Controller and Driver "pair".
  18. * DATA HANDLER:
  19. * Under the context of Linux it generally refers to an interrupt handler.
  20. * However, it correctly describes the 'HOST'
  21. * DATA BLOCK:
  22. * The amount of data needed to be transfered as predefined in the
  23. * setup of the device.
  24. * STORAGE ATOMIC:
  25. * The 'DATA BLOCK' associated to the 'DATA HANDLER', and can be as
  26. * small as a single sector or as large as the entire command block
  27. * request.
  28. */
  29. #include <linux/module.h>
  30. #include <linux/types.h>
  31. #include <linux/string.h>
  32. #include <linux/kernel.h>
  33. #include <linux/timer.h>
  34. #include <linux/mm.h>
  35. #include <linux/sched.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/major.h>
  38. #include <linux/errno.h>
  39. #include <linux/genhd.h>
  40. #include <linux/blkpg.h>
  41. #include <linux/slab.h>
  42. #include <linux/pci.h>
  43. #include <linux/delay.h>
  44. #include <linux/hdreg.h>
  45. #include <linux/ide.h>
  46. #include <linux/bitops.h>
  47. #include <asm/byteorder.h>
  48. #include <asm/irq.h>
  49. #include <asm/uaccess.h>
  50. #include <asm/io.h>
  51. static void ata_bswap_data (void *buffer, int wcount)
  52. {
  53. u16 *p = buffer;
  54. while (wcount--) {
  55. *p = *p << 8 | *p >> 8; p++;
  56. *p = *p << 8 | *p >> 8; p++;
  57. }
  58. }
  59. static void taskfile_input_data(ide_drive_t *drive, void *buffer, u32 wcount)
  60. {
  61. HWIF(drive)->ata_input_data(drive, buffer, wcount);
  62. if (drive->bswap)
  63. ata_bswap_data(buffer, wcount);
  64. }
  65. static void taskfile_output_data(ide_drive_t *drive, void *buffer, u32 wcount)
  66. {
  67. if (drive->bswap) {
  68. ata_bswap_data(buffer, wcount);
  69. HWIF(drive)->ata_output_data(drive, buffer, wcount);
  70. ata_bswap_data(buffer, wcount);
  71. } else {
  72. HWIF(drive)->ata_output_data(drive, buffer, wcount);
  73. }
  74. }
  75. int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
  76. {
  77. ide_task_t args;
  78. memset(&args, 0, sizeof(ide_task_t));
  79. args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
  80. if (drive->media == ide_disk)
  81. args.tfRegister[IDE_COMMAND_OFFSET] = WIN_IDENTIFY;
  82. else
  83. args.tfRegister[IDE_COMMAND_OFFSET] = WIN_PIDENTIFY;
  84. args.command_type = IDE_DRIVE_TASK_IN;
  85. args.data_phase = TASKFILE_IN;
  86. args.handler = &task_in_intr;
  87. return ide_raw_taskfile(drive, &args, buf);
  88. }
  89. ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
  90. {
  91. ide_hwif_t *hwif = HWIF(drive);
  92. task_struct_t *taskfile = (task_struct_t *) task->tfRegister;
  93. hob_struct_t *hobfile = (hob_struct_t *) task->hobRegister;
  94. u8 HIHI = (drive->addressing == 1) ? 0xE0 : 0xEF;
  95. /* ALL Command Block Executions SHALL clear nIEN, unless otherwise */
  96. if (IDE_CONTROL_REG) {
  97. /* clear nIEN */
  98. hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
  99. }
  100. SELECT_MASK(drive, 0);
  101. if (drive->addressing == 1) {
  102. hwif->OUTB(hobfile->feature, IDE_FEATURE_REG);
  103. hwif->OUTB(hobfile->sector_count, IDE_NSECTOR_REG);
  104. hwif->OUTB(hobfile->sector_number, IDE_SECTOR_REG);
  105. hwif->OUTB(hobfile->low_cylinder, IDE_LCYL_REG);
  106. hwif->OUTB(hobfile->high_cylinder, IDE_HCYL_REG);
  107. }
  108. hwif->OUTB(taskfile->feature, IDE_FEATURE_REG);
  109. hwif->OUTB(taskfile->sector_count, IDE_NSECTOR_REG);
  110. hwif->OUTB(taskfile->sector_number, IDE_SECTOR_REG);
  111. hwif->OUTB(taskfile->low_cylinder, IDE_LCYL_REG);
  112. hwif->OUTB(taskfile->high_cylinder, IDE_HCYL_REG);
  113. hwif->OUTB((taskfile->device_head & HIHI) | drive->select.all, IDE_SELECT_REG);
  114. if (task->handler != NULL) {
  115. if (task->prehandler != NULL) {
  116. hwif->OUTBSYNC(drive, taskfile->command, IDE_COMMAND_REG);
  117. ndelay(400); /* FIXME */
  118. return task->prehandler(drive, task->rq);
  119. }
  120. ide_execute_command(drive, taskfile->command, task->handler, WAIT_WORSTCASE, NULL);
  121. return ide_started;
  122. }
  123. if (!drive->using_dma)
  124. return ide_stopped;
  125. switch (taskfile->command) {
  126. case WIN_WRITEDMA_ONCE:
  127. case WIN_WRITEDMA:
  128. case WIN_WRITEDMA_EXT:
  129. case WIN_READDMA_ONCE:
  130. case WIN_READDMA:
  131. case WIN_READDMA_EXT:
  132. case WIN_IDENTIFY_DMA:
  133. if (!hwif->dma_setup(drive)) {
  134. hwif->dma_exec_cmd(drive, taskfile->command);
  135. hwif->dma_start(drive);
  136. return ide_started;
  137. }
  138. break;
  139. default:
  140. if (task->handler == NULL)
  141. return ide_stopped;
  142. }
  143. return ide_stopped;
  144. }
  145. /*
  146. * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
  147. */
  148. ide_startstop_t set_multmode_intr (ide_drive_t *drive)
  149. {
  150. ide_hwif_t *hwif = HWIF(drive);
  151. u8 stat;
  152. if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
  153. drive->mult_count = drive->mult_req;
  154. } else {
  155. drive->mult_req = drive->mult_count = 0;
  156. drive->special.b.recalibrate = 1;
  157. (void) ide_dump_status(drive, "set_multmode", stat);
  158. }
  159. return ide_stopped;
  160. }
  161. /*
  162. * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
  163. */
  164. ide_startstop_t set_geometry_intr (ide_drive_t *drive)
  165. {
  166. ide_hwif_t *hwif = HWIF(drive);
  167. int retries = 5;
  168. u8 stat;
  169. while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
  170. udelay(10);
  171. if (OK_STAT(stat, READY_STAT, BAD_STAT))
  172. return ide_stopped;
  173. if (stat & (ERR_STAT|DRQ_STAT))
  174. return ide_error(drive, "set_geometry_intr", stat);
  175. BUG_ON(HWGROUP(drive)->handler != NULL);
  176. ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
  177. return ide_started;
  178. }
  179. /*
  180. * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
  181. */
  182. ide_startstop_t recal_intr (ide_drive_t *drive)
  183. {
  184. ide_hwif_t *hwif = HWIF(drive);
  185. u8 stat;
  186. if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
  187. return ide_error(drive, "recal_intr", stat);
  188. return ide_stopped;
  189. }
  190. /*
  191. * Handler for commands without a data phase
  192. */
  193. ide_startstop_t task_no_data_intr (ide_drive_t *drive)
  194. {
  195. ide_task_t *args = HWGROUP(drive)->rq->special;
  196. ide_hwif_t *hwif = HWIF(drive);
  197. u8 stat;
  198. local_irq_enable_in_hardirq();
  199. if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
  200. return ide_error(drive, "task_no_data_intr", stat);
  201. /* calls ide_end_drive_cmd */
  202. }
  203. if (args)
  204. ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
  205. return ide_stopped;
  206. }
  207. EXPORT_SYMBOL(task_no_data_intr);
  208. static u8 wait_drive_not_busy(ide_drive_t *drive)
  209. {
  210. ide_hwif_t *hwif = HWIF(drive);
  211. int retries;
  212. u8 stat;
  213. /*
  214. * Last sector was transfered, wait until drive is ready.
  215. * This can take up to 10 usec, but we will wait max 1 ms
  216. * (drive_cmd_intr() waits that long).
  217. */
  218. for (retries = 0; retries < 100; retries++) {
  219. if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT)
  220. udelay(10);
  221. else
  222. break;
  223. }
  224. if (stat & BUSY_STAT)
  225. printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
  226. return stat;
  227. }
  228. static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
  229. {
  230. ide_hwif_t *hwif = drive->hwif;
  231. struct scatterlist *sg = hwif->sg_table;
  232. struct page *page;
  233. #ifdef CONFIG_HIGHMEM
  234. unsigned long flags;
  235. #endif
  236. unsigned int offset;
  237. u8 *buf;
  238. page = sg[hwif->cursg].page;
  239. offset = sg[hwif->cursg].offset + hwif->cursg_ofs * SECTOR_SIZE;
  240. /* get the current page and offset */
  241. page = nth_page(page, (offset >> PAGE_SHIFT));
  242. offset %= PAGE_SIZE;
  243. #ifdef CONFIG_HIGHMEM
  244. local_irq_save(flags);
  245. #endif
  246. buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
  247. hwif->nleft--;
  248. hwif->cursg_ofs++;
  249. if ((hwif->cursg_ofs * SECTOR_SIZE) == sg[hwif->cursg].length) {
  250. hwif->cursg++;
  251. hwif->cursg_ofs = 0;
  252. }
  253. /* do the actual data transfer */
  254. if (write)
  255. taskfile_output_data(drive, buf, SECTOR_WORDS);
  256. else
  257. taskfile_input_data(drive, buf, SECTOR_WORDS);
  258. kunmap_atomic(buf, KM_BIO_SRC_IRQ);
  259. #ifdef CONFIG_HIGHMEM
  260. local_irq_restore(flags);
  261. #endif
  262. }
  263. static void ide_pio_multi(ide_drive_t *drive, unsigned int write)
  264. {
  265. unsigned int nsect;
  266. nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
  267. while (nsect--)
  268. ide_pio_sector(drive, write);
  269. }
  270. static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
  271. unsigned int write)
  272. {
  273. if (rq->bio) /* fs request */
  274. rq->errors = 0;
  275. touch_softlockup_watchdog();
  276. switch (drive->hwif->data_phase) {
  277. case TASKFILE_MULTI_IN:
  278. case TASKFILE_MULTI_OUT:
  279. ide_pio_multi(drive, write);
  280. break;
  281. default:
  282. ide_pio_sector(drive, write);
  283. break;
  284. }
  285. }
  286. static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
  287. const char *s, u8 stat)
  288. {
  289. if (rq->bio) {
  290. ide_hwif_t *hwif = drive->hwif;
  291. int sectors = hwif->nsect - hwif->nleft;
  292. switch (hwif->data_phase) {
  293. case TASKFILE_IN:
  294. if (hwif->nleft)
  295. break;
  296. /* fall through */
  297. case TASKFILE_OUT:
  298. sectors--;
  299. break;
  300. case TASKFILE_MULTI_IN:
  301. if (hwif->nleft)
  302. break;
  303. /* fall through */
  304. case TASKFILE_MULTI_OUT:
  305. sectors -= drive->mult_count;
  306. default:
  307. break;
  308. }
  309. if (sectors > 0) {
  310. ide_driver_t *drv;
  311. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  312. drv->end_request(drive, 1, sectors);
  313. }
  314. }
  315. return ide_error(drive, s, stat);
  316. }
  317. static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
  318. {
  319. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
  320. ide_task_t *task = rq->special;
  321. if (task->tf_out_flags.all) {
  322. u8 err = drive->hwif->INB(IDE_ERROR_REG);
  323. ide_end_drive_cmd(drive, stat, err);
  324. return;
  325. }
  326. }
  327. if (rq->rq_disk) {
  328. ide_driver_t *drv;
  329. drv = *(ide_driver_t **)rq->rq_disk->private_data;;
  330. drv->end_request(drive, 1, rq->hard_nr_sectors);
  331. } else
  332. ide_end_request(drive, 1, rq->hard_nr_sectors);
  333. }
  334. /*
  335. * Handler for command with PIO data-in phase (Read/Read Multiple).
  336. */
  337. ide_startstop_t task_in_intr (ide_drive_t *drive)
  338. {
  339. ide_hwif_t *hwif = drive->hwif;
  340. struct request *rq = HWGROUP(drive)->rq;
  341. u8 stat = hwif->INB(IDE_STATUS_REG);
  342. /* new way for dealing with premature shared PCI interrupts */
  343. if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
  344. if (stat & (ERR_STAT | DRQ_STAT))
  345. return task_error(drive, rq, __FUNCTION__, stat);
  346. /* No data yet, so wait for another IRQ. */
  347. ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
  348. return ide_started;
  349. }
  350. ide_pio_datablock(drive, rq, 0);
  351. /* If it was the last datablock check status and finish transfer. */
  352. if (!hwif->nleft) {
  353. stat = wait_drive_not_busy(drive);
  354. if (!OK_STAT(stat, 0, BAD_R_STAT))
  355. return task_error(drive, rq, __FUNCTION__, stat);
  356. task_end_request(drive, rq, stat);
  357. return ide_stopped;
  358. }
  359. /* Still data left to transfer. */
  360. ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
  361. return ide_started;
  362. }
  363. EXPORT_SYMBOL(task_in_intr);
  364. /*
  365. * Handler for command with PIO data-out phase (Write/Write Multiple).
  366. */
  367. static ide_startstop_t task_out_intr (ide_drive_t *drive)
  368. {
  369. ide_hwif_t *hwif = drive->hwif;
  370. struct request *rq = HWGROUP(drive)->rq;
  371. u8 stat = hwif->INB(IDE_STATUS_REG);
  372. if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
  373. return task_error(drive, rq, __FUNCTION__, stat);
  374. /* Deal with unexpected ATA data phase. */
  375. if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
  376. return task_error(drive, rq, __FUNCTION__, stat);
  377. if (!hwif->nleft) {
  378. task_end_request(drive, rq, stat);
  379. return ide_stopped;
  380. }
  381. /* Still data left to transfer. */
  382. ide_pio_datablock(drive, rq, 1);
  383. ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
  384. return ide_started;
  385. }
  386. ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq)
  387. {
  388. ide_startstop_t startstop;
  389. if (ide_wait_stat(&startstop, drive, DATA_READY,
  390. drive->bad_wstat, WAIT_DRQ)) {
  391. printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
  392. drive->name,
  393. drive->hwif->data_phase ? "MULT" : "",
  394. drive->addressing ? "_EXT" : "");
  395. return startstop;
  396. }
  397. if (!drive->unmask)
  398. local_irq_disable();
  399. ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
  400. ide_pio_datablock(drive, rq, 1);
  401. return ide_started;
  402. }
  403. EXPORT_SYMBOL(pre_task_out_intr);
  404. static int ide_diag_taskfile(ide_drive_t *drive, ide_task_t *args, unsigned long data_size, u8 *buf)
  405. {
  406. struct request rq;
  407. memset(&rq, 0, sizeof(rq));
  408. rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
  409. rq.buffer = buf;
  410. /*
  411. * (ks) We transfer currently only whole sectors.
  412. * This is suffient for now. But, it would be great,
  413. * if we would find a solution to transfer any size.
  414. * To support special commands like READ LONG.
  415. */
  416. if (args->command_type != IDE_DRIVE_TASK_NO_DATA) {
  417. if (data_size == 0)
  418. rq.nr_sectors = (args->hobRegister[IDE_NSECTOR_OFFSET] << 8) | args->tfRegister[IDE_NSECTOR_OFFSET];
  419. else
  420. rq.nr_sectors = data_size / SECTOR_SIZE;
  421. if (!rq.nr_sectors) {
  422. printk(KERN_ERR "%s: in/out command without data\n",
  423. drive->name);
  424. return -EFAULT;
  425. }
  426. rq.hard_nr_sectors = rq.nr_sectors;
  427. rq.hard_cur_sectors = rq.current_nr_sectors = rq.nr_sectors;
  428. if (args->command_type == IDE_DRIVE_TASK_RAW_WRITE)
  429. rq.cmd_flags |= REQ_RW;
  430. }
  431. rq.special = args;
  432. args->rq = &rq;
  433. return ide_do_drive_cmd(drive, &rq, ide_wait);
  434. }
  435. int ide_raw_taskfile (ide_drive_t *drive, ide_task_t *args, u8 *buf)
  436. {
  437. return ide_diag_taskfile(drive, args, 0, buf);
  438. }
  439. EXPORT_SYMBOL(ide_raw_taskfile);
  440. int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  441. {
  442. ide_task_request_t *req_task;
  443. ide_task_t args;
  444. u8 *outbuf = NULL;
  445. u8 *inbuf = NULL;
  446. task_ioreg_t *argsptr = args.tfRegister;
  447. task_ioreg_t *hobsptr = args.hobRegister;
  448. int err = 0;
  449. int tasksize = sizeof(struct ide_task_request_s);
  450. unsigned int taskin = 0;
  451. unsigned int taskout = 0;
  452. u8 io_32bit = drive->io_32bit;
  453. char __user *buf = (char __user *)arg;
  454. // printk("IDE Taskfile ...\n");
  455. req_task = kzalloc(tasksize, GFP_KERNEL);
  456. if (req_task == NULL) return -ENOMEM;
  457. if (copy_from_user(req_task, buf, tasksize)) {
  458. kfree(req_task);
  459. return -EFAULT;
  460. }
  461. taskout = req_task->out_size;
  462. taskin = req_task->in_size;
  463. if (taskin > 65536 || taskout > 65536) {
  464. err = -EINVAL;
  465. goto abort;
  466. }
  467. if (taskout) {
  468. int outtotal = tasksize;
  469. outbuf = kzalloc(taskout, GFP_KERNEL);
  470. if (outbuf == NULL) {
  471. err = -ENOMEM;
  472. goto abort;
  473. }
  474. if (copy_from_user(outbuf, buf + outtotal, taskout)) {
  475. err = -EFAULT;
  476. goto abort;
  477. }
  478. }
  479. if (taskin) {
  480. int intotal = tasksize + taskout;
  481. inbuf = kzalloc(taskin, GFP_KERNEL);
  482. if (inbuf == NULL) {
  483. err = -ENOMEM;
  484. goto abort;
  485. }
  486. if (copy_from_user(inbuf, buf + intotal, taskin)) {
  487. err = -EFAULT;
  488. goto abort;
  489. }
  490. }
  491. memset(&args, 0, sizeof(ide_task_t));
  492. memcpy(argsptr, req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
  493. memcpy(hobsptr, req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE);
  494. args.tf_in_flags = req_task->in_flags;
  495. args.tf_out_flags = req_task->out_flags;
  496. args.data_phase = req_task->data_phase;
  497. args.command_type = req_task->req_cmd;
  498. drive->io_32bit = 0;
  499. switch(req_task->data_phase) {
  500. case TASKFILE_OUT_DMAQ:
  501. case TASKFILE_OUT_DMA:
  502. err = ide_diag_taskfile(drive, &args, taskout, outbuf);
  503. break;
  504. case TASKFILE_IN_DMAQ:
  505. case TASKFILE_IN_DMA:
  506. err = ide_diag_taskfile(drive, &args, taskin, inbuf);
  507. break;
  508. case TASKFILE_MULTI_OUT:
  509. if (!drive->mult_count) {
  510. /* (hs): give up if multcount is not set */
  511. printk(KERN_ERR "%s: %s Multimode Write " \
  512. "multcount is not set\n",
  513. drive->name, __FUNCTION__);
  514. err = -EPERM;
  515. goto abort;
  516. }
  517. /* fall through */
  518. case TASKFILE_OUT:
  519. args.prehandler = &pre_task_out_intr;
  520. args.handler = &task_out_intr;
  521. err = ide_diag_taskfile(drive, &args, taskout, outbuf);
  522. break;
  523. case TASKFILE_MULTI_IN:
  524. if (!drive->mult_count) {
  525. /* (hs): give up if multcount is not set */
  526. printk(KERN_ERR "%s: %s Multimode Read failure " \
  527. "multcount is not set\n",
  528. drive->name, __FUNCTION__);
  529. err = -EPERM;
  530. goto abort;
  531. }
  532. /* fall through */
  533. case TASKFILE_IN:
  534. args.handler = &task_in_intr;
  535. err = ide_diag_taskfile(drive, &args, taskin, inbuf);
  536. break;
  537. case TASKFILE_NO_DATA:
  538. args.handler = &task_no_data_intr;
  539. err = ide_diag_taskfile(drive, &args, 0, NULL);
  540. break;
  541. default:
  542. err = -EFAULT;
  543. goto abort;
  544. }
  545. memcpy(req_task->io_ports, &(args.tfRegister), HDIO_DRIVE_TASK_HDR_SIZE);
  546. memcpy(req_task->hob_ports, &(args.hobRegister), HDIO_DRIVE_HOB_HDR_SIZE);
  547. req_task->in_flags = args.tf_in_flags;
  548. req_task->out_flags = args.tf_out_flags;
  549. if (copy_to_user(buf, req_task, tasksize)) {
  550. err = -EFAULT;
  551. goto abort;
  552. }
  553. if (taskout) {
  554. int outtotal = tasksize;
  555. if (copy_to_user(buf + outtotal, outbuf, taskout)) {
  556. err = -EFAULT;
  557. goto abort;
  558. }
  559. }
  560. if (taskin) {
  561. int intotal = tasksize + taskout;
  562. if (copy_to_user(buf + intotal, inbuf, taskin)) {
  563. err = -EFAULT;
  564. goto abort;
  565. }
  566. }
  567. abort:
  568. kfree(req_task);
  569. kfree(outbuf);
  570. kfree(inbuf);
  571. // printk("IDE Taskfile ioctl ended. rc = %i\n", err);
  572. drive->io_32bit = io_32bit;
  573. return err;
  574. }
  575. int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
  576. {
  577. struct request rq;
  578. u8 buffer[4];
  579. if (!buf)
  580. buf = buffer;
  581. memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
  582. ide_init_drive_cmd(&rq);
  583. rq.buffer = buf;
  584. *buf++ = cmd;
  585. *buf++ = nsect;
  586. *buf++ = feature;
  587. *buf++ = sectors;
  588. return ide_do_drive_cmd(drive, &rq, ide_wait);
  589. }
  590. /*
  591. * FIXME : this needs to map into at taskfile. <andre@linux-ide.org>
  592. */
  593. int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  594. {
  595. int err = 0;
  596. u8 args[4], *argbuf = args;
  597. u8 xfer_rate = 0;
  598. int argsize = 4;
  599. ide_task_t tfargs;
  600. if (NULL == (void *) arg) {
  601. struct request rq;
  602. ide_init_drive_cmd(&rq);
  603. return ide_do_drive_cmd(drive, &rq, ide_wait);
  604. }
  605. if (copy_from_user(args, (void __user *)arg, 4))
  606. return -EFAULT;
  607. memset(&tfargs, 0, sizeof(ide_task_t));
  608. tfargs.tfRegister[IDE_FEATURE_OFFSET] = args[2];
  609. tfargs.tfRegister[IDE_NSECTOR_OFFSET] = args[3];
  610. tfargs.tfRegister[IDE_SECTOR_OFFSET] = args[1];
  611. tfargs.tfRegister[IDE_LCYL_OFFSET] = 0x00;
  612. tfargs.tfRegister[IDE_HCYL_OFFSET] = 0x00;
  613. tfargs.tfRegister[IDE_SELECT_OFFSET] = 0x00;
  614. tfargs.tfRegister[IDE_COMMAND_OFFSET] = args[0];
  615. if (args[3]) {
  616. argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
  617. argbuf = kzalloc(argsize, GFP_KERNEL);
  618. if (argbuf == NULL)
  619. return -ENOMEM;
  620. }
  621. if (set_transfer(drive, &tfargs)) {
  622. xfer_rate = args[1];
  623. if (ide_ata66_check(drive, &tfargs))
  624. goto abort;
  625. }
  626. err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
  627. if (!err && xfer_rate) {
  628. /* active-retuning-calls future */
  629. ide_set_xfer_rate(drive, xfer_rate);
  630. ide_driveid_update(drive);
  631. }
  632. abort:
  633. if (copy_to_user((void __user *)arg, argbuf, argsize))
  634. err = -EFAULT;
  635. if (argsize > 4)
  636. kfree(argbuf);
  637. return err;
  638. }
  639. static int ide_wait_cmd_task(ide_drive_t *drive, u8 *buf)
  640. {
  641. struct request rq;
  642. ide_init_drive_cmd(&rq);
  643. rq.cmd_type = REQ_TYPE_ATA_TASK;
  644. rq.buffer = buf;
  645. return ide_do_drive_cmd(drive, &rq, ide_wait);
  646. }
  647. /*
  648. * FIXME : this needs to map into at taskfile. <andre@linux-ide.org>
  649. */
  650. int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  651. {
  652. void __user *p = (void __user *)arg;
  653. int err = 0;
  654. u8 args[7], *argbuf = args;
  655. int argsize = 7;
  656. if (copy_from_user(args, p, 7))
  657. return -EFAULT;
  658. err = ide_wait_cmd_task(drive, argbuf);
  659. if (copy_to_user(p, argbuf, argsize))
  660. err = -EFAULT;
  661. return err;
  662. }
  663. /*
  664. * NOTICE: This is additions from IBM to provide a discrete interface,
  665. * for selective taskregister access operations. Nice JOB Klaus!!!
  666. * Glad to be able to work and co-develop this with you and IBM.
  667. */
  668. ide_startstop_t flagged_taskfile (ide_drive_t *drive, ide_task_t *task)
  669. {
  670. ide_hwif_t *hwif = HWIF(drive);
  671. task_struct_t *taskfile = (task_struct_t *) task->tfRegister;
  672. hob_struct_t *hobfile = (hob_struct_t *) task->hobRegister;
  673. if (task->data_phase == TASKFILE_MULTI_IN ||
  674. task->data_phase == TASKFILE_MULTI_OUT) {
  675. if (!drive->mult_count) {
  676. printk(KERN_ERR "%s: multimode not set!\n", drive->name);
  677. return ide_stopped;
  678. }
  679. }
  680. /*
  681. * (ks) Check taskfile in flags.
  682. * If set, then execute as it is defined.
  683. * If not set, then define default settings.
  684. * The default values are:
  685. * read all taskfile registers (except data)
  686. * read the hob registers (sector, nsector, lcyl, hcyl)
  687. */
  688. if (task->tf_in_flags.all == 0) {
  689. task->tf_in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
  690. if (drive->addressing == 1)
  691. task->tf_in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
  692. }
  693. /* ALL Command Block Executions SHALL clear nIEN, unless otherwise */
  694. if (IDE_CONTROL_REG)
  695. /* clear nIEN */
  696. hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
  697. SELECT_MASK(drive, 0);
  698. if (task->tf_out_flags.b.data) {
  699. u16 data = taskfile->data + (hobfile->data << 8);
  700. hwif->OUTW(data, IDE_DATA_REG);
  701. }
  702. /* (ks) send hob registers first */
  703. if (task->tf_out_flags.b.nsector_hob)
  704. hwif->OUTB(hobfile->sector_count, IDE_NSECTOR_REG);
  705. if (task->tf_out_flags.b.sector_hob)
  706. hwif->OUTB(hobfile->sector_number, IDE_SECTOR_REG);
  707. if (task->tf_out_flags.b.lcyl_hob)
  708. hwif->OUTB(hobfile->low_cylinder, IDE_LCYL_REG);
  709. if (task->tf_out_flags.b.hcyl_hob)
  710. hwif->OUTB(hobfile->high_cylinder, IDE_HCYL_REG);
  711. /* (ks) Send now the standard registers */
  712. if (task->tf_out_flags.b.error_feature)
  713. hwif->OUTB(taskfile->feature, IDE_FEATURE_REG);
  714. /* refers to number of sectors to transfer */
  715. if (task->tf_out_flags.b.nsector)
  716. hwif->OUTB(taskfile->sector_count, IDE_NSECTOR_REG);
  717. /* refers to sector offset or start sector */
  718. if (task->tf_out_flags.b.sector)
  719. hwif->OUTB(taskfile->sector_number, IDE_SECTOR_REG);
  720. if (task->tf_out_flags.b.lcyl)
  721. hwif->OUTB(taskfile->low_cylinder, IDE_LCYL_REG);
  722. if (task->tf_out_flags.b.hcyl)
  723. hwif->OUTB(taskfile->high_cylinder, IDE_HCYL_REG);
  724. /*
  725. * (ks) In the flagged taskfile approch, we will use all specified
  726. * registers and the register value will not be changed, except the
  727. * select bit (master/slave) in the drive_head register. We must make
  728. * sure that the desired drive is selected.
  729. */
  730. hwif->OUTB(taskfile->device_head | drive->select.all, IDE_SELECT_REG);
  731. switch(task->data_phase) {
  732. case TASKFILE_OUT_DMAQ:
  733. case TASKFILE_OUT_DMA:
  734. case TASKFILE_IN_DMAQ:
  735. case TASKFILE_IN_DMA:
  736. hwif->dma_setup(drive);
  737. hwif->dma_exec_cmd(drive, taskfile->command);
  738. hwif->dma_start(drive);
  739. break;
  740. default:
  741. if (task->handler == NULL)
  742. return ide_stopped;
  743. /* Issue the command */
  744. if (task->prehandler) {
  745. hwif->OUTBSYNC(drive, taskfile->command, IDE_COMMAND_REG);
  746. ndelay(400); /* FIXME */
  747. return task->prehandler(drive, task->rq);
  748. }
  749. ide_execute_command(drive, taskfile->command, task->handler, WAIT_WORSTCASE, NULL);
  750. }
  751. return ide_started;
  752. }