ftape-ctl.c 25 KB

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