ide-taskfile.c 17 KB

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