ftape-ctl.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:25k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *      Copyright (C) 1993-1996 Bas Laarhoven,
  3.  *                    1996-1997 Claus-Justus Heine.
  4.  This program is free software; you can redistribute it and/or modify
  5.  it under the terms of the GNU General Public License as published by
  6.  the Free Software Foundation; either version 2, or (at your option)
  7.  any later version.
  8.  This program is distributed in the hope that it will be useful,
  9.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  GNU General Public License for more details.
  12.  You should have received a copy of the GNU General Public License
  13.  along with this program; see the file COPYING.  If not, write to
  14.  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  *
  16.  * $Source: /homes/cvs/ftape-stacked/ftape/lowlevel/ftape-ctl.c,v $
  17.  * $Revision: 1.4 $
  18.  * $Date: 1997/11/11 14:37:44 $
  19.  *
  20.  *      This file contains the non-read/write ftape functions for the
  21.  *      QIC-40/80/3010/3020 floppy-tape driver "ftape" for Linux.
  22.  */
  23. #include <linux/config.h>
  24. #include <linux/errno.h>
  25. #include <linux/mm.h>
  26. #include <linux/mman.h>
  27. #include <linux/wrapper.h>
  28. #include <linux/ftape.h>
  29. #include <linux/qic117.h>
  30. #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,6)
  31. #include <asm/uaccess.h>
  32. #else
  33. #include <asm/segment.h>
  34. #endif
  35. #include <asm/io.h>
  36. /* ease porting between pre-2.4.x and later kernels */
  37. #define vma_get_pgoff(v)      ((v)->vm_pgoff)
  38. #include "../lowlevel/ftape-tracing.h"
  39. #include "../lowlevel/ftape-io.h"
  40. #include "../lowlevel/ftape-ctl.h"
  41. #include "../lowlevel/ftape-write.h"
  42. #include "../lowlevel/ftape-read.h"
  43. #include "../lowlevel/ftape-rw.h"
  44. #include "../lowlevel/ftape-bsm.h"
  45. /*      Global vars.
  46.  */
  47. ftape_info ftape_status = {
  48. /*  vendor information */
  49. { 0, },     /* drive type */
  50. /*  data rates */
  51. 500,        /* used data rate */
  52. 500,        /* drive max rate */
  53. 500,        /* fdc max rate   */
  54. /*  drive selection, either FTAPE_SEL_A/B/C/D */
  55. -1,     /* drive selection */
  56. /*  flags set after decode the drive and tape status   */
  57. 0,          /* formatted */
  58. 1,          /* no tape */
  59. 1,          /* write protected */
  60. 1,          /* new tape */
  61. /*  values of last queried drive/tape status and error */
  62. {{0,}},     /* last error code */
  63. {{0,}},     /* drive status, configuration, tape status */
  64. /*  cartridge geometry */
  65.         20,         /* tracks_per_tape */
  66.         102,        /* segments_per_track */
  67. /*  location of header segments, etc. */
  68. -1,     /* used_header_segment */
  69. -1,     /* header_segment_1 */
  70. -1,     /* header_segment_2 */
  71. -1,     /* first_data_segment */
  72.         -1,     /* last_data_segment */
  73. /*  the format code as stored in the header segment  */
  74. fmt_normal, /* format code */
  75. /*  the default for the qic std: unknown */
  76. -1,
  77. /*  is tape running? */
  78. idle,       /* runner_state */
  79. /*  is tape reading/writing/verifying/formatting/deleting */
  80. idle,       /* driver state */
  81. /*  flags fatal hardware error */
  82. 1,          /* failure */
  83. /*  history record */
  84. { 0, }      /* history record */
  85. };
  86. int ftape_segments_per_head     = 1020;
  87. int ftape_segments_per_cylinder = 4;
  88. int ftape_init_drive_needed = 1; /* need to be global for ftape_reset_drive()
  89.   * in ftape-io.c
  90.   */
  91. /*      Local vars.
  92.  */
  93. static const vendor_struct vendors[] = QIC117_VENDORS;
  94. static const wakeup_method methods[] = WAKEUP_METHODS;
  95. const ftape_info *ftape_get_status(void)
  96. {
  97. #if defined(STATUS_PARANOYA)
  98. static ftape_info get_status;
  99. get_status = ftape_status;
  100. return &get_status;
  101. #else
  102. return &ftape_status; /*  maybe return only a copy of it to assure 
  103.        *  read only access
  104.        */
  105. #endif
  106. }
  107. void ftape_set_status(const ftape_info *status)
  108. {
  109. ftape_status = *status;
  110. }
  111. static int ftape_not_operational(int status)
  112. {
  113. /* return true if status indicates tape can not be used.
  114.  */
  115. return ((status ^ QIC_STATUS_CARTRIDGE_PRESENT) &
  116. (QIC_STATUS_ERROR |
  117.  QIC_STATUS_CARTRIDGE_PRESENT |
  118.  QIC_STATUS_NEW_CARTRIDGE));
  119. }
  120. int ftape_seek_to_eot(void)
  121. {
  122. int status;
  123. TRACE_FUN(ft_t_any);
  124. TRACE_CATCH(ftape_ready_wait(ftape_timeout.pause, &status),);
  125. while ((status & QIC_STATUS_AT_EOT) == 0) {
  126. if (ftape_not_operational(status)) {
  127. TRACE_EXIT -EIO;
  128. }
  129. TRACE_CATCH(ftape_command_wait(QIC_PHYSICAL_FORWARD,
  130.        ftape_timeout.rewind,&status),);
  131. }
  132. TRACE_EXIT 0;
  133. }
  134. int ftape_seek_to_bot(void)
  135. {
  136. int status;
  137. TRACE_FUN(ft_t_any);
  138. TRACE_CATCH(ftape_ready_wait(ftape_timeout.pause, &status),);
  139. while ((status & QIC_STATUS_AT_BOT) == 0) {
  140. if (ftape_not_operational(status)) {
  141. TRACE_EXIT -EIO;
  142. }
  143. TRACE_CATCH(ftape_command_wait(QIC_PHYSICAL_REVERSE,
  144.        ftape_timeout.rewind,&status),);
  145. }
  146. TRACE_EXIT 0;
  147. }
  148. static int ftape_new_cartridge(void)
  149. {
  150. ft_location.track = -1; /* force seek on first access */
  151. ftape_zap_read_buffers();
  152. ftape_zap_write_buffers();
  153. return 0;
  154. }
  155. int ftape_abort_operation(void)
  156. {
  157. int result = 0;
  158. int status;
  159. TRACE_FUN(ft_t_flow);
  160. if (ft_runner_status == running) {
  161. TRACE(ft_t_noise, "aborting runner, waiting");
  162. ft_runner_status = do_abort;
  163. /* set timeout so that the tape will run to logical EOT
  164.  * if we missed the last sector and there are no queue pulses.
  165.  */
  166. result = ftape_dumb_stop();
  167. }
  168. if (ft_runner_status != idle) {
  169. if (ft_runner_status == do_abort) {
  170. TRACE(ft_t_noise, "forcing runner abort");
  171. }
  172. TRACE(ft_t_noise, "stopping tape");
  173. result = ftape_stop_tape(&status);
  174. ft_location.known = 0;
  175. ft_runner_status  = idle;
  176. }
  177. ftape_reset_buffer();
  178. ftape_zap_read_buffers();
  179. ftape_set_state(idle);
  180. TRACE_EXIT result;
  181. }
  182. static int lookup_vendor_id(unsigned int vendor_id)
  183. {
  184. int i = 0;
  185. while (vendors[i].vendor_id != vendor_id) {
  186. if (++i >= NR_ITEMS(vendors)) {
  187. return -1;
  188. }
  189. }
  190. return i;
  191. }
  192. void ftape_detach_drive(void)
  193. {
  194. TRACE_FUN(ft_t_any);
  195. TRACE(ft_t_flow, "disabling tape drive and fdc");
  196. ftape_put_drive_to_sleep(ft_drive_type.wake_up);
  197. fdc_catch_stray_interrupts(1); /* one always comes */
  198. fdc_disable();
  199. fdc_release_irq_and_dma();
  200. fdc_release_regions();
  201. TRACE_EXIT;
  202. }
  203. static void clear_history(void)
  204. {
  205. ft_history.used = 0;
  206. ft_history.id_am_errors =
  207. ft_history.id_crc_errors =
  208. ft_history.data_am_errors =
  209. ft_history.data_crc_errors =
  210. ft_history.overrun_errors =
  211. ft_history.no_data_errors =
  212. ft_history.retries =
  213. ft_history.crc_errors =
  214. ft_history.crc_failures =
  215. ft_history.ecc_failures =
  216. ft_history.corrected =
  217. ft_history.defects =
  218. ft_history.rewinds = 0;
  219. }
  220. int ftape_activate_drive(vendor_struct * drive_type)
  221. {
  222. int result = 0;
  223. TRACE_FUN(ft_t_flow);
  224. /* If we already know the drive type, wake it up.
  225.  * Else try to find out what kind of drive is attached.
  226.  */
  227. if (drive_type->wake_up != unknown_wake_up) {
  228. TRACE(ft_t_flow, "enabling tape drive and fdc");
  229. result = ftape_wakeup_drive(drive_type->wake_up);
  230. if (result < 0) {
  231. TRACE(ft_t_err, "known wakeup method failed");
  232. }
  233. } else {
  234. wake_up_types method;
  235. const ft_trace_t old_tracing = TRACE_LEVEL;
  236. if (TRACE_LEVEL < ft_t_flow) {
  237. SET_TRACE_LEVEL(ft_t_bug);
  238. }
  239. /*  Try to awaken the drive using all known methods.
  240.  *  Lower tracing for a while.
  241.  */
  242. for (method=no_wake_up; method < NR_ITEMS(methods); ++method) {
  243. drive_type->wake_up = method;
  244. #ifdef CONFIG_FT_TWO_DRIVES
  245. /*  Test setup for dual drive configuration.
  246.  *  /dev/rft2 uses mountain wakeup
  247.  *  /dev/rft3 uses colorado wakeup
  248.  *  Other systems will use the normal scheme.
  249.  */
  250. if ((ft_drive_sel < 2)                            ||
  251.     (ft_drive_sel == 2 && method == FT_WAKE_UP_1) ||
  252.     (ft_drive_sel == 3 && method == FT_WAKE_UP_2)) {
  253. result=ftape_wakeup_drive(drive_type->wake_up);
  254. } else {
  255. result = -EIO;
  256. }
  257. #else
  258. result = ftape_wakeup_drive(drive_type->wake_up);
  259. #endif
  260. if (result >= 0) {
  261. TRACE(ft_t_warn, "drive wakeup method: %s",
  262.       methods[drive_type->wake_up].name);
  263. break;
  264. }
  265. }
  266. SET_TRACE_LEVEL(old_tracing);
  267. if (method >= NR_ITEMS(methods)) {
  268. /* no response at all, cannot open this drive */
  269. drive_type->wake_up = unknown_wake_up;
  270. TRACE(ft_t_err, "no tape drive found !");
  271. result = -ENODEV;
  272. }
  273. }
  274. TRACE_EXIT result;
  275. }
  276. int ftape_get_drive_status(void)
  277. {
  278. int result;
  279. int status;
  280. TRACE_FUN(ft_t_flow);
  281. ft_no_tape = ft_write_protected = 0;
  282. /*    Tape drive is activated now.
  283.  *    First clear error status if present.
  284.  */
  285. do {
  286. result = ftape_ready_wait(ftape_timeout.reset, &status);
  287. if (result < 0) {
  288. if (result == -ETIME) {
  289. TRACE(ft_t_err, "ftape_ready_wait timeout");
  290. } else if (result == -EINTR) {
  291. TRACE(ft_t_err, "ftape_ready_wait aborted");
  292. } else {
  293. TRACE(ft_t_err, "ftape_ready_wait failed");
  294. }
  295. TRACE_EXIT -EIO;
  296. }
  297. /*  Clear error condition (drive is ready !)
  298.  */
  299. if (status & QIC_STATUS_ERROR) {
  300. unsigned int error;
  301. qic117_cmd_t command;
  302. TRACE(ft_t_err, "error status set");
  303. result = ftape_report_error(&error, &command, 1);
  304. if (result < 0) {
  305. TRACE(ft_t_err,
  306.       "report_error_code failed: %d", result);
  307. /* hope it's working next time */
  308. ftape_reset_drive();
  309. TRACE_EXIT -EIO;
  310. } else if (error != 0) {
  311. TRACE(ft_t_noise, "error code   : %d", error);
  312. TRACE(ft_t_noise, "error command: %d", command);
  313. }
  314. }
  315. if (status & QIC_STATUS_NEW_CARTRIDGE) {
  316. unsigned int error;
  317. qic117_cmd_t command;
  318. const ft_trace_t old_tracing = TRACE_LEVEL;
  319. SET_TRACE_LEVEL(ft_t_bug);
  320. /*  Undocumented feature: Must clear (not present!)
  321.  *  error here or we'll fail later.
  322.  */
  323. ftape_report_error(&error, &command, 1);
  324. SET_TRACE_LEVEL(old_tracing);
  325. TRACE(ft_t_info, "status: new cartridge");
  326. ft_new_tape = 1;
  327. } else {
  328. ft_new_tape = 0;
  329. }
  330. FT_SIGNAL_EXIT(_DONT_BLOCK);
  331. } while (status & QIC_STATUS_ERROR);
  332. ft_no_tape = !(status & QIC_STATUS_CARTRIDGE_PRESENT);
  333. ft_write_protected = (status & QIC_STATUS_WRITE_PROTECT) != 0;
  334. if (ft_no_tape) {
  335. TRACE(ft_t_warn, "no cartridge present");
  336. } else {
  337. if (ft_write_protected) {
  338. TRACE(ft_t_noise, "Write protected cartridge");
  339. }
  340. }
  341. TRACE_EXIT 0;
  342. }
  343. void ftape_log_vendor_id(void)
  344. {
  345. int vendor_index;
  346. TRACE_FUN(ft_t_flow);
  347. ftape_report_vendor_id(&ft_drive_type.vendor_id);
  348. vendor_index = lookup_vendor_id(ft_drive_type.vendor_id);
  349. if (ft_drive_type.vendor_id == UNKNOWN_VENDOR &&
  350.     ft_drive_type.wake_up == wake_up_colorado) {
  351. vendor_index = 0;
  352. /* hack to get rid of all this mail */
  353. ft_drive_type.vendor_id = 0;
  354. }
  355. if (vendor_index < 0) {
  356. /* Unknown vendor id, first time opening device.  The
  357.  * drive_type remains set to type found at wakeup
  358.  * time, this will probably keep the driver operating
  359.  * for this new vendor.  
  360.  */
  361. TRACE(ft_t_warn, "n"
  362.       KERN_INFO "============ unknown vendor id ===========n"
  363.       KERN_INFO "A new, yet unsupported tape drive is foundn"
  364.       KERN_INFO "Please report the following values:n"
  365.       KERN_INFO "   Vendor id     : 0x%04xn"
  366.       KERN_INFO "   Wakeup method : %sn"
  367.       KERN_INFO "And a description of your tape driven"
  368.       KERN_INFO "to "THE_FTAPE_MAINTAINER"n"
  369.       KERN_INFO "==========================================",
  370.       ft_drive_type.vendor_id,
  371.       methods[ft_drive_type.wake_up].name);
  372. ft_drive_type.speed = 0; /* unknown */
  373. } else {
  374. ft_drive_type.name  = vendors[vendor_index].name;
  375. ft_drive_type.speed = vendors[vendor_index].speed;
  376. TRACE(ft_t_info, "tape drive type: %s", ft_drive_type.name);
  377. /* scan all methods for this vendor_id in table */
  378. while(ft_drive_type.wake_up != vendors[vendor_index].wake_up) {
  379. if (vendor_index < NR_ITEMS(vendors) - 1 &&
  380.     vendors[vendor_index + 1].vendor_id 
  381.     == 
  382.     ft_drive_type.vendor_id) {
  383. ++vendor_index;
  384. } else {
  385. break;
  386. }
  387. }
  388. if (ft_drive_type.wake_up != vendors[vendor_index].wake_up) {
  389. TRACE(ft_t_warn, "n"
  390.      KERN_INFO "==========================================n"
  391.      KERN_INFO "wakeup type mismatch:n"
  392.      KERN_INFO "found: %s, expected: %sn"
  393.      KERN_INFO "please report this to "THE_FTAPE_MAINTAINER"n"
  394.      KERN_INFO "==========================================",
  395.       methods[ft_drive_type.wake_up].name,
  396.       methods[vendors[vendor_index].wake_up].name);
  397. }
  398. }
  399. TRACE_EXIT;
  400. }
  401. void ftape_calc_timeouts(unsigned int qic_std,
  402.  unsigned int data_rate,
  403.  unsigned int tape_len)
  404. {
  405. int speed; /* deci-ips ! */
  406. int ff_speed;
  407. int length;
  408. TRACE_FUN(ft_t_any);
  409. /*                           tape transport speed
  410.  *  data rate:        QIC-40   QIC-80   QIC-3010 QIC-3020
  411.  *
  412.  *    250 Kbps        25 ips     n/a      n/a      n/a
  413.  *    500 Kbps        50 ips   34 ips   22.6 ips   n/a
  414.  *      1 Mbps          n/a    68 ips   45.2 ips 22.6 ips
  415.  *      2 Mbps          n/a      n/a      n/a    45.2 ips
  416.  *
  417.  *  fast tape transport speed is at least 68 ips.
  418.  */
  419. switch (qic_std) {
  420. case QIC_TAPE_QIC40:
  421. speed = (data_rate == 250) ? 250 : 500;
  422. break;
  423. case QIC_TAPE_QIC80:
  424. speed = (data_rate == 500) ? 340 : 680;
  425. break;
  426. case QIC_TAPE_QIC3010:
  427. speed = (data_rate == 500) ? 226 : 452;
  428. break;
  429. case QIC_TAPE_QIC3020:
  430. speed = (data_rate == 1000) ? 226 : 452;
  431. break;
  432. default:
  433. TRACE(ft_t_bug, "Unknown qic_std (bug) ?");
  434. speed = 500;
  435. break;
  436. }
  437. if (ft_drive_type.speed == 0) {
  438. unsigned long t0;
  439. static int dt = 0;     /* keep gcc from complaining */
  440. static int first_time = 1;
  441. /*  Measure the time it takes to wind to EOT and back to BOT.
  442.  *  If the tape length is known, calculate the rewind speed.
  443.  *  Else keep the time value for calculation of the rewind
  444.  *  speed later on, when the length _is_ known.
  445.  *  Ask for a report only when length and speed are both known.
  446.  */
  447. if (first_time) {
  448. ftape_seek_to_bot();
  449. t0 = jiffies;
  450. ftape_seek_to_eot();
  451. ftape_seek_to_bot();
  452. dt = (int) (((jiffies - t0) * FT_USPT) / 1000);
  453. if (dt < 1) {
  454. dt = 1; /* prevent div by zero on failures */
  455. }
  456. first_time = 0;
  457. TRACE(ft_t_info,
  458.       "trying to determine seek timeout, got %d msec",
  459.       dt);
  460. }
  461. if (tape_len != 0) {
  462. ft_drive_type.speed = 
  463. (2 * 12 * tape_len * 1000) / dt;
  464. TRACE(ft_t_warn, "n"
  465.      KERN_INFO "==========================================n"
  466.      KERN_INFO "drive type: %sn"
  467.      KERN_INFO "delta time = %d ms, length = %d ftn"
  468.      KERN_INFO "has a maximum tape speed of %d ipsn"
  469.      KERN_INFO "please report this to "THE_FTAPE_MAINTAINER"n"
  470.      KERN_INFO "==========================================",
  471.       ft_drive_type.name, dt, tape_len, 
  472.       ft_drive_type.speed);
  473. }
  474. }
  475. /*  Handle unknown length tapes as very long ones. We'll
  476.  *  determine the actual length from a header segment later.
  477.  *  This is normal for all modern (Wide,TR1/2/3) formats.
  478.  */
  479. if (tape_len <= 0) {
  480. TRACE(ft_t_noise,
  481.       "Unknown tape length, using maximal timeouts");
  482. length = QIC_TOP_TAPE_LEN; /* use worst case values */
  483. } else {
  484. length = tape_len; /* use actual values */
  485. }
  486. if (ft_drive_type.speed == 0) {
  487. ff_speed = speed; 
  488. } else {
  489. ff_speed = ft_drive_type.speed;
  490. }
  491. /*  time to go from bot to eot at normal speed (data rate):
  492.  *  time = (1+delta) * length (ft) * 12 (inch/ft) / speed (ips)
  493.  *  delta = 10 % for seek speed, 20 % for rewind speed.
  494.  */
  495. ftape_timeout.seek = (length * 132 * FT_SECOND) / speed;
  496. ftape_timeout.rewind = (length * 144 * FT_SECOND) / (10 * ff_speed);
  497. ftape_timeout.reset = 20 * FT_SECOND + ftape_timeout.rewind;
  498. TRACE(ft_t_noise, "timeouts for speed = %d, length = %dn"
  499.       KERN_INFO "seek timeout  : %d secn"
  500.       KERN_INFO "rewind timeout: %d secn"
  501.       KERN_INFO "reset timeout : %d sec",
  502.       speed, length,
  503.       (ftape_timeout.seek + 500) / 1000,
  504.       (ftape_timeout.rewind + 500) / 1000,
  505.       (ftape_timeout.reset + 500) / 1000);
  506. TRACE_EXIT;
  507. }
  508. /* This function calibrates the datarate (i.e. determines the maximal
  509.  * usable data rate) and sets the global variable ft_qic_std to qic_std
  510.  *
  511.  */
  512. int ftape_calibrate_data_rate(unsigned int qic_std)
  513. {
  514. int rate = ft_fdc_rate_limit;
  515. int result;
  516. TRACE_FUN(ft_t_flow);
  517. ft_qic_std = qic_std;
  518. if (ft_qic_std == -1) {
  519. TRACE_ABORT(-EIO, ft_t_err,
  520. "Unable to determine data rate if QIC standard is unknown");
  521. }
  522. /*  Select highest rate supported by both fdc and drive.
  523.  *  Start with highest rate supported by the fdc.
  524.  */
  525. while (fdc_set_data_rate(rate) < 0 && rate > 250) {
  526. rate /= 2;
  527. }
  528. TRACE(ft_t_info,
  529.       "Highest FDC supported data rate: %d Kbps", rate);
  530. ft_fdc_max_rate = rate;
  531. do {
  532. result = ftape_set_data_rate(rate, ft_qic_std);
  533. } while (result == -EINVAL && (rate /= 2) > 250);
  534. if (result < 0) {
  535. TRACE_ABORT(-EIO, ft_t_err, "set datarate failed");
  536. }
  537. ft_data_rate = rate;
  538. TRACE_EXIT 0;
  539. }
  540. int ftape_init_drive(void)
  541. {
  542. int status;
  543. qic_model model;
  544. unsigned int qic_std;
  545. unsigned int data_rate;
  546. TRACE_FUN(ft_t_flow);
  547. ftape_init_drive_needed = 0; /* don't retry if this fails ? */
  548. TRACE_CATCH(ftape_report_raw_drive_status(&status),);
  549. if (status & QIC_STATUS_CARTRIDGE_PRESENT) {
  550. if (!(status & QIC_STATUS_AT_BOT)) {
  551. /*  Antique drives will get here after a soft reset,
  552.  *  modern ones only if the driver is loaded when the
  553.  *  tape wasn't rewound properly.
  554.  */
  555. /* Tape should be at bot if new cartridge ! */
  556. ftape_seek_to_bot();
  557. }
  558. if (!(status & QIC_STATUS_REFERENCED)) {
  559. TRACE(ft_t_flow, "starting seek_load_point");
  560. TRACE_CATCH(ftape_command_wait(QIC_SEEK_LOAD_POINT,
  561.        ftape_timeout.reset,
  562.        &status),);
  563. }
  564. }
  565. ft_formatted = (status & QIC_STATUS_REFERENCED) != 0;
  566. if (!ft_formatted) {
  567. TRACE(ft_t_warn, "Warning: tape is not formatted !");
  568. }
  569. /*  report configuration aborts when ftape_tape_len == -1
  570.  *  unknown qic_std is okay if not formatted.
  571.  */
  572. TRACE_CATCH(ftape_report_configuration(&model,
  573.        &data_rate,
  574.        &qic_std,
  575.        &ftape_tape_len),);
  576. /*  Maybe add the following to the /proc entry
  577.  */
  578. TRACE(ft_t_info, "%s drive @ %d Kbps",
  579.       (model == prehistoric) ? "prehistoric" :
  580.       ((model == pre_qic117c) ? "pre QIC-117C" :
  581.        ((model == post_qic117b) ? "post QIC-117B" :
  582. "post QIC-117D")), data_rate);
  583. if (ft_formatted) {
  584. /*  initialize ft_used_data_rate to maximum value 
  585.  *  and set ft_qic_std
  586.  */
  587. TRACE_CATCH(ftape_calibrate_data_rate(qic_std),);
  588. if (ftape_tape_len == 0) {
  589. TRACE(ft_t_info, "unknown length QIC-%s tape",
  590.       (ft_qic_std == QIC_TAPE_QIC40) ? "40" :
  591.       ((ft_qic_std == QIC_TAPE_QIC80) ? "80" :
  592.        ((ft_qic_std == QIC_TAPE_QIC3010) 
  593. ? "3010" : "3020")));
  594. } else {
  595. TRACE(ft_t_info, "%d ft. QIC-%s tape", ftape_tape_len,
  596.       (ft_qic_std == QIC_TAPE_QIC40) ? "40" :
  597.       ((ft_qic_std == QIC_TAPE_QIC80) ? "80" :
  598.        ((ft_qic_std == QIC_TAPE_QIC3010)
  599. ? "3010" : "3020")));
  600. }
  601. ftape_calc_timeouts(ft_qic_std, ft_data_rate, ftape_tape_len);
  602. /* soft write-protect QIC-40/QIC-80 cartridges used with a
  603.  * Colorado T3000 drive. Buggy hardware!
  604.  */
  605. if ((ft_drive_type.vendor_id == 0x011c6) &&
  606.     ((ft_qic_std == QIC_TAPE_QIC40 ||
  607.       ft_qic_std == QIC_TAPE_QIC80) &&
  608.      !ft_write_protected)) {
  609. TRACE(ft_t_warn, "n"
  610. KERN_INFO "The famous Colorado T3000 bug:n"
  611. KERN_INFO "%s drives can't write QIC40 and QIC80n"
  612. KERN_INFO "cartridges but don't set the write-protect flag!",
  613.       ft_drive_type.name);
  614. ft_write_protected = 1;
  615. }
  616. } else {
  617. /*  Doesn't make too much sense to set the data rate
  618.  *  because we don't know what to use for the write
  619.  *  precompensation.
  620.  *  Need to do this again when formatting the cartridge.
  621.  */
  622. ft_data_rate = data_rate;
  623. ftape_calc_timeouts(QIC_TAPE_QIC40,
  624.     data_rate,
  625.     ftape_tape_len);
  626. }
  627. ftape_new_cartridge();
  628. TRACE_EXIT 0;
  629. }
  630. static void ftape_munmap(void)
  631. {
  632. int i;
  633. TRACE_FUN(ft_t_flow);
  634. for (i = 0; i < ft_nr_buffers; i++) {
  635. ft_buffer[i]->mmapped = 0;
  636. }
  637. TRACE_EXIT;
  638. }
  639. /*   Map the dma buffers into the virtual address range given by vma.
  640.  *   We only check the caller doesn't map non-existent buffers. We
  641.  *   don't check for multiple mappings.
  642.  */
  643. int ftape_mmap(struct vm_area_struct *vma)
  644. {
  645. int num_buffers;
  646. int i;
  647. TRACE_FUN(ft_t_flow);
  648. if (ft_failure) {
  649. TRACE_EXIT -ENODEV;
  650. }
  651. if (!(vma->vm_flags & (VM_READ|VM_WRITE))) {
  652. TRACE_ABORT(-EINVAL, ft_t_err, "Undefined mmap() access");
  653. }
  654. if (vma_get_pgoff(vma) != 0) {
  655. TRACE_ABORT(-EINVAL, ft_t_err, "page offset must be 0");
  656. }
  657. if ((vma->vm_end - vma->vm_start) % FT_BUFF_SIZE != 0) {
  658. TRACE_ABORT(-EINVAL, ft_t_err,
  659.     "size = %ld, should be a multiple of %d",
  660.     vma->vm_end - vma->vm_start,
  661.     FT_BUFF_SIZE);
  662. }
  663. num_buffers = (vma->vm_end - vma->vm_start) / FT_BUFF_SIZE;
  664. if (num_buffers > ft_nr_buffers) {
  665. TRACE_ABORT(-EINVAL,
  666.     ft_t_err, "size = %ld, should be less than %d",
  667.     vma->vm_end - vma->vm_start,
  668.     ft_nr_buffers * FT_BUFF_SIZE);
  669. }
  670. if (ft_driver_state != idle) {
  671. /* this also clears the buffer states 
  672.  */
  673. ftape_abort_operation();
  674. } else {
  675. ftape_reset_buffer();
  676. }
  677. for (i = 0; i < num_buffers; i++) {
  678. TRACE_CATCH(remap_page_range(vma->vm_start +
  679.      i * FT_BUFF_SIZE,
  680.      virt_to_phys(ft_buffer[i]->address),
  681.      FT_BUFF_SIZE,
  682.      vma->vm_page_prot),
  683.     _res = -EAGAIN);
  684. TRACE(ft_t_noise, "remapped dma buffer @ %p to location @ %p",
  685.       ft_buffer[i]->address,
  686.       (void *)(vma->vm_start + i * FT_BUFF_SIZE));
  687. }
  688. for (i = 0; i < num_buffers; i++) {
  689. memset(ft_buffer[i]->address, 0xAA, FT_BUFF_SIZE);
  690. ft_buffer[i]->mmapped++;
  691. }
  692. TRACE_EXIT 0;
  693. }
  694. static void ftape_init_driver(void); /* forward declaration */
  695. /*      OPEN routine called by kernel-interface code
  696.  */
  697. int ftape_enable(int drive_selection)
  698. {
  699. TRACE_FUN(ft_t_any);
  700. if (ft_drive_sel == -1 || ft_drive_sel != drive_selection) {
  701. /* Other selection than last time
  702.  */
  703. ftape_init_driver();
  704. }
  705. ft_drive_sel = FTAPE_SEL(drive_selection);
  706. ft_failure = 0;
  707. TRACE_CATCH(fdc_init(),); /* init & detect fdc */
  708. TRACE_CATCH(ftape_activate_drive(&ft_drive_type),
  709.     fdc_disable();
  710.     fdc_release_irq_and_dma();
  711.     fdc_release_regions());
  712. TRACE_CATCH(ftape_get_drive_status(), ftape_detach_drive());
  713. if (ft_drive_type.vendor_id == UNKNOWN_VENDOR) {
  714. ftape_log_vendor_id();
  715. }
  716. if (ft_new_tape) {
  717. ftape_init_drive_needed = 1;
  718. }
  719. if (!ft_no_tape && ftape_init_drive_needed) {
  720. TRACE_CATCH(ftape_init_drive(), ftape_detach_drive());
  721. }
  722. ftape_munmap(); /* clear the mmap flag */
  723. clear_history();
  724. TRACE_EXIT 0;
  725. }
  726. /*   release routine called by the high level interface modules
  727.  *   zftape or sftape.
  728.  */
  729. void ftape_disable(void)
  730. {
  731. int i;
  732. TRACE_FUN(ft_t_any);
  733. for (i = 0; i < ft_nr_buffers; i++) {
  734. if (ft_buffer[i]->mmapped) {
  735. TRACE(ft_t_noise, "first byte of buffer %d: 0x%02x",
  736.       i, *ft_buffer[i]->address);
  737. }
  738. }
  739. if (sigtestsetmask(&current->pending.signal, _DONT_BLOCK) && 
  740.     !(sigtestsetmask(&current->pending.signal, _NEVER_BLOCK)) &&
  741.     ftape_tape_running) {
  742. TRACE(ft_t_warn,
  743.       "Interrupted by fatal signal and tape still running");
  744. ftape_dumb_stop();
  745. ftape_abort_operation(); /* it's annoying */
  746. } else {
  747. ftape_set_state(idle);
  748. }
  749. ftape_detach_drive();
  750. if (ft_history.used) {
  751. TRACE(ft_t_info, "== Non-fatal errors this run: ==");
  752. TRACE(ft_t_info, "fdc isr statistics:n"
  753.       KERN_INFO " id_am_errors     : %3dn"
  754.       KERN_INFO " id_crc_errors    : %3dn"
  755.       KERN_INFO " data_am_errors   : %3dn"
  756.       KERN_INFO " data_crc_errors  : %3dn"
  757.       KERN_INFO " overrun_errors   : %3dn"
  758.       KERN_INFO " no_data_errors   : %3dn"
  759.       KERN_INFO " retries          : %3d",
  760.       ft_history.id_am_errors,   ft_history.id_crc_errors,
  761.       ft_history.data_am_errors, ft_history.data_crc_errors,
  762.       ft_history.overrun_errors, ft_history.no_data_errors,
  763.       ft_history.retries);
  764. if (ft_history.used & 1) {
  765. TRACE(ft_t_info, "ecc statistics:n"
  766.       KERN_INFO " crc_errors       : %3dn"
  767.       KERN_INFO " crc_failures     : %3dn"
  768.       KERN_INFO " ecc_failures     : %3dn"
  769.       KERN_INFO " sectors corrected: %3d",
  770.       ft_history.crc_errors,   ft_history.crc_failures,
  771.       ft_history.ecc_failures, ft_history.corrected);
  772. }
  773. if (ft_history.defects > 0) {
  774. TRACE(ft_t_warn, "Warning: %d media defects!",
  775.       ft_history.defects);
  776. }
  777. if (ft_history.rewinds > 0) {
  778. TRACE(ft_t_info, "tape motion statistics:n"
  779.       KERN_INFO "repositions       : %3d",
  780.       ft_history.rewinds);
  781. }
  782. }
  783. ft_failure = 1;
  784. TRACE_EXIT;
  785. }
  786. static void ftape_init_driver(void)
  787. {
  788. TRACE_FUN(ft_t_flow);
  789. ft_drive_type.vendor_id = UNKNOWN_VENDOR;
  790. ft_drive_type.speed     = 0;
  791. ft_drive_type.wake_up   = unknown_wake_up;
  792. ft_drive_type.name      = "Unknown";
  793. ftape_timeout.seek      = 650 * FT_SECOND;
  794. ftape_timeout.reset     = 670 * FT_SECOND;
  795. ftape_timeout.rewind    = 650 * FT_SECOND;
  796. ftape_timeout.head_seek =  15 * FT_SECOND;
  797. ftape_timeout.stop      =   5 * FT_SECOND;
  798. ftape_timeout.pause     =  16 * FT_SECOND;
  799. ft_qic_std             = -1;
  800. ftape_tape_len         = 0;  /* unknown */
  801. ftape_current_command  = 0;
  802. ftape_current_cylinder = -1;
  803. ft_segments_per_track       = 102;
  804. ftape_segments_per_head     = 1020;
  805. ftape_segments_per_cylinder = 4;
  806. ft_tracks_per_tape          = 20;
  807. ft_failure = 1;
  808. ft_formatted       = 0;
  809. ft_no_tape         = 1;
  810. ft_write_protected = 1;
  811. ft_new_tape        = 1;
  812. ft_driver_state = idle;
  813. ft_data_rate = 
  814. ft_fdc_max_rate   = 500;
  815. ft_drive_max_rate = 0; /* triggers set_rate_test() */
  816. ftape_init_drive_needed = 1;
  817. ft_header_segment_1    = -1;
  818. ft_header_segment_2    = -1;
  819. ft_used_header_segment = -1;
  820. ft_first_data_segment  = -1;
  821. ft_last_data_segment   = -1;
  822. ft_location.track = -1;
  823. ft_location.known = 0;
  824. ftape_tape_running = 0;
  825. ftape_might_be_off_track = 1;
  826. ftape_new_cartridge(); /* init some tape related variables */
  827. ftape_init_bsm();
  828. TRACE_EXIT;
  829. }