ide-taskfile.c 20 KB

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