ide-taskfile.c 21 KB

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