tape_core.c 35 KB

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