ide-taskfile.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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. * Problems to be fixed because of BH interface or the lack therefore.
  13. *
  14. * Fill me in stupid !!!
  15. *
  16. * HOST:
  17. * General refers to the Controller and Driver "pair".
  18. * DATA HANDLER:
  19. * Under the context of Linux it generally refers to an interrupt handler.
  20. * However, it correctly describes the 'HOST'
  21. * DATA BLOCK:
  22. * The amount of data needed to be transfered as predefined in the
  23. * setup of the device.
  24. * STORAGE ATOMIC:
  25. * The 'DATA BLOCK' associated to the 'DATA HANDLER', and can be as
  26. * small as a single sector or as large as the entire command block
  27. * request.
  28. */
  29. #include <linux/config.h>
  30. #include <linux/module.h>
  31. #include <linux/types.h>
  32. #include <linux/string.h>
  33. #include <linux/kernel.h>
  34. #include <linux/timer.h>
  35. #include <linux/mm.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/major.h>
  38. #include <linux/errno.h>
  39. #include <linux/genhd.h>
  40. #include <linux/blkpg.h>
  41. #include <linux/slab.h>
  42. #include <linux/pci.h>
  43. #include <linux/delay.h>
  44. #include <linux/hdreg.h>
  45. #include <linux/ide.h>
  46. #include <linux/bitops.h>
  47. #include <asm/byteorder.h>
  48. #include <asm/irq.h>
  49. #include <asm/uaccess.h>
  50. #include <asm/io.h>
  51. #define DEBUG_TASKFILE 0 /* unset when fixed */
  52. static void ata_bswap_data (void *buffer, int wcount)
  53. {
  54. u16 *p = buffer;
  55. while (wcount--) {
  56. *p = *p << 8 | *p >> 8; p++;
  57. *p = *p << 8 | *p >> 8; p++;
  58. }
  59. }
  60. static void taskfile_input_data(ide_drive_t *drive, void *buffer, u32 wcount)
  61. {
  62. HWIF(drive)->ata_input_data(drive, buffer, wcount);
  63. if (drive->bswap)
  64. ata_bswap_data(buffer, wcount);
  65. }
  66. static void taskfile_output_data(ide_drive_t *drive, void *buffer, u32 wcount)
  67. {
  68. if (drive->bswap) {
  69. ata_bswap_data(buffer, wcount);
  70. HWIF(drive)->ata_output_data(drive, buffer, wcount);
  71. ata_bswap_data(buffer, wcount);
  72. } else {
  73. HWIF(drive)->ata_output_data(drive, buffer, wcount);
  74. }
  75. }
  76. int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
  77. {
  78. ide_task_t args;
  79. memset(&args, 0, sizeof(ide_task_t));
  80. args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
  81. if (drive->media == ide_disk)
  82. args.tfRegister[IDE_COMMAND_OFFSET] = WIN_IDENTIFY;
  83. else
  84. args.tfRegister[IDE_COMMAND_OFFSET] = WIN_PIDENTIFY;
  85. args.command_type = IDE_DRIVE_TASK_IN;
  86. args.data_phase = TASKFILE_IN;
  87. args.handler = &task_in_intr;
  88. return ide_raw_taskfile(drive, &args, buf);
  89. }
  90. ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
  91. {
  92. ide_hwif_t *hwif = HWIF(drive);
  93. task_struct_t *taskfile = (task_struct_t *) task->tfRegister;
  94. hob_struct_t *hobfile = (hob_struct_t *) task->hobRegister;
  95. u8 HIHI = (drive->addressing == 1) ? 0xE0 : 0xEF;
  96. /* ALL Command Block Executions SHALL clear nIEN, unless otherwise */
  97. if (IDE_CONTROL_REG) {
  98. /* clear nIEN */
  99. hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
  100. }
  101. SELECT_MASK(drive, 0);
  102. if (drive->addressing == 1) {
  103. hwif->OUTB(hobfile->feature, IDE_FEATURE_REG);
  104. hwif->OUTB(hobfile->sector_count, IDE_NSECTOR_REG);
  105. hwif->OUTB(hobfile->sector_number, IDE_SECTOR_REG);
  106. hwif->OUTB(hobfile->low_cylinder, IDE_LCYL_REG);
  107. hwif->OUTB(hobfile->high_cylinder, IDE_HCYL_REG);
  108. }
  109. hwif->OUTB(taskfile->feature, IDE_FEATURE_REG);
  110. hwif->OUTB(taskfile->sector_count, IDE_NSECTOR_REG);
  111. hwif->OUTB(taskfile->sector_number, IDE_SECTOR_REG);
  112. hwif->OUTB(taskfile->low_cylinder, IDE_LCYL_REG);
  113. hwif->OUTB(taskfile->high_cylinder, IDE_HCYL_REG);
  114. hwif->OUTB((taskfile->device_head & HIHI) | drive->select.all, IDE_SELECT_REG);
  115. if (task->handler != NULL) {
  116. if (task->prehandler != NULL) {
  117. hwif->OUTBSYNC(drive, taskfile->command, IDE_COMMAND_REG);
  118. ndelay(400); /* FIXME */
  119. return task->prehandler(drive, task->rq);
  120. }
  121. ide_execute_command(drive, taskfile->command, task->handler, WAIT_WORSTCASE, NULL);
  122. return ide_started;
  123. }
  124. if (!drive->using_dma)
  125. return ide_stopped;
  126. switch (taskfile->command) {
  127. case WIN_WRITEDMA_ONCE:
  128. case WIN_WRITEDMA:
  129. case WIN_WRITEDMA_EXT:
  130. case WIN_READDMA_ONCE:
  131. case WIN_READDMA:
  132. case WIN_READDMA_EXT:
  133. case WIN_IDENTIFY_DMA:
  134. if (!hwif->dma_setup(drive)) {
  135. hwif->dma_exec_cmd(drive, taskfile->command);
  136. hwif->dma_start(drive);
  137. return ide_started;
  138. }
  139. break;
  140. default:
  141. if (task->handler == NULL)
  142. return ide_stopped;
  143. }
  144. return ide_stopped;
  145. }
  146. /*
  147. * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
  148. */
  149. ide_startstop_t set_multmode_intr (ide_drive_t *drive)
  150. {
  151. ide_hwif_t *hwif = HWIF(drive);
  152. u8 stat;
  153. if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
  154. drive->mult_count = drive->mult_req;
  155. } else {
  156. drive->mult_req = drive->mult_count = 0;
  157. drive->special.b.recalibrate = 1;
  158. (void) ide_dump_status(drive, "set_multmode", stat);
  159. }
  160. return ide_stopped;
  161. }
  162. /*
  163. * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
  164. */
  165. ide_startstop_t set_geometry_intr (ide_drive_t *drive)
  166. {
  167. ide_hwif_t *hwif = HWIF(drive);
  168. int retries = 5;
  169. u8 stat;
  170. while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
  171. udelay(10);
  172. if (OK_STAT(stat, READY_STAT, BAD_STAT))
  173. return ide_stopped;
  174. if (stat & (ERR_STAT|DRQ_STAT))
  175. return ide_error(drive, "set_geometry_intr", stat);
  176. if (HWGROUP(drive)->handler != NULL)
  177. BUG();
  178. ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
  179. return ide_started;
  180. }
  181. /*
  182. * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
  183. */
  184. ide_startstop_t recal_intr (ide_drive_t *drive)
  185. {
  186. ide_hwif_t *hwif = HWIF(drive);
  187. u8 stat;
  188. if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
  189. return ide_error(drive, "recal_intr", stat);
  190. return ide_stopped;
  191. }
  192. /*
  193. * Handler for commands without a data phase
  194. */
  195. ide_startstop_t task_no_data_intr (ide_drive_t *drive)
  196. {
  197. ide_task_t *args = HWGROUP(drive)->rq->special;
  198. ide_hwif_t *hwif = HWIF(drive);
  199. u8 stat;
  200. local_irq_enable();
  201. if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
  202. return ide_error(drive, "task_no_data_intr", stat);
  203. /* calls ide_end_drive_cmd */
  204. }
  205. if (args)
  206. ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
  207. return ide_stopped;
  208. }
  209. EXPORT_SYMBOL(task_no_data_intr);
  210. static u8 wait_drive_not_busy(ide_drive_t *drive)
  211. {
  212. ide_hwif_t *hwif = HWIF(drive);
  213. int retries = 100;
  214. u8 stat;
  215. /*
  216. * Last sector was transfered, wait until drive is ready.
  217. * This can take up to 10 usec, but we will wait max 1 ms
  218. * (drive_cmd_intr() waits that long).
  219. */
  220. while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
  221. udelay(10);
  222. if (!retries)
  223. printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
  224. return stat;
  225. }
  226. static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
  227. {
  228. ide_hwif_t *hwif = drive->hwif;
  229. struct scatterlist *sg = hwif->sg_table;
  230. struct page *page;
  231. #ifdef CONFIG_HIGHMEM
  232. unsigned long flags;
  233. #endif
  234. unsigned int offset;
  235. u8 *buf;
  236. page = sg[hwif->cursg].page;
  237. offset = sg[hwif->cursg].offset + hwif->cursg_ofs * SECTOR_SIZE;
  238. /* get the current page and offset */
  239. page = nth_page(page, (offset >> PAGE_SHIFT));
  240. offset %= PAGE_SIZE;
  241. #ifdef CONFIG_HIGHMEM
  242. local_irq_save(flags);
  243. #endif
  244. buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
  245. hwif->nleft--;
  246. hwif->cursg_ofs++;
  247. if ((hwif->cursg_ofs * SECTOR_SIZE) == sg[hwif->cursg].length) {
  248. hwif->cursg++;
  249. hwif->cursg_ofs = 0;
  250. }
  251. /* do the actual data transfer */
  252. if (write)
  253. taskfile_output_data(drive, buf, SECTOR_WORDS);
  254. else
  255. taskfile_input_data(drive, buf, SECTOR_WORDS);
  256. kunmap_atomic(buf, KM_BIO_SRC_IRQ);
  257. #ifdef CONFIG_HIGHMEM
  258. local_irq_restore(flags);
  259. #endif
  260. }
  261. static void ide_pio_multi(ide_drive_t *drive, unsigned int write)
  262. {
  263. unsigned int nsect;
  264. nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
  265. while (nsect--)
  266. ide_pio_sector(drive, write);
  267. }
  268. static inline void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
  269. unsigned int write)
  270. {
  271. if (rq->bio) /* fs request */
  272. rq->errors = 0;
  273. switch (drive->hwif->data_phase) {
  274. case TASKFILE_MULTI_IN:
  275. case TASKFILE_MULTI_OUT:
  276. ide_pio_multi(drive, write);
  277. break;
  278. default:
  279. ide_pio_sector(drive, write);
  280. break;
  281. }
  282. }
  283. static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
  284. const char *s, u8 stat)
  285. {
  286. if (rq->bio) {
  287. ide_hwif_t *hwif = drive->hwif;
  288. int sectors = hwif->nsect - hwif->nleft;
  289. switch (hwif->data_phase) {
  290. case TASKFILE_IN:
  291. if (hwif->nleft)
  292. break;
  293. /* fall through */
  294. case TASKFILE_OUT:
  295. sectors--;
  296. break;
  297. case TASKFILE_MULTI_IN:
  298. if (hwif->nleft)
  299. break;
  300. /* fall through */
  301. case TASKFILE_MULTI_OUT:
  302. sectors -= drive->mult_count;
  303. default:
  304. break;
  305. }
  306. if (sectors > 0) {
  307. ide_driver_t *drv;
  308. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  309. drv->end_request(drive, 1, sectors);
  310. }
  311. }
  312. return ide_error(drive, s, stat);
  313. }
  314. static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
  315. {
  316. if (rq->flags & REQ_DRIVE_TASKFILE) {
  317. ide_task_t *task = rq->special;
  318. if (task->tf_out_flags.all) {
  319. u8 err = drive->hwif->INB(IDE_ERROR_REG);
  320. ide_end_drive_cmd(drive, stat, err);
  321. return;
  322. }
  323. }
  324. ide_end_request(drive, 1, rq->hard_nr_sectors);
  325. }
  326. /*
  327. * Handler for command with PIO data-in phase (Read/Read Multiple).
  328. */
  329. ide_startstop_t task_in_intr (ide_drive_t *drive)
  330. {
  331. ide_hwif_t *hwif = drive->hwif;
  332. struct request *rq = HWGROUP(drive)->rq;
  333. u8 stat = hwif->INB(IDE_STATUS_REG);
  334. /* new way for dealing with premature shared PCI interrupts */
  335. if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
  336. if (stat & (ERR_STAT | DRQ_STAT))
  337. return task_error(drive, rq, __FUNCTION__, stat);
  338. /* No data yet, so wait for another IRQ. */
  339. ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
  340. return ide_started;
  341. }
  342. ide_pio_datablock(drive, rq, 0);
  343. /* If it was the last datablock check status and finish transfer. */
  344. if (!hwif->nleft) {
  345. stat = wait_drive_not_busy(drive);
  346. if (!OK_STAT(stat, 0, BAD_R_STAT))
  347. return task_error(drive, rq, __FUNCTION__, stat);
  348. task_end_request(drive, rq, stat);
  349. return ide_stopped;
  350. }
  351. /* Still data left to transfer. */
  352. ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
  353. return ide_started;
  354. }
  355. EXPORT_SYMBOL(task_in_intr);
  356. /*
  357. * Handler for command with PIO data-out phase (Write/Write Multiple).
  358. */
  359. static ide_startstop_t task_out_intr (ide_drive_t *drive)
  360. {
  361. ide_hwif_t *hwif = drive->hwif;
  362. struct request *rq = HWGROUP(drive)->rq;
  363. u8 stat = hwif->INB(IDE_STATUS_REG);
  364. if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
  365. return task_error(drive, rq, __FUNCTION__, stat);
  366. /* Deal with unexpected ATA data phase. */
  367. if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
  368. return task_error(drive, rq, __FUNCTION__, stat);
  369. if (!hwif->nleft) {
  370. task_end_request(drive, rq, stat);
  371. return ide_stopped;
  372. }
  373. /* Still data left to transfer. */
  374. ide_pio_datablock(drive, rq, 1);
  375. ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
  376. return ide_started;
  377. }
  378. ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq)
  379. {
  380. ide_startstop_t startstop;
  381. if (ide_wait_stat(&startstop, drive, DATA_READY,
  382. drive->bad_wstat, WAIT_DRQ)) {
  383. printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
  384. drive->name,
  385. drive->hwif->data_phase ? "MULT" : "",
  386. drive->addressing ? "_EXT" : "");
  387. return startstop;
  388. }
  389. if (!drive->unmask)
  390. local_irq_disable();
  391. ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
  392. ide_pio_datablock(drive, rq, 1);
  393. return ide_started;
  394. }
  395. EXPORT_SYMBOL(pre_task_out_intr);
  396. static int ide_diag_taskfile(ide_drive_t *drive, ide_task_t *args, unsigned long data_size, u8 *buf)
  397. {
  398. struct request rq;
  399. memset(&rq, 0, sizeof(rq));
  400. rq.flags = REQ_DRIVE_TASKFILE;
  401. rq.buffer = buf;
  402. /*
  403. * (ks) We transfer currently only whole sectors.
  404. * This is suffient for now. But, it would be great,
  405. * if we would find a solution to transfer any size.
  406. * To support special commands like READ LONG.
  407. */
  408. if (args->command_type != IDE_DRIVE_TASK_NO_DATA) {
  409. if (data_size == 0)
  410. rq.nr_sectors = (args->hobRegister[IDE_NSECTOR_OFFSET] << 8) | args->tfRegister[IDE_NSECTOR_OFFSET];
  411. else
  412. rq.nr_sectors = data_size / SECTOR_SIZE;
  413. if (!rq.nr_sectors) {
  414. printk(KERN_ERR "%s: in/out command without data\n",
  415. drive->name);
  416. return -EFAULT;
  417. }
  418. rq.hard_nr_sectors = rq.nr_sectors;
  419. rq.hard_cur_sectors = rq.current_nr_sectors = rq.nr_sectors;
  420. if (args->command_type == IDE_DRIVE_TASK_RAW_WRITE)
  421. rq.flags |= REQ_RW;
  422. }
  423. rq.special = args;
  424. args->rq = &rq;
  425. return ide_do_drive_cmd(drive, &rq, ide_wait);
  426. }
  427. int ide_raw_taskfile (ide_drive_t *drive, ide_task_t *args, u8 *buf)
  428. {
  429. return ide_diag_taskfile(drive, args, 0, buf);
  430. }
  431. EXPORT_SYMBOL(ide_raw_taskfile);
  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. task_ioreg_t *argsptr = args.tfRegister;
  439. task_ioreg_t *hobsptr = args.hobRegister;
  440. int err = 0;
  441. int tasksize = sizeof(struct ide_task_request_s);
  442. int taskin = 0;
  443. int taskout = 0;
  444. u8 io_32bit = drive->io_32bit;
  445. char __user *buf = (char __user *)arg;
  446. // printk("IDE Taskfile ...\n");
  447. req_task = kzalloc(tasksize, GFP_KERNEL);
  448. if (req_task == NULL) return -ENOMEM;
  449. if (copy_from_user(req_task, buf, tasksize)) {
  450. kfree(req_task);
  451. return -EFAULT;
  452. }
  453. taskout = (int) req_task->out_size;
  454. taskin = (int) req_task->in_size;
  455. if (taskout) {
  456. int outtotal = tasksize;
  457. outbuf = kzalloc(taskout, GFP_KERNEL);
  458. if (outbuf == NULL) {
  459. err = -ENOMEM;
  460. goto abort;
  461. }
  462. if (copy_from_user(outbuf, buf + outtotal, taskout)) {
  463. err = -EFAULT;
  464. goto abort;
  465. }
  466. }
  467. if (taskin) {
  468. int intotal = tasksize + taskout;
  469. inbuf = kzalloc(taskin, GFP_KERNEL);
  470. if (inbuf == NULL) {
  471. err = -ENOMEM;
  472. goto abort;
  473. }
  474. if (copy_from_user(inbuf, buf + intotal, taskin)) {
  475. err = -EFAULT;
  476. goto abort;
  477. }
  478. }
  479. memset(&args, 0, sizeof(ide_task_t));
  480. memcpy(argsptr, req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
  481. memcpy(hobsptr, req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE);
  482. args.tf_in_flags = req_task->in_flags;
  483. args.tf_out_flags = req_task->out_flags;
  484. args.data_phase = req_task->data_phase;
  485. args.command_type = req_task->req_cmd;
  486. drive->io_32bit = 0;
  487. switch(req_task->data_phase) {
  488. case TASKFILE_OUT_DMAQ:
  489. case TASKFILE_OUT_DMA:
  490. err = ide_diag_taskfile(drive, &args, taskout, outbuf);
  491. break;
  492. case TASKFILE_IN_DMAQ:
  493. case TASKFILE_IN_DMA:
  494. err = ide_diag_taskfile(drive, &args, taskin, inbuf);
  495. break;
  496. case TASKFILE_MULTI_OUT:
  497. if (!drive->mult_count) {
  498. /* (hs): give up if multcount is not set */
  499. printk(KERN_ERR "%s: %s Multimode Write " \
  500. "multcount is not set\n",
  501. drive->name, __FUNCTION__);
  502. err = -EPERM;
  503. goto abort;
  504. }
  505. /* fall through */
  506. case TASKFILE_OUT:
  507. args.prehandler = &pre_task_out_intr;
  508. args.handler = &task_out_intr;
  509. err = ide_diag_taskfile(drive, &args, taskout, outbuf);
  510. break;
  511. case TASKFILE_MULTI_IN:
  512. if (!drive->mult_count) {
  513. /* (hs): give up if multcount is not set */
  514. printk(KERN_ERR "%s: %s Multimode Read failure " \
  515. "multcount is not set\n",
  516. drive->name, __FUNCTION__);
  517. err = -EPERM;
  518. goto abort;
  519. }
  520. /* fall through */
  521. case TASKFILE_IN:
  522. args.handler = &task_in_intr;
  523. err = ide_diag_taskfile(drive, &args, taskin, inbuf);
  524. break;
  525. case TASKFILE_NO_DATA:
  526. args.handler = &task_no_data_intr;
  527. err = ide_diag_taskfile(drive, &args, 0, NULL);
  528. break;
  529. default:
  530. err = -EFAULT;
  531. goto abort;
  532. }
  533. memcpy(req_task->io_ports, &(args.tfRegister), HDIO_DRIVE_TASK_HDR_SIZE);
  534. memcpy(req_task->hob_ports, &(args.hobRegister), HDIO_DRIVE_HOB_HDR_SIZE);
  535. req_task->in_flags = args.tf_in_flags;
  536. req_task->out_flags = args.tf_out_flags;
  537. if (copy_to_user(buf, req_task, tasksize)) {
  538. err = -EFAULT;
  539. goto abort;
  540. }
  541. if (taskout) {
  542. int outtotal = tasksize;
  543. if (copy_to_user(buf + outtotal, outbuf, taskout)) {
  544. err = -EFAULT;
  545. goto abort;
  546. }
  547. }
  548. if (taskin) {
  549. int intotal = tasksize + taskout;
  550. if (copy_to_user(buf + intotal, inbuf, taskin)) {
  551. err = -EFAULT;
  552. goto abort;
  553. }
  554. }
  555. abort:
  556. kfree(req_task);
  557. kfree(outbuf);
  558. kfree(inbuf);
  559. // printk("IDE Taskfile ioctl ended. rc = %i\n", err);
  560. drive->io_32bit = io_32bit;
  561. return err;
  562. }
  563. int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
  564. {
  565. struct request rq;
  566. u8 buffer[4];
  567. if (!buf)
  568. buf = buffer;
  569. memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
  570. ide_init_drive_cmd(&rq);
  571. rq.buffer = buf;
  572. *buf++ = cmd;
  573. *buf++ = nsect;
  574. *buf++ = feature;
  575. *buf++ = sectors;
  576. return ide_do_drive_cmd(drive, &rq, ide_wait);
  577. }
  578. /*
  579. * FIXME : this needs to map into at taskfile. <andre@linux-ide.org>
  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.flags = REQ_DRIVE_TASK;
  632. rq.buffer = buf;
  633. return ide_do_drive_cmd(drive, &rq, ide_wait);
  634. }
  635. /*
  636. * FIXME : this needs to map into at taskfile. <andre@linux-ide.org>
  637. */
  638. int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  639. {
  640. void __user *p = (void __user *)arg;
  641. int err = 0;
  642. u8 args[7], *argbuf = args;
  643. int argsize = 7;
  644. if (copy_from_user(args, p, 7))
  645. return -EFAULT;
  646. err = ide_wait_cmd_task(drive, argbuf);
  647. if (copy_to_user(p, argbuf, argsize))
  648. err = -EFAULT;
  649. return err;
  650. }
  651. /*
  652. * NOTICE: This is additions from IBM to provide a discrete interface,
  653. * for selective taskregister access operations. Nice JOB Klaus!!!
  654. * Glad to be able to work and co-develop this with you and IBM.
  655. */
  656. ide_startstop_t flagged_taskfile (ide_drive_t *drive, ide_task_t *task)
  657. {
  658. ide_hwif_t *hwif = HWIF(drive);
  659. task_struct_t *taskfile = (task_struct_t *) task->tfRegister;
  660. hob_struct_t *hobfile = (hob_struct_t *) task->hobRegister;
  661. #if DEBUG_TASKFILE
  662. u8 status;
  663. #endif
  664. if (task->data_phase == TASKFILE_MULTI_IN ||
  665. task->data_phase == TASKFILE_MULTI_OUT) {
  666. if (!drive->mult_count) {
  667. printk(KERN_ERR "%s: multimode not set!\n", drive->name);
  668. return ide_stopped;
  669. }
  670. }
  671. /*
  672. * (ks) Check taskfile in/out flags.
  673. * If set, then execute as it is defined.
  674. * If not set, then define default settings.
  675. * The default values are:
  676. * write and read all taskfile registers (except data)
  677. * write and read the hob registers (sector,nsector,lcyl,hcyl)
  678. */
  679. if (task->tf_out_flags.all == 0) {
  680. task->tf_out_flags.all = IDE_TASKFILE_STD_OUT_FLAGS;
  681. if (drive->addressing == 1)
  682. task->tf_out_flags.all |= (IDE_HOB_STD_OUT_FLAGS << 8);
  683. }
  684. if (task->tf_in_flags.all == 0) {
  685. task->tf_in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
  686. if (drive->addressing == 1)
  687. task->tf_in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
  688. }
  689. /* ALL Command Block Executions SHALL clear nIEN, unless otherwise */
  690. if (IDE_CONTROL_REG)
  691. /* clear nIEN */
  692. hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
  693. SELECT_MASK(drive, 0);
  694. #if DEBUG_TASKFILE
  695. status = hwif->INB(IDE_STATUS_REG);
  696. if (status & 0x80) {
  697. printk("flagged_taskfile -> Bad status. Status = %02x. wait 100 usec ...\n", status);
  698. udelay(100);
  699. status = hwif->INB(IDE_STATUS_REG);
  700. printk("flagged_taskfile -> Status = %02x\n", status);
  701. }
  702. #endif
  703. if (task->tf_out_flags.b.data) {
  704. u16 data = taskfile->data + (hobfile->data << 8);
  705. hwif->OUTW(data, IDE_DATA_REG);
  706. }
  707. /* (ks) send hob registers first */
  708. if (task->tf_out_flags.b.nsector_hob)
  709. hwif->OUTB(hobfile->sector_count, IDE_NSECTOR_REG);
  710. if (task->tf_out_flags.b.sector_hob)
  711. hwif->OUTB(hobfile->sector_number, IDE_SECTOR_REG);
  712. if (task->tf_out_flags.b.lcyl_hob)
  713. hwif->OUTB(hobfile->low_cylinder, IDE_LCYL_REG);
  714. if (task->tf_out_flags.b.hcyl_hob)
  715. hwif->OUTB(hobfile->high_cylinder, IDE_HCYL_REG);
  716. /* (ks) Send now the standard registers */
  717. if (task->tf_out_flags.b.error_feature)
  718. hwif->OUTB(taskfile->feature, IDE_FEATURE_REG);
  719. /* refers to number of sectors to transfer */
  720. if (task->tf_out_flags.b.nsector)
  721. hwif->OUTB(taskfile->sector_count, IDE_NSECTOR_REG);
  722. /* refers to sector offset or start sector */
  723. if (task->tf_out_flags.b.sector)
  724. hwif->OUTB(taskfile->sector_number, IDE_SECTOR_REG);
  725. if (task->tf_out_flags.b.lcyl)
  726. hwif->OUTB(taskfile->low_cylinder, IDE_LCYL_REG);
  727. if (task->tf_out_flags.b.hcyl)
  728. hwif->OUTB(taskfile->high_cylinder, IDE_HCYL_REG);
  729. /*
  730. * (ks) In the flagged taskfile approch, we will use all specified
  731. * registers and the register value will not be changed, except the
  732. * select bit (master/slave) in the drive_head register. We must make
  733. * sure that the desired drive is selected.
  734. */
  735. hwif->OUTB(taskfile->device_head | drive->select.all, IDE_SELECT_REG);
  736. switch(task->data_phase) {
  737. case TASKFILE_OUT_DMAQ:
  738. case TASKFILE_OUT_DMA:
  739. case TASKFILE_IN_DMAQ:
  740. case TASKFILE_IN_DMA:
  741. hwif->dma_setup(drive);
  742. hwif->dma_exec_cmd(drive, taskfile->command);
  743. hwif->dma_start(drive);
  744. break;
  745. default:
  746. if (task->handler == NULL)
  747. return ide_stopped;
  748. /* Issue the command */
  749. if (task->prehandler) {
  750. hwif->OUTBSYNC(drive, taskfile->command, IDE_COMMAND_REG);
  751. ndelay(400); /* FIXME */
  752. return task->prehandler(drive, task->rq);
  753. }
  754. ide_execute_command(drive, taskfile->command, task->handler, WAIT_WORSTCASE, NULL);
  755. }
  756. return ide_started;
  757. }