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