ide-taskfile.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. /*
  2. * Copyright (C) 2000-2002 Michael Cornwell <cornwell@acm.org>
  3. * Copyright (C) 2000-2002 Andre Hedrick <andre@linux-ide.org>
  4. * Copyright (C) 2001-2002 Klaus Smolin
  5. * IBM Storage Technology Division
  6. * Copyright (C) 2003-2004, 2007 Bartlomiej Zolnierkiewicz
  7. *
  8. * The big the bad and the ugly.
  9. */
  10. #include <linux/types.h>
  11. #include <linux/string.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/errno.h>
  16. #include <linux/slab.h>
  17. #include <linux/delay.h>
  18. #include <linux/hdreg.h>
  19. #include <linux/ide.h>
  20. #include <linux/scatterlist.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/io.h>
  23. void ide_tf_dump(const char *s, struct ide_taskfile *tf)
  24. {
  25. #ifdef DEBUG
  26. printk("%s: tf: feat 0x%02x nsect 0x%02x lbal 0x%02x "
  27. "lbam 0x%02x lbah 0x%02x dev 0x%02x cmd 0x%02x\n",
  28. s, tf->feature, tf->nsect, tf->lbal,
  29. tf->lbam, tf->lbah, tf->device, tf->command);
  30. printk("%s: hob: nsect 0x%02x lbal 0x%02x "
  31. "lbam 0x%02x lbah 0x%02x\n",
  32. s, tf->hob_nsect, tf->hob_lbal,
  33. tf->hob_lbam, tf->hob_lbah);
  34. #endif
  35. }
  36. int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
  37. {
  38. struct ide_cmd cmd;
  39. memset(&cmd, 0, sizeof(cmd));
  40. cmd.tf.nsect = 0x01;
  41. if (drive->media == ide_disk)
  42. cmd.tf.command = ATA_CMD_ID_ATA;
  43. else
  44. cmd.tf.command = ATA_CMD_ID_ATAPI;
  45. cmd.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
  46. cmd.protocol = ATA_PROT_PIO;
  47. return ide_raw_taskfile(drive, &cmd, buf, 1);
  48. }
  49. static ide_startstop_t task_no_data_intr(ide_drive_t *);
  50. static ide_startstop_t pre_task_out_intr(ide_drive_t *, struct ide_cmd *);
  51. static ide_startstop_t task_in_intr(ide_drive_t *);
  52. ide_startstop_t do_rw_taskfile(ide_drive_t *drive, struct ide_cmd *orig_cmd)
  53. {
  54. ide_hwif_t *hwif = drive->hwif;
  55. struct ide_cmd *cmd = &hwif->cmd;
  56. struct ide_taskfile *tf = &cmd->tf;
  57. ide_handler_t *handler = NULL;
  58. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  59. const struct ide_dma_ops *dma_ops = hwif->dma_ops;
  60. if (orig_cmd->protocol == ATA_PROT_PIO &&
  61. (orig_cmd->tf_flags & IDE_TFLAG_MULTI_PIO) &&
  62. drive->mult_count == 0) {
  63. printk(KERN_ERR "%s: multimode not set!\n", drive->name);
  64. return ide_stopped;
  65. }
  66. if (orig_cmd->ftf_flags & IDE_FTFLAG_FLAGGED)
  67. orig_cmd->ftf_flags |= IDE_FTFLAG_SET_IN_FLAGS;
  68. memcpy(cmd, orig_cmd, sizeof(*cmd));
  69. if ((cmd->tf_flags & IDE_TFLAG_DMA_PIO_FALLBACK) == 0) {
  70. ide_tf_dump(drive->name, tf);
  71. tp_ops->set_irq(hwif, 1);
  72. SELECT_MASK(drive, 0);
  73. tp_ops->tf_load(drive, cmd);
  74. }
  75. switch (cmd->protocol) {
  76. case ATA_PROT_PIO:
  77. if (cmd->tf_flags & IDE_TFLAG_WRITE) {
  78. tp_ops->exec_command(hwif, tf->command);
  79. ndelay(400); /* FIXME */
  80. return pre_task_out_intr(drive, cmd);
  81. }
  82. handler = task_in_intr;
  83. /* fall-through */
  84. case ATA_PROT_NODATA:
  85. if (handler == NULL)
  86. handler = task_no_data_intr;
  87. ide_execute_command(drive, tf->command, handler,
  88. WAIT_WORSTCASE, NULL);
  89. return ide_started;
  90. default:
  91. if ((drive->dev_flags & IDE_DFLAG_USING_DMA) == 0 ||
  92. ide_build_sglist(drive, hwif->rq) == 0 ||
  93. dma_ops->dma_setup(drive))
  94. return ide_stopped;
  95. dma_ops->dma_exec_cmd(drive, tf->command);
  96. dma_ops->dma_start(drive);
  97. return ide_started;
  98. }
  99. }
  100. EXPORT_SYMBOL_GPL(do_rw_taskfile);
  101. static ide_startstop_t task_no_data_intr(ide_drive_t *drive)
  102. {
  103. ide_hwif_t *hwif = drive->hwif;
  104. struct ide_cmd *cmd = &hwif->cmd;
  105. struct ide_taskfile *tf = &cmd->tf;
  106. int custom = (cmd->tf_flags & IDE_TFLAG_CUSTOM_HANDLER) ? 1 : 0;
  107. int retries = (custom && tf->command == ATA_CMD_INIT_DEV_PARAMS) ? 5 : 1;
  108. u8 stat;
  109. local_irq_enable_in_hardirq();
  110. while (1) {
  111. stat = hwif->tp_ops->read_status(hwif);
  112. if ((stat & ATA_BUSY) == 0 || retries-- == 0)
  113. break;
  114. udelay(10);
  115. };
  116. if (!OK_STAT(stat, ATA_DRDY, BAD_STAT)) {
  117. if (custom && tf->command == ATA_CMD_SET_MULTI) {
  118. drive->mult_req = drive->mult_count = 0;
  119. drive->special.b.recalibrate = 1;
  120. (void)ide_dump_status(drive, __func__, stat);
  121. return ide_stopped;
  122. } else if (custom && tf->command == ATA_CMD_INIT_DEV_PARAMS) {
  123. if ((stat & (ATA_ERR | ATA_DRQ)) == 0) {
  124. ide_set_handler(drive, &task_no_data_intr,
  125. WAIT_WORSTCASE, NULL);
  126. return ide_started;
  127. }
  128. }
  129. return ide_error(drive, "task_no_data_intr", stat);
  130. }
  131. if (custom && tf->command == ATA_CMD_IDLEIMMEDIATE) {
  132. hwif->tp_ops->tf_read(drive, cmd);
  133. if (tf->lbal != 0xc4) {
  134. printk(KERN_ERR "%s: head unload failed!\n",
  135. drive->name);
  136. ide_tf_dump(drive->name, tf);
  137. } else
  138. drive->dev_flags |= IDE_DFLAG_PARKED;
  139. } else if (custom && tf->command == ATA_CMD_SET_MULTI)
  140. drive->mult_count = drive->mult_req;
  141. if (custom == 0 || tf->command == ATA_CMD_IDLEIMMEDIATE) {
  142. struct request *rq = hwif->rq;
  143. u8 err = ide_read_error(drive);
  144. if (blk_pm_request(rq))
  145. ide_complete_pm_rq(drive, rq);
  146. else {
  147. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
  148. ide_complete_cmd(drive, cmd, stat, err);
  149. ide_complete_rq(drive, err);
  150. }
  151. }
  152. return ide_stopped;
  153. }
  154. static u8 wait_drive_not_busy(ide_drive_t *drive)
  155. {
  156. ide_hwif_t *hwif = drive->hwif;
  157. int retries;
  158. u8 stat;
  159. /*
  160. * Last sector was transfered, wait until device is ready. This can
  161. * take up to 6 ms on some ATAPI devices, so we will wait max 10 ms.
  162. */
  163. for (retries = 0; retries < 1000; retries++) {
  164. stat = hwif->tp_ops->read_status(hwif);
  165. if (stat & ATA_BUSY)
  166. udelay(10);
  167. else
  168. break;
  169. }
  170. if (stat & ATA_BUSY)
  171. printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
  172. return stat;
  173. }
  174. static void ide_pio_sector(ide_drive_t *drive, struct ide_cmd *cmd,
  175. unsigned int write)
  176. {
  177. ide_hwif_t *hwif = drive->hwif;
  178. struct scatterlist *sg = hwif->sg_table;
  179. struct scatterlist *cursg = cmd->cursg;
  180. struct page *page;
  181. #ifdef CONFIG_HIGHMEM
  182. unsigned long flags;
  183. #endif
  184. unsigned int offset;
  185. u8 *buf;
  186. cursg = cmd->cursg;
  187. if (!cursg) {
  188. cursg = sg;
  189. cmd->cursg = sg;
  190. }
  191. page = sg_page(cursg);
  192. offset = cursg->offset + cmd->cursg_ofs * SECTOR_SIZE;
  193. /* get the current page and offset */
  194. page = nth_page(page, (offset >> PAGE_SHIFT));
  195. offset %= PAGE_SIZE;
  196. #ifdef CONFIG_HIGHMEM
  197. local_irq_save(flags);
  198. #endif
  199. buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
  200. cmd->nleft--;
  201. cmd->cursg_ofs++;
  202. if ((cmd->cursg_ofs * SECTOR_SIZE) == cursg->length) {
  203. cmd->cursg = sg_next(cmd->cursg);
  204. cmd->cursg_ofs = 0;
  205. }
  206. /* do the actual data transfer */
  207. if (write)
  208. hwif->tp_ops->output_data(drive, cmd, buf, SECTOR_SIZE);
  209. else
  210. hwif->tp_ops->input_data(drive, cmd, buf, SECTOR_SIZE);
  211. kunmap_atomic(buf, KM_BIO_SRC_IRQ);
  212. #ifdef CONFIG_HIGHMEM
  213. local_irq_restore(flags);
  214. #endif
  215. }
  216. static void ide_pio_multi(ide_drive_t *drive, struct ide_cmd *cmd,
  217. unsigned int write)
  218. {
  219. unsigned int nsect;
  220. nsect = min_t(unsigned int, cmd->nleft, drive->mult_count);
  221. while (nsect--)
  222. ide_pio_sector(drive, cmd, write);
  223. }
  224. static void ide_pio_datablock(ide_drive_t *drive, struct ide_cmd *cmd,
  225. unsigned int write)
  226. {
  227. u8 saved_io_32bit = drive->io_32bit;
  228. if (cmd->tf_flags & IDE_TFLAG_FS)
  229. cmd->rq->errors = 0;
  230. if (cmd->tf_flags & IDE_TFLAG_IO_16BIT)
  231. drive->io_32bit = 0;
  232. touch_softlockup_watchdog();
  233. if (cmd->tf_flags & IDE_TFLAG_MULTI_PIO)
  234. ide_pio_multi(drive, cmd, write);
  235. else
  236. ide_pio_sector(drive, cmd, write);
  237. drive->io_32bit = saved_io_32bit;
  238. }
  239. static ide_startstop_t task_error(ide_drive_t *drive, struct ide_cmd *cmd,
  240. const char *s, u8 stat)
  241. {
  242. if (cmd->tf_flags & IDE_TFLAG_FS) {
  243. int sectors = cmd->nsect - cmd->nleft;
  244. if (cmd->protocol == ATA_PROT_PIO &&
  245. ((cmd->tf_flags & IDE_TFLAG_WRITE) || cmd->nleft == 0)) {
  246. if (cmd->tf_flags & IDE_TFLAG_MULTI_PIO)
  247. sectors -= drive->mult_count;
  248. else
  249. sectors--;
  250. }
  251. if (sectors > 0)
  252. ide_end_request(drive, 1, sectors);
  253. }
  254. return ide_error(drive, s, stat);
  255. }
  256. void ide_finish_cmd(ide_drive_t *drive, struct ide_cmd *cmd, u8 stat)
  257. {
  258. if ((cmd->tf_flags & IDE_TFLAG_FS) == 0) {
  259. u8 err = ide_read_error(drive);
  260. ide_complete_cmd(drive, cmd, stat, err);
  261. ide_complete_rq(drive, err);
  262. return;
  263. }
  264. ide_end_request(drive, 1, cmd->rq->nr_sectors);
  265. }
  266. /*
  267. * We got an interrupt on a task_in case, but no errors and no DRQ.
  268. *
  269. * It might be a spurious irq (shared irq), but it might be a
  270. * command that had no output.
  271. */
  272. static ide_startstop_t task_in_unexpected(ide_drive_t *drive,
  273. struct ide_cmd *cmd, u8 stat)
  274. {
  275. /* Command all done? */
  276. if (OK_STAT(stat, ATA_DRDY, ATA_BUSY)) {
  277. ide_finish_cmd(drive, cmd, stat);
  278. return ide_stopped;
  279. }
  280. /* Assume it was a spurious irq */
  281. ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
  282. return ide_started;
  283. }
  284. /*
  285. * Handler for command with PIO data-in phase (Read/Read Multiple).
  286. */
  287. static ide_startstop_t task_in_intr(ide_drive_t *drive)
  288. {
  289. ide_hwif_t *hwif = drive->hwif;
  290. struct ide_cmd *cmd = &drive->hwif->cmd;
  291. u8 stat = hwif->tp_ops->read_status(hwif);
  292. /* Error? */
  293. if (stat & ATA_ERR)
  294. return task_error(drive, cmd, __func__, stat);
  295. /* Didn't want any data? Odd. */
  296. if ((stat & ATA_DRQ) == 0)
  297. return task_in_unexpected(drive, cmd, stat);
  298. ide_pio_datablock(drive, cmd, 0);
  299. /* Are we done? Check status and finish transfer. */
  300. if (cmd->nleft == 0) {
  301. stat = wait_drive_not_busy(drive);
  302. if (!OK_STAT(stat, 0, BAD_STAT))
  303. return task_error(drive, cmd, __func__, stat);
  304. ide_finish_cmd(drive, cmd, stat);
  305. return ide_stopped;
  306. }
  307. /* Still data left to transfer. */
  308. ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
  309. return ide_started;
  310. }
  311. /*
  312. * Handler for command with PIO data-out phase (Write/Write Multiple).
  313. */
  314. static ide_startstop_t task_out_intr (ide_drive_t *drive)
  315. {
  316. ide_hwif_t *hwif = drive->hwif;
  317. struct ide_cmd *cmd = &drive->hwif->cmd;
  318. u8 stat = hwif->tp_ops->read_status(hwif);
  319. if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
  320. return task_error(drive, cmd, __func__, stat);
  321. /* Deal with unexpected ATA data phase. */
  322. if (((stat & ATA_DRQ) == 0) ^ (cmd->nleft == 0))
  323. return task_error(drive, cmd, __func__, stat);
  324. if (cmd->nleft == 0) {
  325. ide_finish_cmd(drive, cmd, stat);
  326. return ide_stopped;
  327. }
  328. /* Still data left to transfer. */
  329. ide_pio_datablock(drive, cmd, 1);
  330. ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
  331. return ide_started;
  332. }
  333. static ide_startstop_t pre_task_out_intr(ide_drive_t *drive,
  334. struct ide_cmd *cmd)
  335. {
  336. ide_startstop_t startstop;
  337. if (ide_wait_stat(&startstop, drive, ATA_DRQ,
  338. drive->bad_wstat, WAIT_DRQ)) {
  339. printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
  340. drive->name,
  341. (cmd->tf_flags & IDE_TFLAG_MULTI_PIO) ? "MULT" : "",
  342. (drive->dev_flags & IDE_DFLAG_LBA48) ? "_EXT" : "");
  343. return startstop;
  344. }
  345. if ((drive->dev_flags & IDE_DFLAG_UNMASK) == 0)
  346. local_irq_disable();
  347. ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
  348. ide_pio_datablock(drive, cmd, 1);
  349. return ide_started;
  350. }
  351. int ide_raw_taskfile(ide_drive_t *drive, struct ide_cmd *cmd, u8 *buf,
  352. u16 nsect)
  353. {
  354. struct request *rq;
  355. int error;
  356. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  357. rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
  358. rq->buffer = buf;
  359. /*
  360. * (ks) We transfer currently only whole sectors.
  361. * This is suffient for now. But, it would be great,
  362. * if we would find a solution to transfer any size.
  363. * To support special commands like READ LONG.
  364. */
  365. rq->hard_nr_sectors = rq->nr_sectors = nsect;
  366. rq->hard_cur_sectors = rq->current_nr_sectors = nsect;
  367. if (cmd->tf_flags & IDE_TFLAG_WRITE)
  368. rq->cmd_flags |= REQ_RW;
  369. rq->special = cmd;
  370. cmd->rq = rq;
  371. error = blk_execute_rq(drive->queue, NULL, rq, 0);
  372. blk_put_request(rq);
  373. return error;
  374. }
  375. EXPORT_SYMBOL(ide_raw_taskfile);
  376. int ide_no_data_taskfile(ide_drive_t *drive, struct ide_cmd *cmd)
  377. {
  378. cmd->protocol = ATA_PROT_NODATA;
  379. return ide_raw_taskfile(drive, cmd, NULL, 0);
  380. }
  381. EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
  382. #ifdef CONFIG_IDE_TASK_IOCTL
  383. int ide_taskfile_ioctl(ide_drive_t *drive, unsigned long arg)
  384. {
  385. ide_task_request_t *req_task;
  386. struct ide_cmd cmd;
  387. u8 *outbuf = NULL;
  388. u8 *inbuf = NULL;
  389. u8 *data_buf = NULL;
  390. int err = 0;
  391. int tasksize = sizeof(struct ide_task_request_s);
  392. unsigned int taskin = 0;
  393. unsigned int taskout = 0;
  394. u16 nsect = 0;
  395. char __user *buf = (char __user *)arg;
  396. // printk("IDE Taskfile ...\n");
  397. req_task = kzalloc(tasksize, GFP_KERNEL);
  398. if (req_task == NULL) return -ENOMEM;
  399. if (copy_from_user(req_task, buf, tasksize)) {
  400. kfree(req_task);
  401. return -EFAULT;
  402. }
  403. taskout = req_task->out_size;
  404. taskin = req_task->in_size;
  405. if (taskin > 65536 || taskout > 65536) {
  406. err = -EINVAL;
  407. goto abort;
  408. }
  409. if (taskout) {
  410. int outtotal = tasksize;
  411. outbuf = kzalloc(taskout, GFP_KERNEL);
  412. if (outbuf == NULL) {
  413. err = -ENOMEM;
  414. goto abort;
  415. }
  416. if (copy_from_user(outbuf, buf + outtotal, taskout)) {
  417. err = -EFAULT;
  418. goto abort;
  419. }
  420. }
  421. if (taskin) {
  422. int intotal = tasksize + taskout;
  423. inbuf = kzalloc(taskin, GFP_KERNEL);
  424. if (inbuf == NULL) {
  425. err = -ENOMEM;
  426. goto abort;
  427. }
  428. if (copy_from_user(inbuf, buf + intotal, taskin)) {
  429. err = -EFAULT;
  430. goto abort;
  431. }
  432. }
  433. memset(&cmd, 0, sizeof(cmd));
  434. memcpy(&cmd.tf_array[0], req_task->hob_ports,
  435. HDIO_DRIVE_HOB_HDR_SIZE - 2);
  436. memcpy(&cmd.tf_array[6], req_task->io_ports,
  437. HDIO_DRIVE_TASK_HDR_SIZE);
  438. cmd.tf_flags = IDE_TFLAG_IO_16BIT | IDE_TFLAG_DEVICE |
  439. IDE_TFLAG_IN_TF;
  440. if (drive->dev_flags & IDE_DFLAG_LBA48)
  441. cmd.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_IN_HOB);
  442. if (req_task->out_flags.all) {
  443. cmd.ftf_flags |= IDE_FTFLAG_FLAGGED;
  444. if (req_task->out_flags.b.data)
  445. cmd.ftf_flags |= IDE_FTFLAG_OUT_DATA;
  446. if (req_task->out_flags.b.nsector_hob)
  447. cmd.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
  448. if (req_task->out_flags.b.sector_hob)
  449. cmd.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
  450. if (req_task->out_flags.b.lcyl_hob)
  451. cmd.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
  452. if (req_task->out_flags.b.hcyl_hob)
  453. cmd.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
  454. if (req_task->out_flags.b.error_feature)
  455. cmd.tf_flags |= IDE_TFLAG_OUT_FEATURE;
  456. if (req_task->out_flags.b.nsector)
  457. cmd.tf_flags |= IDE_TFLAG_OUT_NSECT;
  458. if (req_task->out_flags.b.sector)
  459. cmd.tf_flags |= IDE_TFLAG_OUT_LBAL;
  460. if (req_task->out_flags.b.lcyl)
  461. cmd.tf_flags |= IDE_TFLAG_OUT_LBAM;
  462. if (req_task->out_flags.b.hcyl)
  463. cmd.tf_flags |= IDE_TFLAG_OUT_LBAH;
  464. } else {
  465. cmd.tf_flags |= IDE_TFLAG_OUT_TF;
  466. if (cmd.tf_flags & IDE_TFLAG_LBA48)
  467. cmd.tf_flags |= IDE_TFLAG_OUT_HOB;
  468. }
  469. if (req_task->in_flags.b.data)
  470. cmd.ftf_flags |= IDE_FTFLAG_IN_DATA;
  471. if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE) {
  472. /* fixup data phase if needed */
  473. if (req_task->data_phase == TASKFILE_IN_DMAQ ||
  474. req_task->data_phase == TASKFILE_IN_DMA)
  475. cmd.tf_flags |= IDE_TFLAG_WRITE;
  476. }
  477. cmd.protocol = ATA_PROT_DMA;
  478. switch (req_task->data_phase) {
  479. case TASKFILE_MULTI_OUT:
  480. if (!drive->mult_count) {
  481. /* (hs): give up if multcount is not set */
  482. printk(KERN_ERR "%s: %s Multimode Write " \
  483. "multcount is not set\n",
  484. drive->name, __func__);
  485. err = -EPERM;
  486. goto abort;
  487. }
  488. cmd.tf_flags |= IDE_TFLAG_MULTI_PIO;
  489. /* fall through */
  490. case TASKFILE_OUT:
  491. cmd.protocol = ATA_PROT_PIO;
  492. /* fall through */
  493. case TASKFILE_OUT_DMAQ:
  494. case TASKFILE_OUT_DMA:
  495. cmd.tf_flags |= IDE_TFLAG_WRITE;
  496. nsect = taskout / SECTOR_SIZE;
  497. data_buf = outbuf;
  498. break;
  499. case TASKFILE_MULTI_IN:
  500. if (!drive->mult_count) {
  501. /* (hs): give up if multcount is not set */
  502. printk(KERN_ERR "%s: %s Multimode Read failure " \
  503. "multcount is not set\n",
  504. drive->name, __func__);
  505. err = -EPERM;
  506. goto abort;
  507. }
  508. cmd.tf_flags |= IDE_TFLAG_MULTI_PIO;
  509. /* fall through */
  510. case TASKFILE_IN:
  511. cmd.protocol = ATA_PROT_PIO;
  512. /* fall through */
  513. case TASKFILE_IN_DMAQ:
  514. case TASKFILE_IN_DMA:
  515. nsect = taskin / SECTOR_SIZE;
  516. data_buf = inbuf;
  517. break;
  518. case TASKFILE_NO_DATA:
  519. cmd.protocol = ATA_PROT_NODATA;
  520. break;
  521. default:
  522. err = -EFAULT;
  523. goto abort;
  524. }
  525. if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
  526. nsect = 0;
  527. else if (!nsect) {
  528. nsect = (cmd.tf.hob_nsect << 8) | cmd.tf.nsect;
  529. if (!nsect) {
  530. printk(KERN_ERR "%s: in/out command without data\n",
  531. drive->name);
  532. err = -EFAULT;
  533. goto abort;
  534. }
  535. }
  536. err = ide_raw_taskfile(drive, &cmd, data_buf, nsect);
  537. memcpy(req_task->hob_ports, &cmd.tf_array[0],
  538. HDIO_DRIVE_HOB_HDR_SIZE - 2);
  539. memcpy(req_task->io_ports, &cmd.tf_array[6],
  540. HDIO_DRIVE_TASK_HDR_SIZE);
  541. if ((cmd.ftf_flags & IDE_FTFLAG_SET_IN_FLAGS) &&
  542. req_task->in_flags.all == 0) {
  543. req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
  544. if (drive->dev_flags & IDE_DFLAG_LBA48)
  545. req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
  546. }
  547. if (copy_to_user(buf, req_task, tasksize)) {
  548. err = -EFAULT;
  549. goto abort;
  550. }
  551. if (taskout) {
  552. int outtotal = tasksize;
  553. if (copy_to_user(buf + outtotal, outbuf, taskout)) {
  554. err = -EFAULT;
  555. goto abort;
  556. }
  557. }
  558. if (taskin) {
  559. int intotal = tasksize + taskout;
  560. if (copy_to_user(buf + intotal, inbuf, taskin)) {
  561. err = -EFAULT;
  562. goto abort;
  563. }
  564. }
  565. abort:
  566. kfree(req_task);
  567. kfree(outbuf);
  568. kfree(inbuf);
  569. // printk("IDE Taskfile ioctl ended. rc = %i\n", err);
  570. return err;
  571. }
  572. #endif