ide-taskfile.c 21 KB

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