tape_3590.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  1. /*
  2. * tape device discipline for 3590 tapes.
  3. *
  4. * Copyright IBM Corp. 2001, 2009
  5. * Author(s): Stefan Bader <shbader@de.ibm.com>
  6. * Michael Holzheu <holzheu@de.ibm.com>
  7. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  8. */
  9. #define KMSG_COMPONENT "tape_3590"
  10. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/init.h>
  14. #include <linux/bio.h>
  15. #include <asm/ebcdic.h>
  16. #define TAPE_DBF_AREA tape_3590_dbf
  17. #define BUFSIZE 512 /* size of buffers for dynamic generated messages */
  18. #include "tape.h"
  19. #include "tape_std.h"
  20. #include "tape_3590.h"
  21. static struct workqueue_struct *tape_3590_wq;
  22. /*
  23. * Pointer to debug area.
  24. */
  25. debug_info_t *TAPE_DBF_AREA = NULL;
  26. EXPORT_SYMBOL(TAPE_DBF_AREA);
  27. /*******************************************************************
  28. * Error Recovery functions:
  29. * - Read Opposite: implemented
  30. * - Read Device (buffered) log: BRA
  31. * - Read Library log: BRA
  32. * - Swap Devices: BRA
  33. * - Long Busy: implemented
  34. * - Special Intercept: BRA
  35. * - Read Alternate: implemented
  36. *******************************************************************/
  37. static const char *tape_3590_msg[TAPE_3590_MAX_MSG] = {
  38. [0x00] = "",
  39. [0x10] = "Lost Sense",
  40. [0x11] = "Assigned Elsewhere",
  41. [0x12] = "Allegiance Reset",
  42. [0x13] = "Shared Access Violation",
  43. [0x20] = "Command Reject",
  44. [0x21] = "Configuration Error",
  45. [0x22] = "Protection Exception",
  46. [0x23] = "Write Protect",
  47. [0x24] = "Write Length",
  48. [0x25] = "Read-Only Format",
  49. [0x31] = "Beginning of Partition",
  50. [0x33] = "End of Partition",
  51. [0x34] = "End of Data",
  52. [0x35] = "Block not found",
  53. [0x40] = "Device Intervention",
  54. [0x41] = "Loader Intervention",
  55. [0x42] = "Library Intervention",
  56. [0x50] = "Write Error",
  57. [0x51] = "Erase Error",
  58. [0x52] = "Formatting Error",
  59. [0x53] = "Read Error",
  60. [0x54] = "Unsupported Format",
  61. [0x55] = "No Formatting",
  62. [0x56] = "Positioning lost",
  63. [0x57] = "Read Length",
  64. [0x60] = "Unsupported Medium",
  65. [0x61] = "Medium Length Error",
  66. [0x62] = "Medium removed",
  67. [0x64] = "Load Check",
  68. [0x65] = "Unload Check",
  69. [0x70] = "Equipment Check",
  70. [0x71] = "Bus out Check",
  71. [0x72] = "Protocol Error",
  72. [0x73] = "Interface Error",
  73. [0x74] = "Overrun",
  74. [0x75] = "Halt Signal",
  75. [0x90] = "Device fenced",
  76. [0x91] = "Device Path fenced",
  77. [0xa0] = "Volume misplaced",
  78. [0xa1] = "Volume inaccessible",
  79. [0xa2] = "Volume in input",
  80. [0xa3] = "Volume ejected",
  81. [0xa4] = "All categories reserved",
  82. [0xa5] = "Duplicate Volume",
  83. [0xa6] = "Library Manager Offline",
  84. [0xa7] = "Library Output Station full",
  85. [0xa8] = "Vision System non-operational",
  86. [0xa9] = "Library Manager Equipment Check",
  87. [0xaa] = "Library Equipment Check",
  88. [0xab] = "All Library Cells full",
  89. [0xac] = "No Cleaner Volumes in Library",
  90. [0xad] = "I/O Station door open",
  91. [0xae] = "Subsystem environmental alert",
  92. };
  93. static int crypt_supported(struct tape_device *device)
  94. {
  95. return TAPE390_CRYPT_SUPPORTED(TAPE_3590_CRYPT_INFO(device));
  96. }
  97. static int crypt_enabled(struct tape_device *device)
  98. {
  99. return TAPE390_CRYPT_ON(TAPE_3590_CRYPT_INFO(device));
  100. }
  101. static void ext_to_int_kekl(struct tape390_kekl *in,
  102. struct tape3592_kekl *out)
  103. {
  104. int i;
  105. memset(out, 0, sizeof(*out));
  106. if (in->type == TAPE390_KEKL_TYPE_HASH)
  107. out->flags |= 0x40;
  108. if (in->type_on_tape == TAPE390_KEKL_TYPE_HASH)
  109. out->flags |= 0x80;
  110. strncpy(out->label, in->label, 64);
  111. for (i = strlen(in->label); i < sizeof(out->label); i++)
  112. out->label[i] = ' ';
  113. ASCEBC(out->label, sizeof(out->label));
  114. }
  115. static void int_to_ext_kekl(struct tape3592_kekl *in,
  116. struct tape390_kekl *out)
  117. {
  118. memset(out, 0, sizeof(*out));
  119. if(in->flags & 0x40)
  120. out->type = TAPE390_KEKL_TYPE_HASH;
  121. else
  122. out->type = TAPE390_KEKL_TYPE_LABEL;
  123. if(in->flags & 0x80)
  124. out->type_on_tape = TAPE390_KEKL_TYPE_HASH;
  125. else
  126. out->type_on_tape = TAPE390_KEKL_TYPE_LABEL;
  127. memcpy(out->label, in->label, sizeof(in->label));
  128. EBCASC(out->label, sizeof(in->label));
  129. strim(out->label);
  130. }
  131. static void int_to_ext_kekl_pair(struct tape3592_kekl_pair *in,
  132. struct tape390_kekl_pair *out)
  133. {
  134. if (in->count == 0) {
  135. out->kekl[0].type = TAPE390_KEKL_TYPE_NONE;
  136. out->kekl[0].type_on_tape = TAPE390_KEKL_TYPE_NONE;
  137. out->kekl[1].type = TAPE390_KEKL_TYPE_NONE;
  138. out->kekl[1].type_on_tape = TAPE390_KEKL_TYPE_NONE;
  139. } else if (in->count == 1) {
  140. int_to_ext_kekl(&in->kekl[0], &out->kekl[0]);
  141. out->kekl[1].type = TAPE390_KEKL_TYPE_NONE;
  142. out->kekl[1].type_on_tape = TAPE390_KEKL_TYPE_NONE;
  143. } else if (in->count == 2) {
  144. int_to_ext_kekl(&in->kekl[0], &out->kekl[0]);
  145. int_to_ext_kekl(&in->kekl[1], &out->kekl[1]);
  146. } else {
  147. printk("Invalid KEKL number: %d\n", in->count);
  148. BUG();
  149. }
  150. }
  151. static int check_ext_kekl(struct tape390_kekl *kekl)
  152. {
  153. if (kekl->type == TAPE390_KEKL_TYPE_NONE)
  154. goto invalid;
  155. if (kekl->type > TAPE390_KEKL_TYPE_HASH)
  156. goto invalid;
  157. if (kekl->type_on_tape == TAPE390_KEKL_TYPE_NONE)
  158. goto invalid;
  159. if (kekl->type_on_tape > TAPE390_KEKL_TYPE_HASH)
  160. goto invalid;
  161. if ((kekl->type == TAPE390_KEKL_TYPE_HASH) &&
  162. (kekl->type_on_tape == TAPE390_KEKL_TYPE_LABEL))
  163. goto invalid;
  164. return 0;
  165. invalid:
  166. return -EINVAL;
  167. }
  168. static int check_ext_kekl_pair(struct tape390_kekl_pair *kekls)
  169. {
  170. if (check_ext_kekl(&kekls->kekl[0]))
  171. goto invalid;
  172. if (check_ext_kekl(&kekls->kekl[1]))
  173. goto invalid;
  174. return 0;
  175. invalid:
  176. return -EINVAL;
  177. }
  178. /*
  179. * Query KEKLs
  180. */
  181. static int tape_3592_kekl_query(struct tape_device *device,
  182. struct tape390_kekl_pair *ext_kekls)
  183. {
  184. struct tape_request *request;
  185. struct tape3592_kekl_query_order *order;
  186. struct tape3592_kekl_query_data *int_kekls;
  187. int rc;
  188. DBF_EVENT(6, "tape3592_kekl_query\n");
  189. int_kekls = kmalloc(sizeof(*int_kekls), GFP_KERNEL|GFP_DMA);
  190. if (!int_kekls)
  191. return -ENOMEM;
  192. request = tape_alloc_request(2, sizeof(*order));
  193. if (IS_ERR(request)) {
  194. rc = PTR_ERR(request);
  195. goto fail_malloc;
  196. }
  197. order = request->cpdata;
  198. memset(order,0,sizeof(*order));
  199. order->code = 0xe2;
  200. order->max_count = 2;
  201. request->op = TO_KEKL_QUERY;
  202. tape_ccw_cc(request->cpaddr, PERF_SUBSYS_FUNC, sizeof(*order), order);
  203. tape_ccw_end(request->cpaddr + 1, READ_SS_DATA, sizeof(*int_kekls),
  204. int_kekls);
  205. rc = tape_do_io(device, request);
  206. if (rc)
  207. goto fail_request;
  208. int_to_ext_kekl_pair(&int_kekls->kekls, ext_kekls);
  209. rc = 0;
  210. fail_request:
  211. tape_free_request(request);
  212. fail_malloc:
  213. kfree(int_kekls);
  214. return rc;
  215. }
  216. /*
  217. * IOCTL: Query KEKLs
  218. */
  219. static int tape_3592_ioctl_kekl_query(struct tape_device *device,
  220. unsigned long arg)
  221. {
  222. int rc;
  223. struct tape390_kekl_pair *ext_kekls;
  224. DBF_EVENT(6, "tape_3592_ioctl_kekl_query\n");
  225. if (!crypt_supported(device))
  226. return -ENOSYS;
  227. if (!crypt_enabled(device))
  228. return -EUNATCH;
  229. ext_kekls = kmalloc(sizeof(*ext_kekls), GFP_KERNEL);
  230. if (!ext_kekls)
  231. return -ENOMEM;
  232. rc = tape_3592_kekl_query(device, ext_kekls);
  233. if (rc != 0)
  234. goto fail;
  235. if (copy_to_user((char __user *) arg, ext_kekls, sizeof(*ext_kekls))) {
  236. rc = -EFAULT;
  237. goto fail;
  238. }
  239. rc = 0;
  240. fail:
  241. kfree(ext_kekls);
  242. return rc;
  243. }
  244. static int tape_3590_mttell(struct tape_device *device, int mt_count);
  245. /*
  246. * Set KEKLs
  247. */
  248. static int tape_3592_kekl_set(struct tape_device *device,
  249. struct tape390_kekl_pair *ext_kekls)
  250. {
  251. struct tape_request *request;
  252. struct tape3592_kekl_set_order *order;
  253. DBF_EVENT(6, "tape3592_kekl_set\n");
  254. if (check_ext_kekl_pair(ext_kekls)) {
  255. DBF_EVENT(6, "invalid kekls\n");
  256. return -EINVAL;
  257. }
  258. if (tape_3590_mttell(device, 0) != 0)
  259. return -EBADSLT;
  260. request = tape_alloc_request(1, sizeof(*order));
  261. if (IS_ERR(request))
  262. return PTR_ERR(request);
  263. order = request->cpdata;
  264. memset(order, 0, sizeof(*order));
  265. order->code = 0xe3;
  266. order->kekls.count = 2;
  267. ext_to_int_kekl(&ext_kekls->kekl[0], &order->kekls.kekl[0]);
  268. ext_to_int_kekl(&ext_kekls->kekl[1], &order->kekls.kekl[1]);
  269. request->op = TO_KEKL_SET;
  270. tape_ccw_end(request->cpaddr, PERF_SUBSYS_FUNC, sizeof(*order), order);
  271. return tape_do_io_free(device, request);
  272. }
  273. /*
  274. * IOCTL: Set KEKLs
  275. */
  276. static int tape_3592_ioctl_kekl_set(struct tape_device *device,
  277. unsigned long arg)
  278. {
  279. int rc;
  280. struct tape390_kekl_pair *ext_kekls;
  281. DBF_EVENT(6, "tape_3592_ioctl_kekl_set\n");
  282. if (!crypt_supported(device))
  283. return -ENOSYS;
  284. if (!crypt_enabled(device))
  285. return -EUNATCH;
  286. ext_kekls = memdup_user((char __user *)arg, sizeof(*ext_kekls));
  287. if (IS_ERR(ext_kekls))
  288. return PTR_ERR(ext_kekls);
  289. rc = tape_3592_kekl_set(device, ext_kekls);
  290. kfree(ext_kekls);
  291. return rc;
  292. }
  293. /*
  294. * Enable encryption
  295. */
  296. static struct tape_request *__tape_3592_enable_crypt(struct tape_device *device)
  297. {
  298. struct tape_request *request;
  299. char *data;
  300. DBF_EVENT(6, "tape_3592_enable_crypt\n");
  301. if (!crypt_supported(device))
  302. return ERR_PTR(-ENOSYS);
  303. request = tape_alloc_request(2, 72);
  304. if (IS_ERR(request))
  305. return request;
  306. data = request->cpdata;
  307. memset(data,0,72);
  308. data[0] = 0x05;
  309. data[36 + 0] = 0x03;
  310. data[36 + 1] = 0x03;
  311. data[36 + 4] = 0x40;
  312. data[36 + 6] = 0x01;
  313. data[36 + 14] = 0x2f;
  314. data[36 + 18] = 0xc3;
  315. data[36 + 35] = 0x72;
  316. request->op = TO_CRYPT_ON;
  317. tape_ccw_cc(request->cpaddr, MODE_SET_CB, 36, data);
  318. tape_ccw_end(request->cpaddr + 1, MODE_SET_CB, 36, data + 36);
  319. return request;
  320. }
  321. static int tape_3592_enable_crypt(struct tape_device *device)
  322. {
  323. struct tape_request *request;
  324. request = __tape_3592_enable_crypt(device);
  325. if (IS_ERR(request))
  326. return PTR_ERR(request);
  327. return tape_do_io_free(device, request);
  328. }
  329. static void tape_3592_enable_crypt_async(struct tape_device *device)
  330. {
  331. struct tape_request *request;
  332. request = __tape_3592_enable_crypt(device);
  333. if (!IS_ERR(request))
  334. tape_do_io_async_free(device, request);
  335. }
  336. /*
  337. * Disable encryption
  338. */
  339. static struct tape_request *__tape_3592_disable_crypt(struct tape_device *device)
  340. {
  341. struct tape_request *request;
  342. char *data;
  343. DBF_EVENT(6, "tape_3592_disable_crypt\n");
  344. if (!crypt_supported(device))
  345. return ERR_PTR(-ENOSYS);
  346. request = tape_alloc_request(2, 72);
  347. if (IS_ERR(request))
  348. return request;
  349. data = request->cpdata;
  350. memset(data,0,72);
  351. data[0] = 0x05;
  352. data[36 + 0] = 0x03;
  353. data[36 + 1] = 0x03;
  354. data[36 + 35] = 0x32;
  355. request->op = TO_CRYPT_OFF;
  356. tape_ccw_cc(request->cpaddr, MODE_SET_CB, 36, data);
  357. tape_ccw_end(request->cpaddr + 1, MODE_SET_CB, 36, data + 36);
  358. return request;
  359. }
  360. static int tape_3592_disable_crypt(struct tape_device *device)
  361. {
  362. struct tape_request *request;
  363. request = __tape_3592_disable_crypt(device);
  364. if (IS_ERR(request))
  365. return PTR_ERR(request);
  366. return tape_do_io_free(device, request);
  367. }
  368. static void tape_3592_disable_crypt_async(struct tape_device *device)
  369. {
  370. struct tape_request *request;
  371. request = __tape_3592_disable_crypt(device);
  372. if (!IS_ERR(request))
  373. tape_do_io_async_free(device, request);
  374. }
  375. /*
  376. * IOCTL: Set encryption status
  377. */
  378. static int tape_3592_ioctl_crypt_set(struct tape_device *device,
  379. unsigned long arg)
  380. {
  381. struct tape390_crypt_info info;
  382. DBF_EVENT(6, "tape_3592_ioctl_crypt_set\n");
  383. if (!crypt_supported(device))
  384. return -ENOSYS;
  385. if (copy_from_user(&info, (char __user *)arg, sizeof(info)))
  386. return -EFAULT;
  387. if (info.status & ~TAPE390_CRYPT_ON_MASK)
  388. return -EINVAL;
  389. if (info.status & TAPE390_CRYPT_ON_MASK)
  390. return tape_3592_enable_crypt(device);
  391. else
  392. return tape_3592_disable_crypt(device);
  393. }
  394. static int tape_3590_sense_medium(struct tape_device *device);
  395. /*
  396. * IOCTL: Query enryption status
  397. */
  398. static int tape_3592_ioctl_crypt_query(struct tape_device *device,
  399. unsigned long arg)
  400. {
  401. DBF_EVENT(6, "tape_3592_ioctl_crypt_query\n");
  402. if (!crypt_supported(device))
  403. return -ENOSYS;
  404. tape_3590_sense_medium(device);
  405. if (copy_to_user((char __user *) arg, &TAPE_3590_CRYPT_INFO(device),
  406. sizeof(TAPE_3590_CRYPT_INFO(device))))
  407. return -EFAULT;
  408. else
  409. return 0;
  410. }
  411. /*
  412. * 3590 IOCTL Overload
  413. */
  414. static int
  415. tape_3590_ioctl(struct tape_device *device, unsigned int cmd, unsigned long arg)
  416. {
  417. switch (cmd) {
  418. case TAPE390_DISPLAY: {
  419. struct display_struct disp;
  420. if (copy_from_user(&disp, (char __user *) arg, sizeof(disp)))
  421. return -EFAULT;
  422. return tape_std_display(device, &disp);
  423. }
  424. case TAPE390_KEKL_SET:
  425. return tape_3592_ioctl_kekl_set(device, arg);
  426. case TAPE390_KEKL_QUERY:
  427. return tape_3592_ioctl_kekl_query(device, arg);
  428. case TAPE390_CRYPT_SET:
  429. return tape_3592_ioctl_crypt_set(device, arg);
  430. case TAPE390_CRYPT_QUERY:
  431. return tape_3592_ioctl_crypt_query(device, arg);
  432. default:
  433. return -EINVAL; /* no additional ioctls */
  434. }
  435. }
  436. /*
  437. * SENSE Medium: Get Sense data about medium state
  438. */
  439. static int tape_3590_sense_medium(struct tape_device *device)
  440. {
  441. struct tape_request *request;
  442. request = tape_alloc_request(1, 128);
  443. if (IS_ERR(request))
  444. return PTR_ERR(request);
  445. request->op = TO_MSEN;
  446. tape_ccw_end(request->cpaddr, MEDIUM_SENSE, 128, request->cpdata);
  447. return tape_do_io_free(device, request);
  448. }
  449. static void tape_3590_sense_medium_async(struct tape_device *device)
  450. {
  451. struct tape_request *request;
  452. request = tape_alloc_request(1, 128);
  453. if (IS_ERR(request))
  454. return;
  455. request->op = TO_MSEN;
  456. tape_ccw_end(request->cpaddr, MEDIUM_SENSE, 128, request->cpdata);
  457. tape_do_io_async_free(device, request);
  458. }
  459. /*
  460. * MTTELL: Tell block. Return the number of block relative to current file.
  461. */
  462. static int
  463. tape_3590_mttell(struct tape_device *device, int mt_count)
  464. {
  465. __u64 block_id;
  466. int rc;
  467. rc = tape_std_read_block_id(device, &block_id);
  468. if (rc)
  469. return rc;
  470. return block_id >> 32;
  471. }
  472. /*
  473. * MTSEEK: seek to the specified block.
  474. */
  475. static int
  476. tape_3590_mtseek(struct tape_device *device, int count)
  477. {
  478. struct tape_request *request;
  479. DBF_EVENT(6, "xsee id: %x\n", count);
  480. request = tape_alloc_request(3, 4);
  481. if (IS_ERR(request))
  482. return PTR_ERR(request);
  483. request->op = TO_LBL;
  484. tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
  485. *(__u32 *) request->cpdata = count;
  486. tape_ccw_cc(request->cpaddr + 1, LOCATE, 4, request->cpdata);
  487. tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
  488. return tape_do_io_free(device, request);
  489. }
  490. /*
  491. * Read Opposite Error Recovery Function:
  492. * Used, when Read Forward does not work
  493. */
  494. static void
  495. tape_3590_read_opposite(struct tape_device *device,
  496. struct tape_request *request)
  497. {
  498. struct tape_3590_disc_data *data;
  499. /*
  500. * We have allocated 4 ccws in tape_std_read, so we can now
  501. * transform the request to a read backward, followed by a
  502. * forward space block.
  503. */
  504. request->op = TO_RBA;
  505. tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
  506. data = device->discdata;
  507. tape_ccw_cc_idal(request->cpaddr + 1, data->read_back_op,
  508. device->char_data.idal_buf);
  509. tape_ccw_cc(request->cpaddr + 2, FORSPACEBLOCK, 0, NULL);
  510. tape_ccw_end(request->cpaddr + 3, NOP, 0, NULL);
  511. DBF_EVENT(6, "xrop ccwg\n");
  512. }
  513. /*
  514. * Read Attention Msg
  515. * This should be done after an interrupt with attention bit (0x80)
  516. * in device state.
  517. *
  518. * After a "read attention message" request there are two possible
  519. * results:
  520. *
  521. * 1. A unit check is presented, when attention sense is present (e.g. when
  522. * a medium has been unloaded). The attention sense comes then
  523. * together with the unit check. The recovery action is either "retry"
  524. * (in case there is an attention message pending) or "permanent error".
  525. *
  526. * 2. The attention msg is written to the "read subsystem data" buffer.
  527. * In this case we probably should print it to the console.
  528. */
  529. static void tape_3590_read_attmsg_async(struct tape_device *device)
  530. {
  531. struct tape_request *request;
  532. char *buf;
  533. request = tape_alloc_request(3, 4096);
  534. if (IS_ERR(request))
  535. return;
  536. request->op = TO_READ_ATTMSG;
  537. buf = request->cpdata;
  538. buf[0] = PREP_RD_SS_DATA;
  539. buf[6] = RD_ATTMSG; /* read att msg */
  540. tape_ccw_cc(request->cpaddr, PERFORM_SS_FUNC, 12, buf);
  541. tape_ccw_cc(request->cpaddr + 1, READ_SS_DATA, 4096 - 12, buf + 12);
  542. tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
  543. tape_do_io_async_free(device, request);
  544. }
  545. /*
  546. * These functions are used to schedule follow-up actions from within an
  547. * interrupt context (like unsolicited interrupts).
  548. * Note: the work handler is called by the system work queue. The tape
  549. * commands started by the handler need to be asynchrounous, otherwise
  550. * a deadlock can occur e.g. in case of a deferred cc=1 (see __tape_do_irq).
  551. */
  552. struct work_handler_data {
  553. struct tape_device *device;
  554. enum tape_op op;
  555. struct work_struct work;
  556. };
  557. static void
  558. tape_3590_work_handler(struct work_struct *work)
  559. {
  560. struct work_handler_data *p =
  561. container_of(work, struct work_handler_data, work);
  562. switch (p->op) {
  563. case TO_MSEN:
  564. tape_3590_sense_medium_async(p->device);
  565. break;
  566. case TO_READ_ATTMSG:
  567. tape_3590_read_attmsg_async(p->device);
  568. break;
  569. case TO_CRYPT_ON:
  570. tape_3592_enable_crypt_async(p->device);
  571. break;
  572. case TO_CRYPT_OFF:
  573. tape_3592_disable_crypt_async(p->device);
  574. break;
  575. default:
  576. DBF_EVENT(3, "T3590: work handler undefined for "
  577. "operation 0x%02x\n", p->op);
  578. }
  579. tape_put_device(p->device);
  580. kfree(p);
  581. }
  582. static int
  583. tape_3590_schedule_work(struct tape_device *device, enum tape_op op)
  584. {
  585. struct work_handler_data *p;
  586. if ((p = kzalloc(sizeof(*p), GFP_ATOMIC)) == NULL)
  587. return -ENOMEM;
  588. INIT_WORK(&p->work, tape_3590_work_handler);
  589. p->device = tape_get_device(device);
  590. p->op = op;
  591. queue_work(tape_3590_wq, &p->work);
  592. return 0;
  593. }
  594. static void tape_3590_med_state_set(struct tape_device *device,
  595. struct tape_3590_med_sense *sense)
  596. {
  597. struct tape390_crypt_info *c_info;
  598. c_info = &TAPE_3590_CRYPT_INFO(device);
  599. DBF_EVENT(6, "medium state: %x:%x\n", sense->macst, sense->masst);
  600. switch (sense->macst) {
  601. case 0x04:
  602. case 0x05:
  603. case 0x06:
  604. tape_med_state_set(device, MS_UNLOADED);
  605. TAPE_3590_CRYPT_INFO(device).medium_status = 0;
  606. return;
  607. case 0x08:
  608. case 0x09:
  609. tape_med_state_set(device, MS_LOADED);
  610. break;
  611. default:
  612. tape_med_state_set(device, MS_UNKNOWN);
  613. return;
  614. }
  615. c_info->medium_status |= TAPE390_MEDIUM_LOADED_MASK;
  616. if (sense->flags & MSENSE_CRYPT_MASK) {
  617. DBF_EVENT(6, "Medium is encrypted (%04x)\n", sense->flags);
  618. c_info->medium_status |= TAPE390_MEDIUM_ENCRYPTED_MASK;
  619. } else {
  620. DBF_EVENT(6, "Medium is not encrypted %04x\n", sense->flags);
  621. c_info->medium_status &= ~TAPE390_MEDIUM_ENCRYPTED_MASK;
  622. }
  623. }
  624. /*
  625. * The done handler is called at device/channel end and wakes up the sleeping
  626. * process
  627. */
  628. static int
  629. tape_3590_done(struct tape_device *device, struct tape_request *request)
  630. {
  631. DBF_EVENT(6, "%s done\n", tape_op_verbose[request->op]);
  632. switch (request->op) {
  633. case TO_BSB:
  634. case TO_BSF:
  635. case TO_DSE:
  636. case TO_FSB:
  637. case TO_FSF:
  638. case TO_LBL:
  639. case TO_RFO:
  640. case TO_RBA:
  641. case TO_REW:
  642. case TO_WRI:
  643. case TO_WTM:
  644. case TO_BLOCK:
  645. case TO_LOAD:
  646. tape_med_state_set(device, MS_LOADED);
  647. break;
  648. case TO_RUN:
  649. tape_med_state_set(device, MS_UNLOADED);
  650. tape_3590_schedule_work(device, TO_CRYPT_OFF);
  651. break;
  652. case TO_MSEN:
  653. tape_3590_med_state_set(device, request->cpdata);
  654. break;
  655. case TO_CRYPT_ON:
  656. TAPE_3590_CRYPT_INFO(device).status
  657. |= TAPE390_CRYPT_ON_MASK;
  658. *(device->modeset_byte) |= 0x03;
  659. break;
  660. case TO_CRYPT_OFF:
  661. TAPE_3590_CRYPT_INFO(device).status
  662. &= ~TAPE390_CRYPT_ON_MASK;
  663. *(device->modeset_byte) &= ~0x03;
  664. break;
  665. case TO_RBI: /* RBI seems to succeed even without medium loaded. */
  666. case TO_NOP: /* Same to NOP. */
  667. case TO_READ_CONFIG:
  668. case TO_READ_ATTMSG:
  669. case TO_DIS:
  670. case TO_ASSIGN:
  671. case TO_UNASSIGN:
  672. case TO_SIZE:
  673. case TO_KEKL_SET:
  674. case TO_KEKL_QUERY:
  675. case TO_RDC:
  676. break;
  677. }
  678. return TAPE_IO_SUCCESS;
  679. }
  680. /*
  681. * This function is called, when error recovery was successful
  682. */
  683. static inline int
  684. tape_3590_erp_succeded(struct tape_device *device, struct tape_request *request)
  685. {
  686. DBF_EVENT(3, "Error Recovery successful for %s\n",
  687. tape_op_verbose[request->op]);
  688. return tape_3590_done(device, request);
  689. }
  690. /*
  691. * This function is called, when error recovery was not successful
  692. */
  693. static inline int
  694. tape_3590_erp_failed(struct tape_device *device, struct tape_request *request,
  695. struct irb *irb, int rc)
  696. {
  697. DBF_EVENT(3, "Error Recovery failed for %s\n",
  698. tape_op_verbose[request->op]);
  699. tape_dump_sense_dbf(device, request, irb);
  700. return rc;
  701. }
  702. /*
  703. * Error Recovery do retry
  704. */
  705. static inline int
  706. tape_3590_erp_retry(struct tape_device *device, struct tape_request *request,
  707. struct irb *irb)
  708. {
  709. DBF_EVENT(2, "Retry: %s\n", tape_op_verbose[request->op]);
  710. tape_dump_sense_dbf(device, request, irb);
  711. return TAPE_IO_RETRY;
  712. }
  713. /*
  714. * Handle unsolicited interrupts
  715. */
  716. static int
  717. tape_3590_unsolicited_irq(struct tape_device *device, struct irb *irb)
  718. {
  719. if (irb->scsw.cmd.dstat == DEV_STAT_CHN_END)
  720. /* Probably result of halt ssch */
  721. return TAPE_IO_PENDING;
  722. else if (irb->scsw.cmd.dstat == 0x85)
  723. /* Device Ready */
  724. DBF_EVENT(3, "unsol.irq! tape ready: %08x\n", device->cdev_id);
  725. else if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
  726. tape_3590_schedule_work(device, TO_READ_ATTMSG);
  727. } else {
  728. DBF_EVENT(3, "unsol.irq! dev end: %08x\n", device->cdev_id);
  729. tape_dump_sense_dbf(device, NULL, irb);
  730. }
  731. /* check medium state */
  732. tape_3590_schedule_work(device, TO_MSEN);
  733. return TAPE_IO_SUCCESS;
  734. }
  735. /*
  736. * Basic Recovery routine
  737. */
  738. static int
  739. tape_3590_erp_basic(struct tape_device *device, struct tape_request *request,
  740. struct irb *irb, int rc)
  741. {
  742. struct tape_3590_sense *sense;
  743. sense = (struct tape_3590_sense *) irb->ecw;
  744. switch (sense->bra) {
  745. case SENSE_BRA_PER:
  746. return tape_3590_erp_failed(device, request, irb, rc);
  747. case SENSE_BRA_CONT:
  748. return tape_3590_erp_succeded(device, request);
  749. case SENSE_BRA_RE:
  750. return tape_3590_erp_retry(device, request, irb);
  751. case SENSE_BRA_DRE:
  752. return tape_3590_erp_failed(device, request, irb, rc);
  753. default:
  754. BUG();
  755. return TAPE_IO_STOP;
  756. }
  757. }
  758. /*
  759. * RDL: Read Device (buffered) log
  760. */
  761. static int
  762. tape_3590_erp_read_buf_log(struct tape_device *device,
  763. struct tape_request *request, struct irb *irb)
  764. {
  765. /*
  766. * We just do the basic error recovery at the moment (retry).
  767. * Perhaps in the future, we read the log and dump it somewhere...
  768. */
  769. return tape_3590_erp_basic(device, request, irb, -EIO);
  770. }
  771. /*
  772. * SWAP: Swap Devices
  773. */
  774. static int
  775. tape_3590_erp_swap(struct tape_device *device, struct tape_request *request,
  776. struct irb *irb)
  777. {
  778. /*
  779. * This error recovery should swap the tapes
  780. * if the original has a problem. The operation
  781. * should proceed with the new tape... this
  782. * should probably be done in user space!
  783. */
  784. dev_warn (&device->cdev->dev, "The tape medium must be loaded into a "
  785. "different tape unit\n");
  786. return tape_3590_erp_basic(device, request, irb, -EIO);
  787. }
  788. /*
  789. * LBY: Long Busy
  790. */
  791. static int
  792. tape_3590_erp_long_busy(struct tape_device *device,
  793. struct tape_request *request, struct irb *irb)
  794. {
  795. DBF_EVENT(6, "Device is busy\n");
  796. return TAPE_IO_LONG_BUSY;
  797. }
  798. /*
  799. * SPI: Special Intercept
  800. */
  801. static int
  802. tape_3590_erp_special_interrupt(struct tape_device *device,
  803. struct tape_request *request, struct irb *irb)
  804. {
  805. return tape_3590_erp_basic(device, request, irb, -EIO);
  806. }
  807. /*
  808. * RDA: Read Alternate
  809. */
  810. static int
  811. tape_3590_erp_read_alternate(struct tape_device *device,
  812. struct tape_request *request, struct irb *irb)
  813. {
  814. struct tape_3590_disc_data *data;
  815. /*
  816. * The issued Read Backward or Read Previous command is not
  817. * supported by the device
  818. * The recovery action should be to issue another command:
  819. * Read Revious: if Read Backward is not supported
  820. * Read Backward: if Read Previous is not supported
  821. */
  822. data = device->discdata;
  823. if (data->read_back_op == READ_PREVIOUS) {
  824. DBF_EVENT(2, "(%08x): No support for READ_PREVIOUS command\n",
  825. device->cdev_id);
  826. data->read_back_op = READ_BACKWARD;
  827. } else {
  828. DBF_EVENT(2, "(%08x): No support for READ_BACKWARD command\n",
  829. device->cdev_id);
  830. data->read_back_op = READ_PREVIOUS;
  831. }
  832. tape_3590_read_opposite(device, request);
  833. return tape_3590_erp_retry(device, request, irb);
  834. }
  835. /*
  836. * Error Recovery read opposite
  837. */
  838. static int
  839. tape_3590_erp_read_opposite(struct tape_device *device,
  840. struct tape_request *request, struct irb *irb)
  841. {
  842. switch (request->op) {
  843. case TO_RFO:
  844. /*
  845. * We did read forward, but the data could not be read.
  846. * We will read backward and then skip forward again.
  847. */
  848. tape_3590_read_opposite(device, request);
  849. return tape_3590_erp_retry(device, request, irb);
  850. case TO_RBA:
  851. /* We tried to read forward and backward, but hat no success */
  852. return tape_3590_erp_failed(device, request, irb, -EIO);
  853. break;
  854. default:
  855. return tape_3590_erp_failed(device, request, irb, -EIO);
  856. }
  857. }
  858. /*
  859. * Print an MIM (Media Information Message) (message code f0)
  860. */
  861. static void
  862. tape_3590_print_mim_msg_f0(struct tape_device *device, struct irb *irb)
  863. {
  864. struct tape_3590_sense *sense;
  865. char *exception, *service;
  866. exception = kmalloc(BUFSIZE, GFP_ATOMIC);
  867. service = kmalloc(BUFSIZE, GFP_ATOMIC);
  868. if (!exception || !service)
  869. goto out_nomem;
  870. sense = (struct tape_3590_sense *) irb->ecw;
  871. /* Exception Message */
  872. switch (sense->fmt.f70.emc) {
  873. case 0x02:
  874. snprintf(exception, BUFSIZE, "Data degraded");
  875. break;
  876. case 0x03:
  877. snprintf(exception, BUFSIZE, "Data degraded in partion %i",
  878. sense->fmt.f70.mp);
  879. break;
  880. case 0x04:
  881. snprintf(exception, BUFSIZE, "Medium degraded");
  882. break;
  883. case 0x05:
  884. snprintf(exception, BUFSIZE, "Medium degraded in partition %i",
  885. sense->fmt.f70.mp);
  886. break;
  887. case 0x06:
  888. snprintf(exception, BUFSIZE, "Block 0 Error");
  889. break;
  890. case 0x07:
  891. snprintf(exception, BUFSIZE, "Medium Exception 0x%02x",
  892. sense->fmt.f70.md);
  893. break;
  894. default:
  895. snprintf(exception, BUFSIZE, "0x%02x",
  896. sense->fmt.f70.emc);
  897. break;
  898. }
  899. /* Service Message */
  900. switch (sense->fmt.f70.smc) {
  901. case 0x02:
  902. snprintf(service, BUFSIZE, "Reference Media maintenance "
  903. "procedure %i", sense->fmt.f70.md);
  904. break;
  905. default:
  906. snprintf(service, BUFSIZE, "0x%02x",
  907. sense->fmt.f70.smc);
  908. break;
  909. }
  910. dev_warn (&device->cdev->dev, "Tape media information: exception %s, "
  911. "service %s\n", exception, service);
  912. out_nomem:
  913. kfree(exception);
  914. kfree(service);
  915. }
  916. /*
  917. * Print an I/O Subsystem Service Information Message (message code f1)
  918. */
  919. static void
  920. tape_3590_print_io_sim_msg_f1(struct tape_device *device, struct irb *irb)
  921. {
  922. struct tape_3590_sense *sense;
  923. char *exception, *service;
  924. exception = kmalloc(BUFSIZE, GFP_ATOMIC);
  925. service = kmalloc(BUFSIZE, GFP_ATOMIC);
  926. if (!exception || !service)
  927. goto out_nomem;
  928. sense = (struct tape_3590_sense *) irb->ecw;
  929. /* Exception Message */
  930. switch (sense->fmt.f71.emc) {
  931. case 0x01:
  932. snprintf(exception, BUFSIZE, "Effect of failure is unknown");
  933. break;
  934. case 0x02:
  935. snprintf(exception, BUFSIZE, "CU Exception - no performance "
  936. "impact");
  937. break;
  938. case 0x03:
  939. snprintf(exception, BUFSIZE, "CU Exception on channel "
  940. "interface 0x%02x", sense->fmt.f71.md[0]);
  941. break;
  942. case 0x04:
  943. snprintf(exception, BUFSIZE, "CU Exception on device path "
  944. "0x%02x", sense->fmt.f71.md[0]);
  945. break;
  946. case 0x05:
  947. snprintf(exception, BUFSIZE, "CU Exception on library path "
  948. "0x%02x", sense->fmt.f71.md[0]);
  949. break;
  950. case 0x06:
  951. snprintf(exception, BUFSIZE, "CU Exception on node 0x%02x",
  952. sense->fmt.f71.md[0]);
  953. break;
  954. case 0x07:
  955. snprintf(exception, BUFSIZE, "CU Exception on partition "
  956. "0x%02x", sense->fmt.f71.md[0]);
  957. break;
  958. default:
  959. snprintf(exception, BUFSIZE, "0x%02x",
  960. sense->fmt.f71.emc);
  961. }
  962. /* Service Message */
  963. switch (sense->fmt.f71.smc) {
  964. case 0x01:
  965. snprintf(service, BUFSIZE, "Repair impact is unknown");
  966. break;
  967. case 0x02:
  968. snprintf(service, BUFSIZE, "Repair will not impact cu "
  969. "performance");
  970. break;
  971. case 0x03:
  972. if (sense->fmt.f71.mdf == 0)
  973. snprintf(service, BUFSIZE, "Repair will disable node "
  974. "0x%x on CU", sense->fmt.f71.md[1]);
  975. else
  976. snprintf(service, BUFSIZE, "Repair will disable "
  977. "nodes (0x%x-0x%x) on CU", sense->fmt.f71.md[1],
  978. sense->fmt.f71.md[2]);
  979. break;
  980. case 0x04:
  981. if (sense->fmt.f71.mdf == 0)
  982. snprintf(service, BUFSIZE, "Repair will disable "
  983. "channel path 0x%x on CU",
  984. sense->fmt.f71.md[1]);
  985. else
  986. snprintf(service, BUFSIZE, "Repair will disable channel"
  987. " paths (0x%x-0x%x) on CU",
  988. sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
  989. break;
  990. case 0x05:
  991. if (sense->fmt.f71.mdf == 0)
  992. snprintf(service, BUFSIZE, "Repair will disable device"
  993. " path 0x%x on CU", sense->fmt.f71.md[1]);
  994. else
  995. snprintf(service, BUFSIZE, "Repair will disable device"
  996. " paths (0x%x-0x%x) on CU",
  997. sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
  998. break;
  999. case 0x06:
  1000. if (sense->fmt.f71.mdf == 0)
  1001. snprintf(service, BUFSIZE, "Repair will disable "
  1002. "library path 0x%x on CU",
  1003. sense->fmt.f71.md[1]);
  1004. else
  1005. snprintf(service, BUFSIZE, "Repair will disable "
  1006. "library paths (0x%x-0x%x) on CU",
  1007. sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
  1008. break;
  1009. case 0x07:
  1010. snprintf(service, BUFSIZE, "Repair will disable access to CU");
  1011. break;
  1012. default:
  1013. snprintf(service, BUFSIZE, "0x%02x",
  1014. sense->fmt.f71.smc);
  1015. }
  1016. dev_warn (&device->cdev->dev, "I/O subsystem information: exception"
  1017. " %s, service %s\n", exception, service);
  1018. out_nomem:
  1019. kfree(exception);
  1020. kfree(service);
  1021. }
  1022. /*
  1023. * Print an Device Subsystem Service Information Message (message code f2)
  1024. */
  1025. static void
  1026. tape_3590_print_dev_sim_msg_f2(struct tape_device *device, struct irb *irb)
  1027. {
  1028. struct tape_3590_sense *sense;
  1029. char *exception, *service;
  1030. exception = kmalloc(BUFSIZE, GFP_ATOMIC);
  1031. service = kmalloc(BUFSIZE, GFP_ATOMIC);
  1032. if (!exception || !service)
  1033. goto out_nomem;
  1034. sense = (struct tape_3590_sense *) irb->ecw;
  1035. /* Exception Message */
  1036. switch (sense->fmt.f71.emc) {
  1037. case 0x01:
  1038. snprintf(exception, BUFSIZE, "Effect of failure is unknown");
  1039. break;
  1040. case 0x02:
  1041. snprintf(exception, BUFSIZE, "DV Exception - no performance"
  1042. " impact");
  1043. break;
  1044. case 0x03:
  1045. snprintf(exception, BUFSIZE, "DV Exception on channel "
  1046. "interface 0x%02x", sense->fmt.f71.md[0]);
  1047. break;
  1048. case 0x04:
  1049. snprintf(exception, BUFSIZE, "DV Exception on loader 0x%02x",
  1050. sense->fmt.f71.md[0]);
  1051. break;
  1052. case 0x05:
  1053. snprintf(exception, BUFSIZE, "DV Exception on message display"
  1054. " 0x%02x", sense->fmt.f71.md[0]);
  1055. break;
  1056. case 0x06:
  1057. snprintf(exception, BUFSIZE, "DV Exception in tape path");
  1058. break;
  1059. case 0x07:
  1060. snprintf(exception, BUFSIZE, "DV Exception in drive");
  1061. break;
  1062. default:
  1063. snprintf(exception, BUFSIZE, "0x%02x",
  1064. sense->fmt.f71.emc);
  1065. }
  1066. /* Service Message */
  1067. switch (sense->fmt.f71.smc) {
  1068. case 0x01:
  1069. snprintf(service, BUFSIZE, "Repair impact is unknown");
  1070. break;
  1071. case 0x02:
  1072. snprintf(service, BUFSIZE, "Repair will not impact device "
  1073. "performance");
  1074. break;
  1075. case 0x03:
  1076. if (sense->fmt.f71.mdf == 0)
  1077. snprintf(service, BUFSIZE, "Repair will disable "
  1078. "channel path 0x%x on DV",
  1079. sense->fmt.f71.md[1]);
  1080. else
  1081. snprintf(service, BUFSIZE, "Repair will disable "
  1082. "channel path (0x%x-0x%x) on DV",
  1083. sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
  1084. break;
  1085. case 0x04:
  1086. if (sense->fmt.f71.mdf == 0)
  1087. snprintf(service, BUFSIZE, "Repair will disable "
  1088. "interface 0x%x on DV", sense->fmt.f71.md[1]);
  1089. else
  1090. snprintf(service, BUFSIZE, "Repair will disable "
  1091. "interfaces (0x%x-0x%x) on DV",
  1092. sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
  1093. break;
  1094. case 0x05:
  1095. if (sense->fmt.f71.mdf == 0)
  1096. snprintf(service, BUFSIZE, "Repair will disable loader"
  1097. " 0x%x on DV", sense->fmt.f71.md[1]);
  1098. else
  1099. snprintf(service, BUFSIZE, "Repair will disable loader"
  1100. " (0x%x-0x%x) on DV",
  1101. sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
  1102. break;
  1103. case 0x07:
  1104. snprintf(service, BUFSIZE, "Repair will disable access to DV");
  1105. break;
  1106. case 0x08:
  1107. if (sense->fmt.f71.mdf == 0)
  1108. snprintf(service, BUFSIZE, "Repair will disable "
  1109. "message display 0x%x on DV",
  1110. sense->fmt.f71.md[1]);
  1111. else
  1112. snprintf(service, BUFSIZE, "Repair will disable "
  1113. "message displays (0x%x-0x%x) on DV",
  1114. sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
  1115. break;
  1116. case 0x09:
  1117. snprintf(service, BUFSIZE, "Clean DV");
  1118. break;
  1119. default:
  1120. snprintf(service, BUFSIZE, "0x%02x",
  1121. sense->fmt.f71.smc);
  1122. }
  1123. dev_warn (&device->cdev->dev, "Device subsystem information: exception"
  1124. " %s, service %s\n", exception, service);
  1125. out_nomem:
  1126. kfree(exception);
  1127. kfree(service);
  1128. }
  1129. /*
  1130. * Print standard ERA Message
  1131. */
  1132. static void
  1133. tape_3590_print_era_msg(struct tape_device *device, struct irb *irb)
  1134. {
  1135. struct tape_3590_sense *sense;
  1136. sense = (struct tape_3590_sense *) irb->ecw;
  1137. if (sense->mc == 0)
  1138. return;
  1139. if ((sense->mc > 0) && (sense->mc < TAPE_3590_MAX_MSG)) {
  1140. if (tape_3590_msg[sense->mc] != NULL)
  1141. dev_warn (&device->cdev->dev, "The tape unit has "
  1142. "issued sense message %s\n",
  1143. tape_3590_msg[sense->mc]);
  1144. else
  1145. dev_warn (&device->cdev->dev, "The tape unit has "
  1146. "issued an unknown sense message code 0x%x\n",
  1147. sense->mc);
  1148. return;
  1149. }
  1150. if (sense->mc == 0xf0) {
  1151. /* Standard Media Information Message */
  1152. dev_warn (&device->cdev->dev, "MIM SEV=%i, MC=%02x, ES=%x/%x, "
  1153. "RC=%02x-%04x-%02x\n", sense->fmt.f70.sev, sense->mc,
  1154. sense->fmt.f70.emc, sense->fmt.f70.smc,
  1155. sense->fmt.f70.refcode, sense->fmt.f70.mid,
  1156. sense->fmt.f70.fid);
  1157. tape_3590_print_mim_msg_f0(device, irb);
  1158. return;
  1159. }
  1160. if (sense->mc == 0xf1) {
  1161. /* Standard I/O Subsystem Service Information Message */
  1162. dev_warn (&device->cdev->dev, "IOSIM SEV=%i, DEVTYPE=3590/%02x,"
  1163. " MC=%02x, ES=%x/%x, REF=0x%04x-0x%04x-0x%04x\n",
  1164. sense->fmt.f71.sev, device->cdev->id.dev_model,
  1165. sense->mc, sense->fmt.f71.emc, sense->fmt.f71.smc,
  1166. sense->fmt.f71.refcode1, sense->fmt.f71.refcode2,
  1167. sense->fmt.f71.refcode3);
  1168. tape_3590_print_io_sim_msg_f1(device, irb);
  1169. return;
  1170. }
  1171. if (sense->mc == 0xf2) {
  1172. /* Standard Device Service Information Message */
  1173. dev_warn (&device->cdev->dev, "DEVSIM SEV=%i, DEVTYPE=3590/%02x"
  1174. ", MC=%02x, ES=%x/%x, REF=0x%04x-0x%04x-0x%04x\n",
  1175. sense->fmt.f71.sev, device->cdev->id.dev_model,
  1176. sense->mc, sense->fmt.f71.emc, sense->fmt.f71.smc,
  1177. sense->fmt.f71.refcode1, sense->fmt.f71.refcode2,
  1178. sense->fmt.f71.refcode3);
  1179. tape_3590_print_dev_sim_msg_f2(device, irb);
  1180. return;
  1181. }
  1182. if (sense->mc == 0xf3) {
  1183. /* Standard Library Service Information Message */
  1184. return;
  1185. }
  1186. dev_warn (&device->cdev->dev, "The tape unit has issued an unknown "
  1187. "sense message code %x\n", sense->mc);
  1188. }
  1189. static int tape_3590_crypt_error(struct tape_device *device,
  1190. struct tape_request *request, struct irb *irb)
  1191. {
  1192. u8 cu_rc;
  1193. u16 ekm_rc2;
  1194. char *sense;
  1195. sense = ((struct tape_3590_sense *) irb->ecw)->fmt.data;
  1196. cu_rc = sense[0];
  1197. ekm_rc2 = *((u16*) &sense[10]);
  1198. if ((cu_rc == 0) && (ekm_rc2 == 0xee31))
  1199. /* key not defined on EKM */
  1200. return tape_3590_erp_basic(device, request, irb, -EKEYREJECTED);
  1201. if ((cu_rc == 1) || (cu_rc == 2))
  1202. /* No connection to EKM */
  1203. return tape_3590_erp_basic(device, request, irb, -ENOTCONN);
  1204. dev_err (&device->cdev->dev, "The tape unit failed to obtain the "
  1205. "encryption key from EKM\n");
  1206. return tape_3590_erp_basic(device, request, irb, -ENOKEY);
  1207. }
  1208. /*
  1209. * 3590 error Recovery routine:
  1210. * If possible, it tries to recover from the error. If this is not possible,
  1211. * inform the user about the problem.
  1212. */
  1213. static int
  1214. tape_3590_unit_check(struct tape_device *device, struct tape_request *request,
  1215. struct irb *irb)
  1216. {
  1217. struct tape_3590_sense *sense;
  1218. sense = (struct tape_3590_sense *) irb->ecw;
  1219. DBF_EVENT(6, "Unit Check: RQC = %x\n", sense->rc_rqc);
  1220. /*
  1221. * First check all RC-QRCs where we want to do something special
  1222. * - "break": basic error recovery is done
  1223. * - "goto out:": just print error message if available
  1224. */
  1225. switch (sense->rc_rqc) {
  1226. case 0x1110:
  1227. tape_3590_print_era_msg(device, irb);
  1228. return tape_3590_erp_read_buf_log(device, request, irb);
  1229. case 0x2011:
  1230. tape_3590_print_era_msg(device, irb);
  1231. return tape_3590_erp_read_alternate(device, request, irb);
  1232. case 0x2230:
  1233. case 0x2231:
  1234. tape_3590_print_era_msg(device, irb);
  1235. return tape_3590_erp_special_interrupt(device, request, irb);
  1236. case 0x2240:
  1237. return tape_3590_crypt_error(device, request, irb);
  1238. case 0x3010:
  1239. DBF_EVENT(2, "(%08x): Backward at Beginning of Partition\n",
  1240. device->cdev_id);
  1241. return tape_3590_erp_basic(device, request, irb, -ENOSPC);
  1242. case 0x3012:
  1243. DBF_EVENT(2, "(%08x): Forward at End of Partition\n",
  1244. device->cdev_id);
  1245. return tape_3590_erp_basic(device, request, irb, -ENOSPC);
  1246. case 0x3020:
  1247. DBF_EVENT(2, "(%08x): End of Data Mark\n", device->cdev_id);
  1248. return tape_3590_erp_basic(device, request, irb, -ENOSPC);
  1249. case 0x3122:
  1250. DBF_EVENT(2, "(%08x): Rewind Unload initiated\n",
  1251. device->cdev_id);
  1252. return tape_3590_erp_basic(device, request, irb, -EIO);
  1253. case 0x3123:
  1254. DBF_EVENT(2, "(%08x): Rewind Unload complete\n",
  1255. device->cdev_id);
  1256. tape_med_state_set(device, MS_UNLOADED);
  1257. tape_3590_schedule_work(device, TO_CRYPT_OFF);
  1258. return tape_3590_erp_basic(device, request, irb, 0);
  1259. case 0x4010:
  1260. /*
  1261. * print additional msg since default msg
  1262. * "device intervention" is not very meaningfull
  1263. */
  1264. tape_med_state_set(device, MS_UNLOADED);
  1265. tape_3590_schedule_work(device, TO_CRYPT_OFF);
  1266. return tape_3590_erp_basic(device, request, irb, -ENOMEDIUM);
  1267. case 0x4012: /* Device Long Busy */
  1268. /* XXX: Also use long busy handling here? */
  1269. DBF_EVENT(6, "(%08x): LONG BUSY\n", device->cdev_id);
  1270. tape_3590_print_era_msg(device, irb);
  1271. return tape_3590_erp_basic(device, request, irb, -EBUSY);
  1272. case 0x4014:
  1273. DBF_EVENT(6, "(%08x): Crypto LONG BUSY\n", device->cdev_id);
  1274. return tape_3590_erp_long_busy(device, request, irb);
  1275. case 0x5010:
  1276. if (sense->rac == 0xd0) {
  1277. /* Swap */
  1278. tape_3590_print_era_msg(device, irb);
  1279. return tape_3590_erp_swap(device, request, irb);
  1280. }
  1281. if (sense->rac == 0x26) {
  1282. /* Read Opposite */
  1283. tape_3590_print_era_msg(device, irb);
  1284. return tape_3590_erp_read_opposite(device, request,
  1285. irb);
  1286. }
  1287. return tape_3590_erp_basic(device, request, irb, -EIO);
  1288. case 0x5020:
  1289. case 0x5021:
  1290. case 0x5022:
  1291. case 0x5040:
  1292. case 0x5041:
  1293. case 0x5042:
  1294. tape_3590_print_era_msg(device, irb);
  1295. return tape_3590_erp_swap(device, request, irb);
  1296. case 0x5110:
  1297. case 0x5111:
  1298. return tape_3590_erp_basic(device, request, irb, -EMEDIUMTYPE);
  1299. case 0x5120:
  1300. case 0x1120:
  1301. tape_med_state_set(device, MS_UNLOADED);
  1302. tape_3590_schedule_work(device, TO_CRYPT_OFF);
  1303. return tape_3590_erp_basic(device, request, irb, -ENOMEDIUM);
  1304. case 0x6020:
  1305. return tape_3590_erp_basic(device, request, irb, -EMEDIUMTYPE);
  1306. case 0x8011:
  1307. return tape_3590_erp_basic(device, request, irb, -EPERM);
  1308. case 0x8013:
  1309. dev_warn (&device->cdev->dev, "A different host has privileged"
  1310. " access to the tape unit\n");
  1311. return tape_3590_erp_basic(device, request, irb, -EPERM);
  1312. default:
  1313. return tape_3590_erp_basic(device, request, irb, -EIO);
  1314. }
  1315. }
  1316. /*
  1317. * 3590 interrupt handler:
  1318. */
  1319. static int
  1320. tape_3590_irq(struct tape_device *device, struct tape_request *request,
  1321. struct irb *irb)
  1322. {
  1323. if (request == NULL)
  1324. return tape_3590_unsolicited_irq(device, irb);
  1325. if ((irb->scsw.cmd.dstat & DEV_STAT_UNIT_EXCEP) &&
  1326. (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) &&
  1327. (request->op == TO_WRI)) {
  1328. /* Write at end of volume */
  1329. DBF_EVENT(2, "End of volume\n");
  1330. return tape_3590_erp_failed(device, request, irb, -ENOSPC);
  1331. }
  1332. if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
  1333. return tape_3590_unit_check(device, request, irb);
  1334. if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
  1335. if (irb->scsw.cmd.dstat == DEV_STAT_UNIT_EXCEP) {
  1336. if (request->op == TO_FSB || request->op == TO_BSB)
  1337. request->rescnt++;
  1338. else
  1339. DBF_EVENT(5, "Unit Exception!\n");
  1340. }
  1341. return tape_3590_done(device, request);
  1342. }
  1343. if (irb->scsw.cmd.dstat & DEV_STAT_CHN_END) {
  1344. DBF_EVENT(2, "channel end\n");
  1345. return TAPE_IO_PENDING;
  1346. }
  1347. if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
  1348. DBF_EVENT(2, "Unit Attention when busy..\n");
  1349. return TAPE_IO_PENDING;
  1350. }
  1351. DBF_EVENT(6, "xunknownirq\n");
  1352. tape_dump_sense_dbf(device, request, irb);
  1353. return TAPE_IO_STOP;
  1354. }
  1355. static int tape_3590_read_dev_chars(struct tape_device *device,
  1356. struct tape_3590_rdc_data *rdc_data)
  1357. {
  1358. int rc;
  1359. struct tape_request *request;
  1360. request = tape_alloc_request(1, sizeof(*rdc_data));
  1361. if (IS_ERR(request))
  1362. return PTR_ERR(request);
  1363. request->op = TO_RDC;
  1364. tape_ccw_end(request->cpaddr, CCW_CMD_RDC, sizeof(*rdc_data),
  1365. request->cpdata);
  1366. rc = tape_do_io(device, request);
  1367. if (rc == 0)
  1368. memcpy(rdc_data, request->cpdata, sizeof(*rdc_data));
  1369. tape_free_request(request);
  1370. return rc;
  1371. }
  1372. /*
  1373. * Setup device function
  1374. */
  1375. static int
  1376. tape_3590_setup_device(struct tape_device *device)
  1377. {
  1378. int rc;
  1379. struct tape_3590_disc_data *data;
  1380. struct tape_3590_rdc_data *rdc_data;
  1381. DBF_EVENT(6, "3590 device setup\n");
  1382. data = kzalloc(sizeof(struct tape_3590_disc_data), GFP_KERNEL | GFP_DMA);
  1383. if (data == NULL)
  1384. return -ENOMEM;
  1385. data->read_back_op = READ_PREVIOUS;
  1386. device->discdata = data;
  1387. rdc_data = kmalloc(sizeof(*rdc_data), GFP_KERNEL | GFP_DMA);
  1388. if (!rdc_data) {
  1389. rc = -ENOMEM;
  1390. goto fail_kmalloc;
  1391. }
  1392. rc = tape_3590_read_dev_chars(device, rdc_data);
  1393. if (rc) {
  1394. DBF_LH(3, "Read device characteristics failed!\n");
  1395. goto fail_rdc_data;
  1396. }
  1397. rc = tape_std_assign(device);
  1398. if (rc)
  1399. goto fail_rdc_data;
  1400. if (rdc_data->data[31] == 0x13) {
  1401. data->crypt_info.capability |= TAPE390_CRYPT_SUPPORTED_MASK;
  1402. tape_3592_disable_crypt(device);
  1403. } else {
  1404. DBF_EVENT(6, "Device has NO crypto support\n");
  1405. }
  1406. /* Try to find out if medium is loaded */
  1407. rc = tape_3590_sense_medium(device);
  1408. if (rc) {
  1409. DBF_LH(3, "3590 medium sense returned %d\n", rc);
  1410. goto fail_rdc_data;
  1411. }
  1412. return 0;
  1413. fail_rdc_data:
  1414. kfree(rdc_data);
  1415. fail_kmalloc:
  1416. kfree(data);
  1417. return rc;
  1418. }
  1419. /*
  1420. * Cleanup device function
  1421. */
  1422. static void
  1423. tape_3590_cleanup_device(struct tape_device *device)
  1424. {
  1425. flush_workqueue(tape_3590_wq);
  1426. tape_std_unassign(device);
  1427. kfree(device->discdata);
  1428. device->discdata = NULL;
  1429. }
  1430. /*
  1431. * List of 3590 magnetic tape commands.
  1432. */
  1433. static tape_mtop_fn tape_3590_mtop[TAPE_NR_MTOPS] = {
  1434. [MTRESET] = tape_std_mtreset,
  1435. [MTFSF] = tape_std_mtfsf,
  1436. [MTBSF] = tape_std_mtbsf,
  1437. [MTFSR] = tape_std_mtfsr,
  1438. [MTBSR] = tape_std_mtbsr,
  1439. [MTWEOF] = tape_std_mtweof,
  1440. [MTREW] = tape_std_mtrew,
  1441. [MTOFFL] = tape_std_mtoffl,
  1442. [MTNOP] = tape_std_mtnop,
  1443. [MTRETEN] = tape_std_mtreten,
  1444. [MTBSFM] = tape_std_mtbsfm,
  1445. [MTFSFM] = tape_std_mtfsfm,
  1446. [MTEOM] = tape_std_mteom,
  1447. [MTERASE] = tape_std_mterase,
  1448. [MTRAS1] = NULL,
  1449. [MTRAS2] = NULL,
  1450. [MTRAS3] = NULL,
  1451. [MTSETBLK] = tape_std_mtsetblk,
  1452. [MTSETDENSITY] = NULL,
  1453. [MTSEEK] = tape_3590_mtseek,
  1454. [MTTELL] = tape_3590_mttell,
  1455. [MTSETDRVBUFFER] = NULL,
  1456. [MTFSS] = NULL,
  1457. [MTBSS] = NULL,
  1458. [MTWSM] = NULL,
  1459. [MTLOCK] = NULL,
  1460. [MTUNLOCK] = NULL,
  1461. [MTLOAD] = tape_std_mtload,
  1462. [MTUNLOAD] = tape_std_mtunload,
  1463. [MTCOMPRESSION] = tape_std_mtcompression,
  1464. [MTSETPART] = NULL,
  1465. [MTMKPART] = NULL
  1466. };
  1467. /*
  1468. * Tape discipline structure for 3590.
  1469. */
  1470. static struct tape_discipline tape_discipline_3590 = {
  1471. .owner = THIS_MODULE,
  1472. .setup_device = tape_3590_setup_device,
  1473. .cleanup_device = tape_3590_cleanup_device,
  1474. .process_eov = tape_std_process_eov,
  1475. .irq = tape_3590_irq,
  1476. .read_block = tape_std_read_block,
  1477. .write_block = tape_std_write_block,
  1478. .ioctl_fn = tape_3590_ioctl,
  1479. .mtop_array = tape_3590_mtop
  1480. };
  1481. static struct ccw_device_id tape_3590_ids[] = {
  1482. {CCW_DEVICE_DEVTYPE(0x3590, 0, 0x3590, 0), .driver_info = tape_3590},
  1483. {CCW_DEVICE_DEVTYPE(0x3592, 0, 0x3592, 0), .driver_info = tape_3592},
  1484. { /* end of list */ }
  1485. };
  1486. static int
  1487. tape_3590_online(struct ccw_device *cdev)
  1488. {
  1489. return tape_generic_online(dev_get_drvdata(&cdev->dev),
  1490. &tape_discipline_3590);
  1491. }
  1492. static struct ccw_driver tape_3590_driver = {
  1493. .driver = {
  1494. .name = "tape_3590",
  1495. .owner = THIS_MODULE,
  1496. },
  1497. .ids = tape_3590_ids,
  1498. .probe = tape_generic_probe,
  1499. .remove = tape_generic_remove,
  1500. .set_offline = tape_generic_offline,
  1501. .set_online = tape_3590_online,
  1502. .freeze = tape_generic_pm_suspend,
  1503. .int_class = IRQIO_TAP,
  1504. };
  1505. /*
  1506. * Setup discipline structure.
  1507. */
  1508. static int
  1509. tape_3590_init(void)
  1510. {
  1511. int rc;
  1512. TAPE_DBF_AREA = debug_register("tape_3590", 2, 2, 4 * sizeof(long));
  1513. debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
  1514. #ifdef DBF_LIKE_HELL
  1515. debug_set_level(TAPE_DBF_AREA, 6);
  1516. #endif
  1517. DBF_EVENT(3, "3590 init\n");
  1518. tape_3590_wq = alloc_workqueue("tape_3590", 0, 0);
  1519. if (!tape_3590_wq)
  1520. return -ENOMEM;
  1521. /* Register driver for 3590 tapes. */
  1522. rc = ccw_driver_register(&tape_3590_driver);
  1523. if (rc) {
  1524. destroy_workqueue(tape_3590_wq);
  1525. DBF_EVENT(3, "3590 init failed\n");
  1526. } else
  1527. DBF_EVENT(3, "3590 registered\n");
  1528. return rc;
  1529. }
  1530. static void
  1531. tape_3590_exit(void)
  1532. {
  1533. ccw_driver_unregister(&tape_3590_driver);
  1534. destroy_workqueue(tape_3590_wq);
  1535. debug_unregister(TAPE_DBF_AREA);
  1536. }
  1537. MODULE_DEVICE_TABLE(ccw, tape_3590_ids);
  1538. MODULE_AUTHOR("(C) 2001,2006 IBM Corporation");
  1539. MODULE_DESCRIPTION("Linux on zSeries channel attached 3590 tape device driver");
  1540. MODULE_LICENSE("GPL");
  1541. module_init(tape_3590_init);
  1542. module_exit(tape_3590_exit);