ftape-io.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. /*
  2. * Copyright (C) 1993-1996 Bas Laarhoven,
  3. * (C) 1996 Kai Harrekilde-Petersen,
  4. * (C) 1997 Claus-Justus Heine.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; see the file COPYING. If not, write to
  15. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  16. *
  17. * $Source: /homes/cvs/ftape-stacked/ftape/lowlevel/ftape-io.c,v $
  18. * $Revision: 1.4 $
  19. * $Date: 1997/11/11 14:02:36 $
  20. *
  21. * This file contains the general control functions for the
  22. * QIC-40/80/3010/3020 floppy-tape driver "ftape" for Linux.
  23. */
  24. #include <linux/errno.h>
  25. #include <linux/sched.h>
  26. #include <linux/mm.h>
  27. #include <asm/system.h>
  28. #include <linux/ioctl.h>
  29. #include <linux/mtio.h>
  30. #include <linux/delay.h>
  31. #include <linux/ftape.h>
  32. #include <linux/qic117.h>
  33. #include "../lowlevel/ftape-tracing.h"
  34. #include "../lowlevel/fdc-io.h"
  35. #include "../lowlevel/ftape-io.h"
  36. #include "../lowlevel/ftape-ctl.h"
  37. #include "../lowlevel/ftape-rw.h"
  38. #include "../lowlevel/ftape-write.h"
  39. #include "../lowlevel/ftape-read.h"
  40. #include "../lowlevel/ftape-init.h"
  41. #include "../lowlevel/ftape-calibr.h"
  42. /* Global vars.
  43. */
  44. /* NOTE: sectors start numbering at 1, all others at 0 ! */
  45. ft_timeout_table ftape_timeout;
  46. unsigned int ftape_tape_len;
  47. volatile qic117_cmd_t ftape_current_command;
  48. const struct qic117_command_table qic117_cmds[] = QIC117_COMMANDS;
  49. int ftape_might_be_off_track;
  50. /* Local vars.
  51. */
  52. static int diagnostic_mode;
  53. static unsigned int ftape_udelay_count;
  54. static unsigned int ftape_udelay_time;
  55. void ftape_udelay(unsigned int usecs)
  56. {
  57. volatile int count = (ftape_udelay_count * usecs +
  58. ftape_udelay_count - 1) / ftape_udelay_time;
  59. volatile int i;
  60. while (count-- > 0) {
  61. for (i = 0; i < 20; ++i);
  62. }
  63. }
  64. void ftape_udelay_calibrate(void)
  65. {
  66. ftape_calibrate("ftape_udelay",
  67. ftape_udelay, &ftape_udelay_count, &ftape_udelay_time);
  68. }
  69. /* Delay (msec) routine.
  70. */
  71. void ftape_sleep(unsigned int time)
  72. {
  73. TRACE_FUN(ft_t_any);
  74. time *= 1000; /* msecs -> usecs */
  75. if (time < FT_USPT) {
  76. /* Time too small for scheduler, do a busy wait ! */
  77. ftape_udelay(time);
  78. } else {
  79. long timeout;
  80. unsigned long flags;
  81. unsigned int ticks = (time + FT_USPT - 1) / FT_USPT;
  82. TRACE(ft_t_any, "%d msec, %d ticks", time/1000, ticks);
  83. timeout = ticks;
  84. save_flags(flags);
  85. sti();
  86. msleep_interruptible(jiffies_to_msecs(timeout));
  87. /* Mmm. Isn't current->blocked == 0xffffffff ?
  88. */
  89. if (signal_pending(current)) {
  90. TRACE(ft_t_err, "awoken by non-blocked signal :-(");
  91. }
  92. restore_flags(flags);
  93. }
  94. TRACE_EXIT;
  95. }
  96. /* send a command or parameter to the drive
  97. * Generates # of step pulses.
  98. */
  99. static inline int ft_send_to_drive(int arg)
  100. {
  101. /* Always wait for a command_timeout period to separate
  102. * individuals commands and/or parameters.
  103. */
  104. ftape_sleep(3 * FT_MILLISECOND);
  105. /* Keep cylinder nr within range, step towards home if possible.
  106. */
  107. if (ftape_current_cylinder >= arg) {
  108. return fdc_seek(ftape_current_cylinder - arg);
  109. } else {
  110. return fdc_seek(ftape_current_cylinder + arg);
  111. }
  112. }
  113. /* forward */ int ftape_report_raw_drive_status(int *status);
  114. static int ft_check_cmd_restrictions(qic117_cmd_t command)
  115. {
  116. int status = -1;
  117. TRACE_FUN(ft_t_any);
  118. TRACE(ft_t_flow, "%s", qic117_cmds[command].name);
  119. /* A new motion command during an uninterruptible (motion)
  120. * command requires a ready status before the new command can
  121. * be issued. Otherwise a new motion command needs to be
  122. * checked against required status.
  123. */
  124. if (qic117_cmds[command].cmd_type == motion &&
  125. qic117_cmds[ftape_current_command].non_intr) {
  126. ftape_report_raw_drive_status(&status);
  127. if ((status & QIC_STATUS_READY) == 0) {
  128. TRACE(ft_t_noise,
  129. "motion cmd (%d) during non-intr cmd (%d)",
  130. command, ftape_current_command);
  131. TRACE(ft_t_noise, "waiting until drive gets ready");
  132. ftape_ready_wait(ftape_timeout.seek,
  133. &status);
  134. }
  135. }
  136. if (qic117_cmds[command].mask != 0) {
  137. __u8 difference;
  138. /* Some commands do require a certain status:
  139. */
  140. if (status == -1) { /* not yet set */
  141. ftape_report_raw_drive_status(&status);
  142. }
  143. difference = ((status ^ qic117_cmds[command].state) &
  144. qic117_cmds[command].mask);
  145. /* Wait until the drive gets
  146. * ready. This may last forever if
  147. * the drive never gets ready...
  148. */
  149. while ((difference & QIC_STATUS_READY) != 0) {
  150. TRACE(ft_t_noise, "command %d issued while not ready",
  151. command);
  152. TRACE(ft_t_noise, "waiting until drive gets ready");
  153. if (ftape_ready_wait(ftape_timeout.seek,
  154. &status) == -EINTR) {
  155. /* Bail out on signal !
  156. */
  157. TRACE_ABORT(-EINTR, ft_t_warn,
  158. "interrupted by non-blockable signal");
  159. }
  160. difference = ((status ^ qic117_cmds[command].state) &
  161. qic117_cmds[command].mask);
  162. }
  163. while ((difference & QIC_STATUS_ERROR) != 0) {
  164. int err;
  165. qic117_cmd_t cmd;
  166. TRACE(ft_t_noise,
  167. "command %d issued while error pending",
  168. command);
  169. TRACE(ft_t_noise, "clearing error status");
  170. ftape_report_error(&err, &cmd, 1);
  171. ftape_report_raw_drive_status(&status);
  172. difference = ((status ^ qic117_cmds[command].state) &
  173. qic117_cmds[command].mask);
  174. if ((difference & QIC_STATUS_ERROR) != 0) {
  175. /* Bail out on fatal signal !
  176. */
  177. FT_SIGNAL_EXIT(_NEVER_BLOCK);
  178. }
  179. }
  180. if (difference) {
  181. /* Any remaining difference can't be solved
  182. * here.
  183. */
  184. if (difference & (QIC_STATUS_CARTRIDGE_PRESENT |
  185. QIC_STATUS_NEW_CARTRIDGE |
  186. QIC_STATUS_REFERENCED)) {
  187. TRACE(ft_t_warn,
  188. "Fatal: tape removed or reinserted !");
  189. ft_failure = 1;
  190. } else {
  191. TRACE(ft_t_err, "wrong state: 0x%02x should be: 0x%02x",
  192. status & qic117_cmds[command].mask,
  193. qic117_cmds[command].state);
  194. }
  195. TRACE_EXIT -EIO;
  196. }
  197. if (~status & QIC_STATUS_READY & qic117_cmds[command].mask) {
  198. TRACE_ABORT(-EBUSY, ft_t_err, "Bad: still busy!");
  199. }
  200. }
  201. TRACE_EXIT 0;
  202. }
  203. /* Issue a tape command:
  204. */
  205. int ftape_command(qic117_cmd_t command)
  206. {
  207. int result = 0;
  208. static int level;
  209. TRACE_FUN(ft_t_any);
  210. if ((unsigned int)command > NR_ITEMS(qic117_cmds)) {
  211. /* This is a bug we'll want to know about too.
  212. */
  213. TRACE_ABORT(-EIO, ft_t_bug, "bug - bad command: %d", command);
  214. }
  215. if (++level > 5) { /* This is a bug we'll want to know about. */
  216. --level;
  217. TRACE_ABORT(-EIO, ft_t_bug, "bug - recursion for command: %d",
  218. command);
  219. }
  220. /* disable logging and restriction check for some commands,
  221. * check all other commands that have a prescribed starting
  222. * status.
  223. */
  224. if (diagnostic_mode) {
  225. TRACE(ft_t_flow, "diagnostic command %d", command);
  226. } else if (command == QIC_REPORT_DRIVE_STATUS ||
  227. command == QIC_REPORT_NEXT_BIT) {
  228. TRACE(ft_t_any, "%s", qic117_cmds[command].name);
  229. } else {
  230. TRACE_CATCH(ft_check_cmd_restrictions(command), --level);
  231. }
  232. /* Now all conditions are met or result was < 0.
  233. */
  234. result = ft_send_to_drive((unsigned int)command);
  235. if (qic117_cmds[command].cmd_type == motion &&
  236. command != QIC_LOGICAL_FORWARD && command != QIC_STOP_TAPE) {
  237. ft_location.known = 0;
  238. }
  239. ftape_current_command = command;
  240. --level;
  241. TRACE_EXIT result;
  242. }
  243. /* Send a tape command parameter:
  244. * Generates command # of step pulses.
  245. * Skips tape-status call !
  246. */
  247. int ftape_parameter(unsigned int parameter)
  248. {
  249. TRACE_FUN(ft_t_any);
  250. TRACE(ft_t_flow, "called with parameter = %d", parameter);
  251. TRACE_EXIT ft_send_to_drive(parameter + 2);
  252. }
  253. /* Wait for the drive to get ready.
  254. * timeout time in milli-seconds
  255. * Returned status is valid if result != -EIO
  256. *
  257. * Should we allow to be killed by SIGINT? (^C)
  258. * Would be nice at least for large timeouts.
  259. */
  260. int ftape_ready_wait(unsigned int timeout, int *status)
  261. {
  262. unsigned long t0;
  263. unsigned int poll_delay;
  264. int signal_retries;
  265. TRACE_FUN(ft_t_any);
  266. /* the following ** REALLY ** reduces the system load when
  267. * e.g. one simply rewinds or retensions. The tape is slow
  268. * anyway. It is really not necessary to detect error
  269. * conditions with 1/10 seconds granularity
  270. *
  271. * On my AMD 133MHZ 486: 100 ms: 23% system load
  272. * 1 sec: 5%
  273. * 5 sec: 0.6%, yeah
  274. */
  275. if (timeout <= FT_SECOND) {
  276. poll_delay = 100 * FT_MILLISECOND;
  277. signal_retries = 20; /* two seconds */
  278. } else if (timeout < 20 * FT_SECOND) {
  279. TRACE(ft_t_flow, "setting poll delay to 1 second");
  280. poll_delay = FT_SECOND;
  281. signal_retries = 2; /* two seconds */
  282. } else {
  283. TRACE(ft_t_flow, "setting poll delay to 5 seconds");
  284. poll_delay = 5 * FT_SECOND;
  285. signal_retries = 1; /* five seconds */
  286. }
  287. for (;;) {
  288. t0 = jiffies;
  289. TRACE_CATCH(ftape_report_raw_drive_status(status),);
  290. if (*status & QIC_STATUS_READY) {
  291. TRACE_EXIT 0;
  292. }
  293. if (!signal_retries--) {
  294. FT_SIGNAL_EXIT(_NEVER_BLOCK);
  295. }
  296. if ((int)timeout >= 0) {
  297. /* this will fail when jiffies wraps around about
  298. * once every year :-)
  299. */
  300. timeout -= ((jiffies - t0) * FT_SECOND) / HZ;
  301. if (timeout <= 0) {
  302. TRACE_ABORT(-ETIME, ft_t_err, "timeout");
  303. }
  304. ftape_sleep(poll_delay);
  305. timeout -= poll_delay;
  306. } else {
  307. ftape_sleep(poll_delay);
  308. }
  309. }
  310. TRACE_EXIT -ETIME;
  311. }
  312. /* Issue command and wait up to timeout milli seconds for drive ready
  313. */
  314. int ftape_command_wait(qic117_cmd_t command, unsigned int timeout, int *status)
  315. {
  316. int result;
  317. /* Drive should be ready, issue command
  318. */
  319. result = ftape_command(command);
  320. if (result >= 0) {
  321. result = ftape_ready_wait(timeout, status);
  322. }
  323. return result;
  324. }
  325. static int ftape_parameter_wait(unsigned int parm, unsigned int timeout, int *status)
  326. {
  327. int result;
  328. /* Drive should be ready, issue command
  329. */
  330. result = ftape_parameter(parm);
  331. if (result >= 0) {
  332. result = ftape_ready_wait(timeout, status);
  333. }
  334. return result;
  335. }
  336. /*--------------------------------------------------------------------------
  337. * Report operations
  338. */
  339. /* Query the drive about its status. The command is sent and
  340. result_length bits of status are returned (2 extra bits are read
  341. for start and stop). */
  342. int ftape_report_operation(int *status,
  343. qic117_cmd_t command,
  344. int result_length)
  345. {
  346. int i, st3;
  347. unsigned int t0;
  348. unsigned int dt;
  349. TRACE_FUN(ft_t_any);
  350. TRACE_CATCH(ftape_command(command),);
  351. t0 = ftape_timestamp();
  352. i = 0;
  353. do {
  354. ++i;
  355. ftape_sleep(3 * FT_MILLISECOND); /* see remark below */
  356. TRACE_CATCH(fdc_sense_drive_status(&st3),);
  357. dt = ftape_timediff(t0, ftape_timestamp());
  358. /* Ack should be asserted within Ttimout + Tack = 6 msec.
  359. * Looks like some drives fail to do this so extend this
  360. * period to 300 msec.
  361. */
  362. } while (!(st3 & ST3_TRACK_0) && dt < 300000);
  363. if (!(st3 & ST3_TRACK_0)) {
  364. TRACE(ft_t_err,
  365. "No acknowledge after %u msec. (%i iter)", dt / 1000, i);
  366. TRACE_ABORT(-EIO, ft_t_err, "timeout on Acknowledge");
  367. }
  368. /* dt may be larger than expected because of other tasks
  369. * scheduled while we were sleeping.
  370. */
  371. if (i > 1 && dt > 6000) {
  372. TRACE(ft_t_err, "Acknowledge after %u msec. (%i iter)",
  373. dt / 1000, i);
  374. }
  375. *status = 0;
  376. for (i = 0; i < result_length + 1; i++) {
  377. TRACE_CATCH(ftape_command(QIC_REPORT_NEXT_BIT),);
  378. TRACE_CATCH(fdc_sense_drive_status(&st3),);
  379. if (i < result_length) {
  380. *status |= ((st3 & ST3_TRACK_0) ? 1 : 0) << i;
  381. } else if ((st3 & ST3_TRACK_0) == 0) {
  382. TRACE_ABORT(-EIO, ft_t_err, "missing status stop bit");
  383. }
  384. }
  385. /* this command will put track zero and index back into normal state */
  386. (void)ftape_command(QIC_REPORT_NEXT_BIT);
  387. TRACE_EXIT 0;
  388. }
  389. /* Report the current drive status. */
  390. int ftape_report_raw_drive_status(int *status)
  391. {
  392. int result;
  393. int count = 0;
  394. TRACE_FUN(ft_t_any);
  395. do {
  396. result = ftape_report_operation(status,
  397. QIC_REPORT_DRIVE_STATUS, 8);
  398. } while (result < 0 && ++count <= 3);
  399. if (result < 0) {
  400. TRACE_ABORT(-EIO, ft_t_err,
  401. "report_operation failed after %d trials", count);
  402. }
  403. if ((*status & 0xff) == 0xff) {
  404. TRACE_ABORT(-EIO, ft_t_err,
  405. "impossible drive status 0xff");
  406. }
  407. if (*status & QIC_STATUS_READY) {
  408. ftape_current_command = QIC_NO_COMMAND; /* completed */
  409. }
  410. ft_last_status.status.drive_status = (__u8)(*status & 0xff);
  411. TRACE_EXIT 0;
  412. }
  413. int ftape_report_drive_status(int *status)
  414. {
  415. TRACE_FUN(ft_t_any);
  416. TRACE_CATCH(ftape_report_raw_drive_status(status),);
  417. if (*status & QIC_STATUS_NEW_CARTRIDGE ||
  418. !(*status & QIC_STATUS_CARTRIDGE_PRESENT)) {
  419. ft_failure = 1; /* will inhibit further operations */
  420. TRACE_EXIT -EIO;
  421. }
  422. if (*status & QIC_STATUS_READY && *status & QIC_STATUS_ERROR) {
  423. /* Let caller handle all errors */
  424. TRACE_ABORT(1, ft_t_warn, "warning: error status set!");
  425. }
  426. TRACE_EXIT 0;
  427. }
  428. int ftape_report_error(unsigned int *error,
  429. qic117_cmd_t *command, int report)
  430. {
  431. static const ftape_error ftape_errors[] = QIC117_ERRORS;
  432. int code;
  433. TRACE_FUN(ft_t_any);
  434. TRACE_CATCH(ftape_report_operation(&code, QIC_REPORT_ERROR_CODE, 16),);
  435. *error = (unsigned int)(code & 0xff);
  436. *command = (qic117_cmd_t)((code>>8)&0xff);
  437. /* remember hardware status, maybe useful for status ioctls
  438. */
  439. ft_last_error.error.command = (__u8)*command;
  440. ft_last_error.error.error = (__u8)*error;
  441. if (!report) {
  442. TRACE_EXIT 0;
  443. }
  444. if (*error == 0) {
  445. TRACE_ABORT(0, ft_t_info, "No error");
  446. }
  447. TRACE(ft_t_info, "errorcode: %d", *error);
  448. if (*error < NR_ITEMS(ftape_errors)) {
  449. TRACE(ft_t_noise, "%sFatal ERROR:",
  450. (ftape_errors[*error].fatal ? "" : "Non-"));
  451. TRACE(ft_t_noise, "%s ...", ftape_errors[*error].message);
  452. } else {
  453. TRACE(ft_t_noise, "Unknown ERROR !");
  454. }
  455. if ((unsigned int)*command < NR_ITEMS(qic117_cmds) &&
  456. qic117_cmds[*command].name != NULL) {
  457. TRACE(ft_t_noise, "... caused by command \'%s\'",
  458. qic117_cmds[*command].name);
  459. } else {
  460. TRACE(ft_t_noise, "... caused by unknown command %d",
  461. *command);
  462. }
  463. TRACE_EXIT 0;
  464. }
  465. int ftape_report_configuration(qic_model *model,
  466. unsigned int *rate,
  467. int *qic_std,
  468. int *tape_len)
  469. {
  470. int result;
  471. int config;
  472. int status;
  473. static const unsigned int qic_rates[ 4] = { 250, 2000, 500, 1000 };
  474. TRACE_FUN(ft_t_any);
  475. result = ftape_report_operation(&config,
  476. QIC_REPORT_DRIVE_CONFIGURATION, 8);
  477. if (result < 0) {
  478. ft_last_status.status.drive_config = (__u8)0x00;
  479. *model = prehistoric;
  480. *rate = 500;
  481. *qic_std = QIC_TAPE_QIC40;
  482. *tape_len = 205;
  483. TRACE_EXIT 0;
  484. } else {
  485. ft_last_status.status.drive_config = (__u8)(config & 0xff);
  486. }
  487. *rate = qic_rates[(config & QIC_CONFIG_RATE_MASK) >> QIC_CONFIG_RATE_SHIFT];
  488. result = ftape_report_operation(&status, QIC_REPORT_TAPE_STATUS, 8);
  489. if (result < 0) {
  490. ft_last_status.status.tape_status = (__u8)0x00;
  491. /* pre- QIC117 rev C spec. drive, QIC_CONFIG_80 bit is valid.
  492. */
  493. *qic_std = (config & QIC_CONFIG_80) ?
  494. QIC_TAPE_QIC80 : QIC_TAPE_QIC40;
  495. /* ?? how's about 425ft tapes? */
  496. *tape_len = (config & QIC_CONFIG_LONG) ? 307 : 0;
  497. *model = pre_qic117c;
  498. result = 0;
  499. } else {
  500. ft_last_status.status.tape_status = (__u8)(status & 0xff);
  501. *model = post_qic117b;
  502. TRACE(ft_t_any, "report tape status result = %02x", status);
  503. /* post- QIC117 rev C spec. drive, QIC_CONFIG_80 bit is
  504. * invalid.
  505. */
  506. switch (status & QIC_TAPE_STD_MASK) {
  507. case QIC_TAPE_QIC40:
  508. case QIC_TAPE_QIC80:
  509. case QIC_TAPE_QIC3020:
  510. case QIC_TAPE_QIC3010:
  511. *qic_std = status & QIC_TAPE_STD_MASK;
  512. break;
  513. default:
  514. *qic_std = -1;
  515. break;
  516. }
  517. switch (status & QIC_TAPE_LEN_MASK) {
  518. case QIC_TAPE_205FT:
  519. /* 205 or 425+ ft 550 Oe tape */
  520. *tape_len = 0;
  521. break;
  522. case QIC_TAPE_307FT:
  523. /* 307.5 ft 550 Oe Extended Length (XL) tape */
  524. *tape_len = 307;
  525. break;
  526. case QIC_TAPE_VARIABLE:
  527. /* Variable length 550 Oe tape */
  528. *tape_len = 0;
  529. break;
  530. case QIC_TAPE_1100FT:
  531. /* 1100 ft 550 Oe tape */
  532. *tape_len = 1100;
  533. break;
  534. case QIC_TAPE_FLEX:
  535. /* Variable length 900 Oe tape */
  536. *tape_len = 0;
  537. break;
  538. default:
  539. *tape_len = -1;
  540. break;
  541. }
  542. if (*qic_std == -1 || *tape_len == -1) {
  543. TRACE(ft_t_any,
  544. "post qic-117b spec drive with unknown tape");
  545. }
  546. result = *tape_len == -1 ? -EIO : 0;
  547. if (status & QIC_TAPE_WIDE) {
  548. switch (*qic_std) {
  549. case QIC_TAPE_QIC80:
  550. TRACE(ft_t_info, "TR-1 tape detected");
  551. break;
  552. case QIC_TAPE_QIC3010:
  553. TRACE(ft_t_info, "TR-2 tape detected");
  554. break;
  555. case QIC_TAPE_QIC3020:
  556. TRACE(ft_t_info, "TR-3 tape detected");
  557. break;
  558. default:
  559. TRACE(ft_t_warn,
  560. "Unknown Travan tape type detected");
  561. break;
  562. }
  563. }
  564. }
  565. TRACE_EXIT (result < 0) ? -EIO : 0;
  566. }
  567. static int ftape_report_rom_version(int *version)
  568. {
  569. if (ftape_report_operation(version, QIC_REPORT_ROM_VERSION, 8) < 0) {
  570. return -EIO;
  571. } else {
  572. return 0;
  573. }
  574. }
  575. void ftape_report_vendor_id(unsigned int *id)
  576. {
  577. int result;
  578. TRACE_FUN(ft_t_any);
  579. /* We'll try to get a vendor id from the drive. First
  580. * according to the QIC-117 spec, a 16-bit id is requested.
  581. * If that fails we'll try an 8-bit version, otherwise we'll
  582. * try an undocumented query.
  583. */
  584. result = ftape_report_operation((int *) id, QIC_REPORT_VENDOR_ID, 16);
  585. if (result < 0) {
  586. result = ftape_report_operation((int *) id,
  587. QIC_REPORT_VENDOR_ID, 8);
  588. if (result < 0) {
  589. /* The following is an undocumented call found
  590. * in the CMS code.
  591. */
  592. result = ftape_report_operation((int *) id, 24, 8);
  593. if (result < 0) {
  594. *id = UNKNOWN_VENDOR;
  595. } else {
  596. TRACE(ft_t_noise, "got old 8 bit id: %04x",
  597. *id);
  598. *id |= 0x20000;
  599. }
  600. } else {
  601. TRACE(ft_t_noise, "got 8 bit id: %04x", *id);
  602. *id |= 0x10000;
  603. }
  604. } else {
  605. TRACE(ft_t_noise, "got 16 bit id: %04x", *id);
  606. }
  607. if (*id == 0x0047) {
  608. int version;
  609. int sign;
  610. if (ftape_report_rom_version(&version) < 0) {
  611. TRACE(ft_t_bug, "report rom version failed");
  612. TRACE_EXIT;
  613. }
  614. TRACE(ft_t_noise, "CMS rom version: %d", version);
  615. ftape_command(QIC_ENTER_DIAGNOSTIC_1);
  616. ftape_command(QIC_ENTER_DIAGNOSTIC_1);
  617. diagnostic_mode = 1;
  618. if (ftape_report_operation(&sign, 9, 8) < 0) {
  619. unsigned int error;
  620. qic117_cmd_t command;
  621. ftape_report_error(&error, &command, 1);
  622. ftape_command(QIC_ENTER_PRIMARY_MODE);
  623. diagnostic_mode = 0;
  624. TRACE_EXIT; /* failure ! */
  625. } else {
  626. TRACE(ft_t_noise, "CMS signature: %02x", sign);
  627. }
  628. if (sign == 0xa5) {
  629. result = ftape_report_operation(&sign, 37, 8);
  630. if (result < 0) {
  631. if (version >= 63) {
  632. *id = 0x8880;
  633. TRACE(ft_t_noise,
  634. "This is an Iomega drive !");
  635. } else {
  636. *id = 0x0047;
  637. TRACE(ft_t_noise,
  638. "This is a real CMS drive !");
  639. }
  640. } else {
  641. *id = 0x0047;
  642. TRACE(ft_t_noise, "CMS status: %d", sign);
  643. }
  644. } else {
  645. *id = UNKNOWN_VENDOR;
  646. }
  647. ftape_command(QIC_ENTER_PRIMARY_MODE);
  648. diagnostic_mode = 0;
  649. }
  650. TRACE_EXIT;
  651. }
  652. static int qic_rate_code(unsigned int rate)
  653. {
  654. switch (rate) {
  655. case 250:
  656. return QIC_CONFIG_RATE_250;
  657. case 500:
  658. return QIC_CONFIG_RATE_500;
  659. case 1000:
  660. return QIC_CONFIG_RATE_1000;
  661. case 2000:
  662. return QIC_CONFIG_RATE_2000;
  663. default:
  664. return QIC_CONFIG_RATE_500;
  665. }
  666. }
  667. static int ftape_set_rate_test(unsigned int *max_rate)
  668. {
  669. unsigned int error;
  670. qic117_cmd_t command;
  671. int status;
  672. int supported = 0;
  673. TRACE_FUN(ft_t_any);
  674. /* Check if the drive does support the select rate command
  675. * by testing all different settings. If any one is accepted
  676. * we assume the command is supported, else not.
  677. */
  678. for (*max_rate = 2000; *max_rate >= 250; *max_rate /= 2) {
  679. if (ftape_command(QIC_SELECT_RATE) < 0) {
  680. continue;
  681. }
  682. if (ftape_parameter_wait(qic_rate_code(*max_rate),
  683. 1 * FT_SECOND, &status) < 0) {
  684. continue;
  685. }
  686. if (status & QIC_STATUS_ERROR) {
  687. ftape_report_error(&error, &command, 0);
  688. continue;
  689. }
  690. supported = 1; /* did accept a request */
  691. break;
  692. }
  693. TRACE(ft_t_noise, "Select Rate command is%s supported",
  694. supported ? "" : " not");
  695. TRACE_EXIT supported;
  696. }
  697. int ftape_set_data_rate(unsigned int new_rate /* Kbps */, unsigned int qic_std)
  698. {
  699. int status;
  700. int result = 0;
  701. unsigned int data_rate = new_rate;
  702. static int supported;
  703. int rate_changed = 0;
  704. qic_model dummy_model;
  705. unsigned int dummy_qic_std, dummy_tape_len;
  706. TRACE_FUN(ft_t_any);
  707. if (ft_drive_max_rate == 0) { /* first time */
  708. supported = ftape_set_rate_test(&ft_drive_max_rate);
  709. }
  710. if (supported) {
  711. ftape_command(QIC_SELECT_RATE);
  712. result = ftape_parameter_wait(qic_rate_code(new_rate),
  713. 1 * FT_SECOND, &status);
  714. if (result >= 0 && !(status & QIC_STATUS_ERROR)) {
  715. rate_changed = 1;
  716. }
  717. }
  718. TRACE_CATCH(result = ftape_report_configuration(&dummy_model,
  719. &data_rate,
  720. &dummy_qic_std,
  721. &dummy_tape_len),);
  722. if (data_rate != new_rate) {
  723. if (!supported) {
  724. TRACE(ft_t_warn, "Rate change not supported!");
  725. } else if (rate_changed) {
  726. TRACE(ft_t_warn, "Requested: %d, got %d",
  727. new_rate, data_rate);
  728. } else {
  729. TRACE(ft_t_warn, "Rate change failed!");
  730. }
  731. result = -EINVAL;
  732. }
  733. /*
  734. * Set data rate and write precompensation as specified:
  735. *
  736. * | QIC-40/80 | QIC-3010/3020
  737. * rate | precomp | precomp
  738. * ----------+-------------+--------------
  739. * 250 Kbps. | 250 ns. | 0 ns.
  740. * 500 Kbps. | 125 ns. | 0 ns.
  741. * 1 Mbps. | 42 ns. | 0 ns.
  742. * 2 Mbps | N/A | 0 ns.
  743. */
  744. if ((qic_std == QIC_TAPE_QIC40 && data_rate > 500) ||
  745. (qic_std == QIC_TAPE_QIC80 && data_rate > 1000)) {
  746. TRACE_ABORT(-EINVAL,
  747. ft_t_warn, "Datarate too high for QIC-mode");
  748. }
  749. TRACE_CATCH(fdc_set_data_rate(data_rate),_res = -EINVAL);
  750. ft_data_rate = data_rate;
  751. if (qic_std == QIC_TAPE_QIC40 || qic_std == QIC_TAPE_QIC80) {
  752. switch (data_rate) {
  753. case 250:
  754. fdc_set_write_precomp(250);
  755. break;
  756. default:
  757. case 500:
  758. fdc_set_write_precomp(125);
  759. break;
  760. case 1000:
  761. fdc_set_write_precomp(42);
  762. break;
  763. }
  764. } else {
  765. fdc_set_write_precomp(0);
  766. }
  767. TRACE_EXIT result;
  768. }
  769. /* The next two functions are used to cope with excessive overrun errors
  770. */
  771. int ftape_increase_threshold(void)
  772. {
  773. TRACE_FUN(ft_t_flow);
  774. if (fdc.type < i82077 || ft_fdc_threshold >= 12) {
  775. TRACE_ABORT(-EIO, ft_t_err, "cannot increase fifo threshold");
  776. }
  777. if (fdc_fifo_threshold(++ft_fdc_threshold, NULL, NULL, NULL) < 0) {
  778. TRACE(ft_t_err, "cannot increase fifo threshold");
  779. ft_fdc_threshold --;
  780. fdc_reset();
  781. }
  782. TRACE(ft_t_info, "New FIFO threshold: %d", ft_fdc_threshold);
  783. TRACE_EXIT 0;
  784. }
  785. int ftape_half_data_rate(void)
  786. {
  787. if (ft_data_rate < 500) {
  788. return -1;
  789. }
  790. if (ftape_set_data_rate(ft_data_rate / 2, ft_qic_std) < 0) {
  791. return -EIO;
  792. }
  793. ftape_calc_timeouts(ft_qic_std, ft_data_rate, ftape_tape_len);
  794. return 0;
  795. }
  796. /* Seek the head to the specified track.
  797. */
  798. int ftape_seek_head_to_track(unsigned int track)
  799. {
  800. int status;
  801. TRACE_FUN(ft_t_any);
  802. ft_location.track = -1; /* remains set in case of error */
  803. if (track >= ft_tracks_per_tape) {
  804. TRACE_ABORT(-EINVAL, ft_t_bug, "track out of bounds");
  805. }
  806. TRACE(ft_t_flow, "seeking track %d", track);
  807. TRACE_CATCH(ftape_command(QIC_SEEK_HEAD_TO_TRACK),);
  808. TRACE_CATCH(ftape_parameter_wait(track, ftape_timeout.head_seek,
  809. &status),);
  810. ft_location.track = track;
  811. ftape_might_be_off_track = 0;
  812. TRACE_EXIT 0;
  813. }
  814. int ftape_wakeup_drive(wake_up_types method)
  815. {
  816. int status;
  817. int motor_on = 0;
  818. TRACE_FUN(ft_t_any);
  819. switch (method) {
  820. case wake_up_colorado:
  821. TRACE_CATCH(ftape_command(QIC_PHANTOM_SELECT),);
  822. TRACE_CATCH(ftape_parameter(0 /* ft_drive_sel ?? */),);
  823. break;
  824. case wake_up_mountain:
  825. TRACE_CATCH(ftape_command(QIC_SOFT_SELECT),);
  826. ftape_sleep(FT_MILLISECOND); /* NEEDED */
  827. TRACE_CATCH(ftape_parameter(18),);
  828. break;
  829. case wake_up_insight:
  830. ftape_sleep(100 * FT_MILLISECOND);
  831. motor_on = 1;
  832. fdc_motor(motor_on); /* enable is done by motor-on */
  833. case no_wake_up:
  834. break;
  835. default:
  836. TRACE_EXIT -ENODEV; /* unknown wakeup method */
  837. break;
  838. }
  839. /* If wakeup succeeded we shouldn't get an error here..
  840. */
  841. TRACE_CATCH(ftape_report_raw_drive_status(&status),
  842. if (motor_on) {
  843. fdc_motor(0);
  844. });
  845. TRACE_EXIT 0;
  846. }
  847. int ftape_put_drive_to_sleep(wake_up_types method)
  848. {
  849. TRACE_FUN(ft_t_any);
  850. switch (method) {
  851. case wake_up_colorado:
  852. TRACE_CATCH(ftape_command(QIC_PHANTOM_DESELECT),);
  853. break;
  854. case wake_up_mountain:
  855. TRACE_CATCH(ftape_command(QIC_SOFT_DESELECT),);
  856. break;
  857. case wake_up_insight:
  858. fdc_motor(0); /* enable is done by motor-on */
  859. case no_wake_up: /* no wakeup / no sleep ! */
  860. break;
  861. default:
  862. TRACE_EXIT -ENODEV; /* unknown wakeup method */
  863. }
  864. TRACE_EXIT 0;
  865. }
  866. int ftape_reset_drive(void)
  867. {
  868. int result = 0;
  869. int status;
  870. unsigned int err_code;
  871. qic117_cmd_t err_command;
  872. int i;
  873. TRACE_FUN(ft_t_any);
  874. /* We want to re-establish contact with our drive. Fire a
  875. * number of reset commands (single step pulses) and pray for
  876. * success.
  877. */
  878. for (i = 0; i < 2; ++i) {
  879. TRACE(ft_t_flow, "Resetting fdc");
  880. fdc_reset();
  881. ftape_sleep(10 * FT_MILLISECOND);
  882. TRACE(ft_t_flow, "Reset command to drive");
  883. result = ftape_command(QIC_RESET);
  884. if (result == 0) {
  885. ftape_sleep(1 * FT_SECOND); /* drive not
  886. * accessible
  887. * during 1 second
  888. */
  889. TRACE(ft_t_flow, "Re-selecting drive");
  890. /* Strange, the QIC-117 specs don't mention
  891. * this but the drive gets deselected after a
  892. * soft reset ! So we need to enable it
  893. * again.
  894. */
  895. if (ftape_wakeup_drive(ft_drive_type.wake_up) < 0) {
  896. TRACE(ft_t_err, "Wakeup failed !");
  897. }
  898. TRACE(ft_t_flow, "Waiting until drive gets ready");
  899. result= ftape_ready_wait(ftape_timeout.reset, &status);
  900. if (result == 0 && (status & QIC_STATUS_ERROR)) {
  901. result = ftape_report_error(&err_code,
  902. &err_command, 1);
  903. if (result == 0 && err_code == 27) {
  904. /* Okay, drive saw reset
  905. * command and responded as it
  906. * should
  907. */
  908. break;
  909. } else {
  910. result = -EIO;
  911. }
  912. } else {
  913. result = -EIO;
  914. }
  915. }
  916. FT_SIGNAL_EXIT(_DONT_BLOCK);
  917. }
  918. if (result != 0) {
  919. TRACE(ft_t_err, "General failure to reset tape drive");
  920. } else {
  921. /* Restore correct settings: keep original rate
  922. */
  923. ftape_set_data_rate(ft_data_rate, ft_qic_std);
  924. }
  925. ftape_init_drive_needed = 1;
  926. TRACE_EXIT result;
  927. }