ide-taskfile.c 20 KB

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