tape_core.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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. kfree(request->cpaddr);
  601. kfree(request);
  602. return ERR_PTR(-ENOMEM);
  603. }
  604. memset(request->cpdata, 0, datasize);
  605. }
  606. DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr,
  607. request->cpdata);
  608. return request;
  609. }
  610. /*
  611. * Free tape ccw request
  612. */
  613. void
  614. tape_free_request (struct tape_request * request)
  615. {
  616. DBF_LH(6, "Free request %p\n", request);
  617. if (request->device != NULL) {
  618. request->device = tape_put_device(request->device);
  619. }
  620. kfree(request->cpdata);
  621. kfree(request->cpaddr);
  622. kfree(request);
  623. }
  624. static inline int
  625. __tape_start_io(struct tape_device *device, struct tape_request *request)
  626. {
  627. int rc;
  628. #ifdef CONFIG_S390_TAPE_BLOCK
  629. if (request->op == TO_BLOCK)
  630. device->discipline->check_locate(device, request);
  631. #endif
  632. rc = ccw_device_start(
  633. device->cdev,
  634. request->cpaddr,
  635. (unsigned long) request,
  636. 0x00,
  637. request->options
  638. );
  639. if (rc == 0) {
  640. request->status = TAPE_REQUEST_IN_IO;
  641. } else if (rc == -EBUSY) {
  642. /* The common I/O subsystem is currently busy. Retry later. */
  643. request->status = TAPE_REQUEST_QUEUED;
  644. schedule_work(&device->tape_dnr);
  645. rc = 0;
  646. } else {
  647. /* Start failed. Remove request and indicate failure. */
  648. DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc);
  649. }
  650. return rc;
  651. }
  652. static inline void
  653. __tape_start_next_request(struct tape_device *device)
  654. {
  655. struct list_head *l, *n;
  656. struct tape_request *request;
  657. int rc;
  658. DBF_LH(6, "__tape_start_next_request(%p)\n", device);
  659. /*
  660. * Try to start each request on request queue until one is
  661. * started successful.
  662. */
  663. list_for_each_safe(l, n, &device->req_queue) {
  664. request = list_entry(l, struct tape_request, list);
  665. /*
  666. * Avoid race condition if bottom-half was triggered more than
  667. * once.
  668. */
  669. if (request->status == TAPE_REQUEST_IN_IO)
  670. return;
  671. /*
  672. * We wanted to cancel the request but the common I/O layer
  673. * was busy at that time. This can only happen if this
  674. * function is called by delayed_next_request.
  675. * Otherwise we start the next request on the queue.
  676. */
  677. if (request->status == TAPE_REQUEST_CANCEL) {
  678. rc = __tape_cancel_io(device, request);
  679. } else {
  680. rc = __tape_start_io(device, request);
  681. }
  682. if (rc == 0)
  683. return;
  684. /* Set ending status. */
  685. request->rc = rc;
  686. request->status = TAPE_REQUEST_DONE;
  687. /* Remove from request queue. */
  688. list_del(&request->list);
  689. /* Do callback. */
  690. if (request->callback != NULL)
  691. request->callback(request, request->callback_data);
  692. }
  693. }
  694. static void
  695. tape_delayed_next_request(void *data)
  696. {
  697. struct tape_device * device;
  698. device = (struct tape_device *) data;
  699. DBF_LH(6, "tape_delayed_next_request(%p)\n", device);
  700. spin_lock_irq(get_ccwdev_lock(device->cdev));
  701. __tape_start_next_request(device);
  702. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  703. }
  704. static inline void
  705. __tape_end_request(
  706. struct tape_device * device,
  707. struct tape_request * request,
  708. int rc)
  709. {
  710. DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device, request, rc);
  711. if (request) {
  712. request->rc = rc;
  713. request->status = TAPE_REQUEST_DONE;
  714. /* Remove from request queue. */
  715. list_del(&request->list);
  716. /* Do callback. */
  717. if (request->callback != NULL)
  718. request->callback(request, request->callback_data);
  719. }
  720. /* Start next request. */
  721. if (!list_empty(&device->req_queue))
  722. __tape_start_next_request(device);
  723. }
  724. /*
  725. * Write sense data to console/dbf
  726. */
  727. void
  728. tape_dump_sense(struct tape_device* device, struct tape_request *request,
  729. struct irb *irb)
  730. {
  731. unsigned int *sptr;
  732. PRINT_INFO("-------------------------------------------------\n");
  733. PRINT_INFO("DSTAT : %02x CSTAT: %02x CPA: %04x\n",
  734. irb->scsw.dstat, irb->scsw.cstat, irb->scsw.cpa);
  735. PRINT_INFO("DEVICE: %s\n", device->cdev->dev.bus_id);
  736. if (request != NULL)
  737. PRINT_INFO("OP : %s\n", tape_op_verbose[request->op]);
  738. sptr = (unsigned int *) irb->ecw;
  739. PRINT_INFO("Sense data: %08X %08X %08X %08X \n",
  740. sptr[0], sptr[1], sptr[2], sptr[3]);
  741. PRINT_INFO("Sense data: %08X %08X %08X %08X \n",
  742. sptr[4], sptr[5], sptr[6], sptr[7]);
  743. PRINT_INFO("--------------------------------------------------\n");
  744. }
  745. /*
  746. * Write sense data to dbf
  747. */
  748. void
  749. tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request,
  750. struct irb *irb)
  751. {
  752. unsigned int *sptr;
  753. const char* op;
  754. if (request != NULL)
  755. op = tape_op_verbose[request->op];
  756. else
  757. op = "---";
  758. DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n",
  759. irb->scsw.dstat,irb->scsw.cstat);
  760. DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op);
  761. sptr = (unsigned int *) irb->ecw;
  762. DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]);
  763. DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]);
  764. DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]);
  765. DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]);
  766. }
  767. /*
  768. * I/O helper function. Adds the request to the request queue
  769. * and starts it if the tape is idle. Has to be called with
  770. * the device lock held.
  771. */
  772. static inline int
  773. __tape_start_request(struct tape_device *device, struct tape_request *request)
  774. {
  775. int rc;
  776. switch (request->op) {
  777. case TO_MSEN:
  778. case TO_ASSIGN:
  779. case TO_UNASSIGN:
  780. case TO_READ_ATTMSG:
  781. if (device->tape_state == TS_INIT)
  782. break;
  783. if (device->tape_state == TS_UNUSED)
  784. break;
  785. default:
  786. if (device->tape_state == TS_BLKUSE)
  787. break;
  788. if (device->tape_state != TS_IN_USE)
  789. return -ENODEV;
  790. }
  791. /* Increase use count of device for the added request. */
  792. request->device = tape_get_device_reference(device);
  793. if (list_empty(&device->req_queue)) {
  794. /* No other requests are on the queue. Start this one. */
  795. rc = __tape_start_io(device, request);
  796. if (rc)
  797. return rc;
  798. DBF_LH(5, "Request %p added for execution.\n", request);
  799. list_add(&request->list, &device->req_queue);
  800. } else {
  801. DBF_LH(5, "Request %p add to queue.\n", request);
  802. request->status = TAPE_REQUEST_QUEUED;
  803. list_add_tail(&request->list, &device->req_queue);
  804. }
  805. return 0;
  806. }
  807. /*
  808. * Add the request to the request queue, try to start it if the
  809. * tape is idle. Return without waiting for end of i/o.
  810. */
  811. int
  812. tape_do_io_async(struct tape_device *device, struct tape_request *request)
  813. {
  814. int rc;
  815. DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request);
  816. spin_lock_irq(get_ccwdev_lock(device->cdev));
  817. /* Add request to request queue and try to start it. */
  818. rc = __tape_start_request(device, request);
  819. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  820. return rc;
  821. }
  822. /*
  823. * tape_do_io/__tape_wake_up
  824. * Add the request to the request queue, try to start it if the
  825. * tape is idle and wait uninterruptible for its completion.
  826. */
  827. static void
  828. __tape_wake_up(struct tape_request *request, void *data)
  829. {
  830. request->callback = NULL;
  831. wake_up((wait_queue_head_t *) data);
  832. }
  833. int
  834. tape_do_io(struct tape_device *device, struct tape_request *request)
  835. {
  836. wait_queue_head_t wq;
  837. int rc;
  838. init_waitqueue_head(&wq);
  839. spin_lock_irq(get_ccwdev_lock(device->cdev));
  840. /* Setup callback */
  841. request->callback = __tape_wake_up;
  842. request->callback_data = &wq;
  843. /* Add request to request queue and try to start it. */
  844. rc = __tape_start_request(device, request);
  845. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  846. if (rc)
  847. return rc;
  848. /* Request added to the queue. Wait for its completion. */
  849. wait_event(wq, (request->callback == NULL));
  850. /* Get rc from request */
  851. return request->rc;
  852. }
  853. /*
  854. * tape_do_io_interruptible/__tape_wake_up_interruptible
  855. * Add the request to the request queue, try to start it if the
  856. * tape is idle and wait uninterruptible for its completion.
  857. */
  858. static void
  859. __tape_wake_up_interruptible(struct tape_request *request, void *data)
  860. {
  861. request->callback = NULL;
  862. wake_up_interruptible((wait_queue_head_t *) data);
  863. }
  864. int
  865. tape_do_io_interruptible(struct tape_device *device,
  866. struct tape_request *request)
  867. {
  868. wait_queue_head_t wq;
  869. int rc;
  870. init_waitqueue_head(&wq);
  871. spin_lock_irq(get_ccwdev_lock(device->cdev));
  872. /* Setup callback */
  873. request->callback = __tape_wake_up_interruptible;
  874. request->callback_data = &wq;
  875. rc = __tape_start_request(device, request);
  876. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  877. if (rc)
  878. return rc;
  879. /* Request added to the queue. Wait for its completion. */
  880. rc = wait_event_interruptible(wq, (request->callback == NULL));
  881. if (rc != -ERESTARTSYS)
  882. /* Request finished normally. */
  883. return request->rc;
  884. /* Interrupted by a signal. We have to stop the current request. */
  885. spin_lock_irq(get_ccwdev_lock(device->cdev));
  886. rc = __tape_cancel_io(device, request);
  887. spin_unlock_irq(get_ccwdev_lock(device->cdev));
  888. if (rc == 0) {
  889. /* Wait for the interrupt that acknowledges the halt. */
  890. do {
  891. rc = wait_event_interruptible(
  892. wq,
  893. (request->callback == NULL)
  894. );
  895. } while (rc != -ERESTARTSYS);
  896. DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id);
  897. rc = -ERESTARTSYS;
  898. }
  899. return rc;
  900. }
  901. /*
  902. * Tape interrupt routine, called from the ccw_device layer
  903. */
  904. static void
  905. __tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
  906. {
  907. struct tape_device *device;
  908. struct tape_request *request;
  909. int rc;
  910. device = (struct tape_device *) cdev->dev.driver_data;
  911. if (device == NULL) {
  912. PRINT_ERR("could not get device structure for %s "
  913. "in interrupt\n", cdev->dev.bus_id);
  914. return;
  915. }
  916. request = (struct tape_request *) intparm;
  917. DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request);
  918. /* On special conditions irb is an error pointer */
  919. if (IS_ERR(irb)) {
  920. /* FIXME: What to do with the request? */
  921. switch (PTR_ERR(irb)) {
  922. case -ETIMEDOUT:
  923. PRINT_WARN("(%s): Request timed out\n",
  924. cdev->dev.bus_id);
  925. case -EIO:
  926. __tape_end_request(device, request, -EIO);
  927. break;
  928. default:
  929. PRINT_ERR("(%s): Unexpected i/o error %li\n",
  930. cdev->dev.bus_id,
  931. PTR_ERR(irb));
  932. }
  933. return;
  934. }
  935. /*
  936. * If the condition code is not zero and the start function bit is
  937. * still set, this is an deferred error and the last start I/O did
  938. * not succeed. Restart the request now.
  939. */
  940. if (irb->scsw.cc != 0 && (irb->scsw.fctl & SCSW_FCTL_START_FUNC)) {
  941. PRINT_WARN("(%s): deferred cc=%i. restaring\n",
  942. cdev->dev.bus_id,
  943. irb->scsw.cc);
  944. rc = __tape_start_io(device, request);
  945. if (rc)
  946. __tape_end_request(device, request, rc);
  947. return;
  948. }
  949. /* May be an unsolicited irq */
  950. if(request != NULL)
  951. request->rescnt = irb->scsw.count;
  952. if (irb->scsw.dstat != 0x0c) {
  953. /* Set the 'ONLINE' flag depending on sense byte 1 */
  954. if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE)
  955. device->tape_generic_status |= GMT_ONLINE(~0);
  956. else
  957. device->tape_generic_status &= ~GMT_ONLINE(~0);
  958. /*
  959. * Any request that does not come back with channel end
  960. * and device end is unusual. Log the sense data.
  961. */
  962. DBF_EVENT(3,"-- Tape Interrupthandler --\n");
  963. tape_dump_sense_dbf(device, request, irb);
  964. } else {
  965. /* Upon normal completion the device _is_ online */
  966. device->tape_generic_status |= GMT_ONLINE(~0);
  967. }
  968. if (device->tape_state == TS_NOT_OPER) {
  969. DBF_EVENT(6, "tape:device is not operational\n");
  970. return;
  971. }
  972. /*
  973. * Request that were canceled still come back with an interrupt.
  974. * To detect these request the state will be set to TAPE_REQUEST_DONE.
  975. */
  976. if(request != NULL && request->status == TAPE_REQUEST_DONE) {
  977. __tape_end_request(device, request, -EIO);
  978. return;
  979. }
  980. rc = device->discipline->irq(device, request, irb);
  981. /*
  982. * rc < 0 : request finished unsuccessfully.
  983. * rc == TAPE_IO_SUCCESS: request finished successfully.
  984. * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
  985. * rc == TAPE_IO_RETRY: request finished but needs another go.
  986. * rc == TAPE_IO_STOP: request needs to get terminated.
  987. */
  988. switch (rc) {
  989. case TAPE_IO_SUCCESS:
  990. /* Upon normal completion the device _is_ online */
  991. device->tape_generic_status |= GMT_ONLINE(~0);
  992. __tape_end_request(device, request, rc);
  993. break;
  994. case TAPE_IO_PENDING:
  995. break;
  996. case TAPE_IO_RETRY:
  997. rc = __tape_start_io(device, request);
  998. if (rc)
  999. __tape_end_request(device, request, rc);
  1000. break;
  1001. case TAPE_IO_STOP:
  1002. rc = __tape_cancel_io(device, request);
  1003. if (rc)
  1004. __tape_end_request(device, request, rc);
  1005. break;
  1006. default:
  1007. if (rc > 0) {
  1008. DBF_EVENT(6, "xunknownrc\n");
  1009. PRINT_ERR("Invalid return code from discipline "
  1010. "interrupt function.\n");
  1011. __tape_end_request(device, request, -EIO);
  1012. } else {
  1013. __tape_end_request(device, request, rc);
  1014. }
  1015. break;
  1016. }
  1017. }
  1018. /*
  1019. * Tape device open function used by tape_char & tape_block frontends.
  1020. */
  1021. int
  1022. tape_open(struct tape_device *device)
  1023. {
  1024. int rc;
  1025. spin_lock(get_ccwdev_lock(device->cdev));
  1026. if (device->tape_state == TS_NOT_OPER) {
  1027. DBF_EVENT(6, "TAPE:nodev\n");
  1028. rc = -ENODEV;
  1029. } else if (device->tape_state == TS_IN_USE) {
  1030. DBF_EVENT(6, "TAPE:dbusy\n");
  1031. rc = -EBUSY;
  1032. } else if (device->tape_state == TS_BLKUSE) {
  1033. DBF_EVENT(6, "TAPE:dbusy\n");
  1034. rc = -EBUSY;
  1035. } else if (device->discipline != NULL &&
  1036. !try_module_get(device->discipline->owner)) {
  1037. DBF_EVENT(6, "TAPE:nodisc\n");
  1038. rc = -ENODEV;
  1039. } else {
  1040. tape_state_set(device, TS_IN_USE);
  1041. rc = 0;
  1042. }
  1043. spin_unlock(get_ccwdev_lock(device->cdev));
  1044. return rc;
  1045. }
  1046. /*
  1047. * Tape device release function used by tape_char & tape_block frontends.
  1048. */
  1049. int
  1050. tape_release(struct tape_device *device)
  1051. {
  1052. spin_lock(get_ccwdev_lock(device->cdev));
  1053. if (device->tape_state == TS_IN_USE)
  1054. tape_state_set(device, TS_UNUSED);
  1055. module_put(device->discipline->owner);
  1056. spin_unlock(get_ccwdev_lock(device->cdev));
  1057. return 0;
  1058. }
  1059. /*
  1060. * Execute a magnetic tape command a number of times.
  1061. */
  1062. int
  1063. tape_mtop(struct tape_device *device, int mt_op, int mt_count)
  1064. {
  1065. tape_mtop_fn fn;
  1066. int rc;
  1067. DBF_EVENT(6, "TAPE:mtio\n");
  1068. DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op);
  1069. DBF_EVENT(6, "TAPE:arg: %x\n", mt_count);
  1070. if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS)
  1071. return -EINVAL;
  1072. fn = device->discipline->mtop_array[mt_op];
  1073. if (fn == NULL)
  1074. return -EINVAL;
  1075. /* We assume that the backends can handle count up to 500. */
  1076. if (mt_op == MTBSR || mt_op == MTFSR || mt_op == MTFSF ||
  1077. mt_op == MTBSF || mt_op == MTFSFM || mt_op == MTBSFM) {
  1078. rc = 0;
  1079. for (; mt_count > 500; mt_count -= 500)
  1080. if ((rc = fn(device, 500)) != 0)
  1081. break;
  1082. if (rc == 0)
  1083. rc = fn(device, mt_count);
  1084. } else
  1085. rc = fn(device, mt_count);
  1086. return rc;
  1087. }
  1088. /*
  1089. * Tape init function.
  1090. */
  1091. static int
  1092. tape_init (void)
  1093. {
  1094. TAPE_DBF_AREA = debug_register ( "tape", 2, 2, 4*sizeof(long));
  1095. debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
  1096. #ifdef DBF_LIKE_HELL
  1097. debug_set_level(TAPE_DBF_AREA, 6);
  1098. #endif
  1099. DBF_EVENT(3, "tape init\n");
  1100. tape_proc_init();
  1101. tapechar_init ();
  1102. tapeblock_init ();
  1103. return 0;
  1104. }
  1105. /*
  1106. * Tape exit function.
  1107. */
  1108. static void
  1109. tape_exit(void)
  1110. {
  1111. DBF_EVENT(6, "tape exit\n");
  1112. /* Get rid of the frontends */
  1113. tapechar_exit();
  1114. tapeblock_exit();
  1115. tape_proc_cleanup();
  1116. debug_unregister (TAPE_DBF_AREA);
  1117. }
  1118. MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
  1119. "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
  1120. MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
  1121. MODULE_LICENSE("GPL");
  1122. module_init(tape_init);
  1123. module_exit(tape_exit);
  1124. EXPORT_SYMBOL(tape_generic_remove);
  1125. EXPORT_SYMBOL(tape_generic_probe);
  1126. EXPORT_SYMBOL(tape_generic_online);
  1127. EXPORT_SYMBOL(tape_generic_offline);
  1128. EXPORT_SYMBOL(tape_put_device);
  1129. EXPORT_SYMBOL(tape_get_device_reference);
  1130. EXPORT_SYMBOL(tape_state_verbose);
  1131. EXPORT_SYMBOL(tape_op_verbose);
  1132. EXPORT_SYMBOL(tape_state_set);
  1133. EXPORT_SYMBOL(tape_med_state_set);
  1134. EXPORT_SYMBOL(tape_alloc_request);
  1135. EXPORT_SYMBOL(tape_free_request);
  1136. EXPORT_SYMBOL(tape_dump_sense);
  1137. EXPORT_SYMBOL(tape_dump_sense_dbf);
  1138. EXPORT_SYMBOL(tape_do_io);
  1139. EXPORT_SYMBOL(tape_do_io_async);
  1140. EXPORT_SYMBOL(tape_do_io_interruptible);
  1141. EXPORT_SYMBOL(tape_mtop);