ide-taskfile.c 22 KB

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