tape_core.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  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_warning("%s: A tape unit was detached while in "
  614. "use\n", 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);