tape_core.c 35 KB

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