tape_core.c 32 KB

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