ide-taskfile.c 21 KB

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