ide-taskfile.c 21 KB

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