tape_core.c 32 KB

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