ide-taskfile.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. /*
  2. * Copyright (C) 2000-2002 Michael Cornwell <cornwell@acm.org>
  3. * Copyright (C) 2000-2002 Andre Hedrick <andre@linux-ide.org>
  4. * Copyright (C) 2001-2002 Klaus Smolin
  5. * IBM Storage Technology Division
  6. * Copyright (C) 2003-2004, 2007 Bartlomiej Zolnierkiewicz
  7. *
  8. * The big the bad and the ugly.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/types.h>
  12. #include <linux/string.h>
  13. #include <linux/kernel.h>
  14. #include <linux/timer.h>
  15. #include <linux/mm.h>
  16. #include <linux/sched.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/major.h>
  19. #include <linux/errno.h>
  20. #include <linux/genhd.h>
  21. #include <linux/blkpg.h>
  22. #include <linux/slab.h>
  23. #include <linux/pci.h>
  24. #include <linux/delay.h>
  25. #include <linux/hdreg.h>
  26. #include <linux/ide.h>
  27. #include <linux/bitops.h>
  28. #include <linux/scatterlist.h>
  29. #include <asm/byteorder.h>
  30. #include <asm/irq.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/io.h>
  33. void ide_tf_load(ide_drive_t *drive, ide_task_t *task)
  34. {
  35. ide_hwif_t *hwif = drive->hwif;
  36. struct ide_taskfile *tf = &task->tf;
  37. u8 HIHI = (task->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
  38. if (task->tf_flags & IDE_TFLAG_FLAGGED)
  39. HIHI = 0xFF;
  40. #ifdef DEBUG
  41. printk("%s: tf: feat 0x%02x nsect 0x%02x lbal 0x%02x "
  42. "lbam 0x%02x lbah 0x%02x dev 0x%02x cmd 0x%02x\n",
  43. drive->name, tf->feature, tf->nsect, tf->lbal,
  44. tf->lbam, tf->lbah, tf->device, tf->command);
  45. printk("%s: hob: nsect 0x%02x lbal 0x%02x "
  46. "lbam 0x%02x lbah 0x%02x\n",
  47. drive->name, tf->hob_nsect, tf->hob_lbal,
  48. tf->hob_lbam, tf->hob_lbah);
  49. #endif
  50. ide_set_irq(drive, 1);
  51. if ((task->tf_flags & IDE_TFLAG_NO_SELECT_MASK) == 0)
  52. SELECT_MASK(drive, 0);
  53. if (task->tf_flags & IDE_TFLAG_OUT_DATA)
  54. hwif->OUTW((tf->hob_data << 8) | tf->data, IDE_DATA_REG);
  55. if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
  56. hwif->OUTB(tf->hob_feature, IDE_FEATURE_REG);
  57. if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
  58. hwif->OUTB(tf->hob_nsect, IDE_NSECTOR_REG);
  59. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
  60. hwif->OUTB(tf->hob_lbal, IDE_SECTOR_REG);
  61. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
  62. hwif->OUTB(tf->hob_lbam, IDE_LCYL_REG);
  63. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
  64. hwif->OUTB(tf->hob_lbah, IDE_HCYL_REG);
  65. if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
  66. hwif->OUTB(tf->feature, IDE_FEATURE_REG);
  67. if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
  68. hwif->OUTB(tf->nsect, IDE_NSECTOR_REG);
  69. if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
  70. hwif->OUTB(tf->lbal, IDE_SECTOR_REG);
  71. if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
  72. hwif->OUTB(tf->lbam, IDE_LCYL_REG);
  73. if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
  74. hwif->OUTB(tf->lbah, IDE_HCYL_REG);
  75. if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
  76. hwif->OUTB((tf->device & HIHI) | drive->select.all, IDE_SELECT_REG);
  77. }
  78. int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
  79. {
  80. ide_task_t args;
  81. memset(&args, 0, sizeof(ide_task_t));
  82. args.tf.nsect = 0x01;
  83. if (drive->media == ide_disk)
  84. args.tf.command = WIN_IDENTIFY;
  85. else
  86. args.tf.command = WIN_PIDENTIFY;
  87. args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
  88. args.data_phase = TASKFILE_IN;
  89. return ide_raw_taskfile(drive, &args, buf, 1);
  90. }
  91. static int inline task_dma_ok(ide_task_t *task)
  92. {
  93. if (blk_fs_request(task->rq) || (task->tf_flags & IDE_TFLAG_FLAGGED))
  94. return 1;
  95. switch (task->tf.command) {
  96. case WIN_WRITEDMA_ONCE:
  97. case WIN_WRITEDMA:
  98. case WIN_WRITEDMA_EXT:
  99. case WIN_READDMA_ONCE:
  100. case WIN_READDMA:
  101. case WIN_READDMA_EXT:
  102. case WIN_IDENTIFY_DMA:
  103. return 1;
  104. }
  105. return 0;
  106. }
  107. static ide_startstop_t task_no_data_intr(ide_drive_t *);
  108. static ide_startstop_t set_geometry_intr(ide_drive_t *);
  109. static ide_startstop_t recal_intr(ide_drive_t *);
  110. static ide_startstop_t set_multmode_intr(ide_drive_t *);
  111. static ide_startstop_t pre_task_out_intr(ide_drive_t *, struct request *);
  112. static ide_startstop_t task_in_intr(ide_drive_t *);
  113. ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
  114. {
  115. ide_hwif_t *hwif = HWIF(drive);
  116. struct ide_taskfile *tf = &task->tf;
  117. ide_handler_t *handler = NULL;
  118. if (task->data_phase == TASKFILE_MULTI_IN ||
  119. task->data_phase == TASKFILE_MULTI_OUT) {
  120. if (!drive->mult_count) {
  121. printk(KERN_ERR "%s: multimode not set!\n",
  122. drive->name);
  123. return ide_stopped;
  124. }
  125. }
  126. if (task->tf_flags & IDE_TFLAG_FLAGGED)
  127. task->tf_flags |= IDE_TFLAG_FLAGGED_SET_IN_FLAGS;
  128. if ((task->tf_flags & IDE_TFLAG_DMA_PIO_FALLBACK) == 0)
  129. ide_tf_load(drive, task);
  130. switch (task->data_phase) {
  131. case TASKFILE_MULTI_OUT:
  132. case TASKFILE_OUT:
  133. hwif->OUTBSYNC(drive, tf->command, IDE_COMMAND_REG);
  134. ndelay(400); /* FIXME */
  135. return pre_task_out_intr(drive, task->rq);
  136. case TASKFILE_MULTI_IN:
  137. case TASKFILE_IN:
  138. handler = task_in_intr;
  139. /* fall-through */
  140. case TASKFILE_NO_DATA:
  141. if (handler == NULL)
  142. handler = task_no_data_intr;
  143. /* WIN_{SPECIFY,RESTORE,SETMULT} use custom handlers */
  144. if (task->tf_flags & IDE_TFLAG_CUSTOM_HANDLER) {
  145. switch (tf->command) {
  146. case WIN_SPECIFY: handler = set_geometry_intr; break;
  147. case WIN_RESTORE: handler = recal_intr; break;
  148. case WIN_SETMULT: handler = set_multmode_intr; break;
  149. }
  150. }
  151. ide_execute_command(drive, tf->command, handler,
  152. WAIT_WORSTCASE, NULL);
  153. return ide_started;
  154. default:
  155. if (task_dma_ok(task) == 0 || drive->using_dma == 0 ||
  156. hwif->dma_setup(drive))
  157. return ide_stopped;
  158. hwif->dma_exec_cmd(drive, tf->command);
  159. hwif->dma_start(drive);
  160. return ide_started;
  161. }
  162. }
  163. EXPORT_SYMBOL_GPL(do_rw_taskfile);
  164. /*
  165. * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
  166. */
  167. static ide_startstop_t set_multmode_intr(ide_drive_t *drive)
  168. {
  169. ide_hwif_t *hwif = HWIF(drive);
  170. u8 stat;
  171. if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
  172. drive->mult_count = drive->mult_req;
  173. } else {
  174. drive->mult_req = drive->mult_count = 0;
  175. drive->special.b.recalibrate = 1;
  176. (void) ide_dump_status(drive, "set_multmode", stat);
  177. }
  178. return ide_stopped;
  179. }
  180. /*
  181. * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
  182. */
  183. static ide_startstop_t set_geometry_intr(ide_drive_t *drive)
  184. {
  185. ide_hwif_t *hwif = HWIF(drive);
  186. int retries = 5;
  187. u8 stat;
  188. while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
  189. udelay(10);
  190. if (OK_STAT(stat, READY_STAT, BAD_STAT))
  191. return ide_stopped;
  192. if (stat & (ERR_STAT|DRQ_STAT))
  193. return ide_error(drive, "set_geometry_intr", stat);
  194. BUG_ON(HWGROUP(drive)->handler != NULL);
  195. ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
  196. return ide_started;
  197. }
  198. /*
  199. * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
  200. */
  201. static ide_startstop_t recal_intr(ide_drive_t *drive)
  202. {
  203. ide_hwif_t *hwif = HWIF(drive);
  204. u8 stat;
  205. if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), 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. ide_hwif_t *hwif = HWIF(drive);
  216. u8 stat;
  217. local_irq_enable_in_hardirq();
  218. if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
  219. return ide_error(drive, "task_no_data_intr", stat);
  220. /* calls ide_end_drive_cmd */
  221. }
  222. if (args)
  223. ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
  224. return ide_stopped;
  225. }
  226. static u8 wait_drive_not_busy(ide_drive_t *drive)
  227. {
  228. ide_hwif_t *hwif = HWIF(drive);
  229. int retries;
  230. u8 stat;
  231. /*
  232. * Last sector was transfered, wait until drive is ready.
  233. * This can take up to 10 usec, but we will wait max 1 ms.
  234. */
  235. for (retries = 0; retries < 100; retries++) {
  236. if ((stat = hwif->INB(IDE_STATUS_REG)) & 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 = drive->hwif->INB(IDE_ERROR_REG);
  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. * Handler for command with PIO data-in phase (Read/Read Multiple).
  363. */
  364. static ide_startstop_t task_in_intr(ide_drive_t *drive)
  365. {
  366. ide_hwif_t *hwif = drive->hwif;
  367. struct request *rq = HWGROUP(drive)->rq;
  368. u8 stat = hwif->INB(IDE_STATUS_REG);
  369. /* new way for dealing with premature shared PCI interrupts */
  370. if (!OK_STAT(stat, DRQ_STAT, BAD_R_STAT)) {
  371. if (stat & (ERR_STAT | DRQ_STAT))
  372. return task_error(drive, rq, __FUNCTION__, stat);
  373. /* No data yet, so wait for another IRQ. */
  374. ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
  375. return ide_started;
  376. }
  377. ide_pio_datablock(drive, rq, 0);
  378. /* If it was the last datablock check status and finish transfer. */
  379. if (!hwif->nleft) {
  380. stat = wait_drive_not_busy(drive);
  381. if (!OK_STAT(stat, 0, BAD_STAT))
  382. return task_error(drive, rq, __FUNCTION__, stat);
  383. task_end_request(drive, rq, stat);
  384. return ide_stopped;
  385. }
  386. /* Still data left to transfer. */
  387. ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
  388. return ide_started;
  389. }
  390. /*
  391. * Handler for command with PIO data-out phase (Write/Write Multiple).
  392. */
  393. static ide_startstop_t task_out_intr (ide_drive_t *drive)
  394. {
  395. ide_hwif_t *hwif = drive->hwif;
  396. struct request *rq = HWGROUP(drive)->rq;
  397. u8 stat = hwif->INB(IDE_STATUS_REG);
  398. if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
  399. return task_error(drive, rq, __FUNCTION__, stat);
  400. /* Deal with unexpected ATA data phase. */
  401. if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
  402. return task_error(drive, rq, __FUNCTION__, stat);
  403. if (!hwif->nleft) {
  404. task_end_request(drive, rq, stat);
  405. return ide_stopped;
  406. }
  407. /* Still data left to transfer. */
  408. ide_pio_datablock(drive, rq, 1);
  409. ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
  410. return ide_started;
  411. }
  412. static ide_startstop_t pre_task_out_intr(ide_drive_t *drive, struct request *rq)
  413. {
  414. ide_startstop_t startstop;
  415. if (ide_wait_stat(&startstop, drive, DRQ_STAT,
  416. drive->bad_wstat, WAIT_DRQ)) {
  417. printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
  418. drive->name,
  419. drive->hwif->data_phase ? "MULT" : "",
  420. drive->addressing ? "_EXT" : "");
  421. return startstop;
  422. }
  423. if (!drive->unmask)
  424. local_irq_disable();
  425. ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
  426. ide_pio_datablock(drive, rq, 1);
  427. return ide_started;
  428. }
  429. int ide_raw_taskfile(ide_drive_t *drive, ide_task_t *task, u8 *buf, u16 nsect)
  430. {
  431. struct request rq;
  432. memset(&rq, 0, sizeof(rq));
  433. rq.ref_count = 1;
  434. rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
  435. rq.buffer = buf;
  436. /*
  437. * (ks) We transfer currently only whole sectors.
  438. * This is suffient for now. But, it would be great,
  439. * if we would find a solution to transfer any size.
  440. * To support special commands like READ LONG.
  441. */
  442. rq.hard_nr_sectors = rq.nr_sectors = nsect;
  443. rq.hard_cur_sectors = rq.current_nr_sectors = nsect;
  444. if (task->tf_flags & IDE_TFLAG_WRITE)
  445. rq.cmd_flags |= REQ_RW;
  446. rq.special = task;
  447. task->rq = &rq;
  448. return ide_do_drive_cmd(drive, &rq, ide_wait);
  449. }
  450. EXPORT_SYMBOL(ide_raw_taskfile);
  451. int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
  452. {
  453. task->data_phase = TASKFILE_NO_DATA;
  454. return ide_raw_taskfile(drive, task, NULL, 0);
  455. }
  456. EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
  457. #ifdef CONFIG_IDE_TASK_IOCTL
  458. int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  459. {
  460. ide_task_request_t *req_task;
  461. ide_task_t args;
  462. u8 *outbuf = NULL;
  463. u8 *inbuf = NULL;
  464. u8 *data_buf = NULL;
  465. int err = 0;
  466. int tasksize = sizeof(struct ide_task_request_s);
  467. unsigned int taskin = 0;
  468. unsigned int taskout = 0;
  469. u16 nsect = 0;
  470. char __user *buf = (char __user *)arg;
  471. // printk("IDE Taskfile ...\n");
  472. req_task = kzalloc(tasksize, GFP_KERNEL);
  473. if (req_task == NULL) return -ENOMEM;
  474. if (copy_from_user(req_task, buf, tasksize)) {
  475. kfree(req_task);
  476. return -EFAULT;
  477. }
  478. taskout = req_task->out_size;
  479. taskin = req_task->in_size;
  480. if (taskin > 65536 || taskout > 65536) {
  481. err = -EINVAL;
  482. goto abort;
  483. }
  484. if (taskout) {
  485. int outtotal = tasksize;
  486. outbuf = kzalloc(taskout, GFP_KERNEL);
  487. if (outbuf == NULL) {
  488. err = -ENOMEM;
  489. goto abort;
  490. }
  491. if (copy_from_user(outbuf, buf + outtotal, taskout)) {
  492. err = -EFAULT;
  493. goto abort;
  494. }
  495. }
  496. if (taskin) {
  497. int intotal = tasksize + taskout;
  498. inbuf = kzalloc(taskin, GFP_KERNEL);
  499. if (inbuf == NULL) {
  500. err = -ENOMEM;
  501. goto abort;
  502. }
  503. if (copy_from_user(inbuf, buf + intotal, taskin)) {
  504. err = -EFAULT;
  505. goto abort;
  506. }
  507. }
  508. memset(&args, 0, sizeof(ide_task_t));
  509. memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
  510. memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
  511. args.data_phase = req_task->data_phase;
  512. args.tf_flags = IDE_TFLAG_IO_16BIT | IDE_TFLAG_DEVICE |
  513. IDE_TFLAG_IN_TF;
  514. if (drive->addressing == 1)
  515. args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_IN_HOB);
  516. if (req_task->out_flags.all) {
  517. args.tf_flags |= IDE_TFLAG_FLAGGED;
  518. if (req_task->out_flags.b.data)
  519. args.tf_flags |= IDE_TFLAG_OUT_DATA;
  520. if (req_task->out_flags.b.nsector_hob)
  521. args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
  522. if (req_task->out_flags.b.sector_hob)
  523. args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
  524. if (req_task->out_flags.b.lcyl_hob)
  525. args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
  526. if (req_task->out_flags.b.hcyl_hob)
  527. args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
  528. if (req_task->out_flags.b.error_feature)
  529. args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
  530. if (req_task->out_flags.b.nsector)
  531. args.tf_flags |= IDE_TFLAG_OUT_NSECT;
  532. if (req_task->out_flags.b.sector)
  533. args.tf_flags |= IDE_TFLAG_OUT_LBAL;
  534. if (req_task->out_flags.b.lcyl)
  535. args.tf_flags |= IDE_TFLAG_OUT_LBAM;
  536. if (req_task->out_flags.b.hcyl)
  537. args.tf_flags |= IDE_TFLAG_OUT_LBAH;
  538. } else {
  539. args.tf_flags |= IDE_TFLAG_OUT_TF;
  540. if (args.tf_flags & IDE_TFLAG_LBA48)
  541. args.tf_flags |= IDE_TFLAG_OUT_HOB;
  542. }
  543. if (req_task->in_flags.b.data)
  544. args.tf_flags |= IDE_TFLAG_IN_DATA;
  545. switch(req_task->data_phase) {
  546. case TASKFILE_MULTI_OUT:
  547. if (!drive->mult_count) {
  548. /* (hs): give up if multcount is not set */
  549. printk(KERN_ERR "%s: %s Multimode Write " \
  550. "multcount is not set\n",
  551. drive->name, __FUNCTION__);
  552. err = -EPERM;
  553. goto abort;
  554. }
  555. /* fall through */
  556. case TASKFILE_OUT:
  557. /* fall through */
  558. case TASKFILE_OUT_DMAQ:
  559. case TASKFILE_OUT_DMA:
  560. nsect = taskout / SECTOR_SIZE;
  561. data_buf = outbuf;
  562. break;
  563. case TASKFILE_MULTI_IN:
  564. if (!drive->mult_count) {
  565. /* (hs): give up if multcount is not set */
  566. printk(KERN_ERR "%s: %s Multimode Read failure " \
  567. "multcount is not set\n",
  568. drive->name, __FUNCTION__);
  569. err = -EPERM;
  570. goto abort;
  571. }
  572. /* fall through */
  573. case TASKFILE_IN:
  574. /* fall through */
  575. case TASKFILE_IN_DMAQ:
  576. case TASKFILE_IN_DMA:
  577. nsect = taskin / SECTOR_SIZE;
  578. data_buf = inbuf;
  579. break;
  580. case TASKFILE_NO_DATA:
  581. break;
  582. default:
  583. err = -EFAULT;
  584. goto abort;
  585. }
  586. if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
  587. nsect = 0;
  588. else if (!nsect) {
  589. nsect = (args.tf.hob_nsect << 8) | args.tf.nsect;
  590. if (!nsect) {
  591. printk(KERN_ERR "%s: in/out command without data\n",
  592. drive->name);
  593. err = -EFAULT;
  594. goto abort;
  595. }
  596. }
  597. if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE)
  598. args.tf_flags |= IDE_TFLAG_WRITE;
  599. err = ide_raw_taskfile(drive, &args, data_buf, nsect);
  600. memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
  601. memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
  602. if ((args.tf_flags & IDE_TFLAG_FLAGGED_SET_IN_FLAGS) &&
  603. req_task->in_flags.all == 0) {
  604. req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
  605. if (drive->addressing == 1)
  606. req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
  607. }
  608. if (copy_to_user(buf, req_task, tasksize)) {
  609. err = -EFAULT;
  610. goto abort;
  611. }
  612. if (taskout) {
  613. int outtotal = tasksize;
  614. if (copy_to_user(buf + outtotal, outbuf, taskout)) {
  615. err = -EFAULT;
  616. goto abort;
  617. }
  618. }
  619. if (taskin) {
  620. int intotal = tasksize + taskout;
  621. if (copy_to_user(buf + intotal, inbuf, taskin)) {
  622. err = -EFAULT;
  623. goto abort;
  624. }
  625. }
  626. abort:
  627. kfree(req_task);
  628. kfree(outbuf);
  629. kfree(inbuf);
  630. // printk("IDE Taskfile ioctl ended. rc = %i\n", err);
  631. return err;
  632. }
  633. #endif
  634. int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  635. {
  636. u8 *buf = NULL;
  637. int bufsize = 0, err = 0;
  638. u8 args[4], xfer_rate = 0;
  639. ide_task_t tfargs;
  640. struct ide_taskfile *tf = &tfargs.tf;
  641. struct hd_driveid *id = drive->id;
  642. if (NULL == (void *) arg) {
  643. struct request rq;
  644. ide_init_drive_cmd(&rq);
  645. rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
  646. return ide_do_drive_cmd(drive, &rq, ide_wait);
  647. }
  648. if (copy_from_user(args, (void __user *)arg, 4))
  649. return -EFAULT;
  650. memset(&tfargs, 0, sizeof(ide_task_t));
  651. tf->feature = args[2];
  652. if (args[0] == WIN_SMART) {
  653. tf->nsect = args[3];
  654. tf->lbal = args[1];
  655. tf->lbam = 0x4f;
  656. tf->lbah = 0xc2;
  657. tfargs.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_IN_NSECT;
  658. } else {
  659. tf->nsect = args[1];
  660. tfargs.tf_flags = IDE_TFLAG_OUT_FEATURE |
  661. IDE_TFLAG_OUT_NSECT | IDE_TFLAG_IN_NSECT;
  662. }
  663. tf->command = args[0];
  664. tfargs.data_phase = args[3] ? TASKFILE_IN : TASKFILE_NO_DATA;
  665. if (args[3]) {
  666. tfargs.tf_flags |= IDE_TFLAG_IO_16BIT;
  667. bufsize = SECTOR_WORDS * 4 * args[3];
  668. buf = kzalloc(bufsize, GFP_KERNEL);
  669. if (buf == NULL)
  670. return -ENOMEM;
  671. }
  672. if (tf->command == WIN_SETFEATURES &&
  673. tf->feature == SETFEATURES_XFER &&
  674. tf->nsect >= XFER_SW_DMA_0 &&
  675. (id->dma_ultra || id->dma_mword || id->dma_1word)) {
  676. xfer_rate = args[1];
  677. if (tf->nsect > XFER_UDMA_2 && !eighty_ninty_three(drive)) {
  678. printk(KERN_WARNING "%s: UDMA speeds >UDMA33 cannot "
  679. "be set\n", drive->name);
  680. goto abort;
  681. }
  682. }
  683. err = ide_raw_taskfile(drive, &tfargs, buf, args[3]);
  684. args[0] = tf->status;
  685. args[1] = tf->error;
  686. args[2] = tf->nsect;
  687. if (!err && xfer_rate) {
  688. /* active-retuning-calls future */
  689. ide_set_xfer_rate(drive, xfer_rate);
  690. ide_driveid_update(drive);
  691. }
  692. abort:
  693. if (copy_to_user((void __user *)arg, &args, 4))
  694. err = -EFAULT;
  695. if (buf) {
  696. if (copy_to_user((void __user *)(arg + 4), buf, bufsize))
  697. err = -EFAULT;
  698. kfree(buf);
  699. }
  700. return err;
  701. }
  702. int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  703. {
  704. void __user *p = (void __user *)arg;
  705. int err = 0;
  706. u8 args[7];
  707. ide_task_t task;
  708. if (copy_from_user(args, p, 7))
  709. return -EFAULT;
  710. memset(&task, 0, sizeof(task));
  711. memcpy(&task.tf_array[7], &args[1], 6);
  712. task.tf.command = args[0];
  713. task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
  714. err = ide_no_data_taskfile(drive, &task);
  715. args[0] = task.tf.command;
  716. memcpy(&args[1], &task.tf_array[7], 6);
  717. if (copy_to_user(p, args, 7))
  718. err = -EFAULT;
  719. return err;
  720. }