ide-taskfile.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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. ide_task_t args;
  39. memset(&args, 0, sizeof(ide_task_t));
  40. args.tf.nsect = 0x01;
  41. if (drive->media == ide_disk)
  42. args.tf.command = WIN_IDENTIFY;
  43. else
  44. args.tf.command = WIN_PIDENTIFY;
  45. args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
  46. args.data_phase = TASKFILE_IN;
  47. return ide_raw_taskfile(drive, &args, buf, 1);
  48. }
  49. static ide_startstop_t task_no_data_intr(ide_drive_t *);
  50. static ide_startstop_t set_geometry_intr(ide_drive_t *);
  51. static ide_startstop_t recal_intr(ide_drive_t *);
  52. static ide_startstop_t set_multmode_intr(ide_drive_t *);
  53. static ide_startstop_t pre_task_out_intr(ide_drive_t *, struct request *);
  54. static ide_startstop_t task_in_intr(ide_drive_t *);
  55. ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
  56. {
  57. ide_hwif_t *hwif = HWIF(drive);
  58. struct ide_taskfile *tf = &task->tf;
  59. ide_handler_t *handler = NULL;
  60. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  61. const struct ide_dma_ops *dma_ops = hwif->dma_ops;
  62. if (task->data_phase == TASKFILE_MULTI_IN ||
  63. task->data_phase == TASKFILE_MULTI_OUT) {
  64. if (!drive->mult_count) {
  65. printk(KERN_ERR "%s: multimode not set!\n",
  66. drive->name);
  67. return ide_stopped;
  68. }
  69. }
  70. if (task->tf_flags & IDE_TFLAG_FLAGGED)
  71. task->tf_flags |= IDE_TFLAG_FLAGGED_SET_IN_FLAGS;
  72. if ((task->tf_flags & IDE_TFLAG_DMA_PIO_FALLBACK) == 0) {
  73. ide_tf_dump(drive->name, tf);
  74. tp_ops->set_irq(hwif, 1);
  75. SELECT_MASK(drive, 0);
  76. tp_ops->tf_load(drive, task);
  77. }
  78. switch (task->data_phase) {
  79. case TASKFILE_MULTI_OUT:
  80. case TASKFILE_OUT:
  81. tp_ops->exec_command(hwif, tf->command);
  82. ndelay(400); /* FIXME */
  83. return pre_task_out_intr(drive, task->rq);
  84. case TASKFILE_MULTI_IN:
  85. case TASKFILE_IN:
  86. handler = task_in_intr;
  87. /* fall-through */
  88. case TASKFILE_NO_DATA:
  89. if (handler == NULL)
  90. handler = task_no_data_intr;
  91. /* WIN_{SPECIFY,RESTORE,SETMULT} use custom handlers */
  92. if (task->tf_flags & IDE_TFLAG_CUSTOM_HANDLER) {
  93. switch (tf->command) {
  94. case WIN_SPECIFY: handler = set_geometry_intr; break;
  95. case WIN_RESTORE: handler = recal_intr; break;
  96. case WIN_SETMULT: handler = set_multmode_intr; break;
  97. }
  98. }
  99. ide_execute_command(drive, tf->command, handler,
  100. WAIT_WORSTCASE, NULL);
  101. return ide_started;
  102. default:
  103. if (drive->using_dma == 0 || dma_ops->dma_setup(drive))
  104. return ide_stopped;
  105. dma_ops->dma_exec_cmd(drive, tf->command);
  106. dma_ops->dma_start(drive);
  107. return ide_started;
  108. }
  109. }
  110. EXPORT_SYMBOL_GPL(do_rw_taskfile);
  111. /*
  112. * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
  113. */
  114. static ide_startstop_t set_multmode_intr(ide_drive_t *drive)
  115. {
  116. ide_hwif_t *hwif = drive->hwif;
  117. u8 stat;
  118. local_irq_enable_in_hardirq();
  119. stat = hwif->tp_ops->read_status(hwif);
  120. if (OK_STAT(stat, READY_STAT, BAD_STAT))
  121. drive->mult_count = drive->mult_req;
  122. else {
  123. drive->mult_req = drive->mult_count = 0;
  124. drive->special.b.recalibrate = 1;
  125. (void) ide_dump_status(drive, "set_multmode", stat);
  126. }
  127. return ide_stopped;
  128. }
  129. /*
  130. * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
  131. */
  132. static ide_startstop_t set_geometry_intr(ide_drive_t *drive)
  133. {
  134. ide_hwif_t *hwif = drive->hwif;
  135. int retries = 5;
  136. u8 stat;
  137. local_irq_enable_in_hardirq();
  138. while (1) {
  139. stat = hwif->tp_ops->read_status(hwif);
  140. if ((stat & BUSY_STAT) == 0 || retries-- == 0)
  141. break;
  142. udelay(10);
  143. };
  144. if (OK_STAT(stat, READY_STAT, BAD_STAT))
  145. return ide_stopped;
  146. if (stat & (ERR_STAT|DRQ_STAT))
  147. return ide_error(drive, "set_geometry_intr", stat);
  148. ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
  149. return ide_started;
  150. }
  151. /*
  152. * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
  153. */
  154. static ide_startstop_t recal_intr(ide_drive_t *drive)
  155. {
  156. ide_hwif_t *hwif = drive->hwif;
  157. u8 stat;
  158. local_irq_enable_in_hardirq();
  159. stat = hwif->tp_ops->read_status(hwif);
  160. if (!OK_STAT(stat, READY_STAT, BAD_STAT))
  161. return ide_error(drive, "recal_intr", stat);
  162. return ide_stopped;
  163. }
  164. /*
  165. * Handler for commands without a data phase
  166. */
  167. static ide_startstop_t task_no_data_intr(ide_drive_t *drive)
  168. {
  169. ide_hwif_t *hwif = drive->hwif;
  170. ide_task_t *args = hwif->hwgroup->rq->special;
  171. u8 stat;
  172. local_irq_enable_in_hardirq();
  173. stat = hwif->tp_ops->read_status(hwif);
  174. if (!OK_STAT(stat, READY_STAT, BAD_STAT))
  175. return ide_error(drive, "task_no_data_intr", stat);
  176. /* calls ide_end_drive_cmd */
  177. if (args)
  178. ide_end_drive_cmd(drive, stat, ide_read_error(drive));
  179. return ide_stopped;
  180. }
  181. static u8 wait_drive_not_busy(ide_drive_t *drive)
  182. {
  183. ide_hwif_t *hwif = drive->hwif;
  184. int retries;
  185. u8 stat;
  186. /*
  187. * Last sector was transfered, wait until device is ready. This can
  188. * take up to 6 ms on some ATAPI devices, so we will wait max 10 ms.
  189. */
  190. for (retries = 0; retries < 1000; retries++) {
  191. stat = hwif->tp_ops->read_status(hwif);
  192. if (stat & BUSY_STAT)
  193. udelay(10);
  194. else
  195. break;
  196. }
  197. if (stat & BUSY_STAT)
  198. printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
  199. return stat;
  200. }
  201. static void ide_pio_sector(ide_drive_t *drive, struct request *rq,
  202. unsigned int write)
  203. {
  204. ide_hwif_t *hwif = drive->hwif;
  205. struct scatterlist *sg = hwif->sg_table;
  206. struct scatterlist *cursg = hwif->cursg;
  207. struct page *page;
  208. #ifdef CONFIG_HIGHMEM
  209. unsigned long flags;
  210. #endif
  211. unsigned int offset;
  212. u8 *buf;
  213. cursg = hwif->cursg;
  214. if (!cursg) {
  215. cursg = sg;
  216. hwif->cursg = sg;
  217. }
  218. page = sg_page(cursg);
  219. offset = cursg->offset + hwif->cursg_ofs * SECTOR_SIZE;
  220. /* get the current page and offset */
  221. page = nth_page(page, (offset >> PAGE_SHIFT));
  222. offset %= PAGE_SIZE;
  223. #ifdef CONFIG_HIGHMEM
  224. local_irq_save(flags);
  225. #endif
  226. buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
  227. hwif->nleft--;
  228. hwif->cursg_ofs++;
  229. if ((hwif->cursg_ofs * SECTOR_SIZE) == cursg->length) {
  230. hwif->cursg = sg_next(hwif->cursg);
  231. hwif->cursg_ofs = 0;
  232. }
  233. /* do the actual data transfer */
  234. if (write)
  235. hwif->tp_ops->output_data(drive, rq, buf, SECTOR_SIZE);
  236. else
  237. hwif->tp_ops->input_data(drive, rq, buf, SECTOR_SIZE);
  238. kunmap_atomic(buf, KM_BIO_SRC_IRQ);
  239. #ifdef CONFIG_HIGHMEM
  240. local_irq_restore(flags);
  241. #endif
  242. }
  243. static void ide_pio_multi(ide_drive_t *drive, struct request *rq,
  244. unsigned int write)
  245. {
  246. unsigned int nsect;
  247. nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
  248. while (nsect--)
  249. ide_pio_sector(drive, rq, write);
  250. }
  251. static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
  252. unsigned int write)
  253. {
  254. u8 saved_io_32bit = drive->io_32bit;
  255. if (rq->bio) /* fs request */
  256. rq->errors = 0;
  257. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
  258. ide_task_t *task = rq->special;
  259. if (task->tf_flags & IDE_TFLAG_IO_16BIT)
  260. drive->io_32bit = 0;
  261. }
  262. touch_softlockup_watchdog();
  263. switch (drive->hwif->data_phase) {
  264. case TASKFILE_MULTI_IN:
  265. case TASKFILE_MULTI_OUT:
  266. ide_pio_multi(drive, rq, write);
  267. break;
  268. default:
  269. ide_pio_sector(drive, rq, write);
  270. break;
  271. }
  272. drive->io_32bit = saved_io_32bit;
  273. }
  274. static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
  275. const char *s, u8 stat)
  276. {
  277. if (rq->bio) {
  278. ide_hwif_t *hwif = drive->hwif;
  279. int sectors = hwif->nsect - hwif->nleft;
  280. switch (hwif->data_phase) {
  281. case TASKFILE_IN:
  282. if (hwif->nleft)
  283. break;
  284. /* fall through */
  285. case TASKFILE_OUT:
  286. sectors--;
  287. break;
  288. case TASKFILE_MULTI_IN:
  289. if (hwif->nleft)
  290. break;
  291. /* fall through */
  292. case TASKFILE_MULTI_OUT:
  293. sectors -= drive->mult_count;
  294. default:
  295. break;
  296. }
  297. if (sectors > 0) {
  298. ide_driver_t *drv;
  299. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  300. drv->end_request(drive, 1, sectors);
  301. }
  302. }
  303. return ide_error(drive, s, stat);
  304. }
  305. void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
  306. {
  307. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
  308. u8 err = ide_read_error(drive);
  309. ide_end_drive_cmd(drive, stat, err);
  310. return;
  311. }
  312. if (rq->rq_disk) {
  313. ide_driver_t *drv;
  314. drv = *(ide_driver_t **)rq->rq_disk->private_data;;
  315. drv->end_request(drive, 1, rq->nr_sectors);
  316. } else
  317. ide_end_request(drive, 1, rq->nr_sectors);
  318. }
  319. /*
  320. * We got an interrupt on a task_in case, but no errors and no DRQ.
  321. *
  322. * It might be a spurious irq (shared irq), but it might be a
  323. * command that had no output.
  324. */
  325. static ide_startstop_t task_in_unexpected(ide_drive_t *drive, struct request *rq, u8 stat)
  326. {
  327. /* Command all done? */
  328. if (OK_STAT(stat, READY_STAT, BUSY_STAT)) {
  329. task_end_request(drive, rq, stat);
  330. return ide_stopped;
  331. }
  332. /* Assume it was a spurious irq */
  333. ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
  334. return ide_started;
  335. }
  336. /*
  337. * Handler for command with PIO data-in phase (Read/Read Multiple).
  338. */
  339. static ide_startstop_t task_in_intr(ide_drive_t *drive)
  340. {
  341. ide_hwif_t *hwif = drive->hwif;
  342. struct request *rq = hwif->hwgroup->rq;
  343. u8 stat = hwif->tp_ops->read_status(hwif);
  344. /* Error? */
  345. if (stat & ERR_STAT)
  346. return task_error(drive, rq, __func__, stat);
  347. /* Didn't want any data? Odd. */
  348. if (!(stat & DRQ_STAT))
  349. return task_in_unexpected(drive, rq, stat);
  350. ide_pio_datablock(drive, rq, 0);
  351. /* Are we done? Check status and finish transfer. */
  352. if (!hwif->nleft) {
  353. stat = wait_drive_not_busy(drive);
  354. if (!OK_STAT(stat, 0, BAD_STAT))
  355. return task_error(drive, rq, __func__, stat);
  356. task_end_request(drive, rq, stat);
  357. return ide_stopped;
  358. }
  359. /* Still data left to transfer. */
  360. ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
  361. return ide_started;
  362. }
  363. /*
  364. * Handler for command with PIO data-out phase (Write/Write Multiple).
  365. */
  366. static ide_startstop_t task_out_intr (ide_drive_t *drive)
  367. {
  368. ide_hwif_t *hwif = drive->hwif;
  369. struct request *rq = HWGROUP(drive)->rq;
  370. u8 stat = hwif->tp_ops->read_status(hwif);
  371. if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
  372. return task_error(drive, rq, __func__, stat);
  373. /* Deal with unexpected ATA data phase. */
  374. if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
  375. return task_error(drive, rq, __func__, stat);
  376. if (!hwif->nleft) {
  377. task_end_request(drive, rq, stat);
  378. return ide_stopped;
  379. }
  380. /* Still data left to transfer. */
  381. ide_pio_datablock(drive, rq, 1);
  382. ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
  383. return ide_started;
  384. }
  385. static ide_startstop_t pre_task_out_intr(ide_drive_t *drive, struct request *rq)
  386. {
  387. ide_startstop_t startstop;
  388. if (ide_wait_stat(&startstop, drive, DRQ_STAT,
  389. drive->bad_wstat, WAIT_DRQ)) {
  390. printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
  391. drive->name,
  392. drive->hwif->data_phase ? "MULT" : "",
  393. drive->addressing ? "_EXT" : "");
  394. return startstop;
  395. }
  396. if (!drive->unmask)
  397. local_irq_disable();
  398. ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
  399. ide_pio_datablock(drive, rq, 1);
  400. return ide_started;
  401. }
  402. int ide_raw_taskfile(ide_drive_t *drive, ide_task_t *task, u8 *buf, u16 nsect)
  403. {
  404. struct request *rq;
  405. int error;
  406. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  407. rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
  408. rq->buffer = buf;
  409. /*
  410. * (ks) We transfer currently only whole sectors.
  411. * This is suffient for now. But, it would be great,
  412. * if we would find a solution to transfer any size.
  413. * To support special commands like READ LONG.
  414. */
  415. rq->hard_nr_sectors = rq->nr_sectors = nsect;
  416. rq->hard_cur_sectors = rq->current_nr_sectors = nsect;
  417. if (task->tf_flags & IDE_TFLAG_WRITE)
  418. rq->cmd_flags |= REQ_RW;
  419. rq->special = task;
  420. task->rq = rq;
  421. error = blk_execute_rq(drive->queue, NULL, rq, 0);
  422. blk_put_request(rq);
  423. return error;
  424. }
  425. EXPORT_SYMBOL(ide_raw_taskfile);
  426. int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
  427. {
  428. task->data_phase = TASKFILE_NO_DATA;
  429. return ide_raw_taskfile(drive, task, NULL, 0);
  430. }
  431. EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
  432. #ifdef CONFIG_IDE_TASK_IOCTL
  433. int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  434. {
  435. ide_task_request_t *req_task;
  436. ide_task_t args;
  437. u8 *outbuf = NULL;
  438. u8 *inbuf = NULL;
  439. u8 *data_buf = NULL;
  440. int err = 0;
  441. int tasksize = sizeof(struct ide_task_request_s);
  442. unsigned int taskin = 0;
  443. unsigned int taskout = 0;
  444. u16 nsect = 0;
  445. char __user *buf = (char __user *)arg;
  446. // printk("IDE Taskfile ...\n");
  447. req_task = kzalloc(tasksize, GFP_KERNEL);
  448. if (req_task == NULL) return -ENOMEM;
  449. if (copy_from_user(req_task, buf, tasksize)) {
  450. kfree(req_task);
  451. return -EFAULT;
  452. }
  453. taskout = req_task->out_size;
  454. taskin = req_task->in_size;
  455. if (taskin > 65536 || taskout > 65536) {
  456. err = -EINVAL;
  457. goto abort;
  458. }
  459. if (taskout) {
  460. int outtotal = tasksize;
  461. outbuf = kzalloc(taskout, GFP_KERNEL);
  462. if (outbuf == NULL) {
  463. err = -ENOMEM;
  464. goto abort;
  465. }
  466. if (copy_from_user(outbuf, buf + outtotal, taskout)) {
  467. err = -EFAULT;
  468. goto abort;
  469. }
  470. }
  471. if (taskin) {
  472. int intotal = tasksize + taskout;
  473. inbuf = kzalloc(taskin, GFP_KERNEL);
  474. if (inbuf == NULL) {
  475. err = -ENOMEM;
  476. goto abort;
  477. }
  478. if (copy_from_user(inbuf, buf + intotal, taskin)) {
  479. err = -EFAULT;
  480. goto abort;
  481. }
  482. }
  483. memset(&args, 0, sizeof(ide_task_t));
  484. memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
  485. memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
  486. args.data_phase = req_task->data_phase;
  487. args.tf_flags = IDE_TFLAG_IO_16BIT | IDE_TFLAG_DEVICE |
  488. IDE_TFLAG_IN_TF;
  489. if (drive->addressing == 1)
  490. args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_IN_HOB);
  491. if (req_task->out_flags.all) {
  492. args.tf_flags |= IDE_TFLAG_FLAGGED;
  493. if (req_task->out_flags.b.data)
  494. args.tf_flags |= IDE_TFLAG_OUT_DATA;
  495. if (req_task->out_flags.b.nsector_hob)
  496. args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
  497. if (req_task->out_flags.b.sector_hob)
  498. args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
  499. if (req_task->out_flags.b.lcyl_hob)
  500. args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
  501. if (req_task->out_flags.b.hcyl_hob)
  502. args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
  503. if (req_task->out_flags.b.error_feature)
  504. args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
  505. if (req_task->out_flags.b.nsector)
  506. args.tf_flags |= IDE_TFLAG_OUT_NSECT;
  507. if (req_task->out_flags.b.sector)
  508. args.tf_flags |= IDE_TFLAG_OUT_LBAL;
  509. if (req_task->out_flags.b.lcyl)
  510. args.tf_flags |= IDE_TFLAG_OUT_LBAM;
  511. if (req_task->out_flags.b.hcyl)
  512. args.tf_flags |= IDE_TFLAG_OUT_LBAH;
  513. } else {
  514. args.tf_flags |= IDE_TFLAG_OUT_TF;
  515. if (args.tf_flags & IDE_TFLAG_LBA48)
  516. args.tf_flags |= IDE_TFLAG_OUT_HOB;
  517. }
  518. if (req_task->in_flags.b.data)
  519. args.tf_flags |= IDE_TFLAG_IN_DATA;
  520. switch(req_task->data_phase) {
  521. case TASKFILE_MULTI_OUT:
  522. if (!drive->mult_count) {
  523. /* (hs): give up if multcount is not set */
  524. printk(KERN_ERR "%s: %s Multimode Write " \
  525. "multcount is not set\n",
  526. drive->name, __func__);
  527. err = -EPERM;
  528. goto abort;
  529. }
  530. /* fall through */
  531. case TASKFILE_OUT:
  532. /* fall through */
  533. case TASKFILE_OUT_DMAQ:
  534. case TASKFILE_OUT_DMA:
  535. nsect = taskout / SECTOR_SIZE;
  536. data_buf = outbuf;
  537. break;
  538. case TASKFILE_MULTI_IN:
  539. if (!drive->mult_count) {
  540. /* (hs): give up if multcount is not set */
  541. printk(KERN_ERR "%s: %s Multimode Read failure " \
  542. "multcount is not set\n",
  543. drive->name, __func__);
  544. err = -EPERM;
  545. goto abort;
  546. }
  547. /* fall through */
  548. case TASKFILE_IN:
  549. /* fall through */
  550. case TASKFILE_IN_DMAQ:
  551. case TASKFILE_IN_DMA:
  552. nsect = taskin / SECTOR_SIZE;
  553. data_buf = inbuf;
  554. break;
  555. case TASKFILE_NO_DATA:
  556. break;
  557. default:
  558. err = -EFAULT;
  559. goto abort;
  560. }
  561. if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
  562. nsect = 0;
  563. else if (!nsect) {
  564. nsect = (args.tf.hob_nsect << 8) | args.tf.nsect;
  565. if (!nsect) {
  566. printk(KERN_ERR "%s: in/out command without data\n",
  567. drive->name);
  568. err = -EFAULT;
  569. goto abort;
  570. }
  571. }
  572. if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE)
  573. args.tf_flags |= IDE_TFLAG_WRITE;
  574. err = ide_raw_taskfile(drive, &args, data_buf, nsect);
  575. memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
  576. memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
  577. if ((args.tf_flags & IDE_TFLAG_FLAGGED_SET_IN_FLAGS) &&
  578. req_task->in_flags.all == 0) {
  579. req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
  580. if (drive->addressing == 1)
  581. req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
  582. }
  583. if (copy_to_user(buf, req_task, tasksize)) {
  584. err = -EFAULT;
  585. goto abort;
  586. }
  587. if (taskout) {
  588. int outtotal = tasksize;
  589. if (copy_to_user(buf + outtotal, outbuf, taskout)) {
  590. err = -EFAULT;
  591. goto abort;
  592. }
  593. }
  594. if (taskin) {
  595. int intotal = tasksize + taskout;
  596. if (copy_to_user(buf + intotal, inbuf, taskin)) {
  597. err = -EFAULT;
  598. goto abort;
  599. }
  600. }
  601. abort:
  602. kfree(req_task);
  603. kfree(outbuf);
  604. kfree(inbuf);
  605. // printk("IDE Taskfile ioctl ended. rc = %i\n", err);
  606. return err;
  607. }
  608. #endif
  609. int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  610. {
  611. u8 *buf = NULL;
  612. int bufsize = 0, err = 0;
  613. u8 args[4], xfer_rate = 0;
  614. ide_task_t tfargs;
  615. struct ide_taskfile *tf = &tfargs.tf;
  616. struct hd_driveid *id = drive->id;
  617. if (NULL == (void *) arg) {
  618. struct request *rq;
  619. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  620. rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
  621. err = blk_execute_rq(drive->queue, NULL, rq, 0);
  622. blk_put_request(rq);
  623. return err;
  624. }
  625. if (copy_from_user(args, (void __user *)arg, 4))
  626. return -EFAULT;
  627. memset(&tfargs, 0, sizeof(ide_task_t));
  628. tf->feature = args[2];
  629. if (args[0] == WIN_SMART) {
  630. tf->nsect = args[3];
  631. tf->lbal = args[1];
  632. tf->lbam = 0x4f;
  633. tf->lbah = 0xc2;
  634. tfargs.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_IN_NSECT;
  635. } else {
  636. tf->nsect = args[1];
  637. tfargs.tf_flags = IDE_TFLAG_OUT_FEATURE |
  638. IDE_TFLAG_OUT_NSECT | IDE_TFLAG_IN_NSECT;
  639. }
  640. tf->command = args[0];
  641. tfargs.data_phase = args[3] ? TASKFILE_IN : TASKFILE_NO_DATA;
  642. if (args[3]) {
  643. tfargs.tf_flags |= IDE_TFLAG_IO_16BIT;
  644. bufsize = SECTOR_WORDS * 4 * args[3];
  645. buf = kzalloc(bufsize, GFP_KERNEL);
  646. if (buf == NULL)
  647. return -ENOMEM;
  648. }
  649. if (tf->command == WIN_SETFEATURES &&
  650. tf->feature == SETFEATURES_XFER &&
  651. tf->nsect >= XFER_SW_DMA_0 &&
  652. (id->dma_ultra || id->dma_mword || id->dma_1word)) {
  653. xfer_rate = args[1];
  654. if (tf->nsect > XFER_UDMA_2 && !eighty_ninty_three(drive)) {
  655. printk(KERN_WARNING "%s: UDMA speeds >UDMA33 cannot "
  656. "be set\n", drive->name);
  657. goto abort;
  658. }
  659. }
  660. err = ide_raw_taskfile(drive, &tfargs, buf, args[3]);
  661. args[0] = tf->status;
  662. args[1] = tf->error;
  663. args[2] = tf->nsect;
  664. if (!err && xfer_rate) {
  665. /* active-retuning-calls future */
  666. ide_set_xfer_rate(drive, xfer_rate);
  667. ide_driveid_update(drive);
  668. }
  669. abort:
  670. if (copy_to_user((void __user *)arg, &args, 4))
  671. err = -EFAULT;
  672. if (buf) {
  673. if (copy_to_user((void __user *)(arg + 4), buf, bufsize))
  674. err = -EFAULT;
  675. kfree(buf);
  676. }
  677. return err;
  678. }
  679. int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  680. {
  681. void __user *p = (void __user *)arg;
  682. int err = 0;
  683. u8 args[7];
  684. ide_task_t task;
  685. if (copy_from_user(args, p, 7))
  686. return -EFAULT;
  687. memset(&task, 0, sizeof(task));
  688. memcpy(&task.tf_array[7], &args[1], 6);
  689. task.tf.command = args[0];
  690. task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
  691. err = ide_no_data_taskfile(drive, &task);
  692. args[0] = task.tf.command;
  693. memcpy(&args[1], &task.tf_array[7], 6);
  694. if (copy_to_user(p, args, 7))
  695. err = -EFAULT;
  696. return err;
  697. }