tape_core.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. /*
  2. * drivers/s390/char/tape_core.c
  3. * basic function of the tape device driver
  4. *
  5. * S390 and zSeries version
  6. * Copyright IBM Corp. 2001, 2009
  7. * Author(s): Carsten Otte <cotte@de.ibm.com>
  8. * Michael Holzheu <holzheu@de.ibm.com>
  9. * Tuan Ngo-Anh <ngoanh@de.ibm.com>
  10. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  11. * Stefan Bader <shbader@de.ibm.com>
  12. */
  13. #define KMSG_COMPONENT "tape"
  14. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  15. #include <linux/module.h>
  16. #include <linux/init.h> // for kernel parameters
  17. #include <linux/kmod.h> // for requesting modules
  18. #include <linux/spinlock.h> // for locks
  19. #include <linux/vmalloc.h>
  20. #include <linux/list.h>
  21. #include <linux/slab.h>
  22. #include <asm/types.h> // for variable types
  23. #define TAPE_DBF_AREA tape_core_dbf
  24. #include "tape.h"
  25. #include "tape_std.h"
  26. #define LONG_BUSY_TIMEOUT 180 /* seconds */
  27. static void __tape_do_irq (struct ccw_device *, unsigned long, struct irb *);
  28. static void tape_delayed_next_request(struct work_struct *);
  29. static void tape_long_busy_timeout(unsigned long data);
  30. /*
  31. * One list to contain all tape devices of all disciplines, so
  32. * we can assign the devices to minor numbers of the same major
  33. * The list is protected by the rwlock
  34. */
  35. static LIST_HEAD(tape_device_list);
  36. static DEFINE_RWLOCK(tape_device_lock);
  37. /*
  38. * Pointer to debug area.
  39. */
  40. debug_info_t *TAPE_DBF_AREA = NULL;
  41. EXPORT_SYMBOL(TAPE_DBF_AREA);
  42. /*
  43. * Printable strings for tape enumerations.
  44. */
  45. const char *tape_state_verbose[TS_SIZE] =
  46. {
  47. [TS_UNUSED] = "UNUSED",
  48. [TS_IN_USE] = "IN_USE",
  49. [TS_BLKUSE] = "BLKUSE",
  50. [TS_INIT] = "INIT ",
  51. [TS_NOT_OPER] = "NOT_OP"
  52. };
  53. const char *tape_op_verbose[TO_SIZE] =
  54. {
  55. [TO_BLOCK] = "BLK", [TO_BSB] = "BSB",
  56. [TO_BSF] = "BSF", [TO_DSE] = "DSE",
  57. [TO_FSB] = "FSB", [TO_FSF] = "FSF",
  58. [TO_LBL] = "LBL", [TO_NOP] = "NOP",
  59. [TO_RBA] = "RBA", [TO_RBI] = "RBI",
  60. [TO_RFO] = "RFO", [TO_REW] = "REW",
  61. [TO_RUN] = "RUN", [TO_WRI] = "WRI",
  62. [TO_WTM] = "WTM", [TO_MSEN] = "MSN",
  63. [TO_LOAD] = "LOA", [TO_READ_CONFIG] = "RCF",
  64. [TO_READ_ATTMSG] = "RAT",
  65. [TO_DIS] = "DIS", [TO_ASSIGN] = "ASS",
  66. [TO_UNASSIGN] = "UAS", [TO_CRYPT_ON] = "CON",
  67. [TO_CRYPT_OFF] = "COF", [TO_KEKL_SET] = "KLS",
  68. [TO_KEKL_QUERY] = "KLQ",[TO_RDC] = "RDC",
  69. };
  70. static int devid_to_int(struct ccw_dev_id *dev_id)
  71. {
  72. return dev_id->devno + (dev_id->ssid << 16);
  73. }
  74. /*
  75. * Some channel attached tape specific attributes.
  76. *
  77. * FIXME: In the future the first_minor and blocksize attribute should be
  78. * replaced by a link to the cdev tree.
  79. */
  80. static ssize_t
  81. tape_medium_state_show(struct device *dev, struct device_attribute *attr, char *buf)
  82. {
  83. struct tape_device *tdev;
  84. tdev = dev_get_drvdata(dev);
  85. return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->medium_state);
  86. }
  87. static
  88. DEVICE_ATTR(medium_state, 0444, tape_medium_state_show, NULL);
  89. static ssize_t
  90. tape_first_minor_show(struct device *dev, struct device_attribute *attr, char *buf)
  91. {
  92. struct tape_device *tdev;
  93. tdev = dev_get_drvdata(dev);
  94. return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->first_minor);
  95. }
  96. static
  97. DEVICE_ATTR(first_minor, 0444, tape_first_minor_show, NULL);
  98. static ssize_t
  99. tape_state_show(struct device *dev, struct device_attribute *attr, char *buf)
  100. {
  101. struct tape_device *tdev;
  102. tdev = dev_get_drvdata(dev);
  103. return scnprintf(buf, PAGE_SIZE, "%s\n", (tdev->first_minor < 0) ?
  104. "OFFLINE" : tape_state_verbose[tdev->tape_state]);
  105. }
  106. static
  107. DEVICE_ATTR(state, 0444, tape_state_show, NULL);
  108. static ssize_t
  109. tape_operation_show(struct device *dev, struct device_attribute *attr, char *buf)
  110. {
  111. struct tape_device *tdev;
  112. ssize_t rc;
  113. tdev = dev_get_drvdata(dev);
  114. if (tdev->first_minor < 0)
  115. return scnprintf(buf, PAGE_SIZE, "N/A\n");
  116. spin_lock_irq(get_ccwdev_lock(tdev->cdev));
  117. if (list_empty(&tdev->req_queue))
  118. rc = scnprintf(buf, PAGE_SIZE, "---\n");
  119. else {
  120. struct tape_request *req;
  121. req = list_entry(tdev->req_queue.next, struct tape_request,
  122. list);
  123. rc = scnprintf(buf,PAGE_SIZE, "%s\n", tape_op_verbose[req->op]);
  124. }
  125. spin_unlock_irq(get_ccwdev_lock(tdev->cdev));
  126. return rc;
  127. }
  128. static
  129. DEVICE_ATTR(operation, 0444, tape_operation_show, NULL);
  130. static ssize_t
  131. tape_blocksize_show(struct device *dev, struct device_attribute *attr, char *buf)
  132. {
  133. struct tape_device *tdev;
  134. tdev = dev_get_drvdata(dev);
  135. return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->char_data.block_size);
  136. }
  137. static
  138. DEVICE_ATTR(blocksize, 0444, tape_blocksize_show, NULL);
  139. static struct attribute *tape_attrs[] = {
  140. &dev_attr_medium_state.attr,
  141. &dev_attr_first_minor.attr,
  142. &dev_attr_state.attr,
  143. &dev_attr_operation.attr,
  144. &dev_attr_blocksize.attr,
  145. NULL
  146. };
  147. static struct attribute_group tape_attr_group = {
  148. .attrs = tape_attrs,
  149. };
  150. /*
  151. * Tape state functions
  152. */
  153. void
  154. tape_state_set(struct tape_device *device, enum tape_state newstate)
  155. {
  156. const char *str;
  157. if (device->tape_state == TS_NOT_OPER) {
  158. DBF_EVENT(3, "ts_set err: not oper\n");
  159. return;
  160. }
  161. DBF_EVENT(4, "ts. dev: %x\n", device->first_minor);
  162. DBF_EVENT(4, "old ts:\t\n");
  163. if (device->tape_state < TS_SIZE && device->tape_state >=0 )
  164. str = tape_state_verbose[device->tape_state];
  165. else
  166. str = "UNKNOWN TS";
  167. DBF_EVENT(4, "%s\n", str);
  168. DBF_EVENT(4, "new ts:\t\n");
  169. if (newstate < TS_SIZE && newstate >= 0)
  170. str = tape_state_verbose[newstate];
  171. else
  172. str = "UNKNOWN TS";
  173. DBF_EVENT(4, "%s\n", str);
  174. device->tape_state = newstate;
  175. wake_up(&device->state_change_wq);
  176. }
  177. struct tape_med_state_work_data {
  178. struct tape_device *device;
  179. enum tape_medium_state state;
  180. struct work_struct work;
  181. };
  182. static void
  183. tape_med_state_work_handler(struct work_struct *work)
  184. {
  185. static char env_state_loaded[] = "MEDIUM_STATE=LOADED";
  186. static char env_state_unloaded[] = "MEDIUM_STATE=UNLOADED";
  187. struct tape_med_state_work_data *p =
  188. container_of(work, struct tape_med_state_work_data, work);
  189. struct tape_device *device = p->device;
  190. char *envp[] = { NULL, NULL };
  191. switch (p->state) {
  192. case MS_UNLOADED:
  193. pr_info("%s: The tape cartridge has been successfully "
  194. "unloaded\n", dev_name(&device->cdev->dev));
  195. envp[0] = env_state_unloaded;
  196. kobject_uevent_env(&device->cdev->dev.kobj, KOBJ_CHANGE, envp);
  197. break;
  198. case MS_LOADED:
  199. pr_info("%s: A tape cartridge has been mounted\n",
  200. dev_name(&device->cdev->dev));
  201. envp[0] = env_state_loaded;
  202. kobject_uevent_env(&device->cdev->dev.kobj, KOBJ_CHANGE, envp);
  203. break;
  204. default:
  205. break;
  206. }
  207. tape_put_device(device);
  208. kfree(p);
  209. }
  210. static void
  211. tape_med_state_work(struct tape_device *device, enum tape_medium_state state)
  212. {
  213. struct tape_med_state_work_data *p;
  214. p = kzalloc(sizeof(*p), GFP_ATOMIC);
  215. if (p) {
  216. INIT_WORK(&p->work, tape_med_state_work_handler);
  217. p->device = tape_get_device(device);
  218. p->state = state;
  219. schedule_work(&p->work);
  220. }
  221. }
  222. void
  223. tape_med_state_set(struct tape_device *device, enum tape_medium_state newstate)
  224. {
  225. enum tape_medium_state oldstate;
  226. oldstate = device->medium_state;
  227. if (oldstate == newstate)
  228. return;
  229. device->medium_state = newstate;
  230. switch(newstate){
  231. case MS_UNLOADED:
  232. device->tape_generic_status |= GMT_DR_OPEN(~0);
  233. if (oldstate == MS_LOADED)
  234. tape_med_state_work(device, MS_UNLOADED);
  235. break;
  236. case MS_LOADED:
  237. device->tape_generic_status &= ~GMT_DR_OPEN(~0);
  238. if (oldstate == MS_UNLOADED)
  239. tape_med_state_work(device, MS_LOADED);
  240. break;
  241. default:
  242. break;
  243. }
  244. wake_up(&device->state_change_wq);
  245. }
  246. /*
  247. * Stop running ccw. Has to be called with the device lock held.
  248. */
  249. static int
  250. __tape_cancel_io(struct tape_device *device, struct tape_request *request)
  251. {
  252. int retries;
  253. int rc;
  254. /* Check if interrupt has already been processed */
  255. if (request->callback == NULL)
  256. return 0;
  257. rc = 0;
  258. for (retries = 0; retries < 5; retries++) {
  259. rc = ccw_device_clear(device->cdev, (long) request);
  260. switch (rc) {
  261. case 0:
  262. request->status = TAPE_REQUEST_DONE;
  263. return 0;
  264. case -EBUSY:
  265. request->status = TAPE_REQUEST_CANCEL;
  266. schedule_delayed_work(&device->tape_dnr, 0);
  267. return 0;
  268. case -ENODEV:
  269. DBF_EXCEPTION(2, "device gone, retry\n");
  270. break;
  271. case -EIO:
  272. DBF_EXCEPTION(2, "I/O error, retry\n");
  273. break;
  274. default:
  275. BUG();
  276. }
  277. }
  278. return rc;
  279. }
  280. /*
  281. * Add device into the sorted list, giving it the first
  282. * available minor number.
  283. */
  284. static int
  285. tape_assign_minor(struct tape_device *device)
  286. {
  287. struct tape_device *tmp;
  288. int minor;
  289. minor = 0;
  290. write_lock(&tape_device_lock);
  291. list_for_each_entry(tmp, &tape_device_list, node) {
  292. if (minor < tmp->first_minor)
  293. break;
  294. minor += TAPE_MINORS_PER_DEV;
  295. }
  296. if (minor >= 256) {
  297. write_unlock(&tape_device_lock);
  298. return -ENODEV;
  299. }
  300. device->first_minor = minor;
  301. list_add_tail(&device->node, &tmp->node);
  302. write_unlock(&tape_device_lock);
  303. return 0;
  304. }
  305. /* remove device from the list */
  306. static void
  307. tape_remove_minor(struct tape_device *device)
  308. {
  309. write_lock(&tape_device_lock);
  310. list_del_init(&device->node);
  311. device->first_minor = -1;
  312. write_unlock(&tape_device_lock);
  313. }
  314. /*
  315. * Set a device online.
  316. *
  317. * This function is called by the common I/O layer to move a device from the
  318. * detected but offline into the online state.
  319. * If we return an error (RC < 0) the device remains in the offline state. This
  320. * can happen if the device is assigned somewhere else, for example.
  321. */
  322. int
  323. tape_generic_online(struct tape_device *device,
  324. struct tape_discipline *discipline)
  325. {
  326. int rc;
  327. DBF_LH(6, "tape_enable_device(%p, %p)\n", device, discipline);
  328. if (device->tape_state != TS_INIT) {
  329. DBF_LH(3, "Tapestate not INIT (%d)\n", device->tape_state);
  330. return -EINVAL;
  331. }
  332. init_timer(&device->lb_timeout);
  333. device->lb_timeout.function = tape_long_busy_timeout;
  334. /* Let the discipline have a go at the device. */
  335. device->discipline = discipline;
  336. if (!try_module_get(discipline->owner)) {
  337. return -EINVAL;
  338. }
  339. rc = discipline->setup_device(device);
  340. if (rc)
  341. goto out;
  342. rc = tape_assign_minor(device);
  343. if (rc)
  344. goto out_discipline;
  345. rc = tapechar_setup_device(device);
  346. if (rc)
  347. goto out_minor;
  348. tape_state_set(device, TS_UNUSED);
  349. DBF_LH(3, "(%08x): Drive set online\n", device->cdev_id);
  350. return 0;
  351. out_minor:
  352. tape_remove_minor(device);
  353. out_discipline:
  354. device->discipline->cleanup_device(device);
  355. device->discipline = NULL;
  356. out:
  357. module_put(discipline->owner);
  358. return rc;
  359. }
  360. static void
  361. tape_cleanup_device(struct tape_device *device)
  362. {
  363. tapechar_cleanup_device(device);
  364. device->discipline->cleanup_device(device);
  365. module_put(device->discipline->owner);
  366. tape_remove_minor(device);
  367. tape_med_state_set(device, MS_UNKNOWN);
  368. }
  369. /*
  370. * Suspend device.
  371. *
  372. * Called by the common I/O layer if the drive should be suspended on user
  373. * request. We refuse to suspend if the device is loaded or in use for the
  374. * following reason:
  375. * While the Linux guest is suspended, it might be logged off which causes
  376. * devices to be detached. Tape devices are automatically rewound and unloaded
  377. * during DETACH processing (unless the tape device was attached with the
  378. * NOASSIGN or MULTIUSER option). After rewind/unload, there is no way to
  379. * resume the original state of the tape device, since we would need to
  380. * manually re-load the cartridge which was active at suspend time.
  381. */
  382. int tape_generic_pm_suspend(struct ccw_device *cdev)
  383. {
  384. struct tape_device *device;
  385. device = dev_get_drvdata(&cdev->dev);
  386. if (!device) {
  387. return -ENODEV;
  388. }
  389. DBF_LH(3, "(%08x): tape_generic_pm_suspend(%p)\n",
  390. device->cdev_id, device);
  391. if (device->medium_state != MS_UNLOADED) {
  392. pr_err("A cartridge is loaded in tape device %s, "
  393. "refusing to suspend\n", dev_name(&cdev->dev));
  394. return -EBUSY;
  395. }
  396. spin_lock_irq(get_ccwdev_lock(device->cdev));
  397. switch (device->tape_state) {
  398. case TS_INIT:
  399. case TS_NOT_OPER:
  400. case TS_UNUSED:
  401. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  402. break;
  403. default:
  404. pr_err("Tape device %s is busy, refusing to "
  405. "suspend\n", dev_name(&cdev->dev));
  406. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  407. return -EBUSY;
  408. }
  409. DBF_LH(3, "(%08x): Drive suspended.\n", device->cdev_id);
  410. return 0;
  411. }
  412. /*
  413. * Set device offline.
  414. *
  415. * Called by the common I/O layer if the drive should set offline on user
  416. * request. We may prevent this by returning an error.
  417. * Manual offline is only allowed while the drive is not in use.
  418. */
  419. int
  420. tape_generic_offline(struct ccw_device *cdev)
  421. {
  422. struct tape_device *device;
  423. device = dev_get_drvdata(&cdev->dev);
  424. if (!device) {
  425. return -ENODEV;
  426. }
  427. DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
  428. device->cdev_id, device);
  429. spin_lock_irq(get_ccwdev_lock(device->cdev));
  430. switch (device->tape_state) {
  431. case TS_INIT:
  432. case TS_NOT_OPER:
  433. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  434. break;
  435. case TS_UNUSED:
  436. tape_state_set(device, TS_INIT);
  437. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  438. tape_cleanup_device(device);
  439. break;
  440. default:
  441. DBF_EVENT(3, "(%08x): Set offline failed "
  442. "- drive in use.\n",
  443. device->cdev_id);
  444. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  445. return -EBUSY;
  446. }
  447. DBF_LH(3, "(%08x): Drive set offline.\n", device->cdev_id);
  448. return 0;
  449. }
  450. /*
  451. * Allocate memory for a new device structure.
  452. */
  453. static struct tape_device *
  454. tape_alloc_device(void)
  455. {
  456. struct tape_device *device;
  457. device = kzalloc(sizeof(struct tape_device), GFP_KERNEL);
  458. if (device == NULL) {
  459. DBF_EXCEPTION(2, "ti:no mem\n");
  460. return ERR_PTR(-ENOMEM);
  461. }
  462. device->modeset_byte = kmalloc(1, GFP_KERNEL | GFP_DMA);
  463. if (device->modeset_byte == NULL) {
  464. DBF_EXCEPTION(2, "ti:no mem\n");
  465. kfree(device);
  466. return ERR_PTR(-ENOMEM);
  467. }
  468. mutex_init(&device->mutex);
  469. INIT_LIST_HEAD(&device->req_queue);
  470. INIT_LIST_HEAD(&device->node);
  471. init_waitqueue_head(&device->state_change_wq);
  472. init_waitqueue_head(&device->wait_queue);
  473. device->tape_state = TS_INIT;
  474. device->medium_state = MS_UNKNOWN;
  475. *device->modeset_byte = 0;
  476. device->first_minor = -1;
  477. atomic_set(&device->ref_count, 1);
  478. INIT_DELAYED_WORK(&device->tape_dnr, tape_delayed_next_request);
  479. return device;
  480. }
  481. /*
  482. * Get a reference to an existing device structure. This will automatically
  483. * increment the reference count.
  484. */
  485. struct tape_device *
  486. tape_get_device(struct tape_device *device)
  487. {
  488. int count;
  489. count = atomic_inc_return(&device->ref_count);
  490. DBF_EVENT(4, "tape_get_device(%p) = %i\n", device, count);
  491. return device;
  492. }
  493. /*
  494. * Decrease the reference counter of a devices structure. If the
  495. * reference counter reaches zero free the device structure.
  496. * The function returns a NULL pointer to be used by the caller
  497. * for clearing reference pointers.
  498. */
  499. void
  500. tape_put_device(struct tape_device *device)
  501. {
  502. int count;
  503. count = atomic_dec_return(&device->ref_count);
  504. DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device, count);
  505. BUG_ON(count < 0);
  506. if (count == 0) {
  507. kfree(device->modeset_byte);
  508. kfree(device);
  509. }
  510. }
  511. /*
  512. * Find tape device by a device index.
  513. */
  514. struct tape_device *
  515. tape_find_device(int devindex)
  516. {
  517. struct tape_device *device, *tmp;
  518. device = ERR_PTR(-ENODEV);
  519. read_lock(&tape_device_lock);
  520. list_for_each_entry(tmp, &tape_device_list, node) {
  521. if (tmp->first_minor / TAPE_MINORS_PER_DEV == devindex) {
  522. device = tape_get_device(tmp);
  523. break;
  524. }
  525. }
  526. read_unlock(&tape_device_lock);
  527. return device;
  528. }
  529. /*
  530. * Driverfs tape probe function.
  531. */
  532. int
  533. tape_generic_probe(struct ccw_device *cdev)
  534. {
  535. struct tape_device *device;
  536. int ret;
  537. struct ccw_dev_id dev_id;
  538. device = tape_alloc_device();
  539. if (IS_ERR(device))
  540. return -ENODEV;
  541. ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP |
  542. CCWDEV_DO_MULTIPATH);
  543. ret = sysfs_create_group(&cdev->dev.kobj, &tape_attr_group);
  544. if (ret) {
  545. tape_put_device(device);
  546. return ret;
  547. }
  548. dev_set_drvdata(&cdev->dev, device);
  549. cdev->handler = __tape_do_irq;
  550. device->cdev = cdev;
  551. ccw_device_get_id(cdev, &dev_id);
  552. device->cdev_id = devid_to_int(&dev_id);
  553. return ret;
  554. }
  555. static void
  556. __tape_discard_requests(struct tape_device *device)
  557. {
  558. struct tape_request * request;
  559. struct list_head * l, *n;
  560. list_for_each_safe(l, n, &device->req_queue) {
  561. request = list_entry(l, struct tape_request, list);
  562. if (request->status == TAPE_REQUEST_IN_IO)
  563. request->status = TAPE_REQUEST_DONE;
  564. list_del(&request->list);
  565. /* Decrease ref_count for removed request. */
  566. request->device = NULL;
  567. tape_put_device(device);
  568. request->rc = -EIO;
  569. if (request->callback != NULL)
  570. request->callback(request, request->callback_data);
  571. }
  572. }
  573. /*
  574. * Driverfs tape remove function.
  575. *
  576. * This function is called whenever the common I/O layer detects the device
  577. * gone. This can happen at any time and we cannot refuse.
  578. */
  579. void
  580. tape_generic_remove(struct ccw_device *cdev)
  581. {
  582. struct tape_device * device;
  583. device = dev_get_drvdata(&cdev->dev);
  584. if (!device) {
  585. return;
  586. }
  587. DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device->cdev_id, cdev);
  588. spin_lock_irq(get_ccwdev_lock(device->cdev));
  589. switch (device->tape_state) {
  590. case TS_INIT:
  591. tape_state_set(device, TS_NOT_OPER);
  592. case TS_NOT_OPER:
  593. /*
  594. * Nothing to do.
  595. */
  596. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  597. break;
  598. case TS_UNUSED:
  599. /*
  600. * Need only to release the device.
  601. */
  602. tape_state_set(device, TS_NOT_OPER);
  603. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  604. tape_cleanup_device(device);
  605. break;
  606. default:
  607. /*
  608. * There may be requests on the queue. We will not get
  609. * an interrupt for a request that was running. So we
  610. * just post them all as I/O errors.
  611. */
  612. DBF_EVENT(3, "(%08x): Drive in use vanished!\n",
  613. device->cdev_id);
  614. pr_warning("%s: A tape unit was detached while in "
  615. "use\n", dev_name(&device->cdev->dev));
  616. tape_state_set(device, TS_NOT_OPER);
  617. __tape_discard_requests(device);
  618. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  619. tape_cleanup_device(device);
  620. }
  621. device = dev_get_drvdata(&cdev->dev);
  622. if (device) {
  623. sysfs_remove_group(&cdev->dev.kobj, &tape_attr_group);
  624. dev_set_drvdata(&cdev->dev, NULL);
  625. tape_put_device(device);
  626. }
  627. }
  628. /*
  629. * Allocate a new tape ccw request
  630. */
  631. struct tape_request *
  632. tape_alloc_request(int cplength, int datasize)
  633. {
  634. struct tape_request *request;
  635. BUG_ON(datasize > PAGE_SIZE || (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
  636. DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength, datasize);
  637. request = kzalloc(sizeof(struct tape_request), GFP_KERNEL);
  638. if (request == NULL) {
  639. DBF_EXCEPTION(1, "cqra nomem\n");
  640. return ERR_PTR(-ENOMEM);
  641. }
  642. /* allocate channel program */
  643. if (cplength > 0) {
  644. request->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
  645. GFP_ATOMIC | GFP_DMA);
  646. if (request->cpaddr == NULL) {
  647. DBF_EXCEPTION(1, "cqra nomem\n");
  648. kfree(request);
  649. return ERR_PTR(-ENOMEM);
  650. }
  651. }
  652. /* alloc small kernel buffer */
  653. if (datasize > 0) {
  654. request->cpdata = kzalloc(datasize, GFP_KERNEL | GFP_DMA);
  655. if (request->cpdata == NULL) {
  656. DBF_EXCEPTION(1, "cqra nomem\n");
  657. kfree(request->cpaddr);
  658. kfree(request);
  659. return ERR_PTR(-ENOMEM);
  660. }
  661. }
  662. DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr,
  663. request->cpdata);
  664. return request;
  665. }
  666. /*
  667. * Free tape ccw request
  668. */
  669. void
  670. tape_free_request (struct tape_request * request)
  671. {
  672. DBF_LH(6, "Free request %p\n", request);
  673. if (request->device)
  674. tape_put_device(request->device);
  675. kfree(request->cpdata);
  676. kfree(request->cpaddr);
  677. kfree(request);
  678. }
  679. static int
  680. __tape_start_io(struct tape_device *device, struct tape_request *request)
  681. {
  682. int rc;
  683. rc = ccw_device_start(
  684. device->cdev,
  685. request->cpaddr,
  686. (unsigned long) request,
  687. 0x00,
  688. request->options
  689. );
  690. if (rc == 0) {
  691. request->status = TAPE_REQUEST_IN_IO;
  692. } else if (rc == -EBUSY) {
  693. /* The common I/O subsystem is currently busy. Retry later. */
  694. request->status = TAPE_REQUEST_QUEUED;
  695. schedule_delayed_work(&device->tape_dnr, 0);
  696. rc = 0;
  697. } else {
  698. /* Start failed. Remove request and indicate failure. */
  699. DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc);
  700. }
  701. return rc;
  702. }
  703. static void
  704. __tape_start_next_request(struct tape_device *device)
  705. {
  706. struct list_head *l, *n;
  707. struct tape_request *request;
  708. int rc;
  709. DBF_LH(6, "__tape_start_next_request(%p)\n", device);
  710. /*
  711. * Try to start each request on request queue until one is
  712. * started successful.
  713. */
  714. list_for_each_safe(l, n, &device->req_queue) {
  715. request = list_entry(l, struct tape_request, list);
  716. /*
  717. * Avoid race condition if bottom-half was triggered more than
  718. * once.
  719. */
  720. if (request->status == TAPE_REQUEST_IN_IO)
  721. return;
  722. /*
  723. * Request has already been stopped. We have to wait until
  724. * the request is removed from the queue in the interrupt
  725. * handling.
  726. */
  727. if (request->status == TAPE_REQUEST_DONE)
  728. return;
  729. /*
  730. * We wanted to cancel the request but the common I/O layer
  731. * was busy at that time. This can only happen if this
  732. * function is called by delayed_next_request.
  733. * Otherwise we start the next request on the queue.
  734. */
  735. if (request->status == TAPE_REQUEST_CANCEL) {
  736. rc = __tape_cancel_io(device, request);
  737. } else {
  738. rc = __tape_start_io(device, request);
  739. }
  740. if (rc == 0)
  741. return;
  742. /* Set ending status. */
  743. request->rc = rc;
  744. request->status = TAPE_REQUEST_DONE;
  745. /* Remove from request queue. */
  746. list_del(&request->list);
  747. /* Do callback. */
  748. if (request->callback != NULL)
  749. request->callback(request, request->callback_data);
  750. }
  751. }
  752. static void
  753. tape_delayed_next_request(struct work_struct *work)
  754. {
  755. struct tape_device *device =
  756. container_of(work, struct tape_device, tape_dnr.work);
  757. DBF_LH(6, "tape_delayed_next_request(%p)\n", device);
  758. spin_lock_irq(get_ccwdev_lock(device->cdev));
  759. __tape_start_next_request(device);
  760. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  761. }
  762. static void tape_long_busy_timeout(unsigned long data)
  763. {
  764. struct tape_request *request;
  765. struct tape_device *device;
  766. device = (struct tape_device *) data;
  767. spin_lock_irq(get_ccwdev_lock(device->cdev));
  768. request = list_entry(device->req_queue.next, struct tape_request, list);
  769. BUG_ON(request->status != TAPE_REQUEST_LONG_BUSY);
  770. DBF_LH(6, "%08x: Long busy timeout.\n", device->cdev_id);
  771. __tape_start_next_request(device);
  772. device->lb_timeout.data = 0UL;
  773. tape_put_device(device);
  774. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  775. }
  776. static void
  777. __tape_end_request(
  778. struct tape_device * device,
  779. struct tape_request * request,
  780. int rc)
  781. {
  782. DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device, request, rc);
  783. if (request) {
  784. request->rc = rc;
  785. request->status = TAPE_REQUEST_DONE;
  786. /* Remove from request queue. */
  787. list_del(&request->list);
  788. /* Do callback. */
  789. if (request->callback != NULL)
  790. request->callback(request, request->callback_data);
  791. }
  792. /* Start next request. */
  793. if (!list_empty(&device->req_queue))
  794. __tape_start_next_request(device);
  795. }
  796. /*
  797. * Write sense data to dbf
  798. */
  799. void
  800. tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request,
  801. struct irb *irb)
  802. {
  803. unsigned int *sptr;
  804. const char* op;
  805. if (request != NULL)
  806. op = tape_op_verbose[request->op];
  807. else
  808. op = "---";
  809. DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n",
  810. irb->scsw.cmd.dstat, irb->scsw.cmd.cstat);
  811. DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op);
  812. sptr = (unsigned int *) irb->ecw;
  813. DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]);
  814. DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]);
  815. DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]);
  816. DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]);
  817. }
  818. /*
  819. * I/O helper function. Adds the request to the request queue
  820. * and starts it if the tape is idle. Has to be called with
  821. * the device lock held.
  822. */
  823. static int
  824. __tape_start_request(struct tape_device *device, struct tape_request *request)
  825. {
  826. int rc;
  827. switch (request->op) {
  828. case TO_MSEN:
  829. case TO_ASSIGN:
  830. case TO_UNASSIGN:
  831. case TO_READ_ATTMSG:
  832. case TO_RDC:
  833. if (device->tape_state == TS_INIT)
  834. break;
  835. if (device->tape_state == TS_UNUSED)
  836. break;
  837. default:
  838. if (device->tape_state == TS_BLKUSE)
  839. break;
  840. if (device->tape_state != TS_IN_USE)
  841. return -ENODEV;
  842. }
  843. /* Increase use count of device for the added request. */
  844. request->device = tape_get_device(device);
  845. if (list_empty(&device->req_queue)) {
  846. /* No other requests are on the queue. Start this one. */
  847. rc = __tape_start_io(device, request);
  848. if (rc)
  849. return rc;
  850. DBF_LH(5, "Request %p added for execution.\n", request);
  851. list_add(&request->list, &device->req_queue);
  852. } else {
  853. DBF_LH(5, "Request %p add to queue.\n", request);
  854. request->status = TAPE_REQUEST_QUEUED;
  855. list_add_tail(&request->list, &device->req_queue);
  856. }
  857. return 0;
  858. }
  859. /*
  860. * Add the request to the request queue, try to start it if the
  861. * tape is idle. Return without waiting for end of i/o.
  862. */
  863. int
  864. tape_do_io_async(struct tape_device *device, struct tape_request *request)
  865. {
  866. int rc;
  867. DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request);
  868. spin_lock_irq(get_ccwdev_lock(device->cdev));
  869. /* Add request to request queue and try to start it. */
  870. rc = __tape_start_request(device, request);
  871. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  872. return rc;
  873. }
  874. /*
  875. * tape_do_io/__tape_wake_up
  876. * Add the request to the request queue, try to start it if the
  877. * tape is idle and wait uninterruptible for its completion.
  878. */
  879. static void
  880. __tape_wake_up(struct tape_request *request, void *data)
  881. {
  882. request->callback = NULL;
  883. wake_up((wait_queue_head_t *) data);
  884. }
  885. int
  886. tape_do_io(struct tape_device *device, struct tape_request *request)
  887. {
  888. int rc;
  889. spin_lock_irq(get_ccwdev_lock(device->cdev));
  890. /* Setup callback */
  891. request->callback = __tape_wake_up;
  892. request->callback_data = &device->wait_queue;
  893. /* Add request to request queue and try to start it. */
  894. rc = __tape_start_request(device, request);
  895. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  896. if (rc)
  897. return rc;
  898. /* Request added to the queue. Wait for its completion. */
  899. wait_event(device->wait_queue, (request->callback == NULL));
  900. /* Get rc from request */
  901. return request->rc;
  902. }
  903. /*
  904. * tape_do_io_interruptible/__tape_wake_up_interruptible
  905. * Add the request to the request queue, try to start it if the
  906. * tape is idle and wait uninterruptible for its completion.
  907. */
  908. static void
  909. __tape_wake_up_interruptible(struct tape_request *request, void *data)
  910. {
  911. request->callback = NULL;
  912. wake_up_interruptible((wait_queue_head_t *) data);
  913. }
  914. int
  915. tape_do_io_interruptible(struct tape_device *device,
  916. struct tape_request *request)
  917. {
  918. int rc;
  919. spin_lock_irq(get_ccwdev_lock(device->cdev));
  920. /* Setup callback */
  921. request->callback = __tape_wake_up_interruptible;
  922. request->callback_data = &device->wait_queue;
  923. rc = __tape_start_request(device, request);
  924. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  925. if (rc)
  926. return rc;
  927. /* Request added to the queue. Wait for its completion. */
  928. rc = wait_event_interruptible(device->wait_queue,
  929. (request->callback == NULL));
  930. if (rc != -ERESTARTSYS)
  931. /* Request finished normally. */
  932. return request->rc;
  933. /* Interrupted by a signal. We have to stop the current request. */
  934. spin_lock_irq(get_ccwdev_lock(device->cdev));
  935. rc = __tape_cancel_io(device, request);
  936. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  937. if (rc == 0) {
  938. /* Wait for the interrupt that acknowledges the halt. */
  939. do {
  940. rc = wait_event_interruptible(
  941. device->wait_queue,
  942. (request->callback == NULL)
  943. );
  944. } while (rc == -ERESTARTSYS);
  945. DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id);
  946. rc = -ERESTARTSYS;
  947. }
  948. return rc;
  949. }
  950. /*
  951. * Stop running ccw.
  952. */
  953. int
  954. tape_cancel_io(struct tape_device *device, struct tape_request *request)
  955. {
  956. int rc;
  957. spin_lock_irq(get_ccwdev_lock(device->cdev));
  958. rc = __tape_cancel_io(device, request);
  959. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  960. return rc;
  961. }
  962. /*
  963. * Tape interrupt routine, called from the ccw_device layer
  964. */
  965. static void
  966. __tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
  967. {
  968. struct tape_device *device;
  969. struct tape_request *request;
  970. int rc;
  971. device = dev_get_drvdata(&cdev->dev);
  972. if (device == NULL) {
  973. return;
  974. }
  975. request = (struct tape_request *) intparm;
  976. DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request);
  977. /* On special conditions irb is an error pointer */
  978. if (IS_ERR(irb)) {
  979. /* FIXME: What to do with the request? */
  980. switch (PTR_ERR(irb)) {
  981. case -ETIMEDOUT:
  982. DBF_LH(1, "(%08x): Request timed out\n",
  983. device->cdev_id);
  984. case -EIO:
  985. __tape_end_request(device, request, -EIO);
  986. break;
  987. default:
  988. DBF_LH(1, "(%08x): Unexpected i/o error %li\n",
  989. device->cdev_id, PTR_ERR(irb));
  990. }
  991. return;
  992. }
  993. /*
  994. * If the condition code is not zero and the start function bit is
  995. * still set, this is an deferred error and the last start I/O did
  996. * not succeed. At this point the condition that caused the deferred
  997. * error might still apply. So we just schedule the request to be
  998. * started later.
  999. */
  1000. if (irb->scsw.cmd.cc != 0 &&
  1001. (irb->scsw.cmd.fctl & SCSW_FCTL_START_FUNC) &&
  1002. (request->status == TAPE_REQUEST_IN_IO)) {
  1003. DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n",
  1004. device->cdev_id, irb->scsw.cmd.cc, irb->scsw.cmd.fctl);
  1005. request->status = TAPE_REQUEST_QUEUED;
  1006. schedule_delayed_work(&device->tape_dnr, HZ);
  1007. return;
  1008. }
  1009. /* May be an unsolicited irq */
  1010. if(request != NULL)
  1011. request->rescnt = irb->scsw.cmd.count;
  1012. else if ((irb->scsw.cmd.dstat == 0x85 || irb->scsw.cmd.dstat == 0x80) &&
  1013. !list_empty(&device->req_queue)) {
  1014. /* Not Ready to Ready after long busy ? */
  1015. struct tape_request *req;
  1016. req = list_entry(device->req_queue.next,
  1017. struct tape_request, list);
  1018. if (req->status == TAPE_REQUEST_LONG_BUSY) {
  1019. DBF_EVENT(3, "(%08x): del timer\n", device->cdev_id);
  1020. if (del_timer(&device->lb_timeout)) {
  1021. device->lb_timeout.data = 0UL;
  1022. tape_put_device(device);
  1023. __tape_start_next_request(device);
  1024. }
  1025. return;
  1026. }
  1027. }
  1028. if (irb->scsw.cmd.dstat != 0x0c) {
  1029. /* Set the 'ONLINE' flag depending on sense byte 1 */
  1030. if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE)
  1031. device->tape_generic_status |= GMT_ONLINE(~0);
  1032. else
  1033. device->tape_generic_status &= ~GMT_ONLINE(~0);
  1034. /*
  1035. * Any request that does not come back with channel end
  1036. * and device end is unusual. Log the sense data.
  1037. */
  1038. DBF_EVENT(3,"-- Tape Interrupthandler --\n");
  1039. tape_dump_sense_dbf(device, request, irb);
  1040. } else {
  1041. /* Upon normal completion the device _is_ online */
  1042. device->tape_generic_status |= GMT_ONLINE(~0);
  1043. }
  1044. if (device->tape_state == TS_NOT_OPER) {
  1045. DBF_EVENT(6, "tape:device is not operational\n");
  1046. return;
  1047. }
  1048. /*
  1049. * Request that were canceled still come back with an interrupt.
  1050. * To detect these request the state will be set to TAPE_REQUEST_DONE.
  1051. */
  1052. if(request != NULL && request->status == TAPE_REQUEST_DONE) {
  1053. __tape_end_request(device, request, -EIO);
  1054. return;
  1055. }
  1056. rc = device->discipline->irq(device, request, irb);
  1057. /*
  1058. * rc < 0 : request finished unsuccessfully.
  1059. * rc == TAPE_IO_SUCCESS: request finished successfully.
  1060. * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
  1061. * rc == TAPE_IO_RETRY: request finished but needs another go.
  1062. * rc == TAPE_IO_STOP: request needs to get terminated.
  1063. */
  1064. switch (rc) {
  1065. case TAPE_IO_SUCCESS:
  1066. /* Upon normal completion the device _is_ online */
  1067. device->tape_generic_status |= GMT_ONLINE(~0);
  1068. __tape_end_request(device, request, rc);
  1069. break;
  1070. case TAPE_IO_PENDING:
  1071. break;
  1072. case TAPE_IO_LONG_BUSY:
  1073. device->lb_timeout.data =
  1074. (unsigned long) tape_get_device(device);
  1075. device->lb_timeout.expires = jiffies +
  1076. LONG_BUSY_TIMEOUT * HZ;
  1077. DBF_EVENT(3, "(%08x): add timer\n", device->cdev_id);
  1078. add_timer(&device->lb_timeout);
  1079. request->status = TAPE_REQUEST_LONG_BUSY;
  1080. break;
  1081. case TAPE_IO_RETRY:
  1082. rc = __tape_start_io(device, request);
  1083. if (rc)
  1084. __tape_end_request(device, request, rc);
  1085. break;
  1086. case TAPE_IO_STOP:
  1087. rc = __tape_cancel_io(device, request);
  1088. if (rc)
  1089. __tape_end_request(device, request, rc);
  1090. break;
  1091. default:
  1092. if (rc > 0) {
  1093. DBF_EVENT(6, "xunknownrc\n");
  1094. __tape_end_request(device, request, -EIO);
  1095. } else {
  1096. __tape_end_request(device, request, rc);
  1097. }
  1098. break;
  1099. }
  1100. }
  1101. /*
  1102. * Tape device open function used by tape_char frontend.
  1103. */
  1104. int
  1105. tape_open(struct tape_device *device)
  1106. {
  1107. int rc;
  1108. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1109. if (device->tape_state == TS_NOT_OPER) {
  1110. DBF_EVENT(6, "TAPE:nodev\n");
  1111. rc = -ENODEV;
  1112. } else if (device->tape_state == TS_IN_USE) {
  1113. DBF_EVENT(6, "TAPE:dbusy\n");
  1114. rc = -EBUSY;
  1115. } else if (device->tape_state == TS_BLKUSE) {
  1116. DBF_EVENT(6, "TAPE:dbusy\n");
  1117. rc = -EBUSY;
  1118. } else if (device->discipline != NULL &&
  1119. !try_module_get(device->discipline->owner)) {
  1120. DBF_EVENT(6, "TAPE:nodisc\n");
  1121. rc = -ENODEV;
  1122. } else {
  1123. tape_state_set(device, TS_IN_USE);
  1124. rc = 0;
  1125. }
  1126. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1127. return rc;
  1128. }
  1129. /*
  1130. * Tape device release function used by tape_char frontend.
  1131. */
  1132. int
  1133. tape_release(struct tape_device *device)
  1134. {
  1135. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1136. if (device->tape_state == TS_IN_USE)
  1137. tape_state_set(device, TS_UNUSED);
  1138. module_put(device->discipline->owner);
  1139. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1140. return 0;
  1141. }
  1142. /*
  1143. * Execute a magnetic tape command a number of times.
  1144. */
  1145. int
  1146. tape_mtop(struct tape_device *device, int mt_op, int mt_count)
  1147. {
  1148. tape_mtop_fn fn;
  1149. int rc;
  1150. DBF_EVENT(6, "TAPE:mtio\n");
  1151. DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op);
  1152. DBF_EVENT(6, "TAPE:arg: %x\n", mt_count);
  1153. if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS)
  1154. return -EINVAL;
  1155. fn = device->discipline->mtop_array[mt_op];
  1156. if (fn == NULL)
  1157. return -EINVAL;
  1158. /* We assume that the backends can handle count up to 500. */
  1159. if (mt_op == MTBSR || mt_op == MTFSR || mt_op == MTFSF ||
  1160. mt_op == MTBSF || mt_op == MTFSFM || mt_op == MTBSFM) {
  1161. rc = 0;
  1162. for (; mt_count > 500; mt_count -= 500)
  1163. if ((rc = fn(device, 500)) != 0)
  1164. break;
  1165. if (rc == 0)
  1166. rc = fn(device, mt_count);
  1167. } else
  1168. rc = fn(device, mt_count);
  1169. return rc;
  1170. }
  1171. /*
  1172. * Tape init function.
  1173. */
  1174. static int
  1175. tape_init (void)
  1176. {
  1177. TAPE_DBF_AREA = debug_register ( "tape", 2, 2, 4*sizeof(long));
  1178. debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
  1179. #ifdef DBF_LIKE_HELL
  1180. debug_set_level(TAPE_DBF_AREA, 6);
  1181. #endif
  1182. DBF_EVENT(3, "tape init\n");
  1183. tape_proc_init();
  1184. tapechar_init ();
  1185. return 0;
  1186. }
  1187. /*
  1188. * Tape exit function.
  1189. */
  1190. static void
  1191. tape_exit(void)
  1192. {
  1193. DBF_EVENT(6, "tape exit\n");
  1194. /* Get rid of the frontends */
  1195. tapechar_exit();
  1196. tape_proc_cleanup();
  1197. debug_unregister (TAPE_DBF_AREA);
  1198. }
  1199. MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
  1200. "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
  1201. MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
  1202. MODULE_LICENSE("GPL");
  1203. module_init(tape_init);
  1204. module_exit(tape_exit);
  1205. EXPORT_SYMBOL(tape_generic_remove);
  1206. EXPORT_SYMBOL(tape_generic_probe);
  1207. EXPORT_SYMBOL(tape_generic_online);
  1208. EXPORT_SYMBOL(tape_generic_offline);
  1209. EXPORT_SYMBOL(tape_generic_pm_suspend);
  1210. EXPORT_SYMBOL(tape_put_device);
  1211. EXPORT_SYMBOL(tape_get_device);
  1212. EXPORT_SYMBOL(tape_state_verbose);
  1213. EXPORT_SYMBOL(tape_op_verbose);
  1214. EXPORT_SYMBOL(tape_state_set);
  1215. EXPORT_SYMBOL(tape_med_state_set);
  1216. EXPORT_SYMBOL(tape_alloc_request);
  1217. EXPORT_SYMBOL(tape_free_request);
  1218. EXPORT_SYMBOL(tape_dump_sense_dbf);
  1219. EXPORT_SYMBOL(tape_do_io);
  1220. EXPORT_SYMBOL(tape_do_io_async);
  1221. EXPORT_SYMBOL(tape_do_io_interruptible);
  1222. EXPORT_SYMBOL(tape_cancel_io);
  1223. EXPORT_SYMBOL(tape_mtop);