1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467 |
- /*
- * Copyright © 2016 Intel Corporation
- *
- * Authors:
- * Scott Bauer <scott.bauer@intel.com>
- * Rafael Antognolli <rafael.antognolli@intel.com>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms and conditions of the GNU General Public License,
- * version 2, as published by the Free Software Foundation.
- *
- * This program is distributed in the hope it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details.
- */
- #define pr_fmt(fmt) KBUILD_MODNAME ":OPAL: " fmt
- #include <linux/delay.h>
- #include <linux/device.h>
- #include <linux/kernel.h>
- #include <linux/list.h>
- #include <linux/genhd.h>
- #include <linux/slab.h>
- #include <linux/uaccess.h>
- #include <uapi/linux/sed-opal.h>
- #include <linux/sed-opal.h>
- #include <linux/string.h>
- #include <linux/kdev_t.h>
- #include "opal_proto.h"
- #define IO_BUFFER_LENGTH 2048
- #define MAX_TOKS 64
- struct opal_step {
- int (*fn)(struct opal_dev *dev, void *data);
- void *data;
- };
- typedef int (cont_fn)(struct opal_dev *dev);
- enum opal_atom_width {
- OPAL_WIDTH_TINY,
- OPAL_WIDTH_SHORT,
- OPAL_WIDTH_MEDIUM,
- OPAL_WIDTH_LONG,
- OPAL_WIDTH_TOKEN
- };
- /*
- * On the parsed response, we don't store again the toks that are already
- * stored in the response buffer. Instead, for each token, we just store a
- * pointer to the position in the buffer where the token starts, and the size
- * of the token in bytes.
- */
- struct opal_resp_tok {
- const u8 *pos;
- size_t len;
- enum opal_response_token type;
- enum opal_atom_width width;
- union {
- u64 u;
- s64 s;
- } stored;
- };
- /*
- * From the response header it's not possible to know how many tokens there are
- * on the payload. So we hardcode that the maximum will be MAX_TOKS, and later
- * if we start dealing with messages that have more than that, we can increase
- * this number. This is done to avoid having to make two passes through the
- * response, the first one counting how many tokens we have and the second one
- * actually storing the positions.
- */
- struct parsed_resp {
- int num;
- struct opal_resp_tok toks[MAX_TOKS];
- };
- struct opal_dev {
- bool supported;
- bool mbr_enabled;
- void *data;
- sec_send_recv *send_recv;
- const struct opal_step *steps;
- struct mutex dev_lock;
- u16 comid;
- u32 hsn;
- u32 tsn;
- u64 align;
- u64 lowest_lba;
- size_t pos;
- u8 cmd[IO_BUFFER_LENGTH];
- u8 resp[IO_BUFFER_LENGTH];
- struct parsed_resp parsed;
- size_t prev_d_len;
- void *prev_data;
- struct list_head unlk_lst;
- };
- static const u8 opaluid[][OPAL_UID_LENGTH] = {
- /* users */
- [OPAL_SMUID_UID] =
- { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff },
- [OPAL_THISSP_UID] =
- { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
- [OPAL_ADMINSP_UID] =
- { 0x00, 0x00, 0x02, 0x05, 0x00, 0x00, 0x00, 0x01 },
- [OPAL_LOCKINGSP_UID] =
- { 0x00, 0x00, 0x02, 0x05, 0x00, 0x00, 0x00, 0x02 },
- [OPAL_ENTERPRISE_LOCKINGSP_UID] =
- { 0x00, 0x00, 0x02, 0x05, 0x00, 0x01, 0x00, 0x01 },
- [OPAL_ANYBODY_UID] =
- { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x01 },
- [OPAL_SID_UID] =
- { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x06 },
- [OPAL_ADMIN1_UID] =
- { 0x00, 0x00, 0x00, 0x09, 0x00, 0x01, 0x00, 0x01 },
- [OPAL_USER1_UID] =
- { 0x00, 0x00, 0x00, 0x09, 0x00, 0x03, 0x00, 0x01 },
- [OPAL_USER2_UID] =
- { 0x00, 0x00, 0x00, 0x09, 0x00, 0x03, 0x00, 0x02 },
- [OPAL_PSID_UID] =
- { 0x00, 0x00, 0x00, 0x09, 0x00, 0x01, 0xff, 0x01 },
- [OPAL_ENTERPRISE_BANDMASTER0_UID] =
- { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x80, 0x01 },
- [OPAL_ENTERPRISE_ERASEMASTER_UID] =
- { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x84, 0x01 },
- /* tables */
- [OPAL_LOCKINGRANGE_GLOBAL] =
- { 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00, 0x01 },
- [OPAL_LOCKINGRANGE_ACE_RDLOCKED] =
- { 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0xE0, 0x01 },
- [OPAL_LOCKINGRANGE_ACE_WRLOCKED] =
- { 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0xE8, 0x01 },
- [OPAL_MBRCONTROL] =
- { 0x00, 0x00, 0x08, 0x03, 0x00, 0x00, 0x00, 0x01 },
- [OPAL_MBR] =
- { 0x00, 0x00, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00 },
- [OPAL_AUTHORITY_TABLE] =
- { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00},
- [OPAL_C_PIN_TABLE] =
- { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x00},
- [OPAL_LOCKING_INFO_TABLE] =
- { 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x01 },
- [OPAL_ENTERPRISE_LOCKING_INFO_TABLE] =
- { 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00 },
- /* C_PIN_TABLE object ID's */
- [OPAL_C_PIN_MSID] =
- { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x84, 0x02},
- [OPAL_C_PIN_SID] =
- { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x01},
- [OPAL_C_PIN_ADMIN1] =
- { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x01, 0x00, 0x01},
- /* half UID's (only first 4 bytes used) */
- [OPAL_HALF_UID_AUTHORITY_OBJ_REF] =
- { 0x00, 0x00, 0x0C, 0x05, 0xff, 0xff, 0xff, 0xff },
- [OPAL_HALF_UID_BOOLEAN_ACE] =
- { 0x00, 0x00, 0x04, 0x0E, 0xff, 0xff, 0xff, 0xff },
- /* special value for omitted optional parameter */
- [OPAL_UID_HEXFF] =
- { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
- };
- /*
- * TCG Storage SSC Methods.
- * Derived from: TCG_Storage_Architecture_Core_Spec_v2.01_r1.00
- * Section: 6.3 Assigned UIDs
- */
- static const u8 opalmethod[][OPAL_UID_LENGTH] = {
- [OPAL_PROPERTIES] =
- { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x01 },
- [OPAL_STARTSESSION] =
- { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x02 },
- [OPAL_REVERT] =
- { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x02, 0x02 },
- [OPAL_ACTIVATE] =
- { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x02, 0x03 },
- [OPAL_EGET] =
- { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x06 },
- [OPAL_ESET] =
- { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x07 },
- [OPAL_NEXT] =
- { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x08 },
- [OPAL_EAUTHENTICATE] =
- { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0c },
- [OPAL_GETACL] =
- { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0d },
- [OPAL_GENKEY] =
- { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x10 },
- [OPAL_REVERTSP] =
- { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x11 },
- [OPAL_GET] =
- { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x16 },
- [OPAL_SET] =
- { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x17 },
- [OPAL_AUTHENTICATE] =
- { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x1c },
- [OPAL_RANDOM] =
- { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x01 },
- [OPAL_ERASE] =
- { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x08, 0x03 },
- };
- static int end_opal_session_error(struct opal_dev *dev);
- struct opal_suspend_data {
- struct opal_lock_unlock unlk;
- u8 lr;
- struct list_head node;
- };
- /*
- * Derived from:
- * TCG_Storage_Architecture_Core_Spec_v2.01_r1.00
- * Section: 5.1.5 Method Status Codes
- */
- static const char * const opal_errors[] = {
- "Success",
- "Not Authorized",
- "Unknown Error",
- "SP Busy",
- "SP Failed",
- "SP Disabled",
- "SP Frozen",
- "No Sessions Available",
- "Uniqueness Conflict",
- "Insufficient Space",
- "Insufficient Rows",
- "Invalid Function",
- "Invalid Parameter",
- "Invalid Reference",
- "Unknown Error",
- "TPER Malfunction",
- "Transaction Failure",
- "Response Overflow",
- "Authority Locked Out",
- };
- static const char *opal_error_to_human(int error)
- {
- if (error == 0x3f)
- return "Failed";
- if (error >= ARRAY_SIZE(opal_errors) || error < 0)
- return "Unknown Error";
- return opal_errors[error];
- }
- static void print_buffer(const u8 *ptr, u32 length)
- {
- #ifdef DEBUG
- print_hex_dump_bytes("OPAL: ", DUMP_PREFIX_OFFSET, ptr, length);
- pr_debug("\n");
- #endif
- }
- static bool check_tper(const void *data)
- {
- const struct d0_tper_features *tper = data;
- u8 flags = tper->supported_features;
- if (!(flags & TPER_SYNC_SUPPORTED)) {
- pr_debug("TPer sync not supported. flags = %d\n",
- tper->supported_features);
- return false;
- }
- return true;
- }
- static bool check_mbrenabled(const void *data)
- {
- const struct d0_locking_features *lfeat = data;
- u8 sup_feat = lfeat->supported_features;
- return !!(sup_feat & MBR_ENABLED_MASK);
- }
- static bool check_sum(const void *data)
- {
- const struct d0_single_user_mode *sum = data;
- u32 nlo = be32_to_cpu(sum->num_locking_objects);
- if (nlo == 0) {
- pr_debug("Need at least one locking object.\n");
- return false;
- }
- pr_debug("Number of locking objects: %d\n", nlo);
- return true;
- }
- static u16 get_comid_v100(const void *data)
- {
- const struct d0_opal_v100 *v100 = data;
- return be16_to_cpu(v100->baseComID);
- }
- static u16 get_comid_v200(const void *data)
- {
- const struct d0_opal_v200 *v200 = data;
- return be16_to_cpu(v200->baseComID);
- }
- static int opal_send_cmd(struct opal_dev *dev)
- {
- return dev->send_recv(dev->data, dev->comid, TCG_SECP_01,
- dev->cmd, IO_BUFFER_LENGTH,
- true);
- }
- static int opal_recv_cmd(struct opal_dev *dev)
- {
- return dev->send_recv(dev->data, dev->comid, TCG_SECP_01,
- dev->resp, IO_BUFFER_LENGTH,
- false);
- }
- static int opal_recv_check(struct opal_dev *dev)
- {
- size_t buflen = IO_BUFFER_LENGTH;
- void *buffer = dev->resp;
- struct opal_header *hdr = buffer;
- int ret;
- do {
- pr_debug("Sent OPAL command: outstanding=%d, minTransfer=%d\n",
- hdr->cp.outstandingData,
- hdr->cp.minTransfer);
- if (hdr->cp.outstandingData == 0 ||
- hdr->cp.minTransfer != 0)
- return 0;
- memset(buffer, 0, buflen);
- ret = opal_recv_cmd(dev);
- } while (!ret);
- return ret;
- }
- static int opal_send_recv(struct opal_dev *dev, cont_fn *cont)
- {
- int ret;
- ret = opal_send_cmd(dev);
- if (ret)
- return ret;
- ret = opal_recv_cmd(dev);
- if (ret)
- return ret;
- ret = opal_recv_check(dev);
- if (ret)
- return ret;
- return cont(dev);
- }
- static void check_geometry(struct opal_dev *dev, const void *data)
- {
- const struct d0_geometry_features *geo = data;
- dev->align = geo->alignment_granularity;
- dev->lowest_lba = geo->lowest_aligned_lba;
- }
- static int next(struct opal_dev *dev)
- {
- const struct opal_step *step;
- int state = 0, error = 0;
- do {
- step = &dev->steps[state];
- if (!step->fn)
- break;
- error = step->fn(dev, step->data);
- if (error) {
- pr_debug("Error on step function: %d with error %d: %s\n",
- state, error,
- opal_error_to_human(error));
- /* For each OPAL command we do a discovery0 then we
- * start some sort of session.
- * If we haven't passed state 1 then there was an error
- * on discovery0 or during the attempt to start a
- * session. Therefore we shouldn't attempt to terminate
- * a session, as one has not yet been created.
- */
- if (state > 1) {
- end_opal_session_error(dev);
- return error;
- }
- }
- state++;
- } while (!error);
- return error;
- }
- static int opal_discovery0_end(struct opal_dev *dev)
- {
- bool found_com_id = false, supported = true, single_user = false;
- const struct d0_header *hdr = (struct d0_header *)dev->resp;
- const u8 *epos = dev->resp, *cpos = dev->resp;
- u16 comid = 0;
- u32 hlen = be32_to_cpu(hdr->length);
- print_buffer(dev->resp, hlen);
- dev->mbr_enabled = false;
- if (hlen > IO_BUFFER_LENGTH - sizeof(*hdr)) {
- pr_debug("Discovery length overflows buffer (%zu+%u)/%u\n",
- sizeof(*hdr), hlen, IO_BUFFER_LENGTH);
- return -EFAULT;
- }
- epos += hlen; /* end of buffer */
- cpos += sizeof(*hdr); /* current position on buffer */
- while (cpos < epos && supported) {
- const struct d0_features *body =
- (const struct d0_features *)cpos;
- switch (be16_to_cpu(body->code)) {
- case FC_TPER:
- supported = check_tper(body->features);
- break;
- case FC_SINGLEUSER:
- single_user = check_sum(body->features);
- break;
- case FC_GEOMETRY:
- check_geometry(dev, body);
- break;
- case FC_LOCKING:
- dev->mbr_enabled = check_mbrenabled(body->features);
- break;
- case FC_ENTERPRISE:
- case FC_DATASTORE:
- /* some ignored properties */
- pr_debug("Found OPAL feature description: %d\n",
- be16_to_cpu(body->code));
- break;
- case FC_OPALV100:
- comid = get_comid_v100(body->features);
- found_com_id = true;
- break;
- case FC_OPALV200:
- comid = get_comid_v200(body->features);
- found_com_id = true;
- break;
- case 0xbfff ... 0xffff:
- /* vendor specific, just ignore */
- break;
- default:
- pr_debug("OPAL Unknown feature: %d\n",
- be16_to_cpu(body->code));
- }
- cpos += body->length + 4;
- }
- if (!supported) {
- pr_debug("This device is not Opal enabled. Not Supported!\n");
- return -EOPNOTSUPP;
- }
- if (!single_user)
- pr_debug("Device doesn't support single user mode\n");
- if (!found_com_id) {
- pr_debug("Could not find OPAL comid for device. Returning early\n");
- return -EOPNOTSUPP;
- }
- dev->comid = comid;
- return 0;
- }
- static int opal_discovery0(struct opal_dev *dev, void *data)
- {
- int ret;
- memset(dev->resp, 0, IO_BUFFER_LENGTH);
- dev->comid = OPAL_DISCOVERY_COMID;
- ret = opal_recv_cmd(dev);
- if (ret)
- return ret;
- return opal_discovery0_end(dev);
- }
- static void add_token_u8(int *err, struct opal_dev *cmd, u8 tok)
- {
- if (*err)
- return;
- if (cmd->pos >= IO_BUFFER_LENGTH - 1) {
- pr_debug("Error adding u8: end of buffer.\n");
- *err = -ERANGE;
- return;
- }
- cmd->cmd[cmd->pos++] = tok;
- }
- static void add_short_atom_header(struct opal_dev *cmd, bool bytestring,
- bool has_sign, int len)
- {
- u8 atom;
- int err = 0;
- atom = SHORT_ATOM_ID;
- atom |= bytestring ? SHORT_ATOM_BYTESTRING : 0;
- atom |= has_sign ? SHORT_ATOM_SIGNED : 0;
- atom |= len & SHORT_ATOM_LEN_MASK;
- add_token_u8(&err, cmd, atom);
- }
- static void add_medium_atom_header(struct opal_dev *cmd, bool bytestring,
- bool has_sign, int len)
- {
- u8 header0;
- header0 = MEDIUM_ATOM_ID;
- header0 |= bytestring ? MEDIUM_ATOM_BYTESTRING : 0;
- header0 |= has_sign ? MEDIUM_ATOM_SIGNED : 0;
- header0 |= (len >> 8) & MEDIUM_ATOM_LEN_MASK;
- cmd->cmd[cmd->pos++] = header0;
- cmd->cmd[cmd->pos++] = len;
- }
- static void add_token_u64(int *err, struct opal_dev *cmd, u64 number)
- {
- size_t len;
- int msb;
- if (!(number & ~TINY_ATOM_DATA_MASK)) {
- add_token_u8(err, cmd, number);
- return;
- }
- msb = fls64(number);
- len = DIV_ROUND_UP(msb, 8);
- if (cmd->pos >= IO_BUFFER_LENGTH - len - 1) {
- pr_debug("Error adding u64: end of buffer.\n");
- *err = -ERANGE;
- return;
- }
- add_short_atom_header(cmd, false, false, len);
- while (len--)
- add_token_u8(err, cmd, number >> (len * 8));
- }
- static void add_token_bytestring(int *err, struct opal_dev *cmd,
- const u8 *bytestring, size_t len)
- {
- size_t header_len = 1;
- bool is_short_atom = true;
- if (*err)
- return;
- if (len & ~SHORT_ATOM_LEN_MASK) {
- header_len = 2;
- is_short_atom = false;
- }
- if (len >= IO_BUFFER_LENGTH - cmd->pos - header_len) {
- pr_debug("Error adding bytestring: end of buffer.\n");
- *err = -ERANGE;
- return;
- }
- if (is_short_atom)
- add_short_atom_header(cmd, true, false, len);
- else
- add_medium_atom_header(cmd, true, false, len);
- memcpy(&cmd->cmd[cmd->pos], bytestring, len);
- cmd->pos += len;
- }
- static int build_locking_range(u8 *buffer, size_t length, u8 lr)
- {
- if (length > OPAL_UID_LENGTH) {
- pr_debug("Can't build locking range. Length OOB\n");
- return -ERANGE;
- }
- memcpy(buffer, opaluid[OPAL_LOCKINGRANGE_GLOBAL], OPAL_UID_LENGTH);
- if (lr == 0)
- return 0;
- buffer[5] = LOCKING_RANGE_NON_GLOBAL;
- buffer[7] = lr;
- return 0;
- }
- static int build_locking_user(u8 *buffer, size_t length, u8 lr)
- {
- if (length > OPAL_UID_LENGTH) {
- pr_debug("Can't build locking range user, Length OOB\n");
- return -ERANGE;
- }
- memcpy(buffer, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
- buffer[7] = lr + 1;
- return 0;
- }
- static void set_comid(struct opal_dev *cmd, u16 comid)
- {
- struct opal_header *hdr = (struct opal_header *)cmd->cmd;
- hdr->cp.extendedComID[0] = comid >> 8;
- hdr->cp.extendedComID[1] = comid;
- hdr->cp.extendedComID[2] = 0;
- hdr->cp.extendedComID[3] = 0;
- }
- static int cmd_finalize(struct opal_dev *cmd, u32 hsn, u32 tsn)
- {
- struct opal_header *hdr;
- int err = 0;
- add_token_u8(&err, cmd, OPAL_ENDOFDATA);
- add_token_u8(&err, cmd, OPAL_STARTLIST);
- add_token_u8(&err, cmd, 0);
- add_token_u8(&err, cmd, 0);
- add_token_u8(&err, cmd, 0);
- add_token_u8(&err, cmd, OPAL_ENDLIST);
- if (err) {
- pr_debug("Error finalizing command.\n");
- return -EFAULT;
- }
- hdr = (struct opal_header *) cmd->cmd;
- hdr->pkt.tsn = cpu_to_be32(tsn);
- hdr->pkt.hsn = cpu_to_be32(hsn);
- hdr->subpkt.length = cpu_to_be32(cmd->pos - sizeof(*hdr));
- while (cmd->pos % 4) {
- if (cmd->pos >= IO_BUFFER_LENGTH) {
- pr_debug("Error: Buffer overrun\n");
- return -ERANGE;
- }
- cmd->cmd[cmd->pos++] = 0;
- }
- hdr->pkt.length = cpu_to_be32(cmd->pos - sizeof(hdr->cp) -
- sizeof(hdr->pkt));
- hdr->cp.length = cpu_to_be32(cmd->pos - sizeof(hdr->cp));
- return 0;
- }
- static const struct opal_resp_tok *response_get_token(
- const struct parsed_resp *resp,
- int n)
- {
- const struct opal_resp_tok *tok;
- if (n >= resp->num) {
- pr_debug("Token number doesn't exist: %d, resp: %d\n",
- n, resp->num);
- return ERR_PTR(-EINVAL);
- }
- tok = &resp->toks[n];
- if (tok->len == 0) {
- pr_debug("Token length must be non-zero\n");
- return ERR_PTR(-EINVAL);
- }
- return tok;
- }
- static ssize_t response_parse_tiny(struct opal_resp_tok *tok,
- const u8 *pos)
- {
- tok->pos = pos;
- tok->len = 1;
- tok->width = OPAL_WIDTH_TINY;
- if (pos[0] & TINY_ATOM_SIGNED) {
- tok->type = OPAL_DTA_TOKENID_SINT;
- } else {
- tok->type = OPAL_DTA_TOKENID_UINT;
- tok->stored.u = pos[0] & 0x3f;
- }
- return tok->len;
- }
- static ssize_t response_parse_short(struct opal_resp_tok *tok,
- const u8 *pos)
- {
- tok->pos = pos;
- tok->len = (pos[0] & SHORT_ATOM_LEN_MASK) + 1;
- tok->width = OPAL_WIDTH_SHORT;
- if (pos[0] & SHORT_ATOM_BYTESTRING) {
- tok->type = OPAL_DTA_TOKENID_BYTESTRING;
- } else if (pos[0] & SHORT_ATOM_SIGNED) {
- tok->type = OPAL_DTA_TOKENID_SINT;
- } else {
- u64 u_integer = 0;
- ssize_t i, b = 0;
- tok->type = OPAL_DTA_TOKENID_UINT;
- if (tok->len > 9) {
- pr_debug("uint64 with more than 8 bytes\n");
- return -EINVAL;
- }
- for (i = tok->len - 1; i > 0; i--) {
- u_integer |= ((u64)pos[i] << (8 * b));
- b++;
- }
- tok->stored.u = u_integer;
- }
- return tok->len;
- }
- static ssize_t response_parse_medium(struct opal_resp_tok *tok,
- const u8 *pos)
- {
- tok->pos = pos;
- tok->len = (((pos[0] & MEDIUM_ATOM_LEN_MASK) << 8) | pos[1]) + 2;
- tok->width = OPAL_WIDTH_MEDIUM;
- if (pos[0] & MEDIUM_ATOM_BYTESTRING)
- tok->type = OPAL_DTA_TOKENID_BYTESTRING;
- else if (pos[0] & MEDIUM_ATOM_SIGNED)
- tok->type = OPAL_DTA_TOKENID_SINT;
- else
- tok->type = OPAL_DTA_TOKENID_UINT;
- return tok->len;
- }
- static ssize_t response_parse_long(struct opal_resp_tok *tok,
- const u8 *pos)
- {
- tok->pos = pos;
- tok->len = ((pos[1] << 16) | (pos[2] << 8) | pos[3]) + 4;
- tok->width = OPAL_WIDTH_LONG;
- if (pos[0] & LONG_ATOM_BYTESTRING)
- tok->type = OPAL_DTA_TOKENID_BYTESTRING;
- else if (pos[0] & LONG_ATOM_SIGNED)
- tok->type = OPAL_DTA_TOKENID_SINT;
- else
- tok->type = OPAL_DTA_TOKENID_UINT;
- return tok->len;
- }
- static ssize_t response_parse_token(struct opal_resp_tok *tok,
- const u8 *pos)
- {
- tok->pos = pos;
- tok->len = 1;
- tok->type = OPAL_DTA_TOKENID_TOKEN;
- tok->width = OPAL_WIDTH_TOKEN;
- return tok->len;
- }
- static int response_parse(const u8 *buf, size_t length,
- struct parsed_resp *resp)
- {
- const struct opal_header *hdr;
- struct opal_resp_tok *iter;
- int num_entries = 0;
- int total;
- ssize_t token_length;
- const u8 *pos;
- u32 clen, plen, slen;
- if (!buf)
- return -EFAULT;
- if (!resp)
- return -EFAULT;
- hdr = (struct opal_header *)buf;
- pos = buf;
- pos += sizeof(*hdr);
- clen = be32_to_cpu(hdr->cp.length);
- plen = be32_to_cpu(hdr->pkt.length);
- slen = be32_to_cpu(hdr->subpkt.length);
- pr_debug("Response size: cp: %u, pkt: %u, subpkt: %u\n",
- clen, plen, slen);
- if (clen == 0 || plen == 0 || slen == 0 ||
- slen > IO_BUFFER_LENGTH - sizeof(*hdr)) {
- pr_debug("Bad header length. cp: %u, pkt: %u, subpkt: %u\n",
- clen, plen, slen);
- print_buffer(pos, sizeof(*hdr));
- return -EINVAL;
- }
- if (pos > buf + length)
- return -EFAULT;
- iter = resp->toks;
- total = slen;
- print_buffer(pos, total);
- while (total > 0) {
- if (pos[0] <= TINY_ATOM_BYTE) /* tiny atom */
- token_length = response_parse_tiny(iter, pos);
- else if (pos[0] <= SHORT_ATOM_BYTE) /* short atom */
- token_length = response_parse_short(iter, pos);
- else if (pos[0] <= MEDIUM_ATOM_BYTE) /* medium atom */
- token_length = response_parse_medium(iter, pos);
- else if (pos[0] <= LONG_ATOM_BYTE) /* long atom */
- token_length = response_parse_long(iter, pos);
- else /* TOKEN */
- token_length = response_parse_token(iter, pos);
- if (token_length < 0)
- return token_length;
- pos += token_length;
- total -= token_length;
- iter++;
- num_entries++;
- }
- if (num_entries == 0) {
- pr_debug("Couldn't parse response.\n");
- return -EINVAL;
- }
- resp->num = num_entries;
- return 0;
- }
- static size_t response_get_string(const struct parsed_resp *resp, int n,
- const char **store)
- {
- u8 skip;
- const struct opal_resp_tok *token;
- *store = NULL;
- if (!resp) {
- pr_debug("Response is NULL\n");
- return 0;
- }
- if (n >= resp->num) {
- pr_debug("Response has %d tokens. Can't access %d\n",
- resp->num, n);
- return 0;
- }
- token = &resp->toks[n];
- if (token->type != OPAL_DTA_TOKENID_BYTESTRING) {
- pr_debug("Token is not a byte string!\n");
- return 0;
- }
- switch (token->width) {
- case OPAL_WIDTH_TINY:
- case OPAL_WIDTH_SHORT:
- skip = 1;
- break;
- case OPAL_WIDTH_MEDIUM:
- skip = 2;
- break;
- case OPAL_WIDTH_LONG:
- skip = 4;
- break;
- default:
- pr_debug("Token has invalid width!\n");
- return 0;
- }
- *store = token->pos + skip;
- return token->len - skip;
- }
- static u64 response_get_u64(const struct parsed_resp *resp, int n)
- {
- if (!resp) {
- pr_debug("Response is NULL\n");
- return 0;
- }
- if (n >= resp->num) {
- pr_debug("Response has %d tokens. Can't access %d\n",
- resp->num, n);
- return 0;
- }
- if (resp->toks[n].type != OPAL_DTA_TOKENID_UINT) {
- pr_debug("Token is not unsigned it: %d\n",
- resp->toks[n].type);
- return 0;
- }
- if (!(resp->toks[n].width == OPAL_WIDTH_TINY ||
- resp->toks[n].width == OPAL_WIDTH_SHORT)) {
- pr_debug("Atom is not short or tiny: %d\n",
- resp->toks[n].width);
- return 0;
- }
- return resp->toks[n].stored.u;
- }
- static bool response_token_matches(const struct opal_resp_tok *token, u8 match)
- {
- if (IS_ERR(token) ||
- token->type != OPAL_DTA_TOKENID_TOKEN ||
- token->pos[0] != match)
- return false;
- return true;
- }
- static u8 response_status(const struct parsed_resp *resp)
- {
- const struct opal_resp_tok *tok;
- tok = response_get_token(resp, 0);
- if (response_token_matches(tok, OPAL_ENDOFSESSION))
- return 0;
- if (resp->num < 5)
- return DTAERROR_NO_METHOD_STATUS;
- tok = response_get_token(resp, resp->num - 5);
- if (!response_token_matches(tok, OPAL_STARTLIST))
- return DTAERROR_NO_METHOD_STATUS;
- tok = response_get_token(resp, resp->num - 1);
- if (!response_token_matches(tok, OPAL_ENDLIST))
- return DTAERROR_NO_METHOD_STATUS;
- return response_get_u64(resp, resp->num - 4);
- }
- /* Parses and checks for errors */
- static int parse_and_check_status(struct opal_dev *dev)
- {
- int error;
- print_buffer(dev->cmd, dev->pos);
- error = response_parse(dev->resp, IO_BUFFER_LENGTH, &dev->parsed);
- if (error) {
- pr_debug("Couldn't parse response.\n");
- return error;
- }
- return response_status(&dev->parsed);
- }
- static void clear_opal_cmd(struct opal_dev *dev)
- {
- dev->pos = sizeof(struct opal_header);
- memset(dev->cmd, 0, IO_BUFFER_LENGTH);
- }
- static int start_opal_session_cont(struct opal_dev *dev)
- {
- u32 hsn, tsn;
- int error = 0;
- error = parse_and_check_status(dev);
- if (error)
- return error;
- hsn = response_get_u64(&dev->parsed, 4);
- tsn = response_get_u64(&dev->parsed, 5);
- if (hsn == 0 && tsn == 0) {
- pr_debug("Couldn't authenticate session\n");
- return -EPERM;
- }
- dev->hsn = hsn;
- dev->tsn = tsn;
- return 0;
- }
- static void add_suspend_info(struct opal_dev *dev,
- struct opal_suspend_data *sus)
- {
- struct opal_suspend_data *iter;
- list_for_each_entry(iter, &dev->unlk_lst, node) {
- if (iter->lr == sus->lr) {
- list_del(&iter->node);
- kfree(iter);
- break;
- }
- }
- list_add_tail(&sus->node, &dev->unlk_lst);
- }
- static int end_session_cont(struct opal_dev *dev)
- {
- dev->hsn = 0;
- dev->tsn = 0;
- return parse_and_check_status(dev);
- }
- static int finalize_and_send(struct opal_dev *dev, cont_fn cont)
- {
- int ret;
- ret = cmd_finalize(dev, dev->hsn, dev->tsn);
- if (ret) {
- pr_debug("Error finalizing command buffer: %d\n", ret);
- return ret;
- }
- print_buffer(dev->cmd, dev->pos);
- return opal_send_recv(dev, cont);
- }
- static int gen_key(struct opal_dev *dev, void *data)
- {
- u8 uid[OPAL_UID_LENGTH];
- int err = 0;
- clear_opal_cmd(dev);
- set_comid(dev, dev->comid);
- memcpy(uid, dev->prev_data, min(sizeof(uid), dev->prev_d_len));
- kfree(dev->prev_data);
- dev->prev_data = NULL;
- add_token_u8(&err, dev, OPAL_CALL);
- add_token_bytestring(&err, dev, uid, OPAL_UID_LENGTH);
- add_token_bytestring(&err, dev, opalmethod[OPAL_GENKEY],
- OPAL_UID_LENGTH);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- if (err) {
- pr_debug("Error building gen key command\n");
- return err;
- }
- return finalize_and_send(dev, parse_and_check_status);
- }
- static int get_active_key_cont(struct opal_dev *dev)
- {
- const char *activekey;
- size_t keylen;
- int error = 0;
- error = parse_and_check_status(dev);
- if (error)
- return error;
- keylen = response_get_string(&dev->parsed, 4, &activekey);
- if (!activekey) {
- pr_debug("%s: Couldn't extract the Activekey from the response\n",
- __func__);
- return OPAL_INVAL_PARAM;
- }
- dev->prev_data = kmemdup(activekey, keylen, GFP_KERNEL);
- if (!dev->prev_data)
- return -ENOMEM;
- dev->prev_d_len = keylen;
- return 0;
- }
- static int get_active_key(struct opal_dev *dev, void *data)
- {
- u8 uid[OPAL_UID_LENGTH];
- int err = 0;
- u8 *lr = data;
- clear_opal_cmd(dev);
- set_comid(dev, dev->comid);
- err = build_locking_range(uid, sizeof(uid), *lr);
- if (err)
- return err;
- err = 0;
- add_token_u8(&err, dev, OPAL_CALL);
- add_token_bytestring(&err, dev, uid, OPAL_UID_LENGTH);
- add_token_bytestring(&err, dev, opalmethod[OPAL_GET], OPAL_UID_LENGTH);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, 3); /* startCloumn */
- add_token_u8(&err, dev, 10); /* ActiveKey */
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, 4); /* endColumn */
- add_token_u8(&err, dev, 10); /* ActiveKey */
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- if (err) {
- pr_debug("Error building get active key command\n");
- return err;
- }
- return finalize_and_send(dev, get_active_key_cont);
- }
- static int generic_lr_enable_disable(struct opal_dev *dev,
- u8 *uid, bool rle, bool wle,
- bool rl, bool wl)
- {
- int err = 0;
- add_token_u8(&err, dev, OPAL_CALL);
- add_token_bytestring(&err, dev, uid, OPAL_UID_LENGTH);
- add_token_bytestring(&err, dev, opalmethod[OPAL_SET], OPAL_UID_LENGTH);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, OPAL_VALUES);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, 5); /* ReadLockEnabled */
- add_token_u8(&err, dev, rle);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, 6); /* WriteLockEnabled */
- add_token_u8(&err, dev, wle);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, OPAL_READLOCKED);
- add_token_u8(&err, dev, rl);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, OPAL_WRITELOCKED);
- add_token_u8(&err, dev, wl);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- return err;
- }
- static inline int enable_global_lr(struct opal_dev *dev, u8 *uid,
- struct opal_user_lr_setup *setup)
- {
- int err;
- err = generic_lr_enable_disable(dev, uid, !!setup->RLE, !!setup->WLE,
- 0, 0);
- if (err)
- pr_debug("Failed to create enable global lr command\n");
- return err;
- }
- static int setup_locking_range(struct opal_dev *dev, void *data)
- {
- u8 uid[OPAL_UID_LENGTH];
- struct opal_user_lr_setup *setup = data;
- u8 lr;
- int err = 0;
- clear_opal_cmd(dev);
- set_comid(dev, dev->comid);
- lr = setup->session.opal_key.lr;
- err = build_locking_range(uid, sizeof(uid), lr);
- if (err)
- return err;
- if (lr == 0)
- err = enable_global_lr(dev, uid, setup);
- else {
- add_token_u8(&err, dev, OPAL_CALL);
- add_token_bytestring(&err, dev, uid, OPAL_UID_LENGTH);
- add_token_bytestring(&err, dev, opalmethod[OPAL_SET],
- OPAL_UID_LENGTH);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, OPAL_VALUES);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, 3); /* Ranges Start */
- add_token_u64(&err, dev, setup->range_start);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, 4); /* Ranges length */
- add_token_u64(&err, dev, setup->range_length);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, 5); /*ReadLockEnabled */
- add_token_u64(&err, dev, !!setup->RLE);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, 6); /*WriteLockEnabled*/
- add_token_u64(&err, dev, !!setup->WLE);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- }
- if (err) {
- pr_debug("Error building Setup Locking range command.\n");
- return err;
- }
- return finalize_and_send(dev, parse_and_check_status);
- }
- static int start_generic_opal_session(struct opal_dev *dev,
- enum opal_uid auth,
- enum opal_uid sp_type,
- const char *key,
- u8 key_len)
- {
- u32 hsn;
- int err = 0;
- if (key == NULL && auth != OPAL_ANYBODY_UID)
- return OPAL_INVAL_PARAM;
- clear_opal_cmd(dev);
- set_comid(dev, dev->comid);
- hsn = GENERIC_HOST_SESSION_NUM;
- add_token_u8(&err, dev, OPAL_CALL);
- add_token_bytestring(&err, dev, opaluid[OPAL_SMUID_UID],
- OPAL_UID_LENGTH);
- add_token_bytestring(&err, dev, opalmethod[OPAL_STARTSESSION],
- OPAL_UID_LENGTH);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u64(&err, dev, hsn);
- add_token_bytestring(&err, dev, opaluid[sp_type], OPAL_UID_LENGTH);
- add_token_u8(&err, dev, 1);
- switch (auth) {
- case OPAL_ANYBODY_UID:
- add_token_u8(&err, dev, OPAL_ENDLIST);
- break;
- case OPAL_ADMIN1_UID:
- case OPAL_SID_UID:
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, 0); /* HostChallenge */
- add_token_bytestring(&err, dev, key, key_len);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, 3); /* HostSignAuth */
- add_token_bytestring(&err, dev, opaluid[auth],
- OPAL_UID_LENGTH);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- break;
- default:
- pr_debug("Cannot start Admin SP session with auth %d\n", auth);
- return OPAL_INVAL_PARAM;
- }
- if (err) {
- pr_debug("Error building start adminsp session command.\n");
- return err;
- }
- return finalize_and_send(dev, start_opal_session_cont);
- }
- static int start_anybodyASP_opal_session(struct opal_dev *dev, void *data)
- {
- return start_generic_opal_session(dev, OPAL_ANYBODY_UID,
- OPAL_ADMINSP_UID, NULL, 0);
- }
- static int start_SIDASP_opal_session(struct opal_dev *dev, void *data)
- {
- int ret;
- const u8 *key = dev->prev_data;
- if (!key) {
- const struct opal_key *okey = data;
- ret = start_generic_opal_session(dev, OPAL_SID_UID,
- OPAL_ADMINSP_UID,
- okey->key,
- okey->key_len);
- } else {
- ret = start_generic_opal_session(dev, OPAL_SID_UID,
- OPAL_ADMINSP_UID,
- key, dev->prev_d_len);
- kfree(key);
- dev->prev_data = NULL;
- }
- return ret;
- }
- static int start_admin1LSP_opal_session(struct opal_dev *dev, void *data)
- {
- struct opal_key *key = data;
- return start_generic_opal_session(dev, OPAL_ADMIN1_UID,
- OPAL_LOCKINGSP_UID,
- key->key, key->key_len);
- }
- static int start_auth_opal_session(struct opal_dev *dev, void *data)
- {
- struct opal_session_info *session = data;
- u8 lk_ul_user[OPAL_UID_LENGTH];
- size_t keylen = session->opal_key.key_len;
- int err = 0;
- u8 *key = session->opal_key.key;
- u32 hsn = GENERIC_HOST_SESSION_NUM;
- clear_opal_cmd(dev);
- set_comid(dev, dev->comid);
- if (session->sum) {
- err = build_locking_user(lk_ul_user, sizeof(lk_ul_user),
- session->opal_key.lr);
- if (err)
- return err;
- } else if (session->who != OPAL_ADMIN1 && !session->sum) {
- err = build_locking_user(lk_ul_user, sizeof(lk_ul_user),
- session->who - 1);
- if (err)
- return err;
- } else
- memcpy(lk_ul_user, opaluid[OPAL_ADMIN1_UID], OPAL_UID_LENGTH);
- add_token_u8(&err, dev, OPAL_CALL);
- add_token_bytestring(&err, dev, opaluid[OPAL_SMUID_UID],
- OPAL_UID_LENGTH);
- add_token_bytestring(&err, dev, opalmethod[OPAL_STARTSESSION],
- OPAL_UID_LENGTH);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u64(&err, dev, hsn);
- add_token_bytestring(&err, dev, opaluid[OPAL_LOCKINGSP_UID],
- OPAL_UID_LENGTH);
- add_token_u8(&err, dev, 1);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, 0);
- add_token_bytestring(&err, dev, key, keylen);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, 3);
- add_token_bytestring(&err, dev, lk_ul_user, OPAL_UID_LENGTH);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- if (err) {
- pr_debug("Error building STARTSESSION command.\n");
- return err;
- }
- return finalize_and_send(dev, start_opal_session_cont);
- }
- static int revert_tper(struct opal_dev *dev, void *data)
- {
- int err = 0;
- clear_opal_cmd(dev);
- set_comid(dev, dev->comid);
- add_token_u8(&err, dev, OPAL_CALL);
- add_token_bytestring(&err, dev, opaluid[OPAL_ADMINSP_UID],
- OPAL_UID_LENGTH);
- add_token_bytestring(&err, dev, opalmethod[OPAL_REVERT],
- OPAL_UID_LENGTH);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- if (err) {
- pr_debug("Error building REVERT TPER command.\n");
- return err;
- }
- return finalize_and_send(dev, parse_and_check_status);
- }
- static int internal_activate_user(struct opal_dev *dev, void *data)
- {
- struct opal_session_info *session = data;
- u8 uid[OPAL_UID_LENGTH];
- int err = 0;
- clear_opal_cmd(dev);
- set_comid(dev, dev->comid);
- memcpy(uid, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
- uid[7] = session->who;
- add_token_u8(&err, dev, OPAL_CALL);
- add_token_bytestring(&err, dev, uid, OPAL_UID_LENGTH);
- add_token_bytestring(&err, dev, opalmethod[OPAL_SET], OPAL_UID_LENGTH);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, OPAL_VALUES);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, 5); /* Enabled */
- add_token_u8(&err, dev, OPAL_TRUE);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- if (err) {
- pr_debug("Error building Activate UserN command.\n");
- return err;
- }
- return finalize_and_send(dev, parse_and_check_status);
- }
- static int erase_locking_range(struct opal_dev *dev, void *data)
- {
- struct opal_session_info *session = data;
- u8 uid[OPAL_UID_LENGTH];
- int err = 0;
- clear_opal_cmd(dev);
- set_comid(dev, dev->comid);
- if (build_locking_range(uid, sizeof(uid), session->opal_key.lr) < 0)
- return -ERANGE;
- add_token_u8(&err, dev, OPAL_CALL);
- add_token_bytestring(&err, dev, uid, OPAL_UID_LENGTH);
- add_token_bytestring(&err, dev, opalmethod[OPAL_ERASE],
- OPAL_UID_LENGTH);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- if (err) {
- pr_debug("Error building Erase Locking Range Command.\n");
- return err;
- }
- return finalize_and_send(dev, parse_and_check_status);
- }
- static int set_mbr_done(struct opal_dev *dev, void *data)
- {
- u8 *mbr_done_tf = data;
- int err = 0;
- clear_opal_cmd(dev);
- set_comid(dev, dev->comid);
- add_token_u8(&err, dev, OPAL_CALL);
- add_token_bytestring(&err, dev, opaluid[OPAL_MBRCONTROL],
- OPAL_UID_LENGTH);
- add_token_bytestring(&err, dev, opalmethod[OPAL_SET], OPAL_UID_LENGTH);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, OPAL_VALUES);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, 2); /* Done */
- add_token_u8(&err, dev, *mbr_done_tf); /* Done T or F */
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- if (err) {
- pr_debug("Error Building set MBR Done command\n");
- return err;
- }
- return finalize_and_send(dev, parse_and_check_status);
- }
- static int set_mbr_enable_disable(struct opal_dev *dev, void *data)
- {
- u8 *mbr_en_dis = data;
- int err = 0;
- clear_opal_cmd(dev);
- set_comid(dev, dev->comid);
- add_token_u8(&err, dev, OPAL_CALL);
- add_token_bytestring(&err, dev, opaluid[OPAL_MBRCONTROL],
- OPAL_UID_LENGTH);
- add_token_bytestring(&err, dev, opalmethod[OPAL_SET], OPAL_UID_LENGTH);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, OPAL_VALUES);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, 1);
- add_token_u8(&err, dev, *mbr_en_dis);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- if (err) {
- pr_debug("Error Building set MBR done command\n");
- return err;
- }
- return finalize_and_send(dev, parse_and_check_status);
- }
- static int generic_pw_cmd(u8 *key, size_t key_len, u8 *cpin_uid,
- struct opal_dev *dev)
- {
- int err = 0;
- clear_opal_cmd(dev);
- set_comid(dev, dev->comid);
- add_token_u8(&err, dev, OPAL_CALL);
- add_token_bytestring(&err, dev, cpin_uid, OPAL_UID_LENGTH);
- add_token_bytestring(&err, dev, opalmethod[OPAL_SET],
- OPAL_UID_LENGTH);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, OPAL_VALUES);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, 3); /* PIN */
- add_token_bytestring(&err, dev, key, key_len);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- return err;
- }
- static int set_new_pw(struct opal_dev *dev, void *data)
- {
- u8 cpin_uid[OPAL_UID_LENGTH];
- struct opal_session_info *usr = data;
- memcpy(cpin_uid, opaluid[OPAL_C_PIN_ADMIN1], OPAL_UID_LENGTH);
- if (usr->who != OPAL_ADMIN1) {
- cpin_uid[5] = 0x03;
- if (usr->sum)
- cpin_uid[7] = usr->opal_key.lr + 1;
- else
- cpin_uid[7] = usr->who;
- }
- if (generic_pw_cmd(usr->opal_key.key, usr->opal_key.key_len,
- cpin_uid, dev)) {
- pr_debug("Error building set password command.\n");
- return -ERANGE;
- }
- return finalize_and_send(dev, parse_and_check_status);
- }
- static int set_sid_cpin_pin(struct opal_dev *dev, void *data)
- {
- u8 cpin_uid[OPAL_UID_LENGTH];
- struct opal_key *key = data;
- memcpy(cpin_uid, opaluid[OPAL_C_PIN_SID], OPAL_UID_LENGTH);
- if (generic_pw_cmd(key->key, key->key_len, cpin_uid, dev)) {
- pr_debug("Error building Set SID cpin\n");
- return -ERANGE;
- }
- return finalize_and_send(dev, parse_and_check_status);
- }
- static int add_user_to_lr(struct opal_dev *dev, void *data)
- {
- u8 lr_buffer[OPAL_UID_LENGTH];
- u8 user_uid[OPAL_UID_LENGTH];
- struct opal_lock_unlock *lkul = data;
- int err = 0;
- clear_opal_cmd(dev);
- set_comid(dev, dev->comid);
- memcpy(lr_buffer, opaluid[OPAL_LOCKINGRANGE_ACE_RDLOCKED],
- OPAL_UID_LENGTH);
- if (lkul->l_state == OPAL_RW)
- memcpy(lr_buffer, opaluid[OPAL_LOCKINGRANGE_ACE_WRLOCKED],
- OPAL_UID_LENGTH);
- lr_buffer[7] = lkul->session.opal_key.lr;
- memcpy(user_uid, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
- user_uid[7] = lkul->session.who;
- add_token_u8(&err, dev, OPAL_CALL);
- add_token_bytestring(&err, dev, lr_buffer, OPAL_UID_LENGTH);
- add_token_bytestring(&err, dev, opalmethod[OPAL_SET],
- OPAL_UID_LENGTH);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, OPAL_VALUES);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, 3);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_bytestring(&err, dev,
- opaluid[OPAL_HALF_UID_AUTHORITY_OBJ_REF],
- OPAL_UID_LENGTH/2);
- add_token_bytestring(&err, dev, user_uid, OPAL_UID_LENGTH);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_bytestring(&err, dev,
- opaluid[OPAL_HALF_UID_AUTHORITY_OBJ_REF],
- OPAL_UID_LENGTH/2);
- add_token_bytestring(&err, dev, user_uid, OPAL_UID_LENGTH);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_bytestring(&err, dev, opaluid[OPAL_HALF_UID_BOOLEAN_ACE],
- OPAL_UID_LENGTH/2);
- add_token_u8(&err, dev, 1);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- if (err) {
- pr_debug("Error building add user to locking range command.\n");
- return err;
- }
- return finalize_and_send(dev, parse_and_check_status);
- }
- static int lock_unlock_locking_range(struct opal_dev *dev, void *data)
- {
- u8 lr_buffer[OPAL_UID_LENGTH];
- struct opal_lock_unlock *lkul = data;
- u8 read_locked = 1, write_locked = 1;
- int err = 0;
- clear_opal_cmd(dev);
- set_comid(dev, dev->comid);
- if (build_locking_range(lr_buffer, sizeof(lr_buffer),
- lkul->session.opal_key.lr) < 0)
- return -ERANGE;
- switch (lkul->l_state) {
- case OPAL_RO:
- read_locked = 0;
- write_locked = 1;
- break;
- case OPAL_RW:
- read_locked = 0;
- write_locked = 0;
- break;
- case OPAL_LK:
- /* vars are initalized to locked */
- break;
- default:
- pr_debug("Tried to set an invalid locking state... returning to uland\n");
- return OPAL_INVAL_PARAM;
- }
- add_token_u8(&err, dev, OPAL_CALL);
- add_token_bytestring(&err, dev, lr_buffer, OPAL_UID_LENGTH);
- add_token_bytestring(&err, dev, opalmethod[OPAL_SET], OPAL_UID_LENGTH);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, OPAL_VALUES);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, OPAL_READLOCKED);
- add_token_u8(&err, dev, read_locked);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, OPAL_WRITELOCKED);
- add_token_u8(&err, dev, write_locked);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- if (err) {
- pr_debug("Error building SET command.\n");
- return err;
- }
- return finalize_and_send(dev, parse_and_check_status);
- }
- static int lock_unlock_locking_range_sum(struct opal_dev *dev, void *data)
- {
- u8 lr_buffer[OPAL_UID_LENGTH];
- u8 read_locked = 1, write_locked = 1;
- struct opal_lock_unlock *lkul = data;
- int ret;
- clear_opal_cmd(dev);
- set_comid(dev, dev->comid);
- if (build_locking_range(lr_buffer, sizeof(lr_buffer),
- lkul->session.opal_key.lr) < 0)
- return -ERANGE;
- switch (lkul->l_state) {
- case OPAL_RO:
- read_locked = 0;
- write_locked = 1;
- break;
- case OPAL_RW:
- read_locked = 0;
- write_locked = 0;
- break;
- case OPAL_LK:
- /* vars are initalized to locked */
- break;
- default:
- pr_debug("Tried to set an invalid locking state.\n");
- return OPAL_INVAL_PARAM;
- }
- ret = generic_lr_enable_disable(dev, lr_buffer, 1, 1,
- read_locked, write_locked);
- if (ret < 0) {
- pr_debug("Error building SET command.\n");
- return ret;
- }
- return finalize_and_send(dev, parse_and_check_status);
- }
- static int activate_lsp(struct opal_dev *dev, void *data)
- {
- struct opal_lr_act *opal_act = data;
- u8 user_lr[OPAL_UID_LENGTH];
- u8 uint_3 = 0x83;
- int err = 0, i;
- clear_opal_cmd(dev);
- set_comid(dev, dev->comid);
- add_token_u8(&err, dev, OPAL_CALL);
- add_token_bytestring(&err, dev, opaluid[OPAL_LOCKINGSP_UID],
- OPAL_UID_LENGTH);
- add_token_bytestring(&err, dev, opalmethod[OPAL_ACTIVATE],
- OPAL_UID_LENGTH);
- if (opal_act->sum) {
- err = build_locking_range(user_lr, sizeof(user_lr),
- opal_act->lr[0]);
- if (err)
- return err;
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, uint_3);
- add_token_u8(&err, dev, 6);
- add_token_u8(&err, dev, 0);
- add_token_u8(&err, dev, 0);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_bytestring(&err, dev, user_lr, OPAL_UID_LENGTH);
- for (i = 1; i < opal_act->num_lrs; i++) {
- user_lr[7] = opal_act->lr[i];
- add_token_bytestring(&err, dev, user_lr, OPAL_UID_LENGTH);
- }
- add_token_u8(&err, dev, OPAL_ENDLIST);
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- } else {
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- }
- if (err) {
- pr_debug("Error building Activate LockingSP command.\n");
- return err;
- }
- return finalize_and_send(dev, parse_and_check_status);
- }
- static int get_lsp_lifecycle_cont(struct opal_dev *dev)
- {
- u8 lc_status;
- int error = 0;
- error = parse_and_check_status(dev);
- if (error)
- return error;
- lc_status = response_get_u64(&dev->parsed, 4);
- /* 0x08 is Manufacured Inactive */
- /* 0x09 is Manufactured */
- if (lc_status != OPAL_MANUFACTURED_INACTIVE) {
- pr_debug("Couldn't determine the status of the Lifecycle state\n");
- return -ENODEV;
- }
- return 0;
- }
- /* Determine if we're in the Manufactured Inactive or Active state */
- static int get_lsp_lifecycle(struct opal_dev *dev, void *data)
- {
- int err = 0;
- clear_opal_cmd(dev);
- set_comid(dev, dev->comid);
- add_token_u8(&err, dev, OPAL_CALL);
- add_token_bytestring(&err, dev, opaluid[OPAL_LOCKINGSP_UID],
- OPAL_UID_LENGTH);
- add_token_bytestring(&err, dev, opalmethod[OPAL_GET], OPAL_UID_LENGTH);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, 3); /* Start Column */
- add_token_u8(&err, dev, 6); /* Lifecycle Column */
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, 4); /* End Column */
- add_token_u8(&err, dev, 6); /* Lifecycle Column */
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- if (err) {
- pr_debug("Error Building GET Lifecycle Status command\n");
- return err;
- }
- return finalize_and_send(dev, get_lsp_lifecycle_cont);
- }
- static int get_msid_cpin_pin_cont(struct opal_dev *dev)
- {
- const char *msid_pin;
- size_t strlen;
- int error = 0;
- error = parse_and_check_status(dev);
- if (error)
- return error;
- strlen = response_get_string(&dev->parsed, 4, &msid_pin);
- if (!msid_pin) {
- pr_debug("%s: Couldn't extract PIN from response\n", __func__);
- return OPAL_INVAL_PARAM;
- }
- dev->prev_data = kmemdup(msid_pin, strlen, GFP_KERNEL);
- if (!dev->prev_data)
- return -ENOMEM;
- dev->prev_d_len = strlen;
- return 0;
- }
- static int get_msid_cpin_pin(struct opal_dev *dev, void *data)
- {
- int err = 0;
- clear_opal_cmd(dev);
- set_comid(dev, dev->comid);
- add_token_u8(&err, dev, OPAL_CALL);
- add_token_bytestring(&err, dev, opaluid[OPAL_C_PIN_MSID],
- OPAL_UID_LENGTH);
- add_token_bytestring(&err, dev, opalmethod[OPAL_GET], OPAL_UID_LENGTH);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTLIST);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, 3); /* Start Column */
- add_token_u8(&err, dev, 3); /* PIN */
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_STARTNAME);
- add_token_u8(&err, dev, 4); /* End Column */
- add_token_u8(&err, dev, 3); /* Lifecycle Column */
- add_token_u8(&err, dev, OPAL_ENDNAME);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- add_token_u8(&err, dev, OPAL_ENDLIST);
- if (err) {
- pr_debug("Error building Get MSID CPIN PIN command.\n");
- return err;
- }
- return finalize_and_send(dev, get_msid_cpin_pin_cont);
- }
- static int end_opal_session(struct opal_dev *dev, void *data)
- {
- int err = 0;
- clear_opal_cmd(dev);
- set_comid(dev, dev->comid);
- add_token_u8(&err, dev, OPAL_ENDOFSESSION);
- if (err < 0)
- return err;
- return finalize_and_send(dev, end_session_cont);
- }
- static int end_opal_session_error(struct opal_dev *dev)
- {
- const struct opal_step error_end_session[] = {
- { end_opal_session, },
- { NULL, }
- };
- dev->steps = error_end_session;
- return next(dev);
- }
- static inline void setup_opal_dev(struct opal_dev *dev,
- const struct opal_step *steps)
- {
- dev->steps = steps;
- dev->tsn = 0;
- dev->hsn = 0;
- dev->prev_data = NULL;
- }
- static int check_opal_support(struct opal_dev *dev)
- {
- const struct opal_step steps[] = {
- { opal_discovery0, },
- { NULL, }
- };
- int ret;
- mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, steps);
- ret = next(dev);
- dev->supported = !ret;
- mutex_unlock(&dev->dev_lock);
- return ret;
- }
- static void clean_opal_dev(struct opal_dev *dev)
- {
- struct opal_suspend_data *suspend, *next;
- mutex_lock(&dev->dev_lock);
- list_for_each_entry_safe(suspend, next, &dev->unlk_lst, node) {
- list_del(&suspend->node);
- kfree(suspend);
- }
- mutex_unlock(&dev->dev_lock);
- }
- void free_opal_dev(struct opal_dev *dev)
- {
- if (!dev)
- return;
- clean_opal_dev(dev);
- kfree(dev);
- }
- EXPORT_SYMBOL(free_opal_dev);
- struct opal_dev *init_opal_dev(void *data, sec_send_recv *send_recv)
- {
- struct opal_dev *dev;
- dev = kmalloc(sizeof(*dev), GFP_KERNEL);
- if (!dev)
- return NULL;
- INIT_LIST_HEAD(&dev->unlk_lst);
- mutex_init(&dev->dev_lock);
- dev->data = data;
- dev->send_recv = send_recv;
- if (check_opal_support(dev) != 0) {
- pr_debug("Opal is not supported on this device\n");
- kfree(dev);
- return NULL;
- }
- return dev;
- }
- EXPORT_SYMBOL(init_opal_dev);
- static int opal_secure_erase_locking_range(struct opal_dev *dev,
- struct opal_session_info *opal_session)
- {
- const struct opal_step erase_steps[] = {
- { opal_discovery0, },
- { start_auth_opal_session, opal_session },
- { get_active_key, &opal_session->opal_key.lr },
- { gen_key, },
- { end_opal_session, },
- { NULL, }
- };
- int ret;
- mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, erase_steps);
- ret = next(dev);
- mutex_unlock(&dev->dev_lock);
- return ret;
- }
- static int opal_erase_locking_range(struct opal_dev *dev,
- struct opal_session_info *opal_session)
- {
- const struct opal_step erase_steps[] = {
- { opal_discovery0, },
- { start_auth_opal_session, opal_session },
- { erase_locking_range, opal_session },
- { end_opal_session, },
- { NULL, }
- };
- int ret;
- mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, erase_steps);
- ret = next(dev);
- mutex_unlock(&dev->dev_lock);
- return ret;
- }
- static int opal_enable_disable_shadow_mbr(struct opal_dev *dev,
- struct opal_mbr_data *opal_mbr)
- {
- u8 enable_disable = opal_mbr->enable_disable == OPAL_MBR_ENABLE ?
- OPAL_TRUE : OPAL_FALSE;
- const struct opal_step mbr_steps[] = {
- { opal_discovery0, },
- { start_admin1LSP_opal_session, &opal_mbr->key },
- { set_mbr_done, &enable_disable },
- { end_opal_session, },
- { start_admin1LSP_opal_session, &opal_mbr->key },
- { set_mbr_enable_disable, &enable_disable },
- { end_opal_session, },
- { NULL, }
- };
- int ret;
- if (opal_mbr->enable_disable != OPAL_MBR_ENABLE &&
- opal_mbr->enable_disable != OPAL_MBR_DISABLE)
- return -EINVAL;
- mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, mbr_steps);
- ret = next(dev);
- mutex_unlock(&dev->dev_lock);
- return ret;
- }
- static int opal_save(struct opal_dev *dev, struct opal_lock_unlock *lk_unlk)
- {
- struct opal_suspend_data *suspend;
- suspend = kzalloc(sizeof(*suspend), GFP_KERNEL);
- if (!suspend)
- return -ENOMEM;
- suspend->unlk = *lk_unlk;
- suspend->lr = lk_unlk->session.opal_key.lr;
- mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, NULL);
- add_suspend_info(dev, suspend);
- mutex_unlock(&dev->dev_lock);
- return 0;
- }
- static int opal_add_user_to_lr(struct opal_dev *dev,
- struct opal_lock_unlock *lk_unlk)
- {
- const struct opal_step steps[] = {
- { opal_discovery0, },
- { start_admin1LSP_opal_session, &lk_unlk->session.opal_key },
- { add_user_to_lr, lk_unlk },
- { end_opal_session, },
- { NULL, }
- };
- int ret;
- if (lk_unlk->l_state != OPAL_RO &&
- lk_unlk->l_state != OPAL_RW) {
- pr_debug("Locking state was not RO or RW\n");
- return -EINVAL;
- }
- if (lk_unlk->session.who < OPAL_USER1 ||
- lk_unlk->session.who > OPAL_USER9) {
- pr_debug("Authority was not within the range of users: %d\n",
- lk_unlk->session.who);
- return -EINVAL;
- }
- if (lk_unlk->session.sum) {
- pr_debug("%s not supported in sum. Use setup locking range\n",
- __func__);
- return -EINVAL;
- }
- mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, steps);
- ret = next(dev);
- mutex_unlock(&dev->dev_lock);
- return ret;
- }
- static int opal_reverttper(struct opal_dev *dev, struct opal_key *opal)
- {
- const struct opal_step revert_steps[] = {
- { opal_discovery0, },
- { start_SIDASP_opal_session, opal },
- { revert_tper, }, /* controller will terminate session */
- { NULL, }
- };
- int ret;
- mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, revert_steps);
- ret = next(dev);
- mutex_unlock(&dev->dev_lock);
- /*
- * If we successfully reverted lets clean
- * any saved locking ranges.
- */
- if (!ret)
- clean_opal_dev(dev);
- return ret;
- }
- static int __opal_lock_unlock(struct opal_dev *dev,
- struct opal_lock_unlock *lk_unlk)
- {
- const struct opal_step unlock_steps[] = {
- { opal_discovery0, },
- { start_auth_opal_session, &lk_unlk->session },
- { lock_unlock_locking_range, lk_unlk },
- { end_opal_session, },
- { NULL, }
- };
- const struct opal_step unlock_sum_steps[] = {
- { opal_discovery0, },
- { start_auth_opal_session, &lk_unlk->session },
- { lock_unlock_locking_range_sum, lk_unlk },
- { end_opal_session, },
- { NULL, }
- };
- dev->steps = lk_unlk->session.sum ? unlock_sum_steps : unlock_steps;
- return next(dev);
- }
- static int __opal_set_mbr_done(struct opal_dev *dev, struct opal_key *key)
- {
- u8 mbr_done_tf = OPAL_TRUE;
- const struct opal_step mbrdone_step [] = {
- { opal_discovery0, },
- { start_admin1LSP_opal_session, key },
- { set_mbr_done, &mbr_done_tf },
- { end_opal_session, },
- { NULL, }
- };
- dev->steps = mbrdone_step;
- return next(dev);
- }
- static int opal_lock_unlock(struct opal_dev *dev,
- struct opal_lock_unlock *lk_unlk)
- {
- int ret;
- if (lk_unlk->session.who < OPAL_ADMIN1 ||
- lk_unlk->session.who > OPAL_USER9)
- return -EINVAL;
- mutex_lock(&dev->dev_lock);
- ret = __opal_lock_unlock(dev, lk_unlk);
- mutex_unlock(&dev->dev_lock);
- return ret;
- }
- static int opal_take_ownership(struct opal_dev *dev, struct opal_key *opal)
- {
- const struct opal_step owner_steps[] = {
- { opal_discovery0, },
- { start_anybodyASP_opal_session, },
- { get_msid_cpin_pin, },
- { end_opal_session, },
- { start_SIDASP_opal_session, opal },
- { set_sid_cpin_pin, opal },
- { end_opal_session, },
- { NULL, }
- };
- int ret;
- if (!dev)
- return -ENODEV;
- mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, owner_steps);
- ret = next(dev);
- mutex_unlock(&dev->dev_lock);
- return ret;
- }
- static int opal_activate_lsp(struct opal_dev *dev, struct opal_lr_act *opal_lr_act)
- {
- const struct opal_step active_steps[] = {
- { opal_discovery0, },
- { start_SIDASP_opal_session, &opal_lr_act->key },
- { get_lsp_lifecycle, },
- { activate_lsp, opal_lr_act },
- { end_opal_session, },
- { NULL, }
- };
- int ret;
- if (!opal_lr_act->num_lrs || opal_lr_act->num_lrs > OPAL_MAX_LRS)
- return -EINVAL;
- mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, active_steps);
- ret = next(dev);
- mutex_unlock(&dev->dev_lock);
- return ret;
- }
- static int opal_setup_locking_range(struct opal_dev *dev,
- struct opal_user_lr_setup *opal_lrs)
- {
- const struct opal_step lr_steps[] = {
- { opal_discovery0, },
- { start_auth_opal_session, &opal_lrs->session },
- { setup_locking_range, opal_lrs },
- { end_opal_session, },
- { NULL, }
- };
- int ret;
- mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, lr_steps);
- ret = next(dev);
- mutex_unlock(&dev->dev_lock);
- return ret;
- }
- static int opal_set_new_pw(struct opal_dev *dev, struct opal_new_pw *opal_pw)
- {
- const struct opal_step pw_steps[] = {
- { opal_discovery0, },
- { start_auth_opal_session, &opal_pw->session },
- { set_new_pw, &opal_pw->new_user_pw },
- { end_opal_session, },
- { NULL }
- };
- int ret;
- if (opal_pw->session.who < OPAL_ADMIN1 ||
- opal_pw->session.who > OPAL_USER9 ||
- opal_pw->new_user_pw.who < OPAL_ADMIN1 ||
- opal_pw->new_user_pw.who > OPAL_USER9)
- return -EINVAL;
- mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, pw_steps);
- ret = next(dev);
- mutex_unlock(&dev->dev_lock);
- return ret;
- }
- static int opal_activate_user(struct opal_dev *dev,
- struct opal_session_info *opal_session)
- {
- const struct opal_step act_steps[] = {
- { opal_discovery0, },
- { start_admin1LSP_opal_session, &opal_session->opal_key },
- { internal_activate_user, opal_session },
- { end_opal_session, },
- { NULL, }
- };
- int ret;
- /* We can't activate Admin1 it's active as manufactured */
- if (opal_session->who < OPAL_USER1 ||
- opal_session->who > OPAL_USER9) {
- pr_debug("Who was not a valid user: %d\n", opal_session->who);
- return -EINVAL;
- }
- mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, act_steps);
- ret = next(dev);
- mutex_unlock(&dev->dev_lock);
- return ret;
- }
- bool opal_unlock_from_suspend(struct opal_dev *dev)
- {
- struct opal_suspend_data *suspend;
- bool was_failure = false;
- int ret = 0;
- if (!dev)
- return false;
- if (!dev->supported)
- return false;
- mutex_lock(&dev->dev_lock);
- setup_opal_dev(dev, NULL);
- list_for_each_entry(suspend, &dev->unlk_lst, node) {
- dev->tsn = 0;
- dev->hsn = 0;
- ret = __opal_lock_unlock(dev, &suspend->unlk);
- if (ret) {
- pr_debug("Failed to unlock LR %hhu with sum %d\n",
- suspend->unlk.session.opal_key.lr,
- suspend->unlk.session.sum);
- was_failure = true;
- }
- if (dev->mbr_enabled) {
- ret = __opal_set_mbr_done(dev, &suspend->unlk.session.opal_key);
- if (ret)
- pr_debug("Failed to set MBR Done in S3 resume\n");
- }
- }
- mutex_unlock(&dev->dev_lock);
- return was_failure;
- }
- EXPORT_SYMBOL(opal_unlock_from_suspend);
- int sed_ioctl(struct opal_dev *dev, unsigned int cmd, void __user *arg)
- {
- void *p;
- int ret = -ENOTTY;
- if (!capable(CAP_SYS_ADMIN))
- return -EACCES;
- if (!dev)
- return -ENOTSUPP;
- if (!dev->supported)
- return -ENOTSUPP;
- p = memdup_user(arg, _IOC_SIZE(cmd));
- if (IS_ERR(p))
- return PTR_ERR(p);
- switch (cmd) {
- case IOC_OPAL_SAVE:
- ret = opal_save(dev, p);
- break;
- case IOC_OPAL_LOCK_UNLOCK:
- ret = opal_lock_unlock(dev, p);
- break;
- case IOC_OPAL_TAKE_OWNERSHIP:
- ret = opal_take_ownership(dev, p);
- break;
- case IOC_OPAL_ACTIVATE_LSP:
- ret = opal_activate_lsp(dev, p);
- break;
- case IOC_OPAL_SET_PW:
- ret = opal_set_new_pw(dev, p);
- break;
- case IOC_OPAL_ACTIVATE_USR:
- ret = opal_activate_user(dev, p);
- break;
- case IOC_OPAL_REVERT_TPR:
- ret = opal_reverttper(dev, p);
- break;
- case IOC_OPAL_LR_SETUP:
- ret = opal_setup_locking_range(dev, p);
- break;
- case IOC_OPAL_ADD_USR_TO_LR:
- ret = opal_add_user_to_lr(dev, p);
- break;
- case IOC_OPAL_ENABLE_DISABLE_MBR:
- ret = opal_enable_disable_shadow_mbr(dev, p);
- break;
- case IOC_OPAL_ERASE_LR:
- ret = opal_erase_locking_range(dev, p);
- break;
- case IOC_OPAL_SECURE_ERASE_LR:
- ret = opal_secure_erase_locking_range(dev, p);
- break;
- default:
- break;
- }
- kfree(p);
- return ret;
- }
- EXPORT_SYMBOL_GPL(sed_ioctl);
|