tape_core.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  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(struct timer_list *t);
  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 const 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. timer_setup(&device->lb_timeout, tape_long_busy_timeout, 0);
  333. /* Let the discipline have a go at the device. */
  334. device->discipline = discipline;
  335. if (!try_module_get(discipline->owner)) {
  336. return -EINVAL;
  337. }
  338. rc = discipline->setup_device(device);
  339. if (rc)
  340. goto out;
  341. rc = tape_assign_minor(device);
  342. if (rc)
  343. goto out_discipline;
  344. rc = tapechar_setup_device(device);
  345. if (rc)
  346. goto out_minor;
  347. tape_state_set(device, TS_UNUSED);
  348. DBF_LH(3, "(%08x): Drive set online\n", device->cdev_id);
  349. return 0;
  350. out_minor:
  351. tape_remove_minor(device);
  352. out_discipline:
  353. device->discipline->cleanup_device(device);
  354. device->discipline = NULL;
  355. out:
  356. module_put(discipline->owner);
  357. return rc;
  358. }
  359. static void
  360. tape_cleanup_device(struct tape_device *device)
  361. {
  362. tapechar_cleanup_device(device);
  363. device->discipline->cleanup_device(device);
  364. module_put(device->discipline->owner);
  365. tape_remove_minor(device);
  366. tape_med_state_set(device, MS_UNKNOWN);
  367. }
  368. /*
  369. * Suspend device.
  370. *
  371. * Called by the common I/O layer if the drive should be suspended on user
  372. * request. We refuse to suspend if the device is loaded or in use for the
  373. * following reason:
  374. * While the Linux guest is suspended, it might be logged off which causes
  375. * devices to be detached. Tape devices are automatically rewound and unloaded
  376. * during DETACH processing (unless the tape device was attached with the
  377. * NOASSIGN or MULTIUSER option). After rewind/unload, there is no way to
  378. * resume the original state of the tape device, since we would need to
  379. * manually re-load the cartridge which was active at suspend time.
  380. */
  381. int tape_generic_pm_suspend(struct ccw_device *cdev)
  382. {
  383. struct tape_device *device;
  384. device = dev_get_drvdata(&cdev->dev);
  385. if (!device) {
  386. return -ENODEV;
  387. }
  388. DBF_LH(3, "(%08x): tape_generic_pm_suspend(%p)\n",
  389. device->cdev_id, device);
  390. if (device->medium_state != MS_UNLOADED) {
  391. pr_err("A cartridge is loaded in tape device %s, "
  392. "refusing to suspend\n", dev_name(&cdev->dev));
  393. return -EBUSY;
  394. }
  395. spin_lock_irq(get_ccwdev_lock(device->cdev));
  396. switch (device->tape_state) {
  397. case TS_INIT:
  398. case TS_NOT_OPER:
  399. case TS_UNUSED:
  400. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  401. break;
  402. default:
  403. pr_err("Tape device %s is busy, refusing to "
  404. "suspend\n", dev_name(&cdev->dev));
  405. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  406. return -EBUSY;
  407. }
  408. DBF_LH(3, "(%08x): Drive suspended.\n", device->cdev_id);
  409. return 0;
  410. }
  411. /*
  412. * Set device offline.
  413. *
  414. * Called by the common I/O layer if the drive should set offline on user
  415. * request. We may prevent this by returning an error.
  416. * Manual offline is only allowed while the drive is not in use.
  417. */
  418. int
  419. tape_generic_offline(struct ccw_device *cdev)
  420. {
  421. struct tape_device *device;
  422. device = dev_get_drvdata(&cdev->dev);
  423. if (!device) {
  424. return -ENODEV;
  425. }
  426. DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
  427. device->cdev_id, device);
  428. spin_lock_irq(get_ccwdev_lock(device->cdev));
  429. switch (device->tape_state) {
  430. case TS_INIT:
  431. case TS_NOT_OPER:
  432. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  433. break;
  434. case TS_UNUSED:
  435. tape_state_set(device, TS_INIT);
  436. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  437. tape_cleanup_device(device);
  438. break;
  439. default:
  440. DBF_EVENT(3, "(%08x): Set offline failed "
  441. "- drive in use.\n",
  442. device->cdev_id);
  443. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  444. return -EBUSY;
  445. }
  446. DBF_LH(3, "(%08x): Drive set offline.\n", device->cdev_id);
  447. return 0;
  448. }
  449. /*
  450. * Allocate memory for a new device structure.
  451. */
  452. static struct tape_device *
  453. tape_alloc_device(void)
  454. {
  455. struct tape_device *device;
  456. device = kzalloc(sizeof(struct tape_device), GFP_KERNEL);
  457. if (device == NULL) {
  458. DBF_EXCEPTION(2, "ti:no mem\n");
  459. return ERR_PTR(-ENOMEM);
  460. }
  461. device->modeset_byte = kmalloc(1, GFP_KERNEL | GFP_DMA);
  462. if (device->modeset_byte == NULL) {
  463. DBF_EXCEPTION(2, "ti:no mem\n");
  464. kfree(device);
  465. return ERR_PTR(-ENOMEM);
  466. }
  467. mutex_init(&device->mutex);
  468. INIT_LIST_HEAD(&device->req_queue);
  469. INIT_LIST_HEAD(&device->node);
  470. init_waitqueue_head(&device->state_change_wq);
  471. init_waitqueue_head(&device->wait_queue);
  472. device->tape_state = TS_INIT;
  473. device->medium_state = MS_UNKNOWN;
  474. *device->modeset_byte = 0;
  475. device->first_minor = -1;
  476. atomic_set(&device->ref_count, 1);
  477. INIT_DELAYED_WORK(&device->tape_dnr, tape_delayed_next_request);
  478. return device;
  479. }
  480. /*
  481. * Get a reference to an existing device structure. This will automatically
  482. * increment the reference count.
  483. */
  484. struct tape_device *
  485. tape_get_device(struct tape_device *device)
  486. {
  487. int count;
  488. count = atomic_inc_return(&device->ref_count);
  489. DBF_EVENT(4, "tape_get_device(%p) = %i\n", device, count);
  490. return device;
  491. }
  492. /*
  493. * Decrease the reference counter of a devices structure. If the
  494. * reference counter reaches zero free the device structure.
  495. * The function returns a NULL pointer to be used by the caller
  496. * for clearing reference pointers.
  497. */
  498. void
  499. tape_put_device(struct tape_device *device)
  500. {
  501. int count;
  502. count = atomic_dec_return(&device->ref_count);
  503. DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device, count);
  504. BUG_ON(count < 0);
  505. if (count == 0) {
  506. kfree(device->modeset_byte);
  507. kfree(device);
  508. }
  509. }
  510. /*
  511. * Find tape device by a device index.
  512. */
  513. struct tape_device *
  514. tape_find_device(int devindex)
  515. {
  516. struct tape_device *device, *tmp;
  517. device = ERR_PTR(-ENODEV);
  518. read_lock(&tape_device_lock);
  519. list_for_each_entry(tmp, &tape_device_list, node) {
  520. if (tmp->first_minor / TAPE_MINORS_PER_DEV == devindex) {
  521. device = tape_get_device(tmp);
  522. break;
  523. }
  524. }
  525. read_unlock(&tape_device_lock);
  526. return device;
  527. }
  528. /*
  529. * Driverfs tape probe function.
  530. */
  531. int
  532. tape_generic_probe(struct ccw_device *cdev)
  533. {
  534. struct tape_device *device;
  535. int ret;
  536. struct ccw_dev_id dev_id;
  537. device = tape_alloc_device();
  538. if (IS_ERR(device))
  539. return -ENODEV;
  540. ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP |
  541. CCWDEV_DO_MULTIPATH);
  542. ret = sysfs_create_group(&cdev->dev.kobj, &tape_attr_group);
  543. if (ret) {
  544. tape_put_device(device);
  545. return ret;
  546. }
  547. dev_set_drvdata(&cdev->dev, device);
  548. cdev->handler = __tape_do_irq;
  549. device->cdev = cdev;
  550. ccw_device_get_id(cdev, &dev_id);
  551. device->cdev_id = devid_to_int(&dev_id);
  552. return ret;
  553. }
  554. static void
  555. __tape_discard_requests(struct tape_device *device)
  556. {
  557. struct tape_request * request;
  558. struct list_head * l, *n;
  559. list_for_each_safe(l, n, &device->req_queue) {
  560. request = list_entry(l, struct tape_request, list);
  561. if (request->status == TAPE_REQUEST_IN_IO)
  562. request->status = TAPE_REQUEST_DONE;
  563. list_del(&request->list);
  564. /* Decrease ref_count for removed request. */
  565. request->device = NULL;
  566. tape_put_device(device);
  567. request->rc = -EIO;
  568. if (request->callback != NULL)
  569. request->callback(request, request->callback_data);
  570. }
  571. }
  572. /*
  573. * Driverfs tape remove function.
  574. *
  575. * This function is called whenever the common I/O layer detects the device
  576. * gone. This can happen at any time and we cannot refuse.
  577. */
  578. void
  579. tape_generic_remove(struct ccw_device *cdev)
  580. {
  581. struct tape_device * device;
  582. device = dev_get_drvdata(&cdev->dev);
  583. if (!device) {
  584. return;
  585. }
  586. DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device->cdev_id, cdev);
  587. spin_lock_irq(get_ccwdev_lock(device->cdev));
  588. switch (device->tape_state) {
  589. case TS_INIT:
  590. tape_state_set(device, TS_NOT_OPER);
  591. case TS_NOT_OPER:
  592. /*
  593. * Nothing to do.
  594. */
  595. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  596. break;
  597. case TS_UNUSED:
  598. /*
  599. * Need only to release the device.
  600. */
  601. tape_state_set(device, TS_NOT_OPER);
  602. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  603. tape_cleanup_device(device);
  604. break;
  605. default:
  606. /*
  607. * There may be requests on the queue. We will not get
  608. * an interrupt for a request that was running. So we
  609. * just post them all as I/O errors.
  610. */
  611. DBF_EVENT(3, "(%08x): Drive in use vanished!\n",
  612. device->cdev_id);
  613. pr_warn("%s: A tape unit was detached while in use\n",
  614. dev_name(&device->cdev->dev));
  615. tape_state_set(device, TS_NOT_OPER);
  616. __tape_discard_requests(device);
  617. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  618. tape_cleanup_device(device);
  619. }
  620. device = dev_get_drvdata(&cdev->dev);
  621. if (device) {
  622. sysfs_remove_group(&cdev->dev.kobj, &tape_attr_group);
  623. dev_set_drvdata(&cdev->dev, NULL);
  624. tape_put_device(device);
  625. }
  626. }
  627. /*
  628. * Allocate a new tape ccw request
  629. */
  630. struct tape_request *
  631. tape_alloc_request(int cplength, int datasize)
  632. {
  633. struct tape_request *request;
  634. BUG_ON(datasize > PAGE_SIZE || (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
  635. DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength, datasize);
  636. request = kzalloc(sizeof(struct tape_request), GFP_KERNEL);
  637. if (request == NULL) {
  638. DBF_EXCEPTION(1, "cqra nomem\n");
  639. return ERR_PTR(-ENOMEM);
  640. }
  641. /* allocate channel program */
  642. if (cplength > 0) {
  643. request->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
  644. GFP_ATOMIC | GFP_DMA);
  645. if (request->cpaddr == NULL) {
  646. DBF_EXCEPTION(1, "cqra nomem\n");
  647. kfree(request);
  648. return ERR_PTR(-ENOMEM);
  649. }
  650. }
  651. /* alloc small kernel buffer */
  652. if (datasize > 0) {
  653. request->cpdata = kzalloc(datasize, GFP_KERNEL | GFP_DMA);
  654. if (request->cpdata == NULL) {
  655. DBF_EXCEPTION(1, "cqra nomem\n");
  656. kfree(request->cpaddr);
  657. kfree(request);
  658. return ERR_PTR(-ENOMEM);
  659. }
  660. }
  661. DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr,
  662. request->cpdata);
  663. return request;
  664. }
  665. /*
  666. * Free tape ccw request
  667. */
  668. void
  669. tape_free_request (struct tape_request * request)
  670. {
  671. DBF_LH(6, "Free request %p\n", request);
  672. if (request->device)
  673. tape_put_device(request->device);
  674. kfree(request->cpdata);
  675. kfree(request->cpaddr);
  676. kfree(request);
  677. }
  678. static int
  679. __tape_start_io(struct tape_device *device, struct tape_request *request)
  680. {
  681. int rc;
  682. rc = ccw_device_start(
  683. device->cdev,
  684. request->cpaddr,
  685. (unsigned long) request,
  686. 0x00,
  687. request->options
  688. );
  689. if (rc == 0) {
  690. request->status = TAPE_REQUEST_IN_IO;
  691. } else if (rc == -EBUSY) {
  692. /* The common I/O subsystem is currently busy. Retry later. */
  693. request->status = TAPE_REQUEST_QUEUED;
  694. schedule_delayed_work(&device->tape_dnr, 0);
  695. rc = 0;
  696. } else {
  697. /* Start failed. Remove request and indicate failure. */
  698. DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc);
  699. }
  700. return rc;
  701. }
  702. static void
  703. __tape_start_next_request(struct tape_device *device)
  704. {
  705. struct list_head *l, *n;
  706. struct tape_request *request;
  707. int rc;
  708. DBF_LH(6, "__tape_start_next_request(%p)\n", device);
  709. /*
  710. * Try to start each request on request queue until one is
  711. * started successful.
  712. */
  713. list_for_each_safe(l, n, &device->req_queue) {
  714. request = list_entry(l, struct tape_request, list);
  715. /*
  716. * Avoid race condition if bottom-half was triggered more than
  717. * once.
  718. */
  719. if (request->status == TAPE_REQUEST_IN_IO)
  720. return;
  721. /*
  722. * Request has already been stopped. We have to wait until
  723. * the request is removed from the queue in the interrupt
  724. * handling.
  725. */
  726. if (request->status == TAPE_REQUEST_DONE)
  727. return;
  728. /*
  729. * We wanted to cancel the request but the common I/O layer
  730. * was busy at that time. This can only happen if this
  731. * function is called by delayed_next_request.
  732. * Otherwise we start the next request on the queue.
  733. */
  734. if (request->status == TAPE_REQUEST_CANCEL) {
  735. rc = __tape_cancel_io(device, request);
  736. } else {
  737. rc = __tape_start_io(device, request);
  738. }
  739. if (rc == 0)
  740. return;
  741. /* Set ending status. */
  742. request->rc = rc;
  743. request->status = TAPE_REQUEST_DONE;
  744. /* Remove from request queue. */
  745. list_del(&request->list);
  746. /* Do callback. */
  747. if (request->callback != NULL)
  748. request->callback(request, request->callback_data);
  749. }
  750. }
  751. static void
  752. tape_delayed_next_request(struct work_struct *work)
  753. {
  754. struct tape_device *device =
  755. container_of(work, struct tape_device, tape_dnr.work);
  756. DBF_LH(6, "tape_delayed_next_request(%p)\n", device);
  757. spin_lock_irq(get_ccwdev_lock(device->cdev));
  758. __tape_start_next_request(device);
  759. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  760. }
  761. static void tape_long_busy_timeout(struct timer_list *t)
  762. {
  763. struct tape_device *device = from_timer(device, t, lb_timeout);
  764. struct tape_request *request;
  765. spin_lock_irq(get_ccwdev_lock(device->cdev));
  766. request = list_entry(device->req_queue.next, struct tape_request, list);
  767. BUG_ON(request->status != TAPE_REQUEST_LONG_BUSY);
  768. DBF_LH(6, "%08x: Long busy timeout.\n", device->cdev_id);
  769. __tape_start_next_request(device);
  770. tape_put_device(device);
  771. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  772. }
  773. static void
  774. __tape_end_request(
  775. struct tape_device * device,
  776. struct tape_request * request,
  777. int rc)
  778. {
  779. DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device, request, rc);
  780. if (request) {
  781. request->rc = rc;
  782. request->status = TAPE_REQUEST_DONE;
  783. /* Remove from request queue. */
  784. list_del(&request->list);
  785. /* Do callback. */
  786. if (request->callback != NULL)
  787. request->callback(request, request->callback_data);
  788. }
  789. /* Start next request. */
  790. if (!list_empty(&device->req_queue))
  791. __tape_start_next_request(device);
  792. }
  793. /*
  794. * Write sense data to dbf
  795. */
  796. void
  797. tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request,
  798. struct irb *irb)
  799. {
  800. unsigned int *sptr;
  801. const char* op;
  802. if (request != NULL)
  803. op = tape_op_verbose[request->op];
  804. else
  805. op = "---";
  806. DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n",
  807. irb->scsw.cmd.dstat, irb->scsw.cmd.cstat);
  808. DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op);
  809. sptr = (unsigned int *) irb->ecw;
  810. DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]);
  811. DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]);
  812. DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]);
  813. DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]);
  814. }
  815. /*
  816. * I/O helper function. Adds the request to the request queue
  817. * and starts it if the tape is idle. Has to be called with
  818. * the device lock held.
  819. */
  820. static int
  821. __tape_start_request(struct tape_device *device, struct tape_request *request)
  822. {
  823. int rc;
  824. switch (request->op) {
  825. case TO_MSEN:
  826. case TO_ASSIGN:
  827. case TO_UNASSIGN:
  828. case TO_READ_ATTMSG:
  829. case TO_RDC:
  830. if (device->tape_state == TS_INIT)
  831. break;
  832. if (device->tape_state == TS_UNUSED)
  833. break;
  834. default:
  835. if (device->tape_state == TS_BLKUSE)
  836. break;
  837. if (device->tape_state != TS_IN_USE)
  838. return -ENODEV;
  839. }
  840. /* Increase use count of device for the added request. */
  841. request->device = tape_get_device(device);
  842. if (list_empty(&device->req_queue)) {
  843. /* No other requests are on the queue. Start this one. */
  844. rc = __tape_start_io(device, request);
  845. if (rc)
  846. return rc;
  847. DBF_LH(5, "Request %p added for execution.\n", request);
  848. list_add(&request->list, &device->req_queue);
  849. } else {
  850. DBF_LH(5, "Request %p add to queue.\n", request);
  851. request->status = TAPE_REQUEST_QUEUED;
  852. list_add_tail(&request->list, &device->req_queue);
  853. }
  854. return 0;
  855. }
  856. /*
  857. * Add the request to the request queue, try to start it if the
  858. * tape is idle. Return without waiting for end of i/o.
  859. */
  860. int
  861. tape_do_io_async(struct tape_device *device, struct tape_request *request)
  862. {
  863. int rc;
  864. DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request);
  865. spin_lock_irq(get_ccwdev_lock(device->cdev));
  866. /* Add request to request queue and try to start it. */
  867. rc = __tape_start_request(device, request);
  868. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  869. return rc;
  870. }
  871. /*
  872. * tape_do_io/__tape_wake_up
  873. * Add the request to the request queue, try to start it if the
  874. * tape is idle and wait uninterruptible for its completion.
  875. */
  876. static void
  877. __tape_wake_up(struct tape_request *request, void *data)
  878. {
  879. request->callback = NULL;
  880. wake_up((wait_queue_head_t *) data);
  881. }
  882. int
  883. tape_do_io(struct tape_device *device, struct tape_request *request)
  884. {
  885. int rc;
  886. spin_lock_irq(get_ccwdev_lock(device->cdev));
  887. /* Setup callback */
  888. request->callback = __tape_wake_up;
  889. request->callback_data = &device->wait_queue;
  890. /* Add request to request queue and try to start it. */
  891. rc = __tape_start_request(device, request);
  892. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  893. if (rc)
  894. return rc;
  895. /* Request added to the queue. Wait for its completion. */
  896. wait_event(device->wait_queue, (request->callback == NULL));
  897. /* Get rc from request */
  898. return request->rc;
  899. }
  900. /*
  901. * tape_do_io_interruptible/__tape_wake_up_interruptible
  902. * Add the request to the request queue, try to start it if the
  903. * tape is idle and wait uninterruptible for its completion.
  904. */
  905. static void
  906. __tape_wake_up_interruptible(struct tape_request *request, void *data)
  907. {
  908. request->callback = NULL;
  909. wake_up_interruptible((wait_queue_head_t *) data);
  910. }
  911. int
  912. tape_do_io_interruptible(struct tape_device *device,
  913. struct tape_request *request)
  914. {
  915. int rc;
  916. spin_lock_irq(get_ccwdev_lock(device->cdev));
  917. /* Setup callback */
  918. request->callback = __tape_wake_up_interruptible;
  919. request->callback_data = &device->wait_queue;
  920. rc = __tape_start_request(device, request);
  921. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  922. if (rc)
  923. return rc;
  924. /* Request added to the queue. Wait for its completion. */
  925. rc = wait_event_interruptible(device->wait_queue,
  926. (request->callback == NULL));
  927. if (rc != -ERESTARTSYS)
  928. /* Request finished normally. */
  929. return request->rc;
  930. /* Interrupted by a signal. We have to stop the current request. */
  931. spin_lock_irq(get_ccwdev_lock(device->cdev));
  932. rc = __tape_cancel_io(device, request);
  933. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  934. if (rc == 0) {
  935. /* Wait for the interrupt that acknowledges the halt. */
  936. do {
  937. rc = wait_event_interruptible(
  938. device->wait_queue,
  939. (request->callback == NULL)
  940. );
  941. } while (rc == -ERESTARTSYS);
  942. DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id);
  943. rc = -ERESTARTSYS;
  944. }
  945. return rc;
  946. }
  947. /*
  948. * Stop running ccw.
  949. */
  950. int
  951. tape_cancel_io(struct tape_device *device, struct tape_request *request)
  952. {
  953. int rc;
  954. spin_lock_irq(get_ccwdev_lock(device->cdev));
  955. rc = __tape_cancel_io(device, request);
  956. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  957. return rc;
  958. }
  959. /*
  960. * Tape interrupt routine, called from the ccw_device layer
  961. */
  962. static void
  963. __tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
  964. {
  965. struct tape_device *device;
  966. struct tape_request *request;
  967. int rc;
  968. device = dev_get_drvdata(&cdev->dev);
  969. if (device == NULL) {
  970. return;
  971. }
  972. request = (struct tape_request *) intparm;
  973. DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request);
  974. /* On special conditions irb is an error pointer */
  975. if (IS_ERR(irb)) {
  976. /* FIXME: What to do with the request? */
  977. switch (PTR_ERR(irb)) {
  978. case -ETIMEDOUT:
  979. DBF_LH(1, "(%08x): Request timed out\n",
  980. device->cdev_id);
  981. case -EIO:
  982. __tape_end_request(device, request, -EIO);
  983. break;
  984. default:
  985. DBF_LH(1, "(%08x): Unexpected i/o error %li\n",
  986. device->cdev_id, PTR_ERR(irb));
  987. }
  988. return;
  989. }
  990. /*
  991. * If the condition code is not zero and the start function bit is
  992. * still set, this is an deferred error and the last start I/O did
  993. * not succeed. At this point the condition that caused the deferred
  994. * error might still apply. So we just schedule the request to be
  995. * started later.
  996. */
  997. if (irb->scsw.cmd.cc != 0 &&
  998. (irb->scsw.cmd.fctl & SCSW_FCTL_START_FUNC) &&
  999. (request->status == TAPE_REQUEST_IN_IO)) {
  1000. DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n",
  1001. device->cdev_id, irb->scsw.cmd.cc, irb->scsw.cmd.fctl);
  1002. request->status = TAPE_REQUEST_QUEUED;
  1003. schedule_delayed_work(&device->tape_dnr, HZ);
  1004. return;
  1005. }
  1006. /* May be an unsolicited irq */
  1007. if(request != NULL)
  1008. request->rescnt = irb->scsw.cmd.count;
  1009. else if ((irb->scsw.cmd.dstat == 0x85 || irb->scsw.cmd.dstat == 0x80) &&
  1010. !list_empty(&device->req_queue)) {
  1011. /* Not Ready to Ready after long busy ? */
  1012. struct tape_request *req;
  1013. req = list_entry(device->req_queue.next,
  1014. struct tape_request, list);
  1015. if (req->status == TAPE_REQUEST_LONG_BUSY) {
  1016. DBF_EVENT(3, "(%08x): del timer\n", device->cdev_id);
  1017. if (del_timer(&device->lb_timeout)) {
  1018. tape_put_device(device);
  1019. __tape_start_next_request(device);
  1020. }
  1021. return;
  1022. }
  1023. }
  1024. if (irb->scsw.cmd.dstat != 0x0c) {
  1025. /* Set the 'ONLINE' flag depending on sense byte 1 */
  1026. if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE)
  1027. device->tape_generic_status |= GMT_ONLINE(~0);
  1028. else
  1029. device->tape_generic_status &= ~GMT_ONLINE(~0);
  1030. /*
  1031. * Any request that does not come back with channel end
  1032. * and device end is unusual. Log the sense data.
  1033. */
  1034. DBF_EVENT(3,"-- Tape Interrupthandler --\n");
  1035. tape_dump_sense_dbf(device, request, irb);
  1036. } else {
  1037. /* Upon normal completion the device _is_ online */
  1038. device->tape_generic_status |= GMT_ONLINE(~0);
  1039. }
  1040. if (device->tape_state == TS_NOT_OPER) {
  1041. DBF_EVENT(6, "tape:device is not operational\n");
  1042. return;
  1043. }
  1044. /*
  1045. * Request that were canceled still come back with an interrupt.
  1046. * To detect these request the state will be set to TAPE_REQUEST_DONE.
  1047. */
  1048. if(request != NULL && request->status == TAPE_REQUEST_DONE) {
  1049. __tape_end_request(device, request, -EIO);
  1050. return;
  1051. }
  1052. rc = device->discipline->irq(device, request, irb);
  1053. /*
  1054. * rc < 0 : request finished unsuccessfully.
  1055. * rc == TAPE_IO_SUCCESS: request finished successfully.
  1056. * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
  1057. * rc == TAPE_IO_RETRY: request finished but needs another go.
  1058. * rc == TAPE_IO_STOP: request needs to get terminated.
  1059. */
  1060. switch (rc) {
  1061. case TAPE_IO_SUCCESS:
  1062. /* Upon normal completion the device _is_ online */
  1063. device->tape_generic_status |= GMT_ONLINE(~0);
  1064. __tape_end_request(device, request, rc);
  1065. break;
  1066. case TAPE_IO_PENDING:
  1067. break;
  1068. case TAPE_IO_LONG_BUSY:
  1069. device->lb_timeout.expires = jiffies +
  1070. LONG_BUSY_TIMEOUT * HZ;
  1071. DBF_EVENT(3, "(%08x): add timer\n", device->cdev_id);
  1072. add_timer(&device->lb_timeout);
  1073. request->status = TAPE_REQUEST_LONG_BUSY;
  1074. break;
  1075. case TAPE_IO_RETRY:
  1076. rc = __tape_start_io(device, request);
  1077. if (rc)
  1078. __tape_end_request(device, request, rc);
  1079. break;
  1080. case TAPE_IO_STOP:
  1081. rc = __tape_cancel_io(device, request);
  1082. if (rc)
  1083. __tape_end_request(device, request, rc);
  1084. break;
  1085. default:
  1086. if (rc > 0) {
  1087. DBF_EVENT(6, "xunknownrc\n");
  1088. __tape_end_request(device, request, -EIO);
  1089. } else {
  1090. __tape_end_request(device, request, rc);
  1091. }
  1092. break;
  1093. }
  1094. }
  1095. /*
  1096. * Tape device open function used by tape_char frontend.
  1097. */
  1098. int
  1099. tape_open(struct tape_device *device)
  1100. {
  1101. int rc;
  1102. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1103. if (device->tape_state == TS_NOT_OPER) {
  1104. DBF_EVENT(6, "TAPE:nodev\n");
  1105. rc = -ENODEV;
  1106. } else if (device->tape_state == TS_IN_USE) {
  1107. DBF_EVENT(6, "TAPE:dbusy\n");
  1108. rc = -EBUSY;
  1109. } else if (device->tape_state == TS_BLKUSE) {
  1110. DBF_EVENT(6, "TAPE:dbusy\n");
  1111. rc = -EBUSY;
  1112. } else if (device->discipline != NULL &&
  1113. !try_module_get(device->discipline->owner)) {
  1114. DBF_EVENT(6, "TAPE:nodisc\n");
  1115. rc = -ENODEV;
  1116. } else {
  1117. tape_state_set(device, TS_IN_USE);
  1118. rc = 0;
  1119. }
  1120. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1121. return rc;
  1122. }
  1123. /*
  1124. * Tape device release function used by tape_char frontend.
  1125. */
  1126. int
  1127. tape_release(struct tape_device *device)
  1128. {
  1129. spin_lock_irq(get_ccwdev_lock(device->cdev));
  1130. if (device->tape_state == TS_IN_USE)
  1131. tape_state_set(device, TS_UNUSED);
  1132. module_put(device->discipline->owner);
  1133. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  1134. return 0;
  1135. }
  1136. /*
  1137. * Execute a magnetic tape command a number of times.
  1138. */
  1139. int
  1140. tape_mtop(struct tape_device *device, int mt_op, int mt_count)
  1141. {
  1142. tape_mtop_fn fn;
  1143. int rc;
  1144. DBF_EVENT(6, "TAPE:mtio\n");
  1145. DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op);
  1146. DBF_EVENT(6, "TAPE:arg: %x\n", mt_count);
  1147. if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS)
  1148. return -EINVAL;
  1149. fn = device->discipline->mtop_array[mt_op];
  1150. if (fn == NULL)
  1151. return -EINVAL;
  1152. /* We assume that the backends can handle count up to 500. */
  1153. if (mt_op == MTBSR || mt_op == MTFSR || mt_op == MTFSF ||
  1154. mt_op == MTBSF || mt_op == MTFSFM || mt_op == MTBSFM) {
  1155. rc = 0;
  1156. for (; mt_count > 500; mt_count -= 500)
  1157. if ((rc = fn(device, 500)) != 0)
  1158. break;
  1159. if (rc == 0)
  1160. rc = fn(device, mt_count);
  1161. } else
  1162. rc = fn(device, mt_count);
  1163. return rc;
  1164. }
  1165. /*
  1166. * Tape init function.
  1167. */
  1168. static int
  1169. tape_init (void)
  1170. {
  1171. TAPE_DBF_AREA = debug_register ( "tape", 2, 2, 4*sizeof(long));
  1172. debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
  1173. #ifdef DBF_LIKE_HELL
  1174. debug_set_level(TAPE_DBF_AREA, 6);
  1175. #endif
  1176. DBF_EVENT(3, "tape init\n");
  1177. tape_proc_init();
  1178. tapechar_init ();
  1179. return 0;
  1180. }
  1181. /*
  1182. * Tape exit function.
  1183. */
  1184. static void
  1185. tape_exit(void)
  1186. {
  1187. DBF_EVENT(6, "tape exit\n");
  1188. /* Get rid of the frontends */
  1189. tapechar_exit();
  1190. tape_proc_cleanup();
  1191. debug_unregister (TAPE_DBF_AREA);
  1192. }
  1193. MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
  1194. "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
  1195. MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
  1196. MODULE_LICENSE("GPL");
  1197. module_init(tape_init);
  1198. module_exit(tape_exit);
  1199. EXPORT_SYMBOL(tape_generic_remove);
  1200. EXPORT_SYMBOL(tape_generic_probe);
  1201. EXPORT_SYMBOL(tape_generic_online);
  1202. EXPORT_SYMBOL(tape_generic_offline);
  1203. EXPORT_SYMBOL(tape_generic_pm_suspend);
  1204. EXPORT_SYMBOL(tape_put_device);
  1205. EXPORT_SYMBOL(tape_get_device);
  1206. EXPORT_SYMBOL(tape_state_verbose);
  1207. EXPORT_SYMBOL(tape_op_verbose);
  1208. EXPORT_SYMBOL(tape_state_set);
  1209. EXPORT_SYMBOL(tape_med_state_set);
  1210. EXPORT_SYMBOL(tape_alloc_request);
  1211. EXPORT_SYMBOL(tape_free_request);
  1212. EXPORT_SYMBOL(tape_dump_sense_dbf);
  1213. EXPORT_SYMBOL(tape_do_io);
  1214. EXPORT_SYMBOL(tape_do_io_async);
  1215. EXPORT_SYMBOL(tape_do_io_interruptible);
  1216. EXPORT_SYMBOL(tape_cancel_io);
  1217. EXPORT_SYMBOL(tape_mtop);