ide-taskfile.c 17 KB

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