ide-taskfile.c 21 KB

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