ide-taskfile.c 21 KB

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