tape_core.c 34 KB

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