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