ide-taskfile.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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. EXPORT_SYMBOL(do_rw_taskfile);
  147. /*
  148. * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
  149. */
  150. ide_startstop_t set_multmode_intr (ide_drive_t *drive)
  151. {
  152. ide_hwif_t *hwif = HWIF(drive);
  153. u8 stat;
  154. if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
  155. drive->mult_count = drive->mult_req;
  156. } else {
  157. drive->mult_req = drive->mult_count = 0;
  158. drive->special.b.recalibrate = 1;
  159. (void) ide_dump_status(drive, "set_multmode", stat);
  160. }
  161. return ide_stopped;
  162. }
  163. /*
  164. * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
  165. */
  166. ide_startstop_t set_geometry_intr (ide_drive_t *drive)
  167. {
  168. ide_hwif_t *hwif = HWIF(drive);
  169. int retries = 5;
  170. u8 stat;
  171. while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
  172. udelay(10);
  173. if (OK_STAT(stat, READY_STAT, BAD_STAT))
  174. return ide_stopped;
  175. if (stat & (ERR_STAT|DRQ_STAT))
  176. return ide_error(drive, "set_geometry_intr", stat);
  177. if (HWGROUP(drive)->handler != NULL)
  178. BUG();
  179. ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
  180. return ide_started;
  181. }
  182. /*
  183. * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
  184. */
  185. ide_startstop_t recal_intr (ide_drive_t *drive)
  186. {
  187. ide_hwif_t *hwif = HWIF(drive);
  188. u8 stat;
  189. if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
  190. return ide_error(drive, "recal_intr", stat);
  191. return ide_stopped;
  192. }
  193. /*
  194. * Handler for commands without a data phase
  195. */
  196. ide_startstop_t task_no_data_intr (ide_drive_t *drive)
  197. {
  198. ide_task_t *args = HWGROUP(drive)->rq->special;
  199. ide_hwif_t *hwif = HWIF(drive);
  200. u8 stat;
  201. local_irq_enable();
  202. if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
  203. return ide_error(drive, "task_no_data_intr", stat);
  204. /* calls ide_end_drive_cmd */
  205. }
  206. if (args)
  207. ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
  208. return ide_stopped;
  209. }
  210. EXPORT_SYMBOL(task_no_data_intr);
  211. static u8 wait_drive_not_busy(ide_drive_t *drive)
  212. {
  213. ide_hwif_t *hwif = HWIF(drive);
  214. int retries = 100;
  215. u8 stat;
  216. /*
  217. * Last sector was transfered, wait until drive is ready.
  218. * This can take up to 10 usec, but we will wait max 1 ms
  219. * (drive_cmd_intr() waits that long).
  220. */
  221. while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
  222. udelay(10);
  223. if (!retries)
  224. printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
  225. return stat;
  226. }
  227. static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
  228. {
  229. ide_hwif_t *hwif = drive->hwif;
  230. struct scatterlist *sg = hwif->sg_table;
  231. struct page *page;
  232. #ifdef CONFIG_HIGHMEM
  233. unsigned long flags;
  234. #endif
  235. unsigned int offset;
  236. u8 *buf;
  237. page = sg[hwif->cursg].page;
  238. offset = sg[hwif->cursg].offset + hwif->cursg_ofs * SECTOR_SIZE;
  239. /* get the current page and offset */
  240. page = nth_page(page, (offset >> PAGE_SHIFT));
  241. offset %= PAGE_SIZE;
  242. #ifdef CONFIG_HIGHMEM
  243. local_irq_save(flags);
  244. #endif
  245. buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
  246. hwif->nleft--;
  247. hwif->cursg_ofs++;
  248. if ((hwif->cursg_ofs * SECTOR_SIZE) == sg[hwif->cursg].length) {
  249. hwif->cursg++;
  250. hwif->cursg_ofs = 0;
  251. }
  252. /* do the actual data transfer */
  253. if (write)
  254. taskfile_output_data(drive, buf, SECTOR_WORDS);
  255. else
  256. taskfile_input_data(drive, buf, SECTOR_WORDS);
  257. kunmap_atomic(buf, KM_BIO_SRC_IRQ);
  258. #ifdef CONFIG_HIGHMEM
  259. local_irq_restore(flags);
  260. #endif
  261. }
  262. static void ide_pio_multi(ide_drive_t *drive, unsigned int write)
  263. {
  264. unsigned int nsect;
  265. nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
  266. while (nsect--)
  267. ide_pio_sector(drive, write);
  268. }
  269. static inline void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
  270. unsigned int write)
  271. {
  272. if (rq->bio) /* fs request */
  273. rq->errors = 0;
  274. switch (drive->hwif->data_phase) {
  275. case TASKFILE_MULTI_IN:
  276. case TASKFILE_MULTI_OUT:
  277. ide_pio_multi(drive, write);
  278. break;
  279. default:
  280. ide_pio_sector(drive, write);
  281. break;
  282. }
  283. }
  284. static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
  285. const char *s, u8 stat)
  286. {
  287. if (rq->bio) {
  288. ide_hwif_t *hwif = drive->hwif;
  289. int sectors = hwif->nsect - hwif->nleft;
  290. switch (hwif->data_phase) {
  291. case TASKFILE_IN:
  292. if (hwif->nleft)
  293. break;
  294. /* fall through */
  295. case TASKFILE_OUT:
  296. sectors--;
  297. break;
  298. case TASKFILE_MULTI_IN:
  299. if (hwif->nleft)
  300. break;
  301. /* fall through */
  302. case TASKFILE_MULTI_OUT:
  303. sectors -= drive->mult_count;
  304. default:
  305. break;
  306. }
  307. if (sectors > 0) {
  308. ide_driver_t *drv;
  309. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  310. drv->end_request(drive, 1, sectors);
  311. }
  312. }
  313. return ide_error(drive, s, stat);
  314. }
  315. static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
  316. {
  317. if (rq->flags & REQ_DRIVE_TASKFILE) {
  318. ide_task_t *task = rq->special;
  319. if (task->tf_out_flags.all) {
  320. u8 err = drive->hwif->INB(IDE_ERROR_REG);
  321. ide_end_drive_cmd(drive, stat, err);
  322. return;
  323. }
  324. }
  325. ide_end_request(drive, 1, rq->hard_nr_sectors);
  326. }
  327. /*
  328. * Handler for command with PIO data-in phase (Read/Read Multiple).
  329. */
  330. ide_startstop_t task_in_intr (ide_drive_t *drive)
  331. {
  332. ide_hwif_t *hwif = drive->hwif;
  333. struct request *rq = HWGROUP(drive)->rq;
  334. u8 stat = hwif->INB(IDE_STATUS_REG);
  335. /* new way for dealing with premature shared PCI interrupts */
  336. if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
  337. if (stat & (ERR_STAT | DRQ_STAT))
  338. return task_error(drive, rq, __FUNCTION__, stat);
  339. /* No data yet, so wait for another IRQ. */
  340. ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
  341. return ide_started;
  342. }
  343. ide_pio_datablock(drive, rq, 0);
  344. /* If it was the last datablock check status and finish transfer. */
  345. if (!hwif->nleft) {
  346. stat = wait_drive_not_busy(drive);
  347. if (!OK_STAT(stat, 0, BAD_R_STAT))
  348. return task_error(drive, rq, __FUNCTION__, stat);
  349. task_end_request(drive, rq, stat);
  350. return ide_stopped;
  351. }
  352. /* Still data left to transfer. */
  353. ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
  354. return ide_started;
  355. }
  356. EXPORT_SYMBOL(task_in_intr);
  357. /*
  358. * Handler for command with PIO data-out phase (Write/Write Multiple).
  359. */
  360. static ide_startstop_t task_out_intr (ide_drive_t *drive)
  361. {
  362. ide_hwif_t *hwif = drive->hwif;
  363. struct request *rq = HWGROUP(drive)->rq;
  364. u8 stat = hwif->INB(IDE_STATUS_REG);
  365. if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
  366. return task_error(drive, rq, __FUNCTION__, stat);
  367. /* Deal with unexpected ATA data phase. */
  368. if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
  369. return task_error(drive, rq, __FUNCTION__, stat);
  370. if (!hwif->nleft) {
  371. task_end_request(drive, rq, stat);
  372. return ide_stopped;
  373. }
  374. /* Still data left to transfer. */
  375. ide_pio_datablock(drive, rq, 1);
  376. ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
  377. return ide_started;
  378. }
  379. ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq)
  380. {
  381. ide_startstop_t startstop;
  382. if (ide_wait_stat(&startstop, drive, DATA_READY,
  383. drive->bad_wstat, WAIT_DRQ)) {
  384. printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
  385. drive->name,
  386. drive->hwif->data_phase ? "MULT" : "",
  387. drive->addressing ? "_EXT" : "");
  388. return startstop;
  389. }
  390. if (!drive->unmask)
  391. local_irq_disable();
  392. ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
  393. ide_pio_datablock(drive, rq, 1);
  394. return ide_started;
  395. }
  396. EXPORT_SYMBOL(pre_task_out_intr);
  397. static int ide_diag_taskfile(ide_drive_t *drive, ide_task_t *args, unsigned long data_size, u8 *buf)
  398. {
  399. struct request rq;
  400. memset(&rq, 0, sizeof(rq));
  401. rq.flags = REQ_DRIVE_TASKFILE;
  402. rq.buffer = buf;
  403. /*
  404. * (ks) We transfer currently only whole sectors.
  405. * This is suffient for now. But, it would be great,
  406. * if we would find a solution to transfer any size.
  407. * To support special commands like READ LONG.
  408. */
  409. if (args->command_type != IDE_DRIVE_TASK_NO_DATA) {
  410. if (data_size == 0)
  411. rq.nr_sectors = (args->hobRegister[IDE_NSECTOR_OFFSET] << 8) | args->tfRegister[IDE_NSECTOR_OFFSET];
  412. else
  413. rq.nr_sectors = data_size / SECTOR_SIZE;
  414. if (!rq.nr_sectors) {
  415. printk(KERN_ERR "%s: in/out command without data\n",
  416. drive->name);
  417. return -EFAULT;
  418. }
  419. rq.hard_nr_sectors = rq.nr_sectors;
  420. rq.hard_cur_sectors = rq.current_nr_sectors = rq.nr_sectors;
  421. if (args->command_type == IDE_DRIVE_TASK_RAW_WRITE)
  422. rq.flags |= REQ_RW;
  423. }
  424. rq.special = args;
  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 = kmalloc(tasksize, GFP_KERNEL);
  448. if (req_task == NULL) return -ENOMEM;
  449. memset(req_task, 0, tasksize);
  450. if (copy_from_user(req_task, buf, tasksize)) {
  451. kfree(req_task);
  452. return -EFAULT;
  453. }
  454. taskout = (int) req_task->out_size;
  455. taskin = (int) req_task->in_size;
  456. if (taskout) {
  457. int outtotal = tasksize;
  458. outbuf = kmalloc(taskout, GFP_KERNEL);
  459. if (outbuf == NULL) {
  460. err = -ENOMEM;
  461. goto abort;
  462. }
  463. memset(outbuf, 0, taskout);
  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 = kmalloc(taskin, GFP_KERNEL);
  472. if (inbuf == NULL) {
  473. err = -ENOMEM;
  474. goto abort;
  475. }
  476. memset(inbuf, 0, taskin);
  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. if (outbuf != NULL)
  561. kfree(outbuf);
  562. if (inbuf != NULL)
  563. kfree(inbuf);
  564. // printk("IDE Taskfile ioctl ended. rc = %i\n", err);
  565. drive->io_32bit = io_32bit;
  566. return err;
  567. }
  568. int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
  569. {
  570. struct request rq;
  571. u8 buffer[4];
  572. if (!buf)
  573. buf = buffer;
  574. memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
  575. ide_init_drive_cmd(&rq);
  576. rq.buffer = buf;
  577. *buf++ = cmd;
  578. *buf++ = nsect;
  579. *buf++ = feature;
  580. *buf++ = sectors;
  581. return ide_do_drive_cmd(drive, &rq, ide_wait);
  582. }
  583. /*
  584. * FIXME : this needs to map into at taskfile. <andre@linux-ide.org>
  585. */
  586. int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  587. {
  588. int err = 0;
  589. u8 args[4], *argbuf = args;
  590. u8 xfer_rate = 0;
  591. int argsize = 4;
  592. ide_task_t tfargs;
  593. if (NULL == (void *) arg) {
  594. struct request rq;
  595. ide_init_drive_cmd(&rq);
  596. return ide_do_drive_cmd(drive, &rq, ide_wait);
  597. }
  598. if (copy_from_user(args, (void __user *)arg, 4))
  599. return -EFAULT;
  600. memset(&tfargs, 0, sizeof(ide_task_t));
  601. tfargs.tfRegister[IDE_FEATURE_OFFSET] = args[2];
  602. tfargs.tfRegister[IDE_NSECTOR_OFFSET] = args[3];
  603. tfargs.tfRegister[IDE_SECTOR_OFFSET] = args[1];
  604. tfargs.tfRegister[IDE_LCYL_OFFSET] = 0x00;
  605. tfargs.tfRegister[IDE_HCYL_OFFSET] = 0x00;
  606. tfargs.tfRegister[IDE_SELECT_OFFSET] = 0x00;
  607. tfargs.tfRegister[IDE_COMMAND_OFFSET] = args[0];
  608. if (args[3]) {
  609. argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
  610. argbuf = kmalloc(argsize, GFP_KERNEL);
  611. if (argbuf == NULL)
  612. return -ENOMEM;
  613. memcpy(argbuf, args, 4);
  614. }
  615. if (set_transfer(drive, &tfargs)) {
  616. xfer_rate = args[1];
  617. if (ide_ata66_check(drive, &tfargs))
  618. goto abort;
  619. }
  620. err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
  621. if (!err && xfer_rate) {
  622. /* active-retuning-calls future */
  623. ide_set_xfer_rate(drive, xfer_rate);
  624. ide_driveid_update(drive);
  625. }
  626. abort:
  627. if (copy_to_user((void __user *)arg, argbuf, argsize))
  628. err = -EFAULT;
  629. if (argsize > 4)
  630. kfree(argbuf);
  631. return err;
  632. }
  633. static int ide_wait_cmd_task(ide_drive_t *drive, u8 *buf)
  634. {
  635. struct request rq;
  636. ide_init_drive_cmd(&rq);
  637. rq.flags = REQ_DRIVE_TASK;
  638. rq.buffer = buf;
  639. return ide_do_drive_cmd(drive, &rq, ide_wait);
  640. }
  641. /*
  642. * FIXME : this needs to map into at taskfile. <andre@linux-ide.org>
  643. */
  644. int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
  645. {
  646. void __user *p = (void __user *)arg;
  647. int err = 0;
  648. u8 args[7], *argbuf = args;
  649. int argsize = 7;
  650. if (copy_from_user(args, p, 7))
  651. return -EFAULT;
  652. err = ide_wait_cmd_task(drive, argbuf);
  653. if (copy_to_user(p, argbuf, argsize))
  654. err = -EFAULT;
  655. return err;
  656. }
  657. /*
  658. * NOTICE: This is additions from IBM to provide a discrete interface,
  659. * for selective taskregister access operations. Nice JOB Klaus!!!
  660. * Glad to be able to work and co-develop this with you and IBM.
  661. */
  662. ide_startstop_t flagged_taskfile (ide_drive_t *drive, ide_task_t *task)
  663. {
  664. ide_hwif_t *hwif = HWIF(drive);
  665. task_struct_t *taskfile = (task_struct_t *) task->tfRegister;
  666. hob_struct_t *hobfile = (hob_struct_t *) task->hobRegister;
  667. #if DEBUG_TASKFILE
  668. u8 status;
  669. #endif
  670. if (task->data_phase == TASKFILE_MULTI_IN ||
  671. task->data_phase == TASKFILE_MULTI_OUT) {
  672. if (!drive->mult_count) {
  673. printk(KERN_ERR "%s: multimode not set!\n", drive->name);
  674. return ide_stopped;
  675. }
  676. }
  677. /*
  678. * (ks) Check taskfile in/out flags.
  679. * If set, then execute as it is defined.
  680. * If not set, then define default settings.
  681. * The default values are:
  682. * write and read all taskfile registers (except data)
  683. * write and read the hob registers (sector,nsector,lcyl,hcyl)
  684. */
  685. if (task->tf_out_flags.all == 0) {
  686. task->tf_out_flags.all = IDE_TASKFILE_STD_OUT_FLAGS;
  687. if (drive->addressing == 1)
  688. task->tf_out_flags.all |= (IDE_HOB_STD_OUT_FLAGS << 8);
  689. }
  690. if (task->tf_in_flags.all == 0) {
  691. task->tf_in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
  692. if (drive->addressing == 1)
  693. task->tf_in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
  694. }
  695. /* ALL Command Block Executions SHALL clear nIEN, unless otherwise */
  696. if (IDE_CONTROL_REG)
  697. /* clear nIEN */
  698. hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
  699. SELECT_MASK(drive, 0);
  700. #if DEBUG_TASKFILE
  701. status = hwif->INB(IDE_STATUS_REG);
  702. if (status & 0x80) {
  703. printk("flagged_taskfile -> Bad status. Status = %02x. wait 100 usec ...\n", status);
  704. udelay(100);
  705. status = hwif->INB(IDE_STATUS_REG);
  706. printk("flagged_taskfile -> Status = %02x\n", status);
  707. }
  708. #endif
  709. if (task->tf_out_flags.b.data) {
  710. u16 data = taskfile->data + (hobfile->data << 8);
  711. hwif->OUTW(data, IDE_DATA_REG);
  712. }
  713. /* (ks) send hob registers first */
  714. if (task->tf_out_flags.b.nsector_hob)
  715. hwif->OUTB(hobfile->sector_count, IDE_NSECTOR_REG);
  716. if (task->tf_out_flags.b.sector_hob)
  717. hwif->OUTB(hobfile->sector_number, IDE_SECTOR_REG);
  718. if (task->tf_out_flags.b.lcyl_hob)
  719. hwif->OUTB(hobfile->low_cylinder, IDE_LCYL_REG);
  720. if (task->tf_out_flags.b.hcyl_hob)
  721. hwif->OUTB(hobfile->high_cylinder, IDE_HCYL_REG);
  722. /* (ks) Send now the standard registers */
  723. if (task->tf_out_flags.b.error_feature)
  724. hwif->OUTB(taskfile->feature, IDE_FEATURE_REG);
  725. /* refers to number of sectors to transfer */
  726. if (task->tf_out_flags.b.nsector)
  727. hwif->OUTB(taskfile->sector_count, IDE_NSECTOR_REG);
  728. /* refers to sector offset or start sector */
  729. if (task->tf_out_flags.b.sector)
  730. hwif->OUTB(taskfile->sector_number, IDE_SECTOR_REG);
  731. if (task->tf_out_flags.b.lcyl)
  732. hwif->OUTB(taskfile->low_cylinder, IDE_LCYL_REG);
  733. if (task->tf_out_flags.b.hcyl)
  734. hwif->OUTB(taskfile->high_cylinder, IDE_HCYL_REG);
  735. /*
  736. * (ks) In the flagged taskfile approch, we will use all specified
  737. * registers and the register value will not be changed, except the
  738. * select bit (master/slave) in the drive_head register. We must make
  739. * sure that the desired drive is selected.
  740. */
  741. hwif->OUTB(taskfile->device_head | drive->select.all, IDE_SELECT_REG);
  742. switch(task->data_phase) {
  743. case TASKFILE_OUT_DMAQ:
  744. case TASKFILE_OUT_DMA:
  745. case TASKFILE_IN_DMAQ:
  746. case TASKFILE_IN_DMA:
  747. hwif->dma_setup(drive);
  748. hwif->dma_exec_cmd(drive, taskfile->command);
  749. hwif->dma_start(drive);
  750. break;
  751. default:
  752. if (task->handler == NULL)
  753. return ide_stopped;
  754. /* Issue the command */
  755. if (task->prehandler) {
  756. hwif->OUTBSYNC(drive, taskfile->command, IDE_COMMAND_REG);
  757. ndelay(400); /* FIXME */
  758. return task->prehandler(drive, task->rq);
  759. }
  760. ide_execute_command(drive, taskfile->command, task->handler, WAIT_WORSTCASE, NULL);
  761. }
  762. return ide_started;
  763. }