ftape-ctl.c 25 KB

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