sed-opal.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467
  1. /*
  2. * Copyright © 2016 Intel Corporation
  3. *
  4. * Authors:
  5. * Scott Bauer <scott.bauer@intel.com>
  6. * Rafael Antognolli <rafael.antognolli@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ":OPAL: " fmt
  18. #include <linux/delay.h>
  19. #include <linux/device.h>
  20. #include <linux/kernel.h>
  21. #include <linux/list.h>
  22. #include <linux/genhd.h>
  23. #include <linux/slab.h>
  24. #include <linux/uaccess.h>
  25. #include <uapi/linux/sed-opal.h>
  26. #include <linux/sed-opal.h>
  27. #include <linux/string.h>
  28. #include <linux/kdev_t.h>
  29. #include "opal_proto.h"
  30. #define IO_BUFFER_LENGTH 2048
  31. #define MAX_TOKS 64
  32. struct opal_step {
  33. int (*fn)(struct opal_dev *dev, void *data);
  34. void *data;
  35. };
  36. typedef int (cont_fn)(struct opal_dev *dev);
  37. enum opal_atom_width {
  38. OPAL_WIDTH_TINY,
  39. OPAL_WIDTH_SHORT,
  40. OPAL_WIDTH_MEDIUM,
  41. OPAL_WIDTH_LONG,
  42. OPAL_WIDTH_TOKEN
  43. };
  44. /*
  45. * On the parsed response, we don't store again the toks that are already
  46. * stored in the response buffer. Instead, for each token, we just store a
  47. * pointer to the position in the buffer where the token starts, and the size
  48. * of the token in bytes.
  49. */
  50. struct opal_resp_tok {
  51. const u8 *pos;
  52. size_t len;
  53. enum opal_response_token type;
  54. enum opal_atom_width width;
  55. union {
  56. u64 u;
  57. s64 s;
  58. } stored;
  59. };
  60. /*
  61. * From the response header it's not possible to know how many tokens there are
  62. * on the payload. So we hardcode that the maximum will be MAX_TOKS, and later
  63. * if we start dealing with messages that have more than that, we can increase
  64. * this number. This is done to avoid having to make two passes through the
  65. * response, the first one counting how many tokens we have and the second one
  66. * actually storing the positions.
  67. */
  68. struct parsed_resp {
  69. int num;
  70. struct opal_resp_tok toks[MAX_TOKS];
  71. };
  72. struct opal_dev {
  73. bool supported;
  74. bool mbr_enabled;
  75. void *data;
  76. sec_send_recv *send_recv;
  77. const struct opal_step *steps;
  78. struct mutex dev_lock;
  79. u16 comid;
  80. u32 hsn;
  81. u32 tsn;
  82. u64 align;
  83. u64 lowest_lba;
  84. size_t pos;
  85. u8 cmd[IO_BUFFER_LENGTH];
  86. u8 resp[IO_BUFFER_LENGTH];
  87. struct parsed_resp parsed;
  88. size_t prev_d_len;
  89. void *prev_data;
  90. struct list_head unlk_lst;
  91. };
  92. static const u8 opaluid[][OPAL_UID_LENGTH] = {
  93. /* users */
  94. [OPAL_SMUID_UID] =
  95. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff },
  96. [OPAL_THISSP_UID] =
  97. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
  98. [OPAL_ADMINSP_UID] =
  99. { 0x00, 0x00, 0x02, 0x05, 0x00, 0x00, 0x00, 0x01 },
  100. [OPAL_LOCKINGSP_UID] =
  101. { 0x00, 0x00, 0x02, 0x05, 0x00, 0x00, 0x00, 0x02 },
  102. [OPAL_ENTERPRISE_LOCKINGSP_UID] =
  103. { 0x00, 0x00, 0x02, 0x05, 0x00, 0x01, 0x00, 0x01 },
  104. [OPAL_ANYBODY_UID] =
  105. { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x01 },
  106. [OPAL_SID_UID] =
  107. { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x06 },
  108. [OPAL_ADMIN1_UID] =
  109. { 0x00, 0x00, 0x00, 0x09, 0x00, 0x01, 0x00, 0x01 },
  110. [OPAL_USER1_UID] =
  111. { 0x00, 0x00, 0x00, 0x09, 0x00, 0x03, 0x00, 0x01 },
  112. [OPAL_USER2_UID] =
  113. { 0x00, 0x00, 0x00, 0x09, 0x00, 0x03, 0x00, 0x02 },
  114. [OPAL_PSID_UID] =
  115. { 0x00, 0x00, 0x00, 0x09, 0x00, 0x01, 0xff, 0x01 },
  116. [OPAL_ENTERPRISE_BANDMASTER0_UID] =
  117. { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x80, 0x01 },
  118. [OPAL_ENTERPRISE_ERASEMASTER_UID] =
  119. { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x84, 0x01 },
  120. /* tables */
  121. [OPAL_LOCKINGRANGE_GLOBAL] =
  122. { 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00, 0x01 },
  123. [OPAL_LOCKINGRANGE_ACE_RDLOCKED] =
  124. { 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0xE0, 0x01 },
  125. [OPAL_LOCKINGRANGE_ACE_WRLOCKED] =
  126. { 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0xE8, 0x01 },
  127. [OPAL_MBRCONTROL] =
  128. { 0x00, 0x00, 0x08, 0x03, 0x00, 0x00, 0x00, 0x01 },
  129. [OPAL_MBR] =
  130. { 0x00, 0x00, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00 },
  131. [OPAL_AUTHORITY_TABLE] =
  132. { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00},
  133. [OPAL_C_PIN_TABLE] =
  134. { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x00},
  135. [OPAL_LOCKING_INFO_TABLE] =
  136. { 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x01 },
  137. [OPAL_ENTERPRISE_LOCKING_INFO_TABLE] =
  138. { 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00 },
  139. /* C_PIN_TABLE object ID's */
  140. [OPAL_C_PIN_MSID] =
  141. { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x84, 0x02},
  142. [OPAL_C_PIN_SID] =
  143. { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x01},
  144. [OPAL_C_PIN_ADMIN1] =
  145. { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x01, 0x00, 0x01},
  146. /* half UID's (only first 4 bytes used) */
  147. [OPAL_HALF_UID_AUTHORITY_OBJ_REF] =
  148. { 0x00, 0x00, 0x0C, 0x05, 0xff, 0xff, 0xff, 0xff },
  149. [OPAL_HALF_UID_BOOLEAN_ACE] =
  150. { 0x00, 0x00, 0x04, 0x0E, 0xff, 0xff, 0xff, 0xff },
  151. /* special value for omitted optional parameter */
  152. [OPAL_UID_HEXFF] =
  153. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  154. };
  155. /*
  156. * TCG Storage SSC Methods.
  157. * Derived from: TCG_Storage_Architecture_Core_Spec_v2.01_r1.00
  158. * Section: 6.3 Assigned UIDs
  159. */
  160. static const u8 opalmethod[][OPAL_UID_LENGTH] = {
  161. [OPAL_PROPERTIES] =
  162. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x01 },
  163. [OPAL_STARTSESSION] =
  164. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x02 },
  165. [OPAL_REVERT] =
  166. { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x02, 0x02 },
  167. [OPAL_ACTIVATE] =
  168. { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x02, 0x03 },
  169. [OPAL_EGET] =
  170. { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x06 },
  171. [OPAL_ESET] =
  172. { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x07 },
  173. [OPAL_NEXT] =
  174. { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x08 },
  175. [OPAL_EAUTHENTICATE] =
  176. { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0c },
  177. [OPAL_GETACL] =
  178. { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0d },
  179. [OPAL_GENKEY] =
  180. { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x10 },
  181. [OPAL_REVERTSP] =
  182. { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x11 },
  183. [OPAL_GET] =
  184. { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x16 },
  185. [OPAL_SET] =
  186. { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x17 },
  187. [OPAL_AUTHENTICATE] =
  188. { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x1c },
  189. [OPAL_RANDOM] =
  190. { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x01 },
  191. [OPAL_ERASE] =
  192. { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x08, 0x03 },
  193. };
  194. static int end_opal_session_error(struct opal_dev *dev);
  195. struct opal_suspend_data {
  196. struct opal_lock_unlock unlk;
  197. u8 lr;
  198. struct list_head node;
  199. };
  200. /*
  201. * Derived from:
  202. * TCG_Storage_Architecture_Core_Spec_v2.01_r1.00
  203. * Section: 5.1.5 Method Status Codes
  204. */
  205. static const char * const opal_errors[] = {
  206. "Success",
  207. "Not Authorized",
  208. "Unknown Error",
  209. "SP Busy",
  210. "SP Failed",
  211. "SP Disabled",
  212. "SP Frozen",
  213. "No Sessions Available",
  214. "Uniqueness Conflict",
  215. "Insufficient Space",
  216. "Insufficient Rows",
  217. "Invalid Function",
  218. "Invalid Parameter",
  219. "Invalid Reference",
  220. "Unknown Error",
  221. "TPER Malfunction",
  222. "Transaction Failure",
  223. "Response Overflow",
  224. "Authority Locked Out",
  225. };
  226. static const char *opal_error_to_human(int error)
  227. {
  228. if (error == 0x3f)
  229. return "Failed";
  230. if (error >= ARRAY_SIZE(opal_errors) || error < 0)
  231. return "Unknown Error";
  232. return opal_errors[error];
  233. }
  234. static void print_buffer(const u8 *ptr, u32 length)
  235. {
  236. #ifdef DEBUG
  237. print_hex_dump_bytes("OPAL: ", DUMP_PREFIX_OFFSET, ptr, length);
  238. pr_debug("\n");
  239. #endif
  240. }
  241. static bool check_tper(const void *data)
  242. {
  243. const struct d0_tper_features *tper = data;
  244. u8 flags = tper->supported_features;
  245. if (!(flags & TPER_SYNC_SUPPORTED)) {
  246. pr_debug("TPer sync not supported. flags = %d\n",
  247. tper->supported_features);
  248. return false;
  249. }
  250. return true;
  251. }
  252. static bool check_mbrenabled(const void *data)
  253. {
  254. const struct d0_locking_features *lfeat = data;
  255. u8 sup_feat = lfeat->supported_features;
  256. return !!(sup_feat & MBR_ENABLED_MASK);
  257. }
  258. static bool check_sum(const void *data)
  259. {
  260. const struct d0_single_user_mode *sum = data;
  261. u32 nlo = be32_to_cpu(sum->num_locking_objects);
  262. if (nlo == 0) {
  263. pr_debug("Need at least one locking object.\n");
  264. return false;
  265. }
  266. pr_debug("Number of locking objects: %d\n", nlo);
  267. return true;
  268. }
  269. static u16 get_comid_v100(const void *data)
  270. {
  271. const struct d0_opal_v100 *v100 = data;
  272. return be16_to_cpu(v100->baseComID);
  273. }
  274. static u16 get_comid_v200(const void *data)
  275. {
  276. const struct d0_opal_v200 *v200 = data;
  277. return be16_to_cpu(v200->baseComID);
  278. }
  279. static int opal_send_cmd(struct opal_dev *dev)
  280. {
  281. return dev->send_recv(dev->data, dev->comid, TCG_SECP_01,
  282. dev->cmd, IO_BUFFER_LENGTH,
  283. true);
  284. }
  285. static int opal_recv_cmd(struct opal_dev *dev)
  286. {
  287. return dev->send_recv(dev->data, dev->comid, TCG_SECP_01,
  288. dev->resp, IO_BUFFER_LENGTH,
  289. false);
  290. }
  291. static int opal_recv_check(struct opal_dev *dev)
  292. {
  293. size_t buflen = IO_BUFFER_LENGTH;
  294. void *buffer = dev->resp;
  295. struct opal_header *hdr = buffer;
  296. int ret;
  297. do {
  298. pr_debug("Sent OPAL command: outstanding=%d, minTransfer=%d\n",
  299. hdr->cp.outstandingData,
  300. hdr->cp.minTransfer);
  301. if (hdr->cp.outstandingData == 0 ||
  302. hdr->cp.minTransfer != 0)
  303. return 0;
  304. memset(buffer, 0, buflen);
  305. ret = opal_recv_cmd(dev);
  306. } while (!ret);
  307. return ret;
  308. }
  309. static int opal_send_recv(struct opal_dev *dev, cont_fn *cont)
  310. {
  311. int ret;
  312. ret = opal_send_cmd(dev);
  313. if (ret)
  314. return ret;
  315. ret = opal_recv_cmd(dev);
  316. if (ret)
  317. return ret;
  318. ret = opal_recv_check(dev);
  319. if (ret)
  320. return ret;
  321. return cont(dev);
  322. }
  323. static void check_geometry(struct opal_dev *dev, const void *data)
  324. {
  325. const struct d0_geometry_features *geo = data;
  326. dev->align = geo->alignment_granularity;
  327. dev->lowest_lba = geo->lowest_aligned_lba;
  328. }
  329. static int next(struct opal_dev *dev)
  330. {
  331. const struct opal_step *step;
  332. int state = 0, error = 0;
  333. do {
  334. step = &dev->steps[state];
  335. if (!step->fn)
  336. break;
  337. error = step->fn(dev, step->data);
  338. if (error) {
  339. pr_debug("Error on step function: %d with error %d: %s\n",
  340. state, error,
  341. opal_error_to_human(error));
  342. /* For each OPAL command we do a discovery0 then we
  343. * start some sort of session.
  344. * If we haven't passed state 1 then there was an error
  345. * on discovery0 or during the attempt to start a
  346. * session. Therefore we shouldn't attempt to terminate
  347. * a session, as one has not yet been created.
  348. */
  349. if (state > 1) {
  350. end_opal_session_error(dev);
  351. return error;
  352. }
  353. }
  354. state++;
  355. } while (!error);
  356. return error;
  357. }
  358. static int opal_discovery0_end(struct opal_dev *dev)
  359. {
  360. bool found_com_id = false, supported = true, single_user = false;
  361. const struct d0_header *hdr = (struct d0_header *)dev->resp;
  362. const u8 *epos = dev->resp, *cpos = dev->resp;
  363. u16 comid = 0;
  364. u32 hlen = be32_to_cpu(hdr->length);
  365. print_buffer(dev->resp, hlen);
  366. dev->mbr_enabled = false;
  367. if (hlen > IO_BUFFER_LENGTH - sizeof(*hdr)) {
  368. pr_debug("Discovery length overflows buffer (%zu+%u)/%u\n",
  369. sizeof(*hdr), hlen, IO_BUFFER_LENGTH);
  370. return -EFAULT;
  371. }
  372. epos += hlen; /* end of buffer */
  373. cpos += sizeof(*hdr); /* current position on buffer */
  374. while (cpos < epos && supported) {
  375. const struct d0_features *body =
  376. (const struct d0_features *)cpos;
  377. switch (be16_to_cpu(body->code)) {
  378. case FC_TPER:
  379. supported = check_tper(body->features);
  380. break;
  381. case FC_SINGLEUSER:
  382. single_user = check_sum(body->features);
  383. break;
  384. case FC_GEOMETRY:
  385. check_geometry(dev, body);
  386. break;
  387. case FC_LOCKING:
  388. dev->mbr_enabled = check_mbrenabled(body->features);
  389. break;
  390. case FC_ENTERPRISE:
  391. case FC_DATASTORE:
  392. /* some ignored properties */
  393. pr_debug("Found OPAL feature description: %d\n",
  394. be16_to_cpu(body->code));
  395. break;
  396. case FC_OPALV100:
  397. comid = get_comid_v100(body->features);
  398. found_com_id = true;
  399. break;
  400. case FC_OPALV200:
  401. comid = get_comid_v200(body->features);
  402. found_com_id = true;
  403. break;
  404. case 0xbfff ... 0xffff:
  405. /* vendor specific, just ignore */
  406. break;
  407. default:
  408. pr_debug("OPAL Unknown feature: %d\n",
  409. be16_to_cpu(body->code));
  410. }
  411. cpos += body->length + 4;
  412. }
  413. if (!supported) {
  414. pr_debug("This device is not Opal enabled. Not Supported!\n");
  415. return -EOPNOTSUPP;
  416. }
  417. if (!single_user)
  418. pr_debug("Device doesn't support single user mode\n");
  419. if (!found_com_id) {
  420. pr_debug("Could not find OPAL comid for device. Returning early\n");
  421. return -EOPNOTSUPP;
  422. }
  423. dev->comid = comid;
  424. return 0;
  425. }
  426. static int opal_discovery0(struct opal_dev *dev, void *data)
  427. {
  428. int ret;
  429. memset(dev->resp, 0, IO_BUFFER_LENGTH);
  430. dev->comid = OPAL_DISCOVERY_COMID;
  431. ret = opal_recv_cmd(dev);
  432. if (ret)
  433. return ret;
  434. return opal_discovery0_end(dev);
  435. }
  436. static void add_token_u8(int *err, struct opal_dev *cmd, u8 tok)
  437. {
  438. if (*err)
  439. return;
  440. if (cmd->pos >= IO_BUFFER_LENGTH - 1) {
  441. pr_debug("Error adding u8: end of buffer.\n");
  442. *err = -ERANGE;
  443. return;
  444. }
  445. cmd->cmd[cmd->pos++] = tok;
  446. }
  447. static void add_short_atom_header(struct opal_dev *cmd, bool bytestring,
  448. bool has_sign, int len)
  449. {
  450. u8 atom;
  451. int err = 0;
  452. atom = SHORT_ATOM_ID;
  453. atom |= bytestring ? SHORT_ATOM_BYTESTRING : 0;
  454. atom |= has_sign ? SHORT_ATOM_SIGNED : 0;
  455. atom |= len & SHORT_ATOM_LEN_MASK;
  456. add_token_u8(&err, cmd, atom);
  457. }
  458. static void add_medium_atom_header(struct opal_dev *cmd, bool bytestring,
  459. bool has_sign, int len)
  460. {
  461. u8 header0;
  462. header0 = MEDIUM_ATOM_ID;
  463. header0 |= bytestring ? MEDIUM_ATOM_BYTESTRING : 0;
  464. header0 |= has_sign ? MEDIUM_ATOM_SIGNED : 0;
  465. header0 |= (len >> 8) & MEDIUM_ATOM_LEN_MASK;
  466. cmd->cmd[cmd->pos++] = header0;
  467. cmd->cmd[cmd->pos++] = len;
  468. }
  469. static void add_token_u64(int *err, struct opal_dev *cmd, u64 number)
  470. {
  471. size_t len;
  472. int msb;
  473. if (!(number & ~TINY_ATOM_DATA_MASK)) {
  474. add_token_u8(err, cmd, number);
  475. return;
  476. }
  477. msb = fls64(number);
  478. len = DIV_ROUND_UP(msb, 8);
  479. if (cmd->pos >= IO_BUFFER_LENGTH - len - 1) {
  480. pr_debug("Error adding u64: end of buffer.\n");
  481. *err = -ERANGE;
  482. return;
  483. }
  484. add_short_atom_header(cmd, false, false, len);
  485. while (len--)
  486. add_token_u8(err, cmd, number >> (len * 8));
  487. }
  488. static void add_token_bytestring(int *err, struct opal_dev *cmd,
  489. const u8 *bytestring, size_t len)
  490. {
  491. size_t header_len = 1;
  492. bool is_short_atom = true;
  493. if (*err)
  494. return;
  495. if (len & ~SHORT_ATOM_LEN_MASK) {
  496. header_len = 2;
  497. is_short_atom = false;
  498. }
  499. if (len >= IO_BUFFER_LENGTH - cmd->pos - header_len) {
  500. pr_debug("Error adding bytestring: end of buffer.\n");
  501. *err = -ERANGE;
  502. return;
  503. }
  504. if (is_short_atom)
  505. add_short_atom_header(cmd, true, false, len);
  506. else
  507. add_medium_atom_header(cmd, true, false, len);
  508. memcpy(&cmd->cmd[cmd->pos], bytestring, len);
  509. cmd->pos += len;
  510. }
  511. static int build_locking_range(u8 *buffer, size_t length, u8 lr)
  512. {
  513. if (length > OPAL_UID_LENGTH) {
  514. pr_debug("Can't build locking range. Length OOB\n");
  515. return -ERANGE;
  516. }
  517. memcpy(buffer, opaluid[OPAL_LOCKINGRANGE_GLOBAL], OPAL_UID_LENGTH);
  518. if (lr == 0)
  519. return 0;
  520. buffer[5] = LOCKING_RANGE_NON_GLOBAL;
  521. buffer[7] = lr;
  522. return 0;
  523. }
  524. static int build_locking_user(u8 *buffer, size_t length, u8 lr)
  525. {
  526. if (length > OPAL_UID_LENGTH) {
  527. pr_debug("Can't build locking range user, Length OOB\n");
  528. return -ERANGE;
  529. }
  530. memcpy(buffer, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
  531. buffer[7] = lr + 1;
  532. return 0;
  533. }
  534. static void set_comid(struct opal_dev *cmd, u16 comid)
  535. {
  536. struct opal_header *hdr = (struct opal_header *)cmd->cmd;
  537. hdr->cp.extendedComID[0] = comid >> 8;
  538. hdr->cp.extendedComID[1] = comid;
  539. hdr->cp.extendedComID[2] = 0;
  540. hdr->cp.extendedComID[3] = 0;
  541. }
  542. static int cmd_finalize(struct opal_dev *cmd, u32 hsn, u32 tsn)
  543. {
  544. struct opal_header *hdr;
  545. int err = 0;
  546. add_token_u8(&err, cmd, OPAL_ENDOFDATA);
  547. add_token_u8(&err, cmd, OPAL_STARTLIST);
  548. add_token_u8(&err, cmd, 0);
  549. add_token_u8(&err, cmd, 0);
  550. add_token_u8(&err, cmd, 0);
  551. add_token_u8(&err, cmd, OPAL_ENDLIST);
  552. if (err) {
  553. pr_debug("Error finalizing command.\n");
  554. return -EFAULT;
  555. }
  556. hdr = (struct opal_header *) cmd->cmd;
  557. hdr->pkt.tsn = cpu_to_be32(tsn);
  558. hdr->pkt.hsn = cpu_to_be32(hsn);
  559. hdr->subpkt.length = cpu_to_be32(cmd->pos - sizeof(*hdr));
  560. while (cmd->pos % 4) {
  561. if (cmd->pos >= IO_BUFFER_LENGTH) {
  562. pr_debug("Error: Buffer overrun\n");
  563. return -ERANGE;
  564. }
  565. cmd->cmd[cmd->pos++] = 0;
  566. }
  567. hdr->pkt.length = cpu_to_be32(cmd->pos - sizeof(hdr->cp) -
  568. sizeof(hdr->pkt));
  569. hdr->cp.length = cpu_to_be32(cmd->pos - sizeof(hdr->cp));
  570. return 0;
  571. }
  572. static const struct opal_resp_tok *response_get_token(
  573. const struct parsed_resp *resp,
  574. int n)
  575. {
  576. const struct opal_resp_tok *tok;
  577. if (n >= resp->num) {
  578. pr_debug("Token number doesn't exist: %d, resp: %d\n",
  579. n, resp->num);
  580. return ERR_PTR(-EINVAL);
  581. }
  582. tok = &resp->toks[n];
  583. if (tok->len == 0) {
  584. pr_debug("Token length must be non-zero\n");
  585. return ERR_PTR(-EINVAL);
  586. }
  587. return tok;
  588. }
  589. static ssize_t response_parse_tiny(struct opal_resp_tok *tok,
  590. const u8 *pos)
  591. {
  592. tok->pos = pos;
  593. tok->len = 1;
  594. tok->width = OPAL_WIDTH_TINY;
  595. if (pos[0] & TINY_ATOM_SIGNED) {
  596. tok->type = OPAL_DTA_TOKENID_SINT;
  597. } else {
  598. tok->type = OPAL_DTA_TOKENID_UINT;
  599. tok->stored.u = pos[0] & 0x3f;
  600. }
  601. return tok->len;
  602. }
  603. static ssize_t response_parse_short(struct opal_resp_tok *tok,
  604. const u8 *pos)
  605. {
  606. tok->pos = pos;
  607. tok->len = (pos[0] & SHORT_ATOM_LEN_MASK) + 1;
  608. tok->width = OPAL_WIDTH_SHORT;
  609. if (pos[0] & SHORT_ATOM_BYTESTRING) {
  610. tok->type = OPAL_DTA_TOKENID_BYTESTRING;
  611. } else if (pos[0] & SHORT_ATOM_SIGNED) {
  612. tok->type = OPAL_DTA_TOKENID_SINT;
  613. } else {
  614. u64 u_integer = 0;
  615. ssize_t i, b = 0;
  616. tok->type = OPAL_DTA_TOKENID_UINT;
  617. if (tok->len > 9) {
  618. pr_debug("uint64 with more than 8 bytes\n");
  619. return -EINVAL;
  620. }
  621. for (i = tok->len - 1; i > 0; i--) {
  622. u_integer |= ((u64)pos[i] << (8 * b));
  623. b++;
  624. }
  625. tok->stored.u = u_integer;
  626. }
  627. return tok->len;
  628. }
  629. static ssize_t response_parse_medium(struct opal_resp_tok *tok,
  630. const u8 *pos)
  631. {
  632. tok->pos = pos;
  633. tok->len = (((pos[0] & MEDIUM_ATOM_LEN_MASK) << 8) | pos[1]) + 2;
  634. tok->width = OPAL_WIDTH_MEDIUM;
  635. if (pos[0] & MEDIUM_ATOM_BYTESTRING)
  636. tok->type = OPAL_DTA_TOKENID_BYTESTRING;
  637. else if (pos[0] & MEDIUM_ATOM_SIGNED)
  638. tok->type = OPAL_DTA_TOKENID_SINT;
  639. else
  640. tok->type = OPAL_DTA_TOKENID_UINT;
  641. return tok->len;
  642. }
  643. static ssize_t response_parse_long(struct opal_resp_tok *tok,
  644. const u8 *pos)
  645. {
  646. tok->pos = pos;
  647. tok->len = ((pos[1] << 16) | (pos[2] << 8) | pos[3]) + 4;
  648. tok->width = OPAL_WIDTH_LONG;
  649. if (pos[0] & LONG_ATOM_BYTESTRING)
  650. tok->type = OPAL_DTA_TOKENID_BYTESTRING;
  651. else if (pos[0] & LONG_ATOM_SIGNED)
  652. tok->type = OPAL_DTA_TOKENID_SINT;
  653. else
  654. tok->type = OPAL_DTA_TOKENID_UINT;
  655. return tok->len;
  656. }
  657. static ssize_t response_parse_token(struct opal_resp_tok *tok,
  658. const u8 *pos)
  659. {
  660. tok->pos = pos;
  661. tok->len = 1;
  662. tok->type = OPAL_DTA_TOKENID_TOKEN;
  663. tok->width = OPAL_WIDTH_TOKEN;
  664. return tok->len;
  665. }
  666. static int response_parse(const u8 *buf, size_t length,
  667. struct parsed_resp *resp)
  668. {
  669. const struct opal_header *hdr;
  670. struct opal_resp_tok *iter;
  671. int num_entries = 0;
  672. int total;
  673. ssize_t token_length;
  674. const u8 *pos;
  675. u32 clen, plen, slen;
  676. if (!buf)
  677. return -EFAULT;
  678. if (!resp)
  679. return -EFAULT;
  680. hdr = (struct opal_header *)buf;
  681. pos = buf;
  682. pos += sizeof(*hdr);
  683. clen = be32_to_cpu(hdr->cp.length);
  684. plen = be32_to_cpu(hdr->pkt.length);
  685. slen = be32_to_cpu(hdr->subpkt.length);
  686. pr_debug("Response size: cp: %u, pkt: %u, subpkt: %u\n",
  687. clen, plen, slen);
  688. if (clen == 0 || plen == 0 || slen == 0 ||
  689. slen > IO_BUFFER_LENGTH - sizeof(*hdr)) {
  690. pr_debug("Bad header length. cp: %u, pkt: %u, subpkt: %u\n",
  691. clen, plen, slen);
  692. print_buffer(pos, sizeof(*hdr));
  693. return -EINVAL;
  694. }
  695. if (pos > buf + length)
  696. return -EFAULT;
  697. iter = resp->toks;
  698. total = slen;
  699. print_buffer(pos, total);
  700. while (total > 0) {
  701. if (pos[0] <= TINY_ATOM_BYTE) /* tiny atom */
  702. token_length = response_parse_tiny(iter, pos);
  703. else if (pos[0] <= SHORT_ATOM_BYTE) /* short atom */
  704. token_length = response_parse_short(iter, pos);
  705. else if (pos[0] <= MEDIUM_ATOM_BYTE) /* medium atom */
  706. token_length = response_parse_medium(iter, pos);
  707. else if (pos[0] <= LONG_ATOM_BYTE) /* long atom */
  708. token_length = response_parse_long(iter, pos);
  709. else /* TOKEN */
  710. token_length = response_parse_token(iter, pos);
  711. if (token_length < 0)
  712. return token_length;
  713. pos += token_length;
  714. total -= token_length;
  715. iter++;
  716. num_entries++;
  717. }
  718. if (num_entries == 0) {
  719. pr_debug("Couldn't parse response.\n");
  720. return -EINVAL;
  721. }
  722. resp->num = num_entries;
  723. return 0;
  724. }
  725. static size_t response_get_string(const struct parsed_resp *resp, int n,
  726. const char **store)
  727. {
  728. u8 skip;
  729. const struct opal_resp_tok *token;
  730. *store = NULL;
  731. if (!resp) {
  732. pr_debug("Response is NULL\n");
  733. return 0;
  734. }
  735. if (n >= resp->num) {
  736. pr_debug("Response has %d tokens. Can't access %d\n",
  737. resp->num, n);
  738. return 0;
  739. }
  740. token = &resp->toks[n];
  741. if (token->type != OPAL_DTA_TOKENID_BYTESTRING) {
  742. pr_debug("Token is not a byte string!\n");
  743. return 0;
  744. }
  745. switch (token->width) {
  746. case OPAL_WIDTH_TINY:
  747. case OPAL_WIDTH_SHORT:
  748. skip = 1;
  749. break;
  750. case OPAL_WIDTH_MEDIUM:
  751. skip = 2;
  752. break;
  753. case OPAL_WIDTH_LONG:
  754. skip = 4;
  755. break;
  756. default:
  757. pr_debug("Token has invalid width!\n");
  758. return 0;
  759. }
  760. *store = token->pos + skip;
  761. return token->len - skip;
  762. }
  763. static u64 response_get_u64(const struct parsed_resp *resp, int n)
  764. {
  765. if (!resp) {
  766. pr_debug("Response is NULL\n");
  767. return 0;
  768. }
  769. if (n >= resp->num) {
  770. pr_debug("Response has %d tokens. Can't access %d\n",
  771. resp->num, n);
  772. return 0;
  773. }
  774. if (resp->toks[n].type != OPAL_DTA_TOKENID_UINT) {
  775. pr_debug("Token is not unsigned it: %d\n",
  776. resp->toks[n].type);
  777. return 0;
  778. }
  779. if (!(resp->toks[n].width == OPAL_WIDTH_TINY ||
  780. resp->toks[n].width == OPAL_WIDTH_SHORT)) {
  781. pr_debug("Atom is not short or tiny: %d\n",
  782. resp->toks[n].width);
  783. return 0;
  784. }
  785. return resp->toks[n].stored.u;
  786. }
  787. static bool response_token_matches(const struct opal_resp_tok *token, u8 match)
  788. {
  789. if (IS_ERR(token) ||
  790. token->type != OPAL_DTA_TOKENID_TOKEN ||
  791. token->pos[0] != match)
  792. return false;
  793. return true;
  794. }
  795. static u8 response_status(const struct parsed_resp *resp)
  796. {
  797. const struct opal_resp_tok *tok;
  798. tok = response_get_token(resp, 0);
  799. if (response_token_matches(tok, OPAL_ENDOFSESSION))
  800. return 0;
  801. if (resp->num < 5)
  802. return DTAERROR_NO_METHOD_STATUS;
  803. tok = response_get_token(resp, resp->num - 5);
  804. if (!response_token_matches(tok, OPAL_STARTLIST))
  805. return DTAERROR_NO_METHOD_STATUS;
  806. tok = response_get_token(resp, resp->num - 1);
  807. if (!response_token_matches(tok, OPAL_ENDLIST))
  808. return DTAERROR_NO_METHOD_STATUS;
  809. return response_get_u64(resp, resp->num - 4);
  810. }
  811. /* Parses and checks for errors */
  812. static int parse_and_check_status(struct opal_dev *dev)
  813. {
  814. int error;
  815. print_buffer(dev->cmd, dev->pos);
  816. error = response_parse(dev->resp, IO_BUFFER_LENGTH, &dev->parsed);
  817. if (error) {
  818. pr_debug("Couldn't parse response.\n");
  819. return error;
  820. }
  821. return response_status(&dev->parsed);
  822. }
  823. static void clear_opal_cmd(struct opal_dev *dev)
  824. {
  825. dev->pos = sizeof(struct opal_header);
  826. memset(dev->cmd, 0, IO_BUFFER_LENGTH);
  827. }
  828. static int start_opal_session_cont(struct opal_dev *dev)
  829. {
  830. u32 hsn, tsn;
  831. int error = 0;
  832. error = parse_and_check_status(dev);
  833. if (error)
  834. return error;
  835. hsn = response_get_u64(&dev->parsed, 4);
  836. tsn = response_get_u64(&dev->parsed, 5);
  837. if (hsn == 0 && tsn == 0) {
  838. pr_debug("Couldn't authenticate session\n");
  839. return -EPERM;
  840. }
  841. dev->hsn = hsn;
  842. dev->tsn = tsn;
  843. return 0;
  844. }
  845. static void add_suspend_info(struct opal_dev *dev,
  846. struct opal_suspend_data *sus)
  847. {
  848. struct opal_suspend_data *iter;
  849. list_for_each_entry(iter, &dev->unlk_lst, node) {
  850. if (iter->lr == sus->lr) {
  851. list_del(&iter->node);
  852. kfree(iter);
  853. break;
  854. }
  855. }
  856. list_add_tail(&sus->node, &dev->unlk_lst);
  857. }
  858. static int end_session_cont(struct opal_dev *dev)
  859. {
  860. dev->hsn = 0;
  861. dev->tsn = 0;
  862. return parse_and_check_status(dev);
  863. }
  864. static int finalize_and_send(struct opal_dev *dev, cont_fn cont)
  865. {
  866. int ret;
  867. ret = cmd_finalize(dev, dev->hsn, dev->tsn);
  868. if (ret) {
  869. pr_debug("Error finalizing command buffer: %d\n", ret);
  870. return ret;
  871. }
  872. print_buffer(dev->cmd, dev->pos);
  873. return opal_send_recv(dev, cont);
  874. }
  875. static int gen_key(struct opal_dev *dev, void *data)
  876. {
  877. u8 uid[OPAL_UID_LENGTH];
  878. int err = 0;
  879. clear_opal_cmd(dev);
  880. set_comid(dev, dev->comid);
  881. memcpy(uid, dev->prev_data, min(sizeof(uid), dev->prev_d_len));
  882. kfree(dev->prev_data);
  883. dev->prev_data = NULL;
  884. add_token_u8(&err, dev, OPAL_CALL);
  885. add_token_bytestring(&err, dev, uid, OPAL_UID_LENGTH);
  886. add_token_bytestring(&err, dev, opalmethod[OPAL_GENKEY],
  887. OPAL_UID_LENGTH);
  888. add_token_u8(&err, dev, OPAL_STARTLIST);
  889. add_token_u8(&err, dev, OPAL_ENDLIST);
  890. if (err) {
  891. pr_debug("Error building gen key command\n");
  892. return err;
  893. }
  894. return finalize_and_send(dev, parse_and_check_status);
  895. }
  896. static int get_active_key_cont(struct opal_dev *dev)
  897. {
  898. const char *activekey;
  899. size_t keylen;
  900. int error = 0;
  901. error = parse_and_check_status(dev);
  902. if (error)
  903. return error;
  904. keylen = response_get_string(&dev->parsed, 4, &activekey);
  905. if (!activekey) {
  906. pr_debug("%s: Couldn't extract the Activekey from the response\n",
  907. __func__);
  908. return OPAL_INVAL_PARAM;
  909. }
  910. dev->prev_data = kmemdup(activekey, keylen, GFP_KERNEL);
  911. if (!dev->prev_data)
  912. return -ENOMEM;
  913. dev->prev_d_len = keylen;
  914. return 0;
  915. }
  916. static int get_active_key(struct opal_dev *dev, void *data)
  917. {
  918. u8 uid[OPAL_UID_LENGTH];
  919. int err = 0;
  920. u8 *lr = data;
  921. clear_opal_cmd(dev);
  922. set_comid(dev, dev->comid);
  923. err = build_locking_range(uid, sizeof(uid), *lr);
  924. if (err)
  925. return err;
  926. err = 0;
  927. add_token_u8(&err, dev, OPAL_CALL);
  928. add_token_bytestring(&err, dev, uid, OPAL_UID_LENGTH);
  929. add_token_bytestring(&err, dev, opalmethod[OPAL_GET], OPAL_UID_LENGTH);
  930. add_token_u8(&err, dev, OPAL_STARTLIST);
  931. add_token_u8(&err, dev, OPAL_STARTLIST);
  932. add_token_u8(&err, dev, OPAL_STARTNAME);
  933. add_token_u8(&err, dev, 3); /* startCloumn */
  934. add_token_u8(&err, dev, 10); /* ActiveKey */
  935. add_token_u8(&err, dev, OPAL_ENDNAME);
  936. add_token_u8(&err, dev, OPAL_STARTNAME);
  937. add_token_u8(&err, dev, 4); /* endColumn */
  938. add_token_u8(&err, dev, 10); /* ActiveKey */
  939. add_token_u8(&err, dev, OPAL_ENDNAME);
  940. add_token_u8(&err, dev, OPAL_ENDLIST);
  941. add_token_u8(&err, dev, OPAL_ENDLIST);
  942. if (err) {
  943. pr_debug("Error building get active key command\n");
  944. return err;
  945. }
  946. return finalize_and_send(dev, get_active_key_cont);
  947. }
  948. static int generic_lr_enable_disable(struct opal_dev *dev,
  949. u8 *uid, bool rle, bool wle,
  950. bool rl, bool wl)
  951. {
  952. int err = 0;
  953. add_token_u8(&err, dev, OPAL_CALL);
  954. add_token_bytestring(&err, dev, uid, OPAL_UID_LENGTH);
  955. add_token_bytestring(&err, dev, opalmethod[OPAL_SET], OPAL_UID_LENGTH);
  956. add_token_u8(&err, dev, OPAL_STARTLIST);
  957. add_token_u8(&err, dev, OPAL_STARTNAME);
  958. add_token_u8(&err, dev, OPAL_VALUES);
  959. add_token_u8(&err, dev, OPAL_STARTLIST);
  960. add_token_u8(&err, dev, OPAL_STARTNAME);
  961. add_token_u8(&err, dev, 5); /* ReadLockEnabled */
  962. add_token_u8(&err, dev, rle);
  963. add_token_u8(&err, dev, OPAL_ENDNAME);
  964. add_token_u8(&err, dev, OPAL_STARTNAME);
  965. add_token_u8(&err, dev, 6); /* WriteLockEnabled */
  966. add_token_u8(&err, dev, wle);
  967. add_token_u8(&err, dev, OPAL_ENDNAME);
  968. add_token_u8(&err, dev, OPAL_STARTNAME);
  969. add_token_u8(&err, dev, OPAL_READLOCKED);
  970. add_token_u8(&err, dev, rl);
  971. add_token_u8(&err, dev, OPAL_ENDNAME);
  972. add_token_u8(&err, dev, OPAL_STARTNAME);
  973. add_token_u8(&err, dev, OPAL_WRITELOCKED);
  974. add_token_u8(&err, dev, wl);
  975. add_token_u8(&err, dev, OPAL_ENDNAME);
  976. add_token_u8(&err, dev, OPAL_ENDLIST);
  977. add_token_u8(&err, dev, OPAL_ENDNAME);
  978. add_token_u8(&err, dev, OPAL_ENDLIST);
  979. return err;
  980. }
  981. static inline int enable_global_lr(struct opal_dev *dev, u8 *uid,
  982. struct opal_user_lr_setup *setup)
  983. {
  984. int err;
  985. err = generic_lr_enable_disable(dev, uid, !!setup->RLE, !!setup->WLE,
  986. 0, 0);
  987. if (err)
  988. pr_debug("Failed to create enable global lr command\n");
  989. return err;
  990. }
  991. static int setup_locking_range(struct opal_dev *dev, void *data)
  992. {
  993. u8 uid[OPAL_UID_LENGTH];
  994. struct opal_user_lr_setup *setup = data;
  995. u8 lr;
  996. int err = 0;
  997. clear_opal_cmd(dev);
  998. set_comid(dev, dev->comid);
  999. lr = setup->session.opal_key.lr;
  1000. err = build_locking_range(uid, sizeof(uid), lr);
  1001. if (err)
  1002. return err;
  1003. if (lr == 0)
  1004. err = enable_global_lr(dev, uid, setup);
  1005. else {
  1006. add_token_u8(&err, dev, OPAL_CALL);
  1007. add_token_bytestring(&err, dev, uid, OPAL_UID_LENGTH);
  1008. add_token_bytestring(&err, dev, opalmethod[OPAL_SET],
  1009. OPAL_UID_LENGTH);
  1010. add_token_u8(&err, dev, OPAL_STARTLIST);
  1011. add_token_u8(&err, dev, OPAL_STARTNAME);
  1012. add_token_u8(&err, dev, OPAL_VALUES);
  1013. add_token_u8(&err, dev, OPAL_STARTLIST);
  1014. add_token_u8(&err, dev, OPAL_STARTNAME);
  1015. add_token_u8(&err, dev, 3); /* Ranges Start */
  1016. add_token_u64(&err, dev, setup->range_start);
  1017. add_token_u8(&err, dev, OPAL_ENDNAME);
  1018. add_token_u8(&err, dev, OPAL_STARTNAME);
  1019. add_token_u8(&err, dev, 4); /* Ranges length */
  1020. add_token_u64(&err, dev, setup->range_length);
  1021. add_token_u8(&err, dev, OPAL_ENDNAME);
  1022. add_token_u8(&err, dev, OPAL_STARTNAME);
  1023. add_token_u8(&err, dev, 5); /*ReadLockEnabled */
  1024. add_token_u64(&err, dev, !!setup->RLE);
  1025. add_token_u8(&err, dev, OPAL_ENDNAME);
  1026. add_token_u8(&err, dev, OPAL_STARTNAME);
  1027. add_token_u8(&err, dev, 6); /*WriteLockEnabled*/
  1028. add_token_u64(&err, dev, !!setup->WLE);
  1029. add_token_u8(&err, dev, OPAL_ENDNAME);
  1030. add_token_u8(&err, dev, OPAL_ENDLIST);
  1031. add_token_u8(&err, dev, OPAL_ENDNAME);
  1032. add_token_u8(&err, dev, OPAL_ENDLIST);
  1033. }
  1034. if (err) {
  1035. pr_debug("Error building Setup Locking range command.\n");
  1036. return err;
  1037. }
  1038. return finalize_and_send(dev, parse_and_check_status);
  1039. }
  1040. static int start_generic_opal_session(struct opal_dev *dev,
  1041. enum opal_uid auth,
  1042. enum opal_uid sp_type,
  1043. const char *key,
  1044. u8 key_len)
  1045. {
  1046. u32 hsn;
  1047. int err = 0;
  1048. if (key == NULL && auth != OPAL_ANYBODY_UID)
  1049. return OPAL_INVAL_PARAM;
  1050. clear_opal_cmd(dev);
  1051. set_comid(dev, dev->comid);
  1052. hsn = GENERIC_HOST_SESSION_NUM;
  1053. add_token_u8(&err, dev, OPAL_CALL);
  1054. add_token_bytestring(&err, dev, opaluid[OPAL_SMUID_UID],
  1055. OPAL_UID_LENGTH);
  1056. add_token_bytestring(&err, dev, opalmethod[OPAL_STARTSESSION],
  1057. OPAL_UID_LENGTH);
  1058. add_token_u8(&err, dev, OPAL_STARTLIST);
  1059. add_token_u64(&err, dev, hsn);
  1060. add_token_bytestring(&err, dev, opaluid[sp_type], OPAL_UID_LENGTH);
  1061. add_token_u8(&err, dev, 1);
  1062. switch (auth) {
  1063. case OPAL_ANYBODY_UID:
  1064. add_token_u8(&err, dev, OPAL_ENDLIST);
  1065. break;
  1066. case OPAL_ADMIN1_UID:
  1067. case OPAL_SID_UID:
  1068. add_token_u8(&err, dev, OPAL_STARTNAME);
  1069. add_token_u8(&err, dev, 0); /* HostChallenge */
  1070. add_token_bytestring(&err, dev, key, key_len);
  1071. add_token_u8(&err, dev, OPAL_ENDNAME);
  1072. add_token_u8(&err, dev, OPAL_STARTNAME);
  1073. add_token_u8(&err, dev, 3); /* HostSignAuth */
  1074. add_token_bytestring(&err, dev, opaluid[auth],
  1075. OPAL_UID_LENGTH);
  1076. add_token_u8(&err, dev, OPAL_ENDNAME);
  1077. add_token_u8(&err, dev, OPAL_ENDLIST);
  1078. break;
  1079. default:
  1080. pr_debug("Cannot start Admin SP session with auth %d\n", auth);
  1081. return OPAL_INVAL_PARAM;
  1082. }
  1083. if (err) {
  1084. pr_debug("Error building start adminsp session command.\n");
  1085. return err;
  1086. }
  1087. return finalize_and_send(dev, start_opal_session_cont);
  1088. }
  1089. static int start_anybodyASP_opal_session(struct opal_dev *dev, void *data)
  1090. {
  1091. return start_generic_opal_session(dev, OPAL_ANYBODY_UID,
  1092. OPAL_ADMINSP_UID, NULL, 0);
  1093. }
  1094. static int start_SIDASP_opal_session(struct opal_dev *dev, void *data)
  1095. {
  1096. int ret;
  1097. const u8 *key = dev->prev_data;
  1098. if (!key) {
  1099. const struct opal_key *okey = data;
  1100. ret = start_generic_opal_session(dev, OPAL_SID_UID,
  1101. OPAL_ADMINSP_UID,
  1102. okey->key,
  1103. okey->key_len);
  1104. } else {
  1105. ret = start_generic_opal_session(dev, OPAL_SID_UID,
  1106. OPAL_ADMINSP_UID,
  1107. key, dev->prev_d_len);
  1108. kfree(key);
  1109. dev->prev_data = NULL;
  1110. }
  1111. return ret;
  1112. }
  1113. static int start_admin1LSP_opal_session(struct opal_dev *dev, void *data)
  1114. {
  1115. struct opal_key *key = data;
  1116. return start_generic_opal_session(dev, OPAL_ADMIN1_UID,
  1117. OPAL_LOCKINGSP_UID,
  1118. key->key, key->key_len);
  1119. }
  1120. static int start_auth_opal_session(struct opal_dev *dev, void *data)
  1121. {
  1122. struct opal_session_info *session = data;
  1123. u8 lk_ul_user[OPAL_UID_LENGTH];
  1124. size_t keylen = session->opal_key.key_len;
  1125. int err = 0;
  1126. u8 *key = session->opal_key.key;
  1127. u32 hsn = GENERIC_HOST_SESSION_NUM;
  1128. clear_opal_cmd(dev);
  1129. set_comid(dev, dev->comid);
  1130. if (session->sum) {
  1131. err = build_locking_user(lk_ul_user, sizeof(lk_ul_user),
  1132. session->opal_key.lr);
  1133. if (err)
  1134. return err;
  1135. } else if (session->who != OPAL_ADMIN1 && !session->sum) {
  1136. err = build_locking_user(lk_ul_user, sizeof(lk_ul_user),
  1137. session->who - 1);
  1138. if (err)
  1139. return err;
  1140. } else
  1141. memcpy(lk_ul_user, opaluid[OPAL_ADMIN1_UID], OPAL_UID_LENGTH);
  1142. add_token_u8(&err, dev, OPAL_CALL);
  1143. add_token_bytestring(&err, dev, opaluid[OPAL_SMUID_UID],
  1144. OPAL_UID_LENGTH);
  1145. add_token_bytestring(&err, dev, opalmethod[OPAL_STARTSESSION],
  1146. OPAL_UID_LENGTH);
  1147. add_token_u8(&err, dev, OPAL_STARTLIST);
  1148. add_token_u64(&err, dev, hsn);
  1149. add_token_bytestring(&err, dev, opaluid[OPAL_LOCKINGSP_UID],
  1150. OPAL_UID_LENGTH);
  1151. add_token_u8(&err, dev, 1);
  1152. add_token_u8(&err, dev, OPAL_STARTNAME);
  1153. add_token_u8(&err, dev, 0);
  1154. add_token_bytestring(&err, dev, key, keylen);
  1155. add_token_u8(&err, dev, OPAL_ENDNAME);
  1156. add_token_u8(&err, dev, OPAL_STARTNAME);
  1157. add_token_u8(&err, dev, 3);
  1158. add_token_bytestring(&err, dev, lk_ul_user, OPAL_UID_LENGTH);
  1159. add_token_u8(&err, dev, OPAL_ENDNAME);
  1160. add_token_u8(&err, dev, OPAL_ENDLIST);
  1161. if (err) {
  1162. pr_debug("Error building STARTSESSION command.\n");
  1163. return err;
  1164. }
  1165. return finalize_and_send(dev, start_opal_session_cont);
  1166. }
  1167. static int revert_tper(struct opal_dev *dev, void *data)
  1168. {
  1169. int err = 0;
  1170. clear_opal_cmd(dev);
  1171. set_comid(dev, dev->comid);
  1172. add_token_u8(&err, dev, OPAL_CALL);
  1173. add_token_bytestring(&err, dev, opaluid[OPAL_ADMINSP_UID],
  1174. OPAL_UID_LENGTH);
  1175. add_token_bytestring(&err, dev, opalmethod[OPAL_REVERT],
  1176. OPAL_UID_LENGTH);
  1177. add_token_u8(&err, dev, OPAL_STARTLIST);
  1178. add_token_u8(&err, dev, OPAL_ENDLIST);
  1179. if (err) {
  1180. pr_debug("Error building REVERT TPER command.\n");
  1181. return err;
  1182. }
  1183. return finalize_and_send(dev, parse_and_check_status);
  1184. }
  1185. static int internal_activate_user(struct opal_dev *dev, void *data)
  1186. {
  1187. struct opal_session_info *session = data;
  1188. u8 uid[OPAL_UID_LENGTH];
  1189. int err = 0;
  1190. clear_opal_cmd(dev);
  1191. set_comid(dev, dev->comid);
  1192. memcpy(uid, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
  1193. uid[7] = session->who;
  1194. add_token_u8(&err, dev, OPAL_CALL);
  1195. add_token_bytestring(&err, dev, uid, OPAL_UID_LENGTH);
  1196. add_token_bytestring(&err, dev, opalmethod[OPAL_SET], OPAL_UID_LENGTH);
  1197. add_token_u8(&err, dev, OPAL_STARTLIST);
  1198. add_token_u8(&err, dev, OPAL_STARTNAME);
  1199. add_token_u8(&err, dev, OPAL_VALUES);
  1200. add_token_u8(&err, dev, OPAL_STARTLIST);
  1201. add_token_u8(&err, dev, OPAL_STARTNAME);
  1202. add_token_u8(&err, dev, 5); /* Enabled */
  1203. add_token_u8(&err, dev, OPAL_TRUE);
  1204. add_token_u8(&err, dev, OPAL_ENDNAME);
  1205. add_token_u8(&err, dev, OPAL_ENDLIST);
  1206. add_token_u8(&err, dev, OPAL_ENDNAME);
  1207. add_token_u8(&err, dev, OPAL_ENDLIST);
  1208. if (err) {
  1209. pr_debug("Error building Activate UserN command.\n");
  1210. return err;
  1211. }
  1212. return finalize_and_send(dev, parse_and_check_status);
  1213. }
  1214. static int erase_locking_range(struct opal_dev *dev, void *data)
  1215. {
  1216. struct opal_session_info *session = data;
  1217. u8 uid[OPAL_UID_LENGTH];
  1218. int err = 0;
  1219. clear_opal_cmd(dev);
  1220. set_comid(dev, dev->comid);
  1221. if (build_locking_range(uid, sizeof(uid), session->opal_key.lr) < 0)
  1222. return -ERANGE;
  1223. add_token_u8(&err, dev, OPAL_CALL);
  1224. add_token_bytestring(&err, dev, uid, OPAL_UID_LENGTH);
  1225. add_token_bytestring(&err, dev, opalmethod[OPAL_ERASE],
  1226. OPAL_UID_LENGTH);
  1227. add_token_u8(&err, dev, OPAL_STARTLIST);
  1228. add_token_u8(&err, dev, OPAL_ENDLIST);
  1229. if (err) {
  1230. pr_debug("Error building Erase Locking Range Command.\n");
  1231. return err;
  1232. }
  1233. return finalize_and_send(dev, parse_and_check_status);
  1234. }
  1235. static int set_mbr_done(struct opal_dev *dev, void *data)
  1236. {
  1237. u8 *mbr_done_tf = data;
  1238. int err = 0;
  1239. clear_opal_cmd(dev);
  1240. set_comid(dev, dev->comid);
  1241. add_token_u8(&err, dev, OPAL_CALL);
  1242. add_token_bytestring(&err, dev, opaluid[OPAL_MBRCONTROL],
  1243. OPAL_UID_LENGTH);
  1244. add_token_bytestring(&err, dev, opalmethod[OPAL_SET], OPAL_UID_LENGTH);
  1245. add_token_u8(&err, dev, OPAL_STARTLIST);
  1246. add_token_u8(&err, dev, OPAL_STARTNAME);
  1247. add_token_u8(&err, dev, OPAL_VALUES);
  1248. add_token_u8(&err, dev, OPAL_STARTLIST);
  1249. add_token_u8(&err, dev, OPAL_STARTNAME);
  1250. add_token_u8(&err, dev, 2); /* Done */
  1251. add_token_u8(&err, dev, *mbr_done_tf); /* Done T or F */
  1252. add_token_u8(&err, dev, OPAL_ENDNAME);
  1253. add_token_u8(&err, dev, OPAL_ENDLIST);
  1254. add_token_u8(&err, dev, OPAL_ENDNAME);
  1255. add_token_u8(&err, dev, OPAL_ENDLIST);
  1256. if (err) {
  1257. pr_debug("Error Building set MBR Done command\n");
  1258. return err;
  1259. }
  1260. return finalize_and_send(dev, parse_and_check_status);
  1261. }
  1262. static int set_mbr_enable_disable(struct opal_dev *dev, void *data)
  1263. {
  1264. u8 *mbr_en_dis = data;
  1265. int err = 0;
  1266. clear_opal_cmd(dev);
  1267. set_comid(dev, dev->comid);
  1268. add_token_u8(&err, dev, OPAL_CALL);
  1269. add_token_bytestring(&err, dev, opaluid[OPAL_MBRCONTROL],
  1270. OPAL_UID_LENGTH);
  1271. add_token_bytestring(&err, dev, opalmethod[OPAL_SET], OPAL_UID_LENGTH);
  1272. add_token_u8(&err, dev, OPAL_STARTLIST);
  1273. add_token_u8(&err, dev, OPAL_STARTNAME);
  1274. add_token_u8(&err, dev, OPAL_VALUES);
  1275. add_token_u8(&err, dev, OPAL_STARTLIST);
  1276. add_token_u8(&err, dev, OPAL_STARTNAME);
  1277. add_token_u8(&err, dev, 1);
  1278. add_token_u8(&err, dev, *mbr_en_dis);
  1279. add_token_u8(&err, dev, OPAL_ENDNAME);
  1280. add_token_u8(&err, dev, OPAL_ENDLIST);
  1281. add_token_u8(&err, dev, OPAL_ENDNAME);
  1282. add_token_u8(&err, dev, OPAL_ENDLIST);
  1283. if (err) {
  1284. pr_debug("Error Building set MBR done command\n");
  1285. return err;
  1286. }
  1287. return finalize_and_send(dev, parse_and_check_status);
  1288. }
  1289. static int generic_pw_cmd(u8 *key, size_t key_len, u8 *cpin_uid,
  1290. struct opal_dev *dev)
  1291. {
  1292. int err = 0;
  1293. clear_opal_cmd(dev);
  1294. set_comid(dev, dev->comid);
  1295. add_token_u8(&err, dev, OPAL_CALL);
  1296. add_token_bytestring(&err, dev, cpin_uid, OPAL_UID_LENGTH);
  1297. add_token_bytestring(&err, dev, opalmethod[OPAL_SET],
  1298. OPAL_UID_LENGTH);
  1299. add_token_u8(&err, dev, OPAL_STARTLIST);
  1300. add_token_u8(&err, dev, OPAL_STARTNAME);
  1301. add_token_u8(&err, dev, OPAL_VALUES);
  1302. add_token_u8(&err, dev, OPAL_STARTLIST);
  1303. add_token_u8(&err, dev, OPAL_STARTNAME);
  1304. add_token_u8(&err, dev, 3); /* PIN */
  1305. add_token_bytestring(&err, dev, key, key_len);
  1306. add_token_u8(&err, dev, OPAL_ENDNAME);
  1307. add_token_u8(&err, dev, OPAL_ENDLIST);
  1308. add_token_u8(&err, dev, OPAL_ENDNAME);
  1309. add_token_u8(&err, dev, OPAL_ENDLIST);
  1310. return err;
  1311. }
  1312. static int set_new_pw(struct opal_dev *dev, void *data)
  1313. {
  1314. u8 cpin_uid[OPAL_UID_LENGTH];
  1315. struct opal_session_info *usr = data;
  1316. memcpy(cpin_uid, opaluid[OPAL_C_PIN_ADMIN1], OPAL_UID_LENGTH);
  1317. if (usr->who != OPAL_ADMIN1) {
  1318. cpin_uid[5] = 0x03;
  1319. if (usr->sum)
  1320. cpin_uid[7] = usr->opal_key.lr + 1;
  1321. else
  1322. cpin_uid[7] = usr->who;
  1323. }
  1324. if (generic_pw_cmd(usr->opal_key.key, usr->opal_key.key_len,
  1325. cpin_uid, dev)) {
  1326. pr_debug("Error building set password command.\n");
  1327. return -ERANGE;
  1328. }
  1329. return finalize_and_send(dev, parse_and_check_status);
  1330. }
  1331. static int set_sid_cpin_pin(struct opal_dev *dev, void *data)
  1332. {
  1333. u8 cpin_uid[OPAL_UID_LENGTH];
  1334. struct opal_key *key = data;
  1335. memcpy(cpin_uid, opaluid[OPAL_C_PIN_SID], OPAL_UID_LENGTH);
  1336. if (generic_pw_cmd(key->key, key->key_len, cpin_uid, dev)) {
  1337. pr_debug("Error building Set SID cpin\n");
  1338. return -ERANGE;
  1339. }
  1340. return finalize_and_send(dev, parse_and_check_status);
  1341. }
  1342. static int add_user_to_lr(struct opal_dev *dev, void *data)
  1343. {
  1344. u8 lr_buffer[OPAL_UID_LENGTH];
  1345. u8 user_uid[OPAL_UID_LENGTH];
  1346. struct opal_lock_unlock *lkul = data;
  1347. int err = 0;
  1348. clear_opal_cmd(dev);
  1349. set_comid(dev, dev->comid);
  1350. memcpy(lr_buffer, opaluid[OPAL_LOCKINGRANGE_ACE_RDLOCKED],
  1351. OPAL_UID_LENGTH);
  1352. if (lkul->l_state == OPAL_RW)
  1353. memcpy(lr_buffer, opaluid[OPAL_LOCKINGRANGE_ACE_WRLOCKED],
  1354. OPAL_UID_LENGTH);
  1355. lr_buffer[7] = lkul->session.opal_key.lr;
  1356. memcpy(user_uid, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
  1357. user_uid[7] = lkul->session.who;
  1358. add_token_u8(&err, dev, OPAL_CALL);
  1359. add_token_bytestring(&err, dev, lr_buffer, OPAL_UID_LENGTH);
  1360. add_token_bytestring(&err, dev, opalmethod[OPAL_SET],
  1361. OPAL_UID_LENGTH);
  1362. add_token_u8(&err, dev, OPAL_STARTLIST);
  1363. add_token_u8(&err, dev, OPAL_STARTNAME);
  1364. add_token_u8(&err, dev, OPAL_VALUES);
  1365. add_token_u8(&err, dev, OPAL_STARTLIST);
  1366. add_token_u8(&err, dev, OPAL_STARTNAME);
  1367. add_token_u8(&err, dev, 3);
  1368. add_token_u8(&err, dev, OPAL_STARTLIST);
  1369. add_token_u8(&err, dev, OPAL_STARTNAME);
  1370. add_token_bytestring(&err, dev,
  1371. opaluid[OPAL_HALF_UID_AUTHORITY_OBJ_REF],
  1372. OPAL_UID_LENGTH/2);
  1373. add_token_bytestring(&err, dev, user_uid, OPAL_UID_LENGTH);
  1374. add_token_u8(&err, dev, OPAL_ENDNAME);
  1375. add_token_u8(&err, dev, OPAL_STARTNAME);
  1376. add_token_bytestring(&err, dev,
  1377. opaluid[OPAL_HALF_UID_AUTHORITY_OBJ_REF],
  1378. OPAL_UID_LENGTH/2);
  1379. add_token_bytestring(&err, dev, user_uid, OPAL_UID_LENGTH);
  1380. add_token_u8(&err, dev, OPAL_ENDNAME);
  1381. add_token_u8(&err, dev, OPAL_STARTNAME);
  1382. add_token_bytestring(&err, dev, opaluid[OPAL_HALF_UID_BOOLEAN_ACE],
  1383. OPAL_UID_LENGTH/2);
  1384. add_token_u8(&err, dev, 1);
  1385. add_token_u8(&err, dev, OPAL_ENDNAME);
  1386. add_token_u8(&err, dev, OPAL_ENDLIST);
  1387. add_token_u8(&err, dev, OPAL_ENDNAME);
  1388. add_token_u8(&err, dev, OPAL_ENDLIST);
  1389. add_token_u8(&err, dev, OPAL_ENDNAME);
  1390. add_token_u8(&err, dev, OPAL_ENDLIST);
  1391. if (err) {
  1392. pr_debug("Error building add user to locking range command.\n");
  1393. return err;
  1394. }
  1395. return finalize_and_send(dev, parse_and_check_status);
  1396. }
  1397. static int lock_unlock_locking_range(struct opal_dev *dev, void *data)
  1398. {
  1399. u8 lr_buffer[OPAL_UID_LENGTH];
  1400. struct opal_lock_unlock *lkul = data;
  1401. u8 read_locked = 1, write_locked = 1;
  1402. int err = 0;
  1403. clear_opal_cmd(dev);
  1404. set_comid(dev, dev->comid);
  1405. if (build_locking_range(lr_buffer, sizeof(lr_buffer),
  1406. lkul->session.opal_key.lr) < 0)
  1407. return -ERANGE;
  1408. switch (lkul->l_state) {
  1409. case OPAL_RO:
  1410. read_locked = 0;
  1411. write_locked = 1;
  1412. break;
  1413. case OPAL_RW:
  1414. read_locked = 0;
  1415. write_locked = 0;
  1416. break;
  1417. case OPAL_LK:
  1418. /* vars are initalized to locked */
  1419. break;
  1420. default:
  1421. pr_debug("Tried to set an invalid locking state... returning to uland\n");
  1422. return OPAL_INVAL_PARAM;
  1423. }
  1424. add_token_u8(&err, dev, OPAL_CALL);
  1425. add_token_bytestring(&err, dev, lr_buffer, OPAL_UID_LENGTH);
  1426. add_token_bytestring(&err, dev, opalmethod[OPAL_SET], OPAL_UID_LENGTH);
  1427. add_token_u8(&err, dev, OPAL_STARTLIST);
  1428. add_token_u8(&err, dev, OPAL_STARTNAME);
  1429. add_token_u8(&err, dev, OPAL_VALUES);
  1430. add_token_u8(&err, dev, OPAL_STARTLIST);
  1431. add_token_u8(&err, dev, OPAL_STARTNAME);
  1432. add_token_u8(&err, dev, OPAL_READLOCKED);
  1433. add_token_u8(&err, dev, read_locked);
  1434. add_token_u8(&err, dev, OPAL_ENDNAME);
  1435. add_token_u8(&err, dev, OPAL_STARTNAME);
  1436. add_token_u8(&err, dev, OPAL_WRITELOCKED);
  1437. add_token_u8(&err, dev, write_locked);
  1438. add_token_u8(&err, dev, OPAL_ENDNAME);
  1439. add_token_u8(&err, dev, OPAL_ENDLIST);
  1440. add_token_u8(&err, dev, OPAL_ENDNAME);
  1441. add_token_u8(&err, dev, OPAL_ENDLIST);
  1442. if (err) {
  1443. pr_debug("Error building SET command.\n");
  1444. return err;
  1445. }
  1446. return finalize_and_send(dev, parse_and_check_status);
  1447. }
  1448. static int lock_unlock_locking_range_sum(struct opal_dev *dev, void *data)
  1449. {
  1450. u8 lr_buffer[OPAL_UID_LENGTH];
  1451. u8 read_locked = 1, write_locked = 1;
  1452. struct opal_lock_unlock *lkul = data;
  1453. int ret;
  1454. clear_opal_cmd(dev);
  1455. set_comid(dev, dev->comid);
  1456. if (build_locking_range(lr_buffer, sizeof(lr_buffer),
  1457. lkul->session.opal_key.lr) < 0)
  1458. return -ERANGE;
  1459. switch (lkul->l_state) {
  1460. case OPAL_RO:
  1461. read_locked = 0;
  1462. write_locked = 1;
  1463. break;
  1464. case OPAL_RW:
  1465. read_locked = 0;
  1466. write_locked = 0;
  1467. break;
  1468. case OPAL_LK:
  1469. /* vars are initalized to locked */
  1470. break;
  1471. default:
  1472. pr_debug("Tried to set an invalid locking state.\n");
  1473. return OPAL_INVAL_PARAM;
  1474. }
  1475. ret = generic_lr_enable_disable(dev, lr_buffer, 1, 1,
  1476. read_locked, write_locked);
  1477. if (ret < 0) {
  1478. pr_debug("Error building SET command.\n");
  1479. return ret;
  1480. }
  1481. return finalize_and_send(dev, parse_and_check_status);
  1482. }
  1483. static int activate_lsp(struct opal_dev *dev, void *data)
  1484. {
  1485. struct opal_lr_act *opal_act = data;
  1486. u8 user_lr[OPAL_UID_LENGTH];
  1487. u8 uint_3 = 0x83;
  1488. int err = 0, i;
  1489. clear_opal_cmd(dev);
  1490. set_comid(dev, dev->comid);
  1491. add_token_u8(&err, dev, OPAL_CALL);
  1492. add_token_bytestring(&err, dev, opaluid[OPAL_LOCKINGSP_UID],
  1493. OPAL_UID_LENGTH);
  1494. add_token_bytestring(&err, dev, opalmethod[OPAL_ACTIVATE],
  1495. OPAL_UID_LENGTH);
  1496. if (opal_act->sum) {
  1497. err = build_locking_range(user_lr, sizeof(user_lr),
  1498. opal_act->lr[0]);
  1499. if (err)
  1500. return err;
  1501. add_token_u8(&err, dev, OPAL_STARTLIST);
  1502. add_token_u8(&err, dev, OPAL_STARTNAME);
  1503. add_token_u8(&err, dev, uint_3);
  1504. add_token_u8(&err, dev, 6);
  1505. add_token_u8(&err, dev, 0);
  1506. add_token_u8(&err, dev, 0);
  1507. add_token_u8(&err, dev, OPAL_STARTLIST);
  1508. add_token_bytestring(&err, dev, user_lr, OPAL_UID_LENGTH);
  1509. for (i = 1; i < opal_act->num_lrs; i++) {
  1510. user_lr[7] = opal_act->lr[i];
  1511. add_token_bytestring(&err, dev, user_lr, OPAL_UID_LENGTH);
  1512. }
  1513. add_token_u8(&err, dev, OPAL_ENDLIST);
  1514. add_token_u8(&err, dev, OPAL_ENDNAME);
  1515. add_token_u8(&err, dev, OPAL_ENDLIST);
  1516. } else {
  1517. add_token_u8(&err, dev, OPAL_STARTLIST);
  1518. add_token_u8(&err, dev, OPAL_ENDLIST);
  1519. }
  1520. if (err) {
  1521. pr_debug("Error building Activate LockingSP command.\n");
  1522. return err;
  1523. }
  1524. return finalize_and_send(dev, parse_and_check_status);
  1525. }
  1526. static int get_lsp_lifecycle_cont(struct opal_dev *dev)
  1527. {
  1528. u8 lc_status;
  1529. int error = 0;
  1530. error = parse_and_check_status(dev);
  1531. if (error)
  1532. return error;
  1533. lc_status = response_get_u64(&dev->parsed, 4);
  1534. /* 0x08 is Manufacured Inactive */
  1535. /* 0x09 is Manufactured */
  1536. if (lc_status != OPAL_MANUFACTURED_INACTIVE) {
  1537. pr_debug("Couldn't determine the status of the Lifecycle state\n");
  1538. return -ENODEV;
  1539. }
  1540. return 0;
  1541. }
  1542. /* Determine if we're in the Manufactured Inactive or Active state */
  1543. static int get_lsp_lifecycle(struct opal_dev *dev, void *data)
  1544. {
  1545. int err = 0;
  1546. clear_opal_cmd(dev);
  1547. set_comid(dev, dev->comid);
  1548. add_token_u8(&err, dev, OPAL_CALL);
  1549. add_token_bytestring(&err, dev, opaluid[OPAL_LOCKINGSP_UID],
  1550. OPAL_UID_LENGTH);
  1551. add_token_bytestring(&err, dev, opalmethod[OPAL_GET], OPAL_UID_LENGTH);
  1552. add_token_u8(&err, dev, OPAL_STARTLIST);
  1553. add_token_u8(&err, dev, OPAL_STARTLIST);
  1554. add_token_u8(&err, dev, OPAL_STARTNAME);
  1555. add_token_u8(&err, dev, 3); /* Start Column */
  1556. add_token_u8(&err, dev, 6); /* Lifecycle Column */
  1557. add_token_u8(&err, dev, OPAL_ENDNAME);
  1558. add_token_u8(&err, dev, OPAL_STARTNAME);
  1559. add_token_u8(&err, dev, 4); /* End Column */
  1560. add_token_u8(&err, dev, 6); /* Lifecycle Column */
  1561. add_token_u8(&err, dev, OPAL_ENDNAME);
  1562. add_token_u8(&err, dev, OPAL_ENDLIST);
  1563. add_token_u8(&err, dev, OPAL_ENDLIST);
  1564. if (err) {
  1565. pr_debug("Error Building GET Lifecycle Status command\n");
  1566. return err;
  1567. }
  1568. return finalize_and_send(dev, get_lsp_lifecycle_cont);
  1569. }
  1570. static int get_msid_cpin_pin_cont(struct opal_dev *dev)
  1571. {
  1572. const char *msid_pin;
  1573. size_t strlen;
  1574. int error = 0;
  1575. error = parse_and_check_status(dev);
  1576. if (error)
  1577. return error;
  1578. strlen = response_get_string(&dev->parsed, 4, &msid_pin);
  1579. if (!msid_pin) {
  1580. pr_debug("%s: Couldn't extract PIN from response\n", __func__);
  1581. return OPAL_INVAL_PARAM;
  1582. }
  1583. dev->prev_data = kmemdup(msid_pin, strlen, GFP_KERNEL);
  1584. if (!dev->prev_data)
  1585. return -ENOMEM;
  1586. dev->prev_d_len = strlen;
  1587. return 0;
  1588. }
  1589. static int get_msid_cpin_pin(struct opal_dev *dev, void *data)
  1590. {
  1591. int err = 0;
  1592. clear_opal_cmd(dev);
  1593. set_comid(dev, dev->comid);
  1594. add_token_u8(&err, dev, OPAL_CALL);
  1595. add_token_bytestring(&err, dev, opaluid[OPAL_C_PIN_MSID],
  1596. OPAL_UID_LENGTH);
  1597. add_token_bytestring(&err, dev, opalmethod[OPAL_GET], OPAL_UID_LENGTH);
  1598. add_token_u8(&err, dev, OPAL_STARTLIST);
  1599. add_token_u8(&err, dev, OPAL_STARTLIST);
  1600. add_token_u8(&err, dev, OPAL_STARTNAME);
  1601. add_token_u8(&err, dev, 3); /* Start Column */
  1602. add_token_u8(&err, dev, 3); /* PIN */
  1603. add_token_u8(&err, dev, OPAL_ENDNAME);
  1604. add_token_u8(&err, dev, OPAL_STARTNAME);
  1605. add_token_u8(&err, dev, 4); /* End Column */
  1606. add_token_u8(&err, dev, 3); /* Lifecycle Column */
  1607. add_token_u8(&err, dev, OPAL_ENDNAME);
  1608. add_token_u8(&err, dev, OPAL_ENDLIST);
  1609. add_token_u8(&err, dev, OPAL_ENDLIST);
  1610. if (err) {
  1611. pr_debug("Error building Get MSID CPIN PIN command.\n");
  1612. return err;
  1613. }
  1614. return finalize_and_send(dev, get_msid_cpin_pin_cont);
  1615. }
  1616. static int end_opal_session(struct opal_dev *dev, void *data)
  1617. {
  1618. int err = 0;
  1619. clear_opal_cmd(dev);
  1620. set_comid(dev, dev->comid);
  1621. add_token_u8(&err, dev, OPAL_ENDOFSESSION);
  1622. if (err < 0)
  1623. return err;
  1624. return finalize_and_send(dev, end_session_cont);
  1625. }
  1626. static int end_opal_session_error(struct opal_dev *dev)
  1627. {
  1628. const struct opal_step error_end_session[] = {
  1629. { end_opal_session, },
  1630. { NULL, }
  1631. };
  1632. dev->steps = error_end_session;
  1633. return next(dev);
  1634. }
  1635. static inline void setup_opal_dev(struct opal_dev *dev,
  1636. const struct opal_step *steps)
  1637. {
  1638. dev->steps = steps;
  1639. dev->tsn = 0;
  1640. dev->hsn = 0;
  1641. dev->prev_data = NULL;
  1642. }
  1643. static int check_opal_support(struct opal_dev *dev)
  1644. {
  1645. const struct opal_step steps[] = {
  1646. { opal_discovery0, },
  1647. { NULL, }
  1648. };
  1649. int ret;
  1650. mutex_lock(&dev->dev_lock);
  1651. setup_opal_dev(dev, steps);
  1652. ret = next(dev);
  1653. dev->supported = !ret;
  1654. mutex_unlock(&dev->dev_lock);
  1655. return ret;
  1656. }
  1657. static void clean_opal_dev(struct opal_dev *dev)
  1658. {
  1659. struct opal_suspend_data *suspend, *next;
  1660. mutex_lock(&dev->dev_lock);
  1661. list_for_each_entry_safe(suspend, next, &dev->unlk_lst, node) {
  1662. list_del(&suspend->node);
  1663. kfree(suspend);
  1664. }
  1665. mutex_unlock(&dev->dev_lock);
  1666. }
  1667. void free_opal_dev(struct opal_dev *dev)
  1668. {
  1669. if (!dev)
  1670. return;
  1671. clean_opal_dev(dev);
  1672. kfree(dev);
  1673. }
  1674. EXPORT_SYMBOL(free_opal_dev);
  1675. struct opal_dev *init_opal_dev(void *data, sec_send_recv *send_recv)
  1676. {
  1677. struct opal_dev *dev;
  1678. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  1679. if (!dev)
  1680. return NULL;
  1681. INIT_LIST_HEAD(&dev->unlk_lst);
  1682. mutex_init(&dev->dev_lock);
  1683. dev->data = data;
  1684. dev->send_recv = send_recv;
  1685. if (check_opal_support(dev) != 0) {
  1686. pr_debug("Opal is not supported on this device\n");
  1687. kfree(dev);
  1688. return NULL;
  1689. }
  1690. return dev;
  1691. }
  1692. EXPORT_SYMBOL(init_opal_dev);
  1693. static int opal_secure_erase_locking_range(struct opal_dev *dev,
  1694. struct opal_session_info *opal_session)
  1695. {
  1696. const struct opal_step erase_steps[] = {
  1697. { opal_discovery0, },
  1698. { start_auth_opal_session, opal_session },
  1699. { get_active_key, &opal_session->opal_key.lr },
  1700. { gen_key, },
  1701. { end_opal_session, },
  1702. { NULL, }
  1703. };
  1704. int ret;
  1705. mutex_lock(&dev->dev_lock);
  1706. setup_opal_dev(dev, erase_steps);
  1707. ret = next(dev);
  1708. mutex_unlock(&dev->dev_lock);
  1709. return ret;
  1710. }
  1711. static int opal_erase_locking_range(struct opal_dev *dev,
  1712. struct opal_session_info *opal_session)
  1713. {
  1714. const struct opal_step erase_steps[] = {
  1715. { opal_discovery0, },
  1716. { start_auth_opal_session, opal_session },
  1717. { erase_locking_range, opal_session },
  1718. { end_opal_session, },
  1719. { NULL, }
  1720. };
  1721. int ret;
  1722. mutex_lock(&dev->dev_lock);
  1723. setup_opal_dev(dev, erase_steps);
  1724. ret = next(dev);
  1725. mutex_unlock(&dev->dev_lock);
  1726. return ret;
  1727. }
  1728. static int opal_enable_disable_shadow_mbr(struct opal_dev *dev,
  1729. struct opal_mbr_data *opal_mbr)
  1730. {
  1731. u8 enable_disable = opal_mbr->enable_disable == OPAL_MBR_ENABLE ?
  1732. OPAL_TRUE : OPAL_FALSE;
  1733. const struct opal_step mbr_steps[] = {
  1734. { opal_discovery0, },
  1735. { start_admin1LSP_opal_session, &opal_mbr->key },
  1736. { set_mbr_done, &enable_disable },
  1737. { end_opal_session, },
  1738. { start_admin1LSP_opal_session, &opal_mbr->key },
  1739. { set_mbr_enable_disable, &enable_disable },
  1740. { end_opal_session, },
  1741. { NULL, }
  1742. };
  1743. int ret;
  1744. if (opal_mbr->enable_disable != OPAL_MBR_ENABLE &&
  1745. opal_mbr->enable_disable != OPAL_MBR_DISABLE)
  1746. return -EINVAL;
  1747. mutex_lock(&dev->dev_lock);
  1748. setup_opal_dev(dev, mbr_steps);
  1749. ret = next(dev);
  1750. mutex_unlock(&dev->dev_lock);
  1751. return ret;
  1752. }
  1753. static int opal_save(struct opal_dev *dev, struct opal_lock_unlock *lk_unlk)
  1754. {
  1755. struct opal_suspend_data *suspend;
  1756. suspend = kzalloc(sizeof(*suspend), GFP_KERNEL);
  1757. if (!suspend)
  1758. return -ENOMEM;
  1759. suspend->unlk = *lk_unlk;
  1760. suspend->lr = lk_unlk->session.opal_key.lr;
  1761. mutex_lock(&dev->dev_lock);
  1762. setup_opal_dev(dev, NULL);
  1763. add_suspend_info(dev, suspend);
  1764. mutex_unlock(&dev->dev_lock);
  1765. return 0;
  1766. }
  1767. static int opal_add_user_to_lr(struct opal_dev *dev,
  1768. struct opal_lock_unlock *lk_unlk)
  1769. {
  1770. const struct opal_step steps[] = {
  1771. { opal_discovery0, },
  1772. { start_admin1LSP_opal_session, &lk_unlk->session.opal_key },
  1773. { add_user_to_lr, lk_unlk },
  1774. { end_opal_session, },
  1775. { NULL, }
  1776. };
  1777. int ret;
  1778. if (lk_unlk->l_state != OPAL_RO &&
  1779. lk_unlk->l_state != OPAL_RW) {
  1780. pr_debug("Locking state was not RO or RW\n");
  1781. return -EINVAL;
  1782. }
  1783. if (lk_unlk->session.who < OPAL_USER1 ||
  1784. lk_unlk->session.who > OPAL_USER9) {
  1785. pr_debug("Authority was not within the range of users: %d\n",
  1786. lk_unlk->session.who);
  1787. return -EINVAL;
  1788. }
  1789. if (lk_unlk->session.sum) {
  1790. pr_debug("%s not supported in sum. Use setup locking range\n",
  1791. __func__);
  1792. return -EINVAL;
  1793. }
  1794. mutex_lock(&dev->dev_lock);
  1795. setup_opal_dev(dev, steps);
  1796. ret = next(dev);
  1797. mutex_unlock(&dev->dev_lock);
  1798. return ret;
  1799. }
  1800. static int opal_reverttper(struct opal_dev *dev, struct opal_key *opal)
  1801. {
  1802. const struct opal_step revert_steps[] = {
  1803. { opal_discovery0, },
  1804. { start_SIDASP_opal_session, opal },
  1805. { revert_tper, }, /* controller will terminate session */
  1806. { NULL, }
  1807. };
  1808. int ret;
  1809. mutex_lock(&dev->dev_lock);
  1810. setup_opal_dev(dev, revert_steps);
  1811. ret = next(dev);
  1812. mutex_unlock(&dev->dev_lock);
  1813. /*
  1814. * If we successfully reverted lets clean
  1815. * any saved locking ranges.
  1816. */
  1817. if (!ret)
  1818. clean_opal_dev(dev);
  1819. return ret;
  1820. }
  1821. static int __opal_lock_unlock(struct opal_dev *dev,
  1822. struct opal_lock_unlock *lk_unlk)
  1823. {
  1824. const struct opal_step unlock_steps[] = {
  1825. { opal_discovery0, },
  1826. { start_auth_opal_session, &lk_unlk->session },
  1827. { lock_unlock_locking_range, lk_unlk },
  1828. { end_opal_session, },
  1829. { NULL, }
  1830. };
  1831. const struct opal_step unlock_sum_steps[] = {
  1832. { opal_discovery0, },
  1833. { start_auth_opal_session, &lk_unlk->session },
  1834. { lock_unlock_locking_range_sum, lk_unlk },
  1835. { end_opal_session, },
  1836. { NULL, }
  1837. };
  1838. dev->steps = lk_unlk->session.sum ? unlock_sum_steps : unlock_steps;
  1839. return next(dev);
  1840. }
  1841. static int __opal_set_mbr_done(struct opal_dev *dev, struct opal_key *key)
  1842. {
  1843. u8 mbr_done_tf = OPAL_TRUE;
  1844. const struct opal_step mbrdone_step [] = {
  1845. { opal_discovery0, },
  1846. { start_admin1LSP_opal_session, key },
  1847. { set_mbr_done, &mbr_done_tf },
  1848. { end_opal_session, },
  1849. { NULL, }
  1850. };
  1851. dev->steps = mbrdone_step;
  1852. return next(dev);
  1853. }
  1854. static int opal_lock_unlock(struct opal_dev *dev,
  1855. struct opal_lock_unlock *lk_unlk)
  1856. {
  1857. int ret;
  1858. if (lk_unlk->session.who < OPAL_ADMIN1 ||
  1859. lk_unlk->session.who > OPAL_USER9)
  1860. return -EINVAL;
  1861. mutex_lock(&dev->dev_lock);
  1862. ret = __opal_lock_unlock(dev, lk_unlk);
  1863. mutex_unlock(&dev->dev_lock);
  1864. return ret;
  1865. }
  1866. static int opal_take_ownership(struct opal_dev *dev, struct opal_key *opal)
  1867. {
  1868. const struct opal_step owner_steps[] = {
  1869. { opal_discovery0, },
  1870. { start_anybodyASP_opal_session, },
  1871. { get_msid_cpin_pin, },
  1872. { end_opal_session, },
  1873. { start_SIDASP_opal_session, opal },
  1874. { set_sid_cpin_pin, opal },
  1875. { end_opal_session, },
  1876. { NULL, }
  1877. };
  1878. int ret;
  1879. if (!dev)
  1880. return -ENODEV;
  1881. mutex_lock(&dev->dev_lock);
  1882. setup_opal_dev(dev, owner_steps);
  1883. ret = next(dev);
  1884. mutex_unlock(&dev->dev_lock);
  1885. return ret;
  1886. }
  1887. static int opal_activate_lsp(struct opal_dev *dev, struct opal_lr_act *opal_lr_act)
  1888. {
  1889. const struct opal_step active_steps[] = {
  1890. { opal_discovery0, },
  1891. { start_SIDASP_opal_session, &opal_lr_act->key },
  1892. { get_lsp_lifecycle, },
  1893. { activate_lsp, opal_lr_act },
  1894. { end_opal_session, },
  1895. { NULL, }
  1896. };
  1897. int ret;
  1898. if (!opal_lr_act->num_lrs || opal_lr_act->num_lrs > OPAL_MAX_LRS)
  1899. return -EINVAL;
  1900. mutex_lock(&dev->dev_lock);
  1901. setup_opal_dev(dev, active_steps);
  1902. ret = next(dev);
  1903. mutex_unlock(&dev->dev_lock);
  1904. return ret;
  1905. }
  1906. static int opal_setup_locking_range(struct opal_dev *dev,
  1907. struct opal_user_lr_setup *opal_lrs)
  1908. {
  1909. const struct opal_step lr_steps[] = {
  1910. { opal_discovery0, },
  1911. { start_auth_opal_session, &opal_lrs->session },
  1912. { setup_locking_range, opal_lrs },
  1913. { end_opal_session, },
  1914. { NULL, }
  1915. };
  1916. int ret;
  1917. mutex_lock(&dev->dev_lock);
  1918. setup_opal_dev(dev, lr_steps);
  1919. ret = next(dev);
  1920. mutex_unlock(&dev->dev_lock);
  1921. return ret;
  1922. }
  1923. static int opal_set_new_pw(struct opal_dev *dev, struct opal_new_pw *opal_pw)
  1924. {
  1925. const struct opal_step pw_steps[] = {
  1926. { opal_discovery0, },
  1927. { start_auth_opal_session, &opal_pw->session },
  1928. { set_new_pw, &opal_pw->new_user_pw },
  1929. { end_opal_session, },
  1930. { NULL }
  1931. };
  1932. int ret;
  1933. if (opal_pw->session.who < OPAL_ADMIN1 ||
  1934. opal_pw->session.who > OPAL_USER9 ||
  1935. opal_pw->new_user_pw.who < OPAL_ADMIN1 ||
  1936. opal_pw->new_user_pw.who > OPAL_USER9)
  1937. return -EINVAL;
  1938. mutex_lock(&dev->dev_lock);
  1939. setup_opal_dev(dev, pw_steps);
  1940. ret = next(dev);
  1941. mutex_unlock(&dev->dev_lock);
  1942. return ret;
  1943. }
  1944. static int opal_activate_user(struct opal_dev *dev,
  1945. struct opal_session_info *opal_session)
  1946. {
  1947. const struct opal_step act_steps[] = {
  1948. { opal_discovery0, },
  1949. { start_admin1LSP_opal_session, &opal_session->opal_key },
  1950. { internal_activate_user, opal_session },
  1951. { end_opal_session, },
  1952. { NULL, }
  1953. };
  1954. int ret;
  1955. /* We can't activate Admin1 it's active as manufactured */
  1956. if (opal_session->who < OPAL_USER1 ||
  1957. opal_session->who > OPAL_USER9) {
  1958. pr_debug("Who was not a valid user: %d\n", opal_session->who);
  1959. return -EINVAL;
  1960. }
  1961. mutex_lock(&dev->dev_lock);
  1962. setup_opal_dev(dev, act_steps);
  1963. ret = next(dev);
  1964. mutex_unlock(&dev->dev_lock);
  1965. return ret;
  1966. }
  1967. bool opal_unlock_from_suspend(struct opal_dev *dev)
  1968. {
  1969. struct opal_suspend_data *suspend;
  1970. bool was_failure = false;
  1971. int ret = 0;
  1972. if (!dev)
  1973. return false;
  1974. if (!dev->supported)
  1975. return false;
  1976. mutex_lock(&dev->dev_lock);
  1977. setup_opal_dev(dev, NULL);
  1978. list_for_each_entry(suspend, &dev->unlk_lst, node) {
  1979. dev->tsn = 0;
  1980. dev->hsn = 0;
  1981. ret = __opal_lock_unlock(dev, &suspend->unlk);
  1982. if (ret) {
  1983. pr_debug("Failed to unlock LR %hhu with sum %d\n",
  1984. suspend->unlk.session.opal_key.lr,
  1985. suspend->unlk.session.sum);
  1986. was_failure = true;
  1987. }
  1988. if (dev->mbr_enabled) {
  1989. ret = __opal_set_mbr_done(dev, &suspend->unlk.session.opal_key);
  1990. if (ret)
  1991. pr_debug("Failed to set MBR Done in S3 resume\n");
  1992. }
  1993. }
  1994. mutex_unlock(&dev->dev_lock);
  1995. return was_failure;
  1996. }
  1997. EXPORT_SYMBOL(opal_unlock_from_suspend);
  1998. int sed_ioctl(struct opal_dev *dev, unsigned int cmd, void __user *arg)
  1999. {
  2000. void *p;
  2001. int ret = -ENOTTY;
  2002. if (!capable(CAP_SYS_ADMIN))
  2003. return -EACCES;
  2004. if (!dev)
  2005. return -ENOTSUPP;
  2006. if (!dev->supported)
  2007. return -ENOTSUPP;
  2008. p = memdup_user(arg, _IOC_SIZE(cmd));
  2009. if (IS_ERR(p))
  2010. return PTR_ERR(p);
  2011. switch (cmd) {
  2012. case IOC_OPAL_SAVE:
  2013. ret = opal_save(dev, p);
  2014. break;
  2015. case IOC_OPAL_LOCK_UNLOCK:
  2016. ret = opal_lock_unlock(dev, p);
  2017. break;
  2018. case IOC_OPAL_TAKE_OWNERSHIP:
  2019. ret = opal_take_ownership(dev, p);
  2020. break;
  2021. case IOC_OPAL_ACTIVATE_LSP:
  2022. ret = opal_activate_lsp(dev, p);
  2023. break;
  2024. case IOC_OPAL_SET_PW:
  2025. ret = opal_set_new_pw(dev, p);
  2026. break;
  2027. case IOC_OPAL_ACTIVATE_USR:
  2028. ret = opal_activate_user(dev, p);
  2029. break;
  2030. case IOC_OPAL_REVERT_TPR:
  2031. ret = opal_reverttper(dev, p);
  2032. break;
  2033. case IOC_OPAL_LR_SETUP:
  2034. ret = opal_setup_locking_range(dev, p);
  2035. break;
  2036. case IOC_OPAL_ADD_USR_TO_LR:
  2037. ret = opal_add_user_to_lr(dev, p);
  2038. break;
  2039. case IOC_OPAL_ENABLE_DISABLE_MBR:
  2040. ret = opal_enable_disable_shadow_mbr(dev, p);
  2041. break;
  2042. case IOC_OPAL_ERASE_LR:
  2043. ret = opal_erase_locking_range(dev, p);
  2044. break;
  2045. case IOC_OPAL_SECURE_ERASE_LR:
  2046. ret = opal_secure_erase_locking_range(dev, p);
  2047. break;
  2048. default:
  2049. break;
  2050. }
  2051. kfree(p);
  2052. return ret;
  2053. }
  2054. EXPORT_SYMBOL_GPL(sed_ioctl);