ide-taskfile.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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. * We got an interrupt on a task_in case, but no errors and no DRQ.
  359. *
  360. * It might be a spurious irq (shared irq), but it might be a
  361. * command that had no output.
  362. */
  363. static ide_startstop_t task_in_unexpected(ide_drive_t *drive, struct request *rq, u8 stat)
  364. {
  365. /* Command all done? */
  366. if (OK_STAT(stat, READY_STAT, BUSY_STAT)) {
  367. task_end_request(drive, rq, stat);
  368. return ide_stopped;
  369. }
  370. /* Assume it was a spurious irq */
  371. ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
  372. return ide_started;
  373. }
  374. /*
  375. * Handler for command with PIO data-in phase (Read/Read Multiple).
  376. */
  377. static ide_startstop_t task_in_intr(ide_drive_t *drive)
  378. {
  379. ide_hwif_t *hwif = drive->hwif;
  380. struct request *rq = HWGROUP(drive)->rq;
  381. u8 stat = ide_read_status(drive);
  382. /* Error? */
  383. if (stat & ERR_STAT)
  384. return task_error(drive, rq, __FUNCTION__, stat);
  385. /* Didn't want any data? Odd. */
  386. if (!(stat & DRQ_STAT))
  387. return task_in_unexpected(drive, rq, stat);
  388. ide_pio_datablock(drive, rq, 0);
  389. /* Are we done? Check status and finish transfer. */
  390. if (!hwif->nleft) {
  391. stat = wait_drive_not_busy(drive);
  392. if (!OK_STAT(stat, 0, BAD_STAT))
  393. return task_error(drive, rq, __FUNCTION__, stat);
  394. task_end_request(drive, rq, stat);
  395. return ide_stopped;
  396. }
  397. /* Still data left to transfer. */
  398. ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
  399. return ide_started;
  400. }
  401. /*
  402. * Handler for command with PIO data-out phase (Write/Write Multiple).
  403. */
  404. static ide_startstop_t task_out_intr (ide_drive_t *drive)
  405. {
  406. ide_hwif_t *hwif = drive->hwif;
  407. struct request *rq = HWGROUP(drive)->rq;
  408. u8 stat = ide_read_status(drive);
  409. if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
  410. return task_error(drive, rq, __FUNCTION__, stat);
  411. /* Deal with unexpected ATA data phase. */
  412. if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
  413. return task_error(drive, rq, __FUNCTION__, stat);
  414. if (!hwif->nleft) {
  415. task_end_request(drive, rq, stat);
  416. return ide_stopped;
  417. }
  418. /* Still data left to transfer. */
  419. ide_pio_datablock(drive, rq, 1);
  420. ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
  421. return ide_started;
  422. }
  423. static ide_startstop_t pre_task_out_intr(ide_drive_t *drive, struct request *rq)
  424. {
  425. ide_startstop_t startstop;
  426. if (ide_wait_stat(&startstop, drive, DRQ_STAT,
  427. drive->bad_wstat, WAIT_DRQ)) {
  428. printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
  429. drive->name,
  430. drive->hwif->data_phase ? "MULT" : "",
  431. drive->addressing ? "_EXT" : "");
  432. return startstop;
  433. }
  434. if (!drive->unmask)
  435. local_irq_disable();
  436. ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
  437. ide_pio_datablock(drive, rq, 1);
  438. return ide_started;
  439. }
  440. int ide_raw_taskfile(ide_drive_t *drive, ide_task_t *task, u8 *buf, u16 nsect)
  441. {
  442. struct request rq;
  443. memset(&rq, 0, sizeof(rq));
  444. rq.ref_count = 1;
  445. rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
  446. rq.buffer = buf;
  447. /*
  448. * (ks) We transfer currently only whole sectors.
  449. * This is suffient for now. But, it would be great,
  450. * if we would find a solution to transfer any size.
  451. * To support special commands like READ LONG.
  452. */
  453. rq.hard_nr_sectors = rq.nr_sectors = nsect;
  454. rq.hard_cur_sectors = rq.current_nr_sectors = nsect;
  455. if (task->tf_flags & IDE_TFLAG_WRITE)
  456. rq.cmd_flags |= REQ_RW;
  457. rq.special = task;
  458. task->rq = &rq;
  459. return ide_do_drive_cmd(drive, &rq, ide_wait);
  460. }
  461. EXPORT_SYMBOL(ide_raw_taskfile);
  462. int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
  463. {
  464. task->data_phase = TASKFILE_NO_DATA;
  465. return ide_raw_taskfile(drive, task, NULL, 0);
  466. }
  467. EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
  468. #ifdef CONFIG_IDE_TASK_IOCTL
  469. int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  470. {
  471. ide_task_request_t *req_task;
  472. ide_task_t args;
  473. u8 *outbuf = NULL;
  474. u8 *inbuf = NULL;
  475. u8 *data_buf = NULL;
  476. int err = 0;
  477. int tasksize = sizeof(struct ide_task_request_s);
  478. unsigned int taskin = 0;
  479. unsigned int taskout = 0;
  480. u16 nsect = 0;
  481. char __user *buf = (char __user *)arg;
  482. // printk("IDE Taskfile ...\n");
  483. req_task = kzalloc(tasksize, GFP_KERNEL);
  484. if (req_task == NULL) return -ENOMEM;
  485. if (copy_from_user(req_task, buf, tasksize)) {
  486. kfree(req_task);
  487. return -EFAULT;
  488. }
  489. taskout = req_task->out_size;
  490. taskin = req_task->in_size;
  491. if (taskin > 65536 || taskout > 65536) {
  492. err = -EINVAL;
  493. goto abort;
  494. }
  495. if (taskout) {
  496. int outtotal = tasksize;
  497. outbuf = kzalloc(taskout, GFP_KERNEL);
  498. if (outbuf == NULL) {
  499. err = -ENOMEM;
  500. goto abort;
  501. }
  502. if (copy_from_user(outbuf, buf + outtotal, taskout)) {
  503. err = -EFAULT;
  504. goto abort;
  505. }
  506. }
  507. if (taskin) {
  508. int intotal = tasksize + taskout;
  509. inbuf = kzalloc(taskin, GFP_KERNEL);
  510. if (inbuf == NULL) {
  511. err = -ENOMEM;
  512. goto abort;
  513. }
  514. if (copy_from_user(inbuf, buf + intotal, taskin)) {
  515. err = -EFAULT;
  516. goto abort;
  517. }
  518. }
  519. memset(&args, 0, sizeof(ide_task_t));
  520. memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
  521. memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
  522. args.data_phase = req_task->data_phase;
  523. args.tf_flags = IDE_TFLAG_IO_16BIT | IDE_TFLAG_DEVICE |
  524. IDE_TFLAG_IN_TF;
  525. if (drive->addressing == 1)
  526. args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_IN_HOB);
  527. if (req_task->out_flags.all) {
  528. args.tf_flags |= IDE_TFLAG_FLAGGED;
  529. if (req_task->out_flags.b.data)
  530. args.tf_flags |= IDE_TFLAG_OUT_DATA;
  531. if (req_task->out_flags.b.nsector_hob)
  532. args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
  533. if (req_task->out_flags.b.sector_hob)
  534. args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
  535. if (req_task->out_flags.b.lcyl_hob)
  536. args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
  537. if (req_task->out_flags.b.hcyl_hob)
  538. args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
  539. if (req_task->out_flags.b.error_feature)
  540. args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
  541. if (req_task->out_flags.b.nsector)
  542. args.tf_flags |= IDE_TFLAG_OUT_NSECT;
  543. if (req_task->out_flags.b.sector)
  544. args.tf_flags |= IDE_TFLAG_OUT_LBAL;
  545. if (req_task->out_flags.b.lcyl)
  546. args.tf_flags |= IDE_TFLAG_OUT_LBAM;
  547. if (req_task->out_flags.b.hcyl)
  548. args.tf_flags |= IDE_TFLAG_OUT_LBAH;
  549. } else {
  550. args.tf_flags |= IDE_TFLAG_OUT_TF;
  551. if (args.tf_flags & IDE_TFLAG_LBA48)
  552. args.tf_flags |= IDE_TFLAG_OUT_HOB;
  553. }
  554. if (req_task->in_flags.b.data)
  555. args.tf_flags |= IDE_TFLAG_IN_DATA;
  556. switch(req_task->data_phase) {
  557. case TASKFILE_MULTI_OUT:
  558. if (!drive->mult_count) {
  559. /* (hs): give up if multcount is not set */
  560. printk(KERN_ERR "%s: %s Multimode Write " \
  561. "multcount is not set\n",
  562. drive->name, __FUNCTION__);
  563. err = -EPERM;
  564. goto abort;
  565. }
  566. /* fall through */
  567. case TASKFILE_OUT:
  568. /* fall through */
  569. case TASKFILE_OUT_DMAQ:
  570. case TASKFILE_OUT_DMA:
  571. nsect = taskout / SECTOR_SIZE;
  572. data_buf = outbuf;
  573. break;
  574. case TASKFILE_MULTI_IN:
  575. if (!drive->mult_count) {
  576. /* (hs): give up if multcount is not set */
  577. printk(KERN_ERR "%s: %s Multimode Read failure " \
  578. "multcount is not set\n",
  579. drive->name, __FUNCTION__);
  580. err = -EPERM;
  581. goto abort;
  582. }
  583. /* fall through */
  584. case TASKFILE_IN:
  585. /* fall through */
  586. case TASKFILE_IN_DMAQ:
  587. case TASKFILE_IN_DMA:
  588. nsect = taskin / SECTOR_SIZE;
  589. data_buf = inbuf;
  590. break;
  591. case TASKFILE_NO_DATA:
  592. break;
  593. default:
  594. err = -EFAULT;
  595. goto abort;
  596. }
  597. if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
  598. nsect = 0;
  599. else if (!nsect) {
  600. nsect = (args.tf.hob_nsect << 8) | args.tf.nsect;
  601. if (!nsect) {
  602. printk(KERN_ERR "%s: in/out command without data\n",
  603. drive->name);
  604. err = -EFAULT;
  605. goto abort;
  606. }
  607. }
  608. if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE)
  609. args.tf_flags |= IDE_TFLAG_WRITE;
  610. err = ide_raw_taskfile(drive, &args, data_buf, nsect);
  611. memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
  612. memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
  613. if ((args.tf_flags & IDE_TFLAG_FLAGGED_SET_IN_FLAGS) &&
  614. req_task->in_flags.all == 0) {
  615. req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
  616. if (drive->addressing == 1)
  617. req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
  618. }
  619. if (copy_to_user(buf, req_task, tasksize)) {
  620. err = -EFAULT;
  621. goto abort;
  622. }
  623. if (taskout) {
  624. int outtotal = tasksize;
  625. if (copy_to_user(buf + outtotal, outbuf, taskout)) {
  626. err = -EFAULT;
  627. goto abort;
  628. }
  629. }
  630. if (taskin) {
  631. int intotal = tasksize + taskout;
  632. if (copy_to_user(buf + intotal, inbuf, taskin)) {
  633. err = -EFAULT;
  634. goto abort;
  635. }
  636. }
  637. abort:
  638. kfree(req_task);
  639. kfree(outbuf);
  640. kfree(inbuf);
  641. // printk("IDE Taskfile ioctl ended. rc = %i\n", err);
  642. return err;
  643. }
  644. #endif
  645. int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  646. {
  647. u8 *buf = NULL;
  648. int bufsize = 0, err = 0;
  649. u8 args[4], xfer_rate = 0;
  650. ide_task_t tfargs;
  651. struct ide_taskfile *tf = &tfargs.tf;
  652. struct hd_driveid *id = drive->id;
  653. if (NULL == (void *) arg) {
  654. struct request rq;
  655. ide_init_drive_cmd(&rq);
  656. rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
  657. return ide_do_drive_cmd(drive, &rq, ide_wait);
  658. }
  659. if (copy_from_user(args, (void __user *)arg, 4))
  660. return -EFAULT;
  661. memset(&tfargs, 0, sizeof(ide_task_t));
  662. tf->feature = args[2];
  663. if (args[0] == WIN_SMART) {
  664. tf->nsect = args[3];
  665. tf->lbal = args[1];
  666. tf->lbam = 0x4f;
  667. tf->lbah = 0xc2;
  668. tfargs.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_IN_NSECT;
  669. } else {
  670. tf->nsect = args[1];
  671. tfargs.tf_flags = IDE_TFLAG_OUT_FEATURE |
  672. IDE_TFLAG_OUT_NSECT | IDE_TFLAG_IN_NSECT;
  673. }
  674. tf->command = args[0];
  675. tfargs.data_phase = args[3] ? TASKFILE_IN : TASKFILE_NO_DATA;
  676. if (args[3]) {
  677. tfargs.tf_flags |= IDE_TFLAG_IO_16BIT;
  678. bufsize = SECTOR_WORDS * 4 * args[3];
  679. buf = kzalloc(bufsize, GFP_KERNEL);
  680. if (buf == NULL)
  681. return -ENOMEM;
  682. }
  683. if (tf->command == WIN_SETFEATURES &&
  684. tf->feature == SETFEATURES_XFER &&
  685. tf->nsect >= XFER_SW_DMA_0 &&
  686. (id->dma_ultra || id->dma_mword || id->dma_1word)) {
  687. xfer_rate = args[1];
  688. if (tf->nsect > XFER_UDMA_2 && !eighty_ninty_three(drive)) {
  689. printk(KERN_WARNING "%s: UDMA speeds >UDMA33 cannot "
  690. "be set\n", drive->name);
  691. goto abort;
  692. }
  693. }
  694. err = ide_raw_taskfile(drive, &tfargs, buf, args[3]);
  695. args[0] = tf->status;
  696. args[1] = tf->error;
  697. args[2] = tf->nsect;
  698. if (!err && xfer_rate) {
  699. /* active-retuning-calls future */
  700. ide_set_xfer_rate(drive, xfer_rate);
  701. ide_driveid_update(drive);
  702. }
  703. abort:
  704. if (copy_to_user((void __user *)arg, &args, 4))
  705. err = -EFAULT;
  706. if (buf) {
  707. if (copy_to_user((void __user *)(arg + 4), buf, bufsize))
  708. err = -EFAULT;
  709. kfree(buf);
  710. }
  711. return err;
  712. }
  713. int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  714. {
  715. void __user *p = (void __user *)arg;
  716. int err = 0;
  717. u8 args[7];
  718. ide_task_t task;
  719. if (copy_from_user(args, p, 7))
  720. return -EFAULT;
  721. memset(&task, 0, sizeof(task));
  722. memcpy(&task.tf_array[7], &args[1], 6);
  723. task.tf.command = args[0];
  724. task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
  725. err = ide_no_data_taskfile(drive, &task);
  726. args[0] = task.tf.command;
  727. memcpy(&args[1], &task.tf_array[7], 6);
  728. if (copy_to_user(p, args, 7))
  729. err = -EFAULT;
  730. return err;
  731. }