tape_3590.c 44 KB

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