ide-taskfile.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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. 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. */
  237. for (retries = 0; retries < 100; retries++) {
  238. if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT)
  239. udelay(10);
  240. else
  241. break;
  242. }
  243. if (stat & BUSY_STAT)
  244. printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
  245. return stat;
  246. }
  247. static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
  248. {
  249. ide_hwif_t *hwif = drive->hwif;
  250. struct scatterlist *sg = hwif->sg_table;
  251. struct scatterlist *cursg = hwif->cursg;
  252. struct page *page;
  253. #ifdef CONFIG_HIGHMEM
  254. unsigned long flags;
  255. #endif
  256. unsigned int offset;
  257. u8 *buf;
  258. cursg = hwif->cursg;
  259. if (!cursg) {
  260. cursg = sg;
  261. hwif->cursg = sg;
  262. }
  263. page = sg_page(cursg);
  264. offset = cursg->offset + hwif->cursg_ofs * SECTOR_SIZE;
  265. /* get the current page and offset */
  266. page = nth_page(page, (offset >> PAGE_SHIFT));
  267. offset %= PAGE_SIZE;
  268. #ifdef CONFIG_HIGHMEM
  269. local_irq_save(flags);
  270. #endif
  271. buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
  272. hwif->nleft--;
  273. hwif->cursg_ofs++;
  274. if ((hwif->cursg_ofs * SECTOR_SIZE) == cursg->length) {
  275. hwif->cursg = sg_next(hwif->cursg);
  276. hwif->cursg_ofs = 0;
  277. }
  278. /* do the actual data transfer */
  279. if (write)
  280. hwif->ata_output_data(drive, buf, SECTOR_WORDS);
  281. else
  282. hwif->ata_input_data(drive, buf, SECTOR_WORDS);
  283. kunmap_atomic(buf, KM_BIO_SRC_IRQ);
  284. #ifdef CONFIG_HIGHMEM
  285. local_irq_restore(flags);
  286. #endif
  287. }
  288. static void ide_pio_multi(ide_drive_t *drive, unsigned int write)
  289. {
  290. unsigned int nsect;
  291. nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
  292. while (nsect--)
  293. ide_pio_sector(drive, write);
  294. }
  295. static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
  296. unsigned int write)
  297. {
  298. u8 saved_io_32bit = drive->io_32bit;
  299. if (rq->bio) /* fs request */
  300. rq->errors = 0;
  301. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
  302. ide_task_t *task = rq->special;
  303. if (task->tf_flags & IDE_TFLAG_IO_16BIT)
  304. drive->io_32bit = 0;
  305. }
  306. touch_softlockup_watchdog();
  307. switch (drive->hwif->data_phase) {
  308. case TASKFILE_MULTI_IN:
  309. case TASKFILE_MULTI_OUT:
  310. ide_pio_multi(drive, write);
  311. break;
  312. default:
  313. ide_pio_sector(drive, write);
  314. break;
  315. }
  316. drive->io_32bit = saved_io_32bit;
  317. }
  318. static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
  319. const char *s, u8 stat)
  320. {
  321. if (rq->bio) {
  322. ide_hwif_t *hwif = drive->hwif;
  323. int sectors = hwif->nsect - hwif->nleft;
  324. switch (hwif->data_phase) {
  325. case TASKFILE_IN:
  326. if (hwif->nleft)
  327. break;
  328. /* fall through */
  329. case TASKFILE_OUT:
  330. sectors--;
  331. break;
  332. case TASKFILE_MULTI_IN:
  333. if (hwif->nleft)
  334. break;
  335. /* fall through */
  336. case TASKFILE_MULTI_OUT:
  337. sectors -= drive->mult_count;
  338. default:
  339. break;
  340. }
  341. if (sectors > 0) {
  342. ide_driver_t *drv;
  343. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  344. drv->end_request(drive, 1, sectors);
  345. }
  346. }
  347. return ide_error(drive, s, stat);
  348. }
  349. void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
  350. {
  351. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
  352. u8 err = drive->hwif->INB(IDE_ERROR_REG);
  353. ide_end_drive_cmd(drive, stat, err);
  354. return;
  355. }
  356. if (rq->rq_disk) {
  357. ide_driver_t *drv;
  358. drv = *(ide_driver_t **)rq->rq_disk->private_data;;
  359. drv->end_request(drive, 1, rq->nr_sectors);
  360. } else
  361. ide_end_request(drive, 1, rq->nr_sectors);
  362. }
  363. /*
  364. * Handler for command with PIO data-in phase (Read/Read Multiple).
  365. */
  366. static ide_startstop_t task_in_intr(ide_drive_t *drive)
  367. {
  368. ide_hwif_t *hwif = drive->hwif;
  369. struct request *rq = HWGROUP(drive)->rq;
  370. u8 stat = hwif->INB(IDE_STATUS_REG);
  371. /* new way for dealing with premature shared PCI interrupts */
  372. if (!OK_STAT(stat, DRQ_STAT, BAD_R_STAT)) {
  373. if (stat & (ERR_STAT | DRQ_STAT))
  374. return task_error(drive, rq, __FUNCTION__, stat);
  375. /* No data yet, so wait for another IRQ. */
  376. ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
  377. return ide_started;
  378. }
  379. ide_pio_datablock(drive, rq, 0);
  380. /* If it was the last datablock check status and finish transfer. */
  381. if (!hwif->nleft) {
  382. stat = wait_drive_not_busy(drive);
  383. if (!OK_STAT(stat, 0, BAD_STAT))
  384. return task_error(drive, rq, __FUNCTION__, stat);
  385. task_end_request(drive, rq, stat);
  386. return ide_stopped;
  387. }
  388. /* Still data left to transfer. */
  389. ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
  390. return ide_started;
  391. }
  392. /*
  393. * Handler for command with PIO data-out phase (Write/Write Multiple).
  394. */
  395. static ide_startstop_t task_out_intr (ide_drive_t *drive)
  396. {
  397. ide_hwif_t *hwif = drive->hwif;
  398. struct request *rq = HWGROUP(drive)->rq;
  399. u8 stat = hwif->INB(IDE_STATUS_REG);
  400. if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
  401. return task_error(drive, rq, __FUNCTION__, stat);
  402. /* Deal with unexpected ATA data phase. */
  403. if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
  404. return task_error(drive, rq, __FUNCTION__, stat);
  405. if (!hwif->nleft) {
  406. task_end_request(drive, rq, stat);
  407. return ide_stopped;
  408. }
  409. /* Still data left to transfer. */
  410. ide_pio_datablock(drive, rq, 1);
  411. ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
  412. return ide_started;
  413. }
  414. static ide_startstop_t pre_task_out_intr(ide_drive_t *drive, struct request *rq)
  415. {
  416. ide_startstop_t startstop;
  417. if (ide_wait_stat(&startstop, drive, DRQ_STAT,
  418. drive->bad_wstat, WAIT_DRQ)) {
  419. printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
  420. drive->name,
  421. drive->hwif->data_phase ? "MULT" : "",
  422. drive->addressing ? "_EXT" : "");
  423. return startstop;
  424. }
  425. if (!drive->unmask)
  426. local_irq_disable();
  427. ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
  428. ide_pio_datablock(drive, rq, 1);
  429. return ide_started;
  430. }
  431. int ide_raw_taskfile(ide_drive_t *drive, ide_task_t *task, u8 *buf, u16 nsect)
  432. {
  433. struct request rq;
  434. memset(&rq, 0, sizeof(rq));
  435. rq.ref_count = 1;
  436. rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
  437. rq.buffer = buf;
  438. /*
  439. * (ks) We transfer currently only whole sectors.
  440. * This is suffient for now. But, it would be great,
  441. * if we would find a solution to transfer any size.
  442. * To support special commands like READ LONG.
  443. */
  444. rq.hard_nr_sectors = rq.nr_sectors = nsect;
  445. rq.hard_cur_sectors = rq.current_nr_sectors = nsect;
  446. if (task->tf_flags & IDE_TFLAG_WRITE)
  447. rq.cmd_flags |= REQ_RW;
  448. rq.special = task;
  449. task->rq = &rq;
  450. return ide_do_drive_cmd(drive, &rq, ide_wait);
  451. }
  452. EXPORT_SYMBOL(ide_raw_taskfile);
  453. int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
  454. {
  455. task->data_phase = TASKFILE_NO_DATA;
  456. return ide_raw_taskfile(drive, task, NULL, 0);
  457. }
  458. EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
  459. #ifdef CONFIG_IDE_TASK_IOCTL
  460. int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  461. {
  462. ide_task_request_t *req_task;
  463. ide_task_t args;
  464. u8 *outbuf = NULL;
  465. u8 *inbuf = NULL;
  466. u8 *data_buf = NULL;
  467. int err = 0;
  468. int tasksize = sizeof(struct ide_task_request_s);
  469. unsigned int taskin = 0;
  470. unsigned int taskout = 0;
  471. u16 nsect = 0;
  472. char __user *buf = (char __user *)arg;
  473. // printk("IDE Taskfile ...\n");
  474. req_task = kzalloc(tasksize, GFP_KERNEL);
  475. if (req_task == NULL) return -ENOMEM;
  476. if (copy_from_user(req_task, buf, tasksize)) {
  477. kfree(req_task);
  478. return -EFAULT;
  479. }
  480. taskout = req_task->out_size;
  481. taskin = req_task->in_size;
  482. if (taskin > 65536 || taskout > 65536) {
  483. err = -EINVAL;
  484. goto abort;
  485. }
  486. if (taskout) {
  487. int outtotal = tasksize;
  488. outbuf = kzalloc(taskout, GFP_KERNEL);
  489. if (outbuf == NULL) {
  490. err = -ENOMEM;
  491. goto abort;
  492. }
  493. if (copy_from_user(outbuf, buf + outtotal, taskout)) {
  494. err = -EFAULT;
  495. goto abort;
  496. }
  497. }
  498. if (taskin) {
  499. int intotal = tasksize + taskout;
  500. inbuf = kzalloc(taskin, GFP_KERNEL);
  501. if (inbuf == NULL) {
  502. err = -ENOMEM;
  503. goto abort;
  504. }
  505. if (copy_from_user(inbuf, buf + intotal, taskin)) {
  506. err = -EFAULT;
  507. goto abort;
  508. }
  509. }
  510. memset(&args, 0, sizeof(ide_task_t));
  511. memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
  512. memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
  513. args.data_phase = req_task->data_phase;
  514. args.tf_flags = IDE_TFLAG_IO_16BIT | IDE_TFLAG_DEVICE |
  515. IDE_TFLAG_IN_TF;
  516. if (drive->addressing == 1)
  517. args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_IN_HOB);
  518. if (req_task->out_flags.all) {
  519. args.tf_flags |= IDE_TFLAG_FLAGGED;
  520. if (req_task->out_flags.b.data)
  521. args.tf_flags |= IDE_TFLAG_OUT_DATA;
  522. if (req_task->out_flags.b.nsector_hob)
  523. args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
  524. if (req_task->out_flags.b.sector_hob)
  525. args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
  526. if (req_task->out_flags.b.lcyl_hob)
  527. args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
  528. if (req_task->out_flags.b.hcyl_hob)
  529. args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
  530. if (req_task->out_flags.b.error_feature)
  531. args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
  532. if (req_task->out_flags.b.nsector)
  533. args.tf_flags |= IDE_TFLAG_OUT_NSECT;
  534. if (req_task->out_flags.b.sector)
  535. args.tf_flags |= IDE_TFLAG_OUT_LBAL;
  536. if (req_task->out_flags.b.lcyl)
  537. args.tf_flags |= IDE_TFLAG_OUT_LBAM;
  538. if (req_task->out_flags.b.hcyl)
  539. args.tf_flags |= IDE_TFLAG_OUT_LBAH;
  540. } else {
  541. args.tf_flags |= IDE_TFLAG_OUT_TF;
  542. if (args.tf_flags & IDE_TFLAG_LBA48)
  543. args.tf_flags |= IDE_TFLAG_OUT_HOB;
  544. }
  545. if (req_task->in_flags.b.data)
  546. args.tf_flags |= IDE_TFLAG_IN_DATA;
  547. switch(req_task->data_phase) {
  548. case TASKFILE_MULTI_OUT:
  549. if (!drive->mult_count) {
  550. /* (hs): give up if multcount is not set */
  551. printk(KERN_ERR "%s: %s Multimode Write " \
  552. "multcount is not set\n",
  553. drive->name, __FUNCTION__);
  554. err = -EPERM;
  555. goto abort;
  556. }
  557. /* fall through */
  558. case TASKFILE_OUT:
  559. /* fall through */
  560. case TASKFILE_OUT_DMAQ:
  561. case TASKFILE_OUT_DMA:
  562. nsect = taskout / SECTOR_SIZE;
  563. data_buf = outbuf;
  564. break;
  565. case TASKFILE_MULTI_IN:
  566. if (!drive->mult_count) {
  567. /* (hs): give up if multcount is not set */
  568. printk(KERN_ERR "%s: %s Multimode Read failure " \
  569. "multcount is not set\n",
  570. drive->name, __FUNCTION__);
  571. err = -EPERM;
  572. goto abort;
  573. }
  574. /* fall through */
  575. case TASKFILE_IN:
  576. /* fall through */
  577. case TASKFILE_IN_DMAQ:
  578. case TASKFILE_IN_DMA:
  579. nsect = taskin / SECTOR_SIZE;
  580. data_buf = inbuf;
  581. break;
  582. case TASKFILE_NO_DATA:
  583. break;
  584. default:
  585. err = -EFAULT;
  586. goto abort;
  587. }
  588. if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
  589. nsect = 0;
  590. else if (!nsect) {
  591. nsect = (args.tf.hob_nsect << 8) | args.tf.nsect;
  592. if (!nsect) {
  593. printk(KERN_ERR "%s: in/out command without data\n",
  594. drive->name);
  595. err = -EFAULT;
  596. goto abort;
  597. }
  598. }
  599. if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE)
  600. args.tf_flags |= IDE_TFLAG_WRITE;
  601. err = ide_raw_taskfile(drive, &args, data_buf, nsect);
  602. memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
  603. memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
  604. if ((args.tf_flags & IDE_TFLAG_FLAGGED_SET_IN_FLAGS) &&
  605. req_task->in_flags.all == 0) {
  606. req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
  607. if (drive->addressing == 1)
  608. req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
  609. }
  610. if (copy_to_user(buf, req_task, tasksize)) {
  611. err = -EFAULT;
  612. goto abort;
  613. }
  614. if (taskout) {
  615. int outtotal = tasksize;
  616. if (copy_to_user(buf + outtotal, outbuf, taskout)) {
  617. err = -EFAULT;
  618. goto abort;
  619. }
  620. }
  621. if (taskin) {
  622. int intotal = tasksize + taskout;
  623. if (copy_to_user(buf + intotal, inbuf, taskin)) {
  624. err = -EFAULT;
  625. goto abort;
  626. }
  627. }
  628. abort:
  629. kfree(req_task);
  630. kfree(outbuf);
  631. kfree(inbuf);
  632. // printk("IDE Taskfile ioctl ended. rc = %i\n", err);
  633. return err;
  634. }
  635. #endif
  636. int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  637. {
  638. u8 *buf = NULL;
  639. int bufsize = 0, err = 0;
  640. u8 args[4], xfer_rate = 0;
  641. ide_task_t tfargs;
  642. struct ide_taskfile *tf = &tfargs.tf;
  643. if (NULL == (void *) arg) {
  644. struct request rq;
  645. ide_init_drive_cmd(&rq);
  646. rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
  647. return ide_do_drive_cmd(drive, &rq, ide_wait);
  648. }
  649. if (copy_from_user(args, (void __user *)arg, 4))
  650. return -EFAULT;
  651. memset(&tfargs, 0, sizeof(ide_task_t));
  652. tf->feature = args[2];
  653. if (args[0] == WIN_SMART) {
  654. tf->nsect = args[3];
  655. tf->lbal = args[1];
  656. tf->lbam = 0x4f;
  657. tf->lbah = 0xc2;
  658. tfargs.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_IN_NSECT;
  659. } else {
  660. tf->nsect = args[1];
  661. tfargs.tf_flags = IDE_TFLAG_OUT_FEATURE |
  662. IDE_TFLAG_OUT_NSECT | IDE_TFLAG_IN_NSECT;
  663. }
  664. tf->command = args[0];
  665. tfargs.data_phase = args[3] ? TASKFILE_IN : TASKFILE_NO_DATA;
  666. if (args[3]) {
  667. tfargs.tf_flags |= IDE_TFLAG_IO_16BIT;
  668. bufsize = SECTOR_WORDS * 4 * args[3];
  669. buf = kzalloc(bufsize, GFP_KERNEL);
  670. if (buf == NULL)
  671. return -ENOMEM;
  672. }
  673. if (set_transfer(drive, &tfargs)) {
  674. xfer_rate = args[1];
  675. if (ide_ata66_check(drive, &tfargs))
  676. goto abort;
  677. }
  678. err = ide_raw_taskfile(drive, &tfargs, buf, args[3]);
  679. args[0] = tf->status;
  680. args[1] = tf->error;
  681. args[2] = tf->nsect;
  682. if (!err && xfer_rate) {
  683. /* active-retuning-calls future */
  684. ide_set_xfer_rate(drive, xfer_rate);
  685. ide_driveid_update(drive);
  686. }
  687. abort:
  688. if (copy_to_user((void __user *)arg, &args, 4))
  689. err = -EFAULT;
  690. if (buf) {
  691. if (copy_to_user((void __user *)(arg + 4), buf, bufsize))
  692. err = -EFAULT;
  693. kfree(buf);
  694. }
  695. return err;
  696. }
  697. int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  698. {
  699. void __user *p = (void __user *)arg;
  700. int err = 0;
  701. u8 args[7];
  702. ide_task_t task;
  703. if (copy_from_user(args, p, 7))
  704. return -EFAULT;
  705. memset(&task, 0, sizeof(task));
  706. memcpy(&task.tf_array[7], &args[1], 6);
  707. task.tf.command = args[0];
  708. task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
  709. err = ide_no_data_taskfile(drive, &task);
  710. args[0] = task.tf.command;
  711. memcpy(&args[1], &task.tf_array[7], 6);
  712. if (copy_to_user(p, args, 7))
  713. err = -EFAULT;
  714. return err;
  715. }