ide-taskfile.c 21 KB

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