ide-taskfile.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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. static void ata_bswap_data (void *buffer, int wcount)
  36. {
  37. u16 *p = buffer;
  38. while (wcount--) {
  39. *p = *p << 8 | *p >> 8; p++;
  40. *p = *p << 8 | *p >> 8; p++;
  41. }
  42. }
  43. static void taskfile_input_data(ide_drive_t *drive, void *buffer, u32 wcount)
  44. {
  45. HWIF(drive)->ata_input_data(drive, buffer, wcount);
  46. if (drive->bswap)
  47. ata_bswap_data(buffer, wcount);
  48. }
  49. static void taskfile_output_data(ide_drive_t *drive, void *buffer, u32 wcount)
  50. {
  51. if (drive->bswap) {
  52. ata_bswap_data(buffer, wcount);
  53. HWIF(drive)->ata_output_data(drive, buffer, wcount);
  54. ata_bswap_data(buffer, wcount);
  55. } else {
  56. HWIF(drive)->ata_output_data(drive, buffer, wcount);
  57. }
  58. }
  59. void ide_tf_load(ide_drive_t *drive, ide_task_t *task)
  60. {
  61. ide_hwif_t *hwif = drive->hwif;
  62. struct ide_taskfile *tf = &task->tf;
  63. u8 HIHI = (task->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
  64. if (task->tf_flags & IDE_TFLAG_FLAGGED)
  65. HIHI = 0xFF;
  66. #ifdef DEBUG
  67. printk("%s: tf: feat 0x%02x nsect 0x%02x lbal 0x%02x "
  68. "lbam 0x%02x lbah 0x%02x dev 0x%02x cmd 0x%02x\n",
  69. drive->name, tf->feature, tf->nsect, tf->lbal,
  70. tf->lbam, tf->lbah, tf->device, tf->command);
  71. #endif
  72. if (IDE_CONTROL_REG)
  73. hwif->OUTB(drive->ctl, IDE_CONTROL_REG); /* clear nIEN */
  74. if ((task->tf_flags & IDE_TFLAG_NO_SELECT_MASK) == 0)
  75. SELECT_MASK(drive, 0);
  76. if (task->tf_flags & IDE_TFLAG_OUT_DATA)
  77. hwif->OUTW((tf->hob_data << 8) | tf->data, IDE_DATA_REG);
  78. if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
  79. hwif->OUTB(tf->hob_feature, IDE_FEATURE_REG);
  80. if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
  81. hwif->OUTB(tf->hob_nsect, IDE_NSECTOR_REG);
  82. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
  83. hwif->OUTB(tf->hob_lbal, IDE_SECTOR_REG);
  84. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
  85. hwif->OUTB(tf->hob_lbam, IDE_LCYL_REG);
  86. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
  87. hwif->OUTB(tf->hob_lbah, IDE_HCYL_REG);
  88. if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
  89. hwif->OUTB(tf->feature, IDE_FEATURE_REG);
  90. if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
  91. hwif->OUTB(tf->nsect, IDE_NSECTOR_REG);
  92. if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
  93. hwif->OUTB(tf->lbal, IDE_SECTOR_REG);
  94. if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
  95. hwif->OUTB(tf->lbam, IDE_LCYL_REG);
  96. if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
  97. hwif->OUTB(tf->lbah, IDE_HCYL_REG);
  98. if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
  99. hwif->OUTB((tf->device & HIHI) | drive->select.all, IDE_SELECT_REG);
  100. }
  101. EXPORT_SYMBOL_GPL(ide_tf_load);
  102. int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
  103. {
  104. ide_task_t args;
  105. memset(&args, 0, sizeof(ide_task_t));
  106. args.tf.nsect = 0x01;
  107. if (drive->media == ide_disk)
  108. args.tf.command = WIN_IDENTIFY;
  109. else
  110. args.tf.command = WIN_PIDENTIFY;
  111. args.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
  112. args.data_phase = TASKFILE_IN;
  113. args.handler = task_in_intr;
  114. return ide_raw_taskfile(drive, &args, buf, 1);
  115. }
  116. static int inline task_dma_ok(ide_task_t *task)
  117. {
  118. if (task->tf_flags & IDE_TFLAG_FLAGGED)
  119. return 1;
  120. switch (task->tf.command) {
  121. case WIN_WRITEDMA_ONCE:
  122. case WIN_WRITEDMA:
  123. case WIN_WRITEDMA_EXT:
  124. case WIN_READDMA_ONCE:
  125. case WIN_READDMA:
  126. case WIN_READDMA_EXT:
  127. case WIN_IDENTIFY_DMA:
  128. return 1;
  129. }
  130. return 0;
  131. }
  132. ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
  133. {
  134. ide_hwif_t *hwif = HWIF(drive);
  135. struct ide_taskfile *tf = &task->tf;
  136. if (task->data_phase == TASKFILE_MULTI_IN ||
  137. task->data_phase == TASKFILE_MULTI_OUT) {
  138. if (!drive->mult_count) {
  139. printk(KERN_ERR "%s: multimode not set!\n",
  140. drive->name);
  141. return ide_stopped;
  142. }
  143. }
  144. if (task->tf_flags & IDE_TFLAG_FLAGGED)
  145. task->tf_flags |= IDE_TFLAG_FLAGGED_SET_IN_FLAGS;
  146. ide_tf_load(drive, task);
  147. if (task->handler != NULL) {
  148. if (task->prehandler != NULL) {
  149. hwif->OUTBSYNC(drive, tf->command, IDE_COMMAND_REG);
  150. ndelay(400); /* FIXME */
  151. return task->prehandler(drive, task->rq);
  152. }
  153. ide_execute_command(drive, tf->command, task->handler, WAIT_WORSTCASE, NULL);
  154. return ide_started;
  155. }
  156. if (task_dma_ok(task) && drive->using_dma && !hwif->dma_setup(drive)) {
  157. hwif->dma_exec_cmd(drive, tf->command);
  158. hwif->dma_start(drive);
  159. return ide_started;
  160. }
  161. return ide_stopped;
  162. }
  163. /*
  164. * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
  165. */
  166. ide_startstop_t set_multmode_intr (ide_drive_t *drive)
  167. {
  168. ide_hwif_t *hwif = HWIF(drive);
  169. u8 stat;
  170. if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
  171. drive->mult_count = drive->mult_req;
  172. } else {
  173. drive->mult_req = drive->mult_count = 0;
  174. drive->special.b.recalibrate = 1;
  175. (void) ide_dump_status(drive, "set_multmode", stat);
  176. }
  177. return ide_stopped;
  178. }
  179. /*
  180. * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
  181. */
  182. ide_startstop_t set_geometry_intr (ide_drive_t *drive)
  183. {
  184. ide_hwif_t *hwif = HWIF(drive);
  185. int retries = 5;
  186. u8 stat;
  187. while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
  188. udelay(10);
  189. if (OK_STAT(stat, READY_STAT, BAD_STAT))
  190. return ide_stopped;
  191. if (stat & (ERR_STAT|DRQ_STAT))
  192. return ide_error(drive, "set_geometry_intr", stat);
  193. BUG_ON(HWGROUP(drive)->handler != NULL);
  194. ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
  195. return ide_started;
  196. }
  197. /*
  198. * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
  199. */
  200. ide_startstop_t recal_intr (ide_drive_t *drive)
  201. {
  202. ide_hwif_t *hwif = HWIF(drive);
  203. u8 stat;
  204. if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
  205. return ide_error(drive, "recal_intr", stat);
  206. return ide_stopped;
  207. }
  208. /*
  209. * Handler for commands without a data phase
  210. */
  211. ide_startstop_t task_no_data_intr (ide_drive_t *drive)
  212. {
  213. ide_task_t *args = HWGROUP(drive)->rq->special;
  214. ide_hwif_t *hwif = HWIF(drive);
  215. u8 stat;
  216. local_irq_enable_in_hardirq();
  217. if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
  218. return ide_error(drive, "task_no_data_intr", stat);
  219. /* calls ide_end_drive_cmd */
  220. }
  221. if (args)
  222. ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
  223. return ide_stopped;
  224. }
  225. static u8 wait_drive_not_busy(ide_drive_t *drive)
  226. {
  227. ide_hwif_t *hwif = HWIF(drive);
  228. int retries;
  229. u8 stat;
  230. /*
  231. * Last sector was transfered, wait until drive is ready.
  232. * This can take up to 10 usec, but we will wait max 1 ms
  233. * (drive_cmd_intr() waits that long).
  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. taskfile_output_data(drive, buf, SECTOR_WORDS);
  279. else
  280. taskfile_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. if (rq->bio) /* fs request */
  297. rq->errors = 0;
  298. touch_softlockup_watchdog();
  299. switch (drive->hwif->data_phase) {
  300. case TASKFILE_MULTI_IN:
  301. case TASKFILE_MULTI_OUT:
  302. ide_pio_multi(drive, write);
  303. break;
  304. default:
  305. ide_pio_sector(drive, write);
  306. break;
  307. }
  308. }
  309. static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
  310. const char *s, u8 stat)
  311. {
  312. if (rq->bio) {
  313. ide_hwif_t *hwif = drive->hwif;
  314. int sectors = hwif->nsect - hwif->nleft;
  315. switch (hwif->data_phase) {
  316. case TASKFILE_IN:
  317. if (hwif->nleft)
  318. break;
  319. /* fall through */
  320. case TASKFILE_OUT:
  321. sectors--;
  322. break;
  323. case TASKFILE_MULTI_IN:
  324. if (hwif->nleft)
  325. break;
  326. /* fall through */
  327. case TASKFILE_MULTI_OUT:
  328. sectors -= drive->mult_count;
  329. default:
  330. break;
  331. }
  332. if (sectors > 0) {
  333. ide_driver_t *drv;
  334. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  335. drv->end_request(drive, 1, sectors);
  336. }
  337. }
  338. return ide_error(drive, s, stat);
  339. }
  340. static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
  341. {
  342. HWIF(drive)->cursg = NULL;
  343. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
  344. ide_task_t *task = rq->special;
  345. if (task->tf_flags & IDE_TFLAG_FLAGGED) {
  346. u8 err = drive->hwif->INB(IDE_ERROR_REG);
  347. ide_end_drive_cmd(drive, stat, err);
  348. return;
  349. }
  350. }
  351. if (rq->rq_disk) {
  352. ide_driver_t *drv;
  353. drv = *(ide_driver_t **)rq->rq_disk->private_data;;
  354. drv->end_request(drive, 1, rq->hard_nr_sectors);
  355. } else
  356. ide_end_request(drive, 1, rq->hard_nr_sectors);
  357. }
  358. /*
  359. * Handler for command with PIO data-in phase (Read/Read Multiple).
  360. */
  361. ide_startstop_t task_in_intr (ide_drive_t *drive)
  362. {
  363. ide_hwif_t *hwif = drive->hwif;
  364. struct request *rq = HWGROUP(drive)->rq;
  365. u8 stat = hwif->INB(IDE_STATUS_REG);
  366. /* new way for dealing with premature shared PCI interrupts */
  367. if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
  368. if (stat & (ERR_STAT | DRQ_STAT))
  369. return task_error(drive, rq, __FUNCTION__, stat);
  370. /* No data yet, so wait for another IRQ. */
  371. ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
  372. return ide_started;
  373. }
  374. ide_pio_datablock(drive, rq, 0);
  375. /* If it was the last datablock check status and finish transfer. */
  376. if (!hwif->nleft) {
  377. stat = wait_drive_not_busy(drive);
  378. if (!OK_STAT(stat, 0, BAD_R_STAT))
  379. return task_error(drive, rq, __FUNCTION__, stat);
  380. task_end_request(drive, rq, stat);
  381. return ide_stopped;
  382. }
  383. /* Still data left to transfer. */
  384. ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
  385. return ide_started;
  386. }
  387. EXPORT_SYMBOL(task_in_intr);
  388. /*
  389. * Handler for command with PIO data-out phase (Write/Write Multiple).
  390. */
  391. static ide_startstop_t task_out_intr (ide_drive_t *drive)
  392. {
  393. ide_hwif_t *hwif = drive->hwif;
  394. struct request *rq = HWGROUP(drive)->rq;
  395. u8 stat = hwif->INB(IDE_STATUS_REG);
  396. if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
  397. return task_error(drive, rq, __FUNCTION__, stat);
  398. /* Deal with unexpected ATA data phase. */
  399. if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
  400. return task_error(drive, rq, __FUNCTION__, stat);
  401. if (!hwif->nleft) {
  402. task_end_request(drive, rq, stat);
  403. return ide_stopped;
  404. }
  405. /* Still data left to transfer. */
  406. ide_pio_datablock(drive, rq, 1);
  407. ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
  408. return ide_started;
  409. }
  410. ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq)
  411. {
  412. ide_startstop_t startstop;
  413. if (ide_wait_stat(&startstop, drive, DATA_READY,
  414. drive->bad_wstat, WAIT_DRQ)) {
  415. printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
  416. drive->name,
  417. drive->hwif->data_phase ? "MULT" : "",
  418. drive->addressing ? "_EXT" : "");
  419. return startstop;
  420. }
  421. if (!drive->unmask)
  422. local_irq_disable();
  423. ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
  424. ide_pio_datablock(drive, rq, 1);
  425. return ide_started;
  426. }
  427. EXPORT_SYMBOL(pre_task_out_intr);
  428. int ide_raw_taskfile(ide_drive_t *drive, ide_task_t *task, u8 *buf, u16 nsect)
  429. {
  430. struct request rq;
  431. memset(&rq, 0, sizeof(rq));
  432. rq.ref_count = 1;
  433. rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
  434. rq.buffer = buf;
  435. /*
  436. * (ks) We transfer currently only whole sectors.
  437. * This is suffient for now. But, it would be great,
  438. * if we would find a solution to transfer any size.
  439. * To support special commands like READ LONG.
  440. */
  441. rq.hard_nr_sectors = rq.nr_sectors = nsect;
  442. rq.hard_cur_sectors = rq.current_nr_sectors = nsect;
  443. if (task->tf_flags & IDE_TFLAG_WRITE)
  444. rq.cmd_flags |= REQ_RW;
  445. rq.special = task;
  446. task->rq = &rq;
  447. return ide_do_drive_cmd(drive, &rq, ide_wait);
  448. }
  449. EXPORT_SYMBOL(ide_raw_taskfile);
  450. int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
  451. {
  452. task->data_phase = TASKFILE_NO_DATA;
  453. task->handler = task_no_data_intr;
  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. u8 io_32bit = drive->io_32bit;
  471. char __user *buf = (char __user *)arg;
  472. // printk("IDE Taskfile ...\n");
  473. req_task = kzalloc(tasksize, GFP_KERNEL);
  474. if (req_task == NULL) return -ENOMEM;
  475. if (copy_from_user(req_task, buf, tasksize)) {
  476. kfree(req_task);
  477. return -EFAULT;
  478. }
  479. taskout = req_task->out_size;
  480. taskin = req_task->in_size;
  481. if (taskin > 65536 || taskout > 65536) {
  482. err = -EINVAL;
  483. goto abort;
  484. }
  485. if (taskout) {
  486. int outtotal = tasksize;
  487. outbuf = kzalloc(taskout, GFP_KERNEL);
  488. if (outbuf == NULL) {
  489. err = -ENOMEM;
  490. goto abort;
  491. }
  492. if (copy_from_user(outbuf, buf + outtotal, taskout)) {
  493. err = -EFAULT;
  494. goto abort;
  495. }
  496. }
  497. if (taskin) {
  498. int intotal = tasksize + taskout;
  499. inbuf = kzalloc(taskin, GFP_KERNEL);
  500. if (inbuf == NULL) {
  501. err = -ENOMEM;
  502. goto abort;
  503. }
  504. if (copy_from_user(inbuf, buf + intotal, taskin)) {
  505. err = -EFAULT;
  506. goto abort;
  507. }
  508. }
  509. memset(&args, 0, sizeof(ide_task_t));
  510. memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
  511. memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
  512. args.data_phase = req_task->data_phase;
  513. args.tf_flags = IDE_TFLAG_OUT_DEVICE;
  514. if (drive->addressing == 1)
  515. args.tf_flags |= IDE_TFLAG_LBA48;
  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. drive->io_32bit = 0;
  546. switch(req_task->data_phase) {
  547. case TASKFILE_MULTI_OUT:
  548. if (!drive->mult_count) {
  549. /* (hs): give up if multcount is not set */
  550. printk(KERN_ERR "%s: %s Multimode Write " \
  551. "multcount is not set\n",
  552. drive->name, __FUNCTION__);
  553. err = -EPERM;
  554. goto abort;
  555. }
  556. /* fall through */
  557. case TASKFILE_OUT:
  558. args.prehandler = &pre_task_out_intr;
  559. args.handler = &task_out_intr;
  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. args.handler = &task_in_intr;
  578. /* fall through */
  579. case TASKFILE_IN_DMAQ:
  580. case TASKFILE_IN_DMA:
  581. nsect = taskin / SECTOR_SIZE;
  582. data_buf = inbuf;
  583. break;
  584. case TASKFILE_NO_DATA:
  585. args.handler = &task_no_data_intr;
  586. break;
  587. default:
  588. err = -EFAULT;
  589. goto abort;
  590. }
  591. if (req_task->req_cmd == IDE_DRIVE_TASK_NO_DATA)
  592. nsect = 0;
  593. else if (!nsect) {
  594. nsect = (args.tf.hob_nsect << 8) | args.tf.nsect;
  595. if (!nsect) {
  596. printk(KERN_ERR "%s: in/out command without data\n",
  597. drive->name);
  598. err = -EFAULT;
  599. goto abort;
  600. }
  601. }
  602. if (req_task->req_cmd == IDE_DRIVE_TASK_RAW_WRITE)
  603. args.tf_flags |= IDE_TFLAG_WRITE;
  604. err = ide_raw_taskfile(drive, &args, data_buf, nsect);
  605. memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
  606. memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
  607. if ((args.tf_flags & IDE_TFLAG_FLAGGED_SET_IN_FLAGS) &&
  608. req_task->in_flags.all == 0) {
  609. req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
  610. if (drive->addressing == 1)
  611. req_task->in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
  612. }
  613. if (copy_to_user(buf, req_task, tasksize)) {
  614. err = -EFAULT;
  615. goto abort;
  616. }
  617. if (taskout) {
  618. int outtotal = tasksize;
  619. if (copy_to_user(buf + outtotal, outbuf, taskout)) {
  620. err = -EFAULT;
  621. goto abort;
  622. }
  623. }
  624. if (taskin) {
  625. int intotal = tasksize + taskout;
  626. if (copy_to_user(buf + intotal, inbuf, taskin)) {
  627. err = -EFAULT;
  628. goto abort;
  629. }
  630. }
  631. abort:
  632. kfree(req_task);
  633. kfree(outbuf);
  634. kfree(inbuf);
  635. // printk("IDE Taskfile ioctl ended. rc = %i\n", err);
  636. drive->io_32bit = io_32bit;
  637. return err;
  638. }
  639. #endif
  640. int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
  641. {
  642. struct request rq;
  643. u8 buffer[4];
  644. if (!buf)
  645. buf = buffer;
  646. memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
  647. ide_init_drive_cmd(&rq);
  648. rq.buffer = buf;
  649. *buf++ = cmd;
  650. *buf++ = nsect;
  651. *buf++ = feature;
  652. *buf++ = sectors;
  653. return ide_do_drive_cmd(drive, &rq, ide_wait);
  654. }
  655. int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  656. {
  657. int err = 0;
  658. u8 args[4], *argbuf = args;
  659. u8 xfer_rate = 0;
  660. int argsize = 4;
  661. ide_task_t tfargs;
  662. struct ide_taskfile *tf = &tfargs.tf;
  663. if (NULL == (void *) arg) {
  664. struct request rq;
  665. ide_init_drive_cmd(&rq);
  666. return ide_do_drive_cmd(drive, &rq, ide_wait);
  667. }
  668. if (copy_from_user(args, (void __user *)arg, 4))
  669. return -EFAULT;
  670. memset(&tfargs, 0, sizeof(ide_task_t));
  671. tf->feature = args[2];
  672. tf->nsect = args[3];
  673. tf->lbal = args[1];
  674. tf->command = args[0];
  675. if (args[3]) {
  676. argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
  677. argbuf = kzalloc(argsize, GFP_KERNEL);
  678. if (argbuf == NULL)
  679. return -ENOMEM;
  680. }
  681. if (set_transfer(drive, &tfargs)) {
  682. xfer_rate = args[1];
  683. if (ide_ata66_check(drive, &tfargs))
  684. goto abort;
  685. }
  686. err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
  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, argbuf, argsize))
  694. err = -EFAULT;
  695. if (argsize > 4)
  696. kfree(argbuf);
  697. return err;
  698. }
  699. int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  700. {
  701. void __user *p = (void __user *)arg;
  702. int err = 0;
  703. u8 args[7];
  704. ide_task_t task;
  705. if (copy_from_user(args, p, 7))
  706. return -EFAULT;
  707. memset(&task, 0, sizeof(task));
  708. memcpy(&task.tf_array[7], &args[1], 6);
  709. task.tf.command = args[0];
  710. task.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE;
  711. err = ide_no_data_taskfile(drive, &task);
  712. args[0] = task.tf.command;
  713. memcpy(&args[1], &task.tf_array[7], 6);
  714. if (copy_to_user(p, args, 7))
  715. err = -EFAULT;
  716. return err;
  717. }