ide-taskfile.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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. if (IDE_CONTROL_REG)
  67. hwif->OUTB(drive->ctl, IDE_CONTROL_REG); /* clear nIEN */
  68. if ((task->tf_flags & IDE_TFLAG_NO_SELECT_MASK) == 0)
  69. SELECT_MASK(drive, 0);
  70. if (task->tf_flags & IDE_TFLAG_OUT_DATA)
  71. hwif->OUTW((tf->hob_data << 8) | tf->data, IDE_DATA_REG);
  72. if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
  73. hwif->OUTB(tf->hob_feature, IDE_FEATURE_REG);
  74. if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
  75. hwif->OUTB(tf->hob_nsect, IDE_NSECTOR_REG);
  76. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
  77. hwif->OUTB(tf->hob_lbal, IDE_SECTOR_REG);
  78. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
  79. hwif->OUTB(tf->hob_lbam, IDE_LCYL_REG);
  80. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
  81. hwif->OUTB(tf->hob_lbah, IDE_HCYL_REG);
  82. if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
  83. hwif->OUTB(tf->feature, IDE_FEATURE_REG);
  84. if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
  85. hwif->OUTB(tf->nsect, IDE_NSECTOR_REG);
  86. if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
  87. hwif->OUTB(tf->lbal, IDE_SECTOR_REG);
  88. if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
  89. hwif->OUTB(tf->lbam, IDE_LCYL_REG);
  90. if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
  91. hwif->OUTB(tf->lbah, IDE_HCYL_REG);
  92. hwif->OUTB((tf->device & HIHI) | drive->select.all, IDE_SELECT_REG);
  93. }
  94. EXPORT_SYMBOL_GPL(ide_tf_load);
  95. int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
  96. {
  97. ide_task_t args;
  98. memset(&args, 0, sizeof(ide_task_t));
  99. args.tf.nsect = 0x01;
  100. if (drive->media == ide_disk)
  101. args.tf.command = WIN_IDENTIFY;
  102. else
  103. args.tf.command = WIN_PIDENTIFY;
  104. args.command_type = IDE_DRIVE_TASK_IN;
  105. args.data_phase = TASKFILE_IN;
  106. args.handler = &task_in_intr;
  107. return ide_raw_taskfile(drive, &args, buf);
  108. }
  109. static int inline task_dma_ok(ide_task_t *task)
  110. {
  111. if (task->tf_flags & IDE_TFLAG_FLAGGED)
  112. return 1;
  113. switch (task->tf.command) {
  114. case WIN_WRITEDMA_ONCE:
  115. case WIN_WRITEDMA:
  116. case WIN_WRITEDMA_EXT:
  117. case WIN_READDMA_ONCE:
  118. case WIN_READDMA:
  119. case WIN_READDMA_EXT:
  120. case WIN_IDENTIFY_DMA:
  121. return 1;
  122. }
  123. return 0;
  124. }
  125. ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
  126. {
  127. ide_hwif_t *hwif = HWIF(drive);
  128. struct ide_taskfile *tf = &task->tf;
  129. ide_tf_load(drive, task);
  130. if (task->handler != NULL) {
  131. if (task->prehandler != NULL) {
  132. hwif->OUTBSYNC(drive, tf->command, IDE_COMMAND_REG);
  133. ndelay(400); /* FIXME */
  134. return task->prehandler(drive, task->rq);
  135. }
  136. ide_execute_command(drive, tf->command, task->handler, WAIT_WORSTCASE, NULL);
  137. return ide_started;
  138. }
  139. if (task_dma_ok(task) && drive->using_dma && !hwif->dma_setup(drive)) {
  140. hwif->dma_exec_cmd(drive, tf->command);
  141. hwif->dma_start(drive);
  142. return ide_started;
  143. }
  144. return ide_stopped;
  145. }
  146. /*
  147. * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
  148. */
  149. ide_startstop_t set_multmode_intr (ide_drive_t *drive)
  150. {
  151. ide_hwif_t *hwif = HWIF(drive);
  152. u8 stat;
  153. if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
  154. drive->mult_count = drive->mult_req;
  155. } else {
  156. drive->mult_req = drive->mult_count = 0;
  157. drive->special.b.recalibrate = 1;
  158. (void) ide_dump_status(drive, "set_multmode", stat);
  159. }
  160. return ide_stopped;
  161. }
  162. /*
  163. * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
  164. */
  165. ide_startstop_t set_geometry_intr (ide_drive_t *drive)
  166. {
  167. ide_hwif_t *hwif = HWIF(drive);
  168. int retries = 5;
  169. u8 stat;
  170. while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
  171. udelay(10);
  172. if (OK_STAT(stat, READY_STAT, BAD_STAT))
  173. return ide_stopped;
  174. if (stat & (ERR_STAT|DRQ_STAT))
  175. return ide_error(drive, "set_geometry_intr", stat);
  176. BUG_ON(HWGROUP(drive)->handler != NULL);
  177. ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
  178. return ide_started;
  179. }
  180. /*
  181. * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
  182. */
  183. ide_startstop_t recal_intr (ide_drive_t *drive)
  184. {
  185. ide_hwif_t *hwif = HWIF(drive);
  186. u8 stat;
  187. if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
  188. return ide_error(drive, "recal_intr", stat);
  189. return ide_stopped;
  190. }
  191. /*
  192. * Handler for commands without a data phase
  193. */
  194. ide_startstop_t task_no_data_intr (ide_drive_t *drive)
  195. {
  196. ide_task_t *args = HWGROUP(drive)->rq->special;
  197. ide_hwif_t *hwif = HWIF(drive);
  198. u8 stat;
  199. local_irq_enable_in_hardirq();
  200. if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
  201. return ide_error(drive, "task_no_data_intr", stat);
  202. /* calls ide_end_drive_cmd */
  203. }
  204. if (args)
  205. ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
  206. return ide_stopped;
  207. }
  208. static u8 wait_drive_not_busy(ide_drive_t *drive)
  209. {
  210. ide_hwif_t *hwif = HWIF(drive);
  211. int retries;
  212. u8 stat;
  213. /*
  214. * Last sector was transfered, wait until drive is ready.
  215. * This can take up to 10 usec, but we will wait max 1 ms
  216. * (drive_cmd_intr() waits that long).
  217. */
  218. for (retries = 0; retries < 100; retries++) {
  219. if ((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT)
  220. udelay(10);
  221. else
  222. break;
  223. }
  224. if (stat & BUSY_STAT)
  225. printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
  226. return stat;
  227. }
  228. static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
  229. {
  230. ide_hwif_t *hwif = drive->hwif;
  231. struct scatterlist *sg = hwif->sg_table;
  232. struct scatterlist *cursg = hwif->cursg;
  233. struct page *page;
  234. #ifdef CONFIG_HIGHMEM
  235. unsigned long flags;
  236. #endif
  237. unsigned int offset;
  238. u8 *buf;
  239. cursg = hwif->cursg;
  240. if (!cursg) {
  241. cursg = sg;
  242. hwif->cursg = sg;
  243. }
  244. page = sg_page(cursg);
  245. offset = cursg->offset + hwif->cursg_ofs * SECTOR_SIZE;
  246. /* get the current page and offset */
  247. page = nth_page(page, (offset >> PAGE_SHIFT));
  248. offset %= PAGE_SIZE;
  249. #ifdef CONFIG_HIGHMEM
  250. local_irq_save(flags);
  251. #endif
  252. buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
  253. hwif->nleft--;
  254. hwif->cursg_ofs++;
  255. if ((hwif->cursg_ofs * SECTOR_SIZE) == cursg->length) {
  256. hwif->cursg = sg_next(hwif->cursg);
  257. hwif->cursg_ofs = 0;
  258. }
  259. /* do the actual data transfer */
  260. if (write)
  261. taskfile_output_data(drive, buf, SECTOR_WORDS);
  262. else
  263. taskfile_input_data(drive, buf, SECTOR_WORDS);
  264. kunmap_atomic(buf, KM_BIO_SRC_IRQ);
  265. #ifdef CONFIG_HIGHMEM
  266. local_irq_restore(flags);
  267. #endif
  268. }
  269. static void ide_pio_multi(ide_drive_t *drive, unsigned int write)
  270. {
  271. unsigned int nsect;
  272. nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
  273. while (nsect--)
  274. ide_pio_sector(drive, write);
  275. }
  276. static void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
  277. unsigned int write)
  278. {
  279. if (rq->bio) /* fs request */
  280. rq->errors = 0;
  281. touch_softlockup_watchdog();
  282. switch (drive->hwif->data_phase) {
  283. case TASKFILE_MULTI_IN:
  284. case TASKFILE_MULTI_OUT:
  285. ide_pio_multi(drive, write);
  286. break;
  287. default:
  288. ide_pio_sector(drive, write);
  289. break;
  290. }
  291. }
  292. static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
  293. const char *s, u8 stat)
  294. {
  295. if (rq->bio) {
  296. ide_hwif_t *hwif = drive->hwif;
  297. int sectors = hwif->nsect - hwif->nleft;
  298. switch (hwif->data_phase) {
  299. case TASKFILE_IN:
  300. if (hwif->nleft)
  301. break;
  302. /* fall through */
  303. case TASKFILE_OUT:
  304. sectors--;
  305. break;
  306. case TASKFILE_MULTI_IN:
  307. if (hwif->nleft)
  308. break;
  309. /* fall through */
  310. case TASKFILE_MULTI_OUT:
  311. sectors -= drive->mult_count;
  312. default:
  313. break;
  314. }
  315. if (sectors > 0) {
  316. ide_driver_t *drv;
  317. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  318. drv->end_request(drive, 1, sectors);
  319. }
  320. }
  321. return ide_error(drive, s, stat);
  322. }
  323. static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
  324. {
  325. HWIF(drive)->cursg = NULL;
  326. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
  327. ide_task_t *task = rq->special;
  328. if (task->tf_flags & IDE_TFLAG_FLAGGED) {
  329. u8 err = drive->hwif->INB(IDE_ERROR_REG);
  330. ide_end_drive_cmd(drive, stat, err);
  331. return;
  332. }
  333. }
  334. if (rq->rq_disk) {
  335. ide_driver_t *drv;
  336. drv = *(ide_driver_t **)rq->rq_disk->private_data;;
  337. drv->end_request(drive, 1, rq->hard_nr_sectors);
  338. } else
  339. ide_end_request(drive, 1, rq->hard_nr_sectors);
  340. }
  341. /*
  342. * Handler for command with PIO data-in phase (Read/Read Multiple).
  343. */
  344. ide_startstop_t task_in_intr (ide_drive_t *drive)
  345. {
  346. ide_hwif_t *hwif = drive->hwif;
  347. struct request *rq = HWGROUP(drive)->rq;
  348. u8 stat = hwif->INB(IDE_STATUS_REG);
  349. /* new way for dealing with premature shared PCI interrupts */
  350. if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
  351. if (stat & (ERR_STAT | DRQ_STAT))
  352. return task_error(drive, rq, __FUNCTION__, stat);
  353. /* No data yet, so wait for another IRQ. */
  354. ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
  355. return ide_started;
  356. }
  357. ide_pio_datablock(drive, rq, 0);
  358. /* If it was the last datablock check status and finish transfer. */
  359. if (!hwif->nleft) {
  360. stat = wait_drive_not_busy(drive);
  361. if (!OK_STAT(stat, 0, BAD_R_STAT))
  362. return task_error(drive, rq, __FUNCTION__, stat);
  363. task_end_request(drive, rq, stat);
  364. return ide_stopped;
  365. }
  366. /* Still data left to transfer. */
  367. ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
  368. return ide_started;
  369. }
  370. EXPORT_SYMBOL(task_in_intr);
  371. /*
  372. * Handler for command with PIO data-out phase (Write/Write Multiple).
  373. */
  374. static ide_startstop_t task_out_intr (ide_drive_t *drive)
  375. {
  376. ide_hwif_t *hwif = drive->hwif;
  377. struct request *rq = HWGROUP(drive)->rq;
  378. u8 stat = hwif->INB(IDE_STATUS_REG);
  379. if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
  380. return task_error(drive, rq, __FUNCTION__, stat);
  381. /* Deal with unexpected ATA data phase. */
  382. if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
  383. return task_error(drive, rq, __FUNCTION__, stat);
  384. if (!hwif->nleft) {
  385. task_end_request(drive, rq, stat);
  386. return ide_stopped;
  387. }
  388. /* Still data left to transfer. */
  389. ide_pio_datablock(drive, rq, 1);
  390. ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
  391. return ide_started;
  392. }
  393. ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq)
  394. {
  395. ide_startstop_t startstop;
  396. if (ide_wait_stat(&startstop, drive, DATA_READY,
  397. drive->bad_wstat, WAIT_DRQ)) {
  398. printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
  399. drive->name,
  400. drive->hwif->data_phase ? "MULT" : "",
  401. drive->addressing ? "_EXT" : "");
  402. return startstop;
  403. }
  404. if (!drive->unmask)
  405. local_irq_disable();
  406. ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
  407. ide_pio_datablock(drive, rq, 1);
  408. return ide_started;
  409. }
  410. EXPORT_SYMBOL(pre_task_out_intr);
  411. static int ide_diag_taskfile(ide_drive_t *drive, ide_task_t *args, unsigned long data_size, u8 *buf)
  412. {
  413. struct request rq;
  414. memset(&rq, 0, sizeof(rq));
  415. rq.ref_count = 1;
  416. rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
  417. rq.buffer = buf;
  418. /*
  419. * (ks) We transfer currently only whole sectors.
  420. * This is suffient for now. But, it would be great,
  421. * if we would find a solution to transfer any size.
  422. * To support special commands like READ LONG.
  423. */
  424. if (args->command_type != IDE_DRIVE_TASK_NO_DATA) {
  425. if (data_size == 0)
  426. rq.nr_sectors = (args->tf.hob_nsect << 8) | args->tf.nsect;
  427. else
  428. rq.nr_sectors = data_size / SECTOR_SIZE;
  429. if (!rq.nr_sectors) {
  430. printk(KERN_ERR "%s: in/out command without data\n",
  431. drive->name);
  432. return -EFAULT;
  433. }
  434. rq.hard_nr_sectors = rq.nr_sectors;
  435. rq.hard_cur_sectors = rq.current_nr_sectors = rq.nr_sectors;
  436. if (args->command_type == IDE_DRIVE_TASK_RAW_WRITE)
  437. rq.cmd_flags |= REQ_RW;
  438. }
  439. rq.special = args;
  440. args->rq = &rq;
  441. return ide_do_drive_cmd(drive, &rq, ide_wait);
  442. }
  443. int ide_raw_taskfile (ide_drive_t *drive, ide_task_t *args, u8 *buf)
  444. {
  445. return ide_diag_taskfile(drive, args, 0, buf);
  446. }
  447. EXPORT_SYMBOL(ide_raw_taskfile);
  448. int ide_no_data_taskfile(ide_drive_t *drive, ide_task_t *task)
  449. {
  450. task->command_type = IDE_DRIVE_TASK_NO_DATA;
  451. task->data_phase = TASKFILE_NO_DATA;
  452. task->handler = task_no_data_intr;
  453. return ide_raw_taskfile(drive, task, NULL);
  454. }
  455. EXPORT_SYMBOL_GPL(ide_no_data_taskfile);
  456. #ifdef CONFIG_IDE_TASK_IOCTL
  457. int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  458. {
  459. ide_task_request_t *req_task;
  460. ide_task_t args;
  461. u8 *outbuf = NULL;
  462. u8 *inbuf = NULL;
  463. int err = 0;
  464. int tasksize = sizeof(struct ide_task_request_s);
  465. unsigned int taskin = 0;
  466. unsigned int taskout = 0;
  467. u8 io_32bit = drive->io_32bit;
  468. char __user *buf = (char __user *)arg;
  469. // printk("IDE Taskfile ...\n");
  470. req_task = kzalloc(tasksize, GFP_KERNEL);
  471. if (req_task == NULL) return -ENOMEM;
  472. if (copy_from_user(req_task, buf, tasksize)) {
  473. kfree(req_task);
  474. return -EFAULT;
  475. }
  476. taskout = req_task->out_size;
  477. taskin = req_task->in_size;
  478. if (taskin > 65536 || taskout > 65536) {
  479. err = -EINVAL;
  480. goto abort;
  481. }
  482. if (taskout) {
  483. int outtotal = tasksize;
  484. outbuf = kzalloc(taskout, GFP_KERNEL);
  485. if (outbuf == NULL) {
  486. err = -ENOMEM;
  487. goto abort;
  488. }
  489. if (copy_from_user(outbuf, buf + outtotal, taskout)) {
  490. err = -EFAULT;
  491. goto abort;
  492. }
  493. }
  494. if (taskin) {
  495. int intotal = tasksize + taskout;
  496. inbuf = kzalloc(taskin, GFP_KERNEL);
  497. if (inbuf == NULL) {
  498. err = -ENOMEM;
  499. goto abort;
  500. }
  501. if (copy_from_user(inbuf, buf + intotal, taskin)) {
  502. err = -EFAULT;
  503. goto abort;
  504. }
  505. }
  506. memset(&args, 0, sizeof(ide_task_t));
  507. memcpy(&args.tf_array[0], req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE - 2);
  508. memcpy(&args.tf_array[6], req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
  509. args.tf_in_flags = req_task->in_flags;
  510. args.data_phase = req_task->data_phase;
  511. args.command_type = req_task->req_cmd;
  512. if (req_task->out_flags.all) {
  513. args.tf_flags |= IDE_TFLAG_FLAGGED;
  514. if (req_task->out_flags.b.data)
  515. args.tf_flags |= IDE_TFLAG_OUT_DATA;
  516. if (req_task->out_flags.b.nsector_hob)
  517. args.tf_flags |= IDE_TFLAG_OUT_HOB_NSECT;
  518. if (req_task->out_flags.b.sector_hob)
  519. args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAL;
  520. if (req_task->out_flags.b.lcyl_hob)
  521. args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAM;
  522. if (req_task->out_flags.b.hcyl_hob)
  523. args.tf_flags |= IDE_TFLAG_OUT_HOB_LBAH;
  524. if (req_task->out_flags.b.error_feature)
  525. args.tf_flags |= IDE_TFLAG_OUT_FEATURE;
  526. if (req_task->out_flags.b.nsector)
  527. args.tf_flags |= IDE_TFLAG_OUT_NSECT;
  528. if (req_task->out_flags.b.sector)
  529. args.tf_flags |= IDE_TFLAG_OUT_LBAL;
  530. if (req_task->out_flags.b.lcyl)
  531. args.tf_flags |= IDE_TFLAG_OUT_LBAM;
  532. if (req_task->out_flags.b.hcyl)
  533. args.tf_flags |= IDE_TFLAG_OUT_LBAH;
  534. }
  535. drive->io_32bit = 0;
  536. switch(req_task->data_phase) {
  537. case TASKFILE_OUT_DMAQ:
  538. case TASKFILE_OUT_DMA:
  539. err = ide_diag_taskfile(drive, &args, taskout, outbuf);
  540. break;
  541. case TASKFILE_IN_DMAQ:
  542. case TASKFILE_IN_DMA:
  543. err = ide_diag_taskfile(drive, &args, taskin, inbuf);
  544. break;
  545. case TASKFILE_MULTI_OUT:
  546. if (!drive->mult_count) {
  547. /* (hs): give up if multcount is not set */
  548. printk(KERN_ERR "%s: %s Multimode Write " \
  549. "multcount is not set\n",
  550. drive->name, __FUNCTION__);
  551. err = -EPERM;
  552. goto abort;
  553. }
  554. /* fall through */
  555. case TASKFILE_OUT:
  556. args.prehandler = &pre_task_out_intr;
  557. args.handler = &task_out_intr;
  558. err = ide_diag_taskfile(drive, &args, taskout, outbuf);
  559. break;
  560. case TASKFILE_MULTI_IN:
  561. if (!drive->mult_count) {
  562. /* (hs): give up if multcount is not set */
  563. printk(KERN_ERR "%s: %s Multimode Read failure " \
  564. "multcount is not set\n",
  565. drive->name, __FUNCTION__);
  566. err = -EPERM;
  567. goto abort;
  568. }
  569. /* fall through */
  570. case TASKFILE_IN:
  571. args.handler = &task_in_intr;
  572. err = ide_diag_taskfile(drive, &args, taskin, inbuf);
  573. break;
  574. case TASKFILE_NO_DATA:
  575. args.handler = &task_no_data_intr;
  576. err = ide_diag_taskfile(drive, &args, 0, NULL);
  577. break;
  578. default:
  579. err = -EFAULT;
  580. goto abort;
  581. }
  582. memcpy(req_task->hob_ports, &args.tf_array[0], HDIO_DRIVE_HOB_HDR_SIZE - 2);
  583. memcpy(req_task->io_ports, &args.tf_array[6], HDIO_DRIVE_TASK_HDR_SIZE);
  584. req_task->in_flags = args.tf_in_flags;
  585. if (copy_to_user(buf, req_task, tasksize)) {
  586. err = -EFAULT;
  587. goto abort;
  588. }
  589. if (taskout) {
  590. int outtotal = tasksize;
  591. if (copy_to_user(buf + outtotal, outbuf, taskout)) {
  592. err = -EFAULT;
  593. goto abort;
  594. }
  595. }
  596. if (taskin) {
  597. int intotal = tasksize + taskout;
  598. if (copy_to_user(buf + intotal, inbuf, taskin)) {
  599. err = -EFAULT;
  600. goto abort;
  601. }
  602. }
  603. abort:
  604. kfree(req_task);
  605. kfree(outbuf);
  606. kfree(inbuf);
  607. // printk("IDE Taskfile ioctl ended. rc = %i\n", err);
  608. drive->io_32bit = io_32bit;
  609. return err;
  610. }
  611. #endif
  612. int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
  613. {
  614. struct request rq;
  615. u8 buffer[4];
  616. if (!buf)
  617. buf = buffer;
  618. memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
  619. ide_init_drive_cmd(&rq);
  620. rq.buffer = buf;
  621. *buf++ = cmd;
  622. *buf++ = nsect;
  623. *buf++ = feature;
  624. *buf++ = sectors;
  625. return ide_do_drive_cmd(drive, &rq, ide_wait);
  626. }
  627. int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  628. {
  629. int err = 0;
  630. u8 args[4], *argbuf = args;
  631. u8 xfer_rate = 0;
  632. int argsize = 4;
  633. ide_task_t tfargs;
  634. struct ide_taskfile *tf = &tfargs.tf;
  635. if (NULL == (void *) arg) {
  636. struct request rq;
  637. ide_init_drive_cmd(&rq);
  638. return ide_do_drive_cmd(drive, &rq, ide_wait);
  639. }
  640. if (copy_from_user(args, (void __user *)arg, 4))
  641. return -EFAULT;
  642. memset(&tfargs, 0, sizeof(ide_task_t));
  643. tf->feature = args[2];
  644. tf->nsect = args[3];
  645. tf->lbal = args[1];
  646. tf->command = args[0];
  647. if (args[3]) {
  648. argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
  649. argbuf = kzalloc(argsize, GFP_KERNEL);
  650. if (argbuf == NULL)
  651. return -ENOMEM;
  652. }
  653. if (set_transfer(drive, &tfargs)) {
  654. xfer_rate = args[1];
  655. if (ide_ata66_check(drive, &tfargs))
  656. goto abort;
  657. }
  658. err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
  659. if (!err && xfer_rate) {
  660. /* active-retuning-calls future */
  661. ide_set_xfer_rate(drive, xfer_rate);
  662. ide_driveid_update(drive);
  663. }
  664. abort:
  665. if (copy_to_user((void __user *)arg, argbuf, argsize))
  666. err = -EFAULT;
  667. if (argsize > 4)
  668. kfree(argbuf);
  669. return err;
  670. }
  671. static int ide_wait_cmd_task(ide_drive_t *drive, u8 *buf)
  672. {
  673. struct request rq;
  674. ide_init_drive_cmd(&rq);
  675. rq.cmd_type = REQ_TYPE_ATA_TASK;
  676. rq.buffer = buf;
  677. return ide_do_drive_cmd(drive, &rq, ide_wait);
  678. }
  679. int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  680. {
  681. void __user *p = (void __user *)arg;
  682. int err = 0;
  683. u8 args[7], *argbuf = args;
  684. int argsize = 7;
  685. if (copy_from_user(args, p, 7))
  686. return -EFAULT;
  687. err = ide_wait_cmd_task(drive, argbuf);
  688. if (copy_to_user(p, argbuf, argsize))
  689. err = -EFAULT;
  690. return err;
  691. }
  692. /*
  693. * NOTICE: This is additions from IBM to provide a discrete interface,
  694. * for selective taskregister access operations. Nice JOB Klaus!!!
  695. * Glad to be able to work and co-develop this with you and IBM.
  696. */
  697. ide_startstop_t flagged_taskfile (ide_drive_t *drive, ide_task_t *task)
  698. {
  699. if (task->data_phase == TASKFILE_MULTI_IN ||
  700. task->data_phase == TASKFILE_MULTI_OUT) {
  701. if (!drive->mult_count) {
  702. printk(KERN_ERR "%s: multimode not set!\n", drive->name);
  703. return ide_stopped;
  704. }
  705. }
  706. /*
  707. * (ks) Check taskfile in flags.
  708. * If set, then execute as it is defined.
  709. * If not set, then define default settings.
  710. * The default values are:
  711. * read all taskfile registers (except data)
  712. * read the hob registers (sector, nsector, lcyl, hcyl)
  713. */
  714. if (task->tf_in_flags.all == 0) {
  715. task->tf_in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
  716. if (drive->addressing == 1)
  717. task->tf_in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
  718. }
  719. return do_rw_taskfile(drive, task);
  720. }