configfs.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Configfs interface for the NVMe target.
  4. * Copyright (c) 2015-2016 HGST, a Western Digital Company.
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/kstrtox.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/stat.h>
  12. #include <linux/ctype.h>
  13. #include <linux/pci.h>
  14. #include <linux/pci-p2pdma.h>
  15. #ifdef CONFIG_NVME_TARGET_AUTH
  16. #include <linux/nvme-auth.h>
  17. #endif
  18. #include <linux/nvme-keyring.h>
  19. #include <crypto/hash.h>
  20. #include <crypto/kpp.h>
  21. #include <linux/nospec.h>
  22. #include "nvmet.h"
  23. static const struct config_item_type nvmet_host_type;
  24. static const struct config_item_type nvmet_subsys_type;
  25. static LIST_HEAD(nvmet_ports_list);
  26. struct list_head *nvmet_ports = &nvmet_ports_list;
  27. struct nvmet_type_name_map {
  28. u8 type;
  29. const char *name;
  30. };
  31. static struct nvmet_type_name_map nvmet_transport[] = {
  32. { NVMF_TRTYPE_RDMA, "rdma" },
  33. { NVMF_TRTYPE_FC, "fc" },
  34. { NVMF_TRTYPE_TCP, "tcp" },
  35. { NVMF_TRTYPE_LOOP, "loop" },
  36. };
  37. static const struct nvmet_type_name_map nvmet_addr_family[] = {
  38. { NVMF_ADDR_FAMILY_PCI, "pcie" },
  39. { NVMF_ADDR_FAMILY_IP4, "ipv4" },
  40. { NVMF_ADDR_FAMILY_IP6, "ipv6" },
  41. { NVMF_ADDR_FAMILY_IB, "ib" },
  42. { NVMF_ADDR_FAMILY_FC, "fc" },
  43. { NVMF_ADDR_FAMILY_LOOP, "loop" },
  44. };
  45. static bool nvmet_is_port_enabled(struct nvmet_port *p, const char *caller)
  46. {
  47. if (p->enabled)
  48. pr_err("Disable port '%u' before changing attribute in %s\n",
  49. le16_to_cpu(p->disc_addr.portid), caller);
  50. return p->enabled;
  51. }
  52. /*
  53. * nvmet_port Generic ConfigFS definitions.
  54. * Used in any place in the ConfigFS tree that refers to an address.
  55. */
  56. static ssize_t nvmet_addr_adrfam_show(struct config_item *item, char *page)
  57. {
  58. u8 adrfam = to_nvmet_port(item)->disc_addr.adrfam;
  59. int i;
  60. for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
  61. if (nvmet_addr_family[i].type == adrfam)
  62. return snprintf(page, PAGE_SIZE, "%s\n",
  63. nvmet_addr_family[i].name);
  64. }
  65. return snprintf(page, PAGE_SIZE, "\n");
  66. }
  67. static ssize_t nvmet_addr_adrfam_store(struct config_item *item,
  68. const char *page, size_t count)
  69. {
  70. struct nvmet_port *port = to_nvmet_port(item);
  71. int i;
  72. if (nvmet_is_port_enabled(port, __func__))
  73. return -EACCES;
  74. for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
  75. if (sysfs_streq(page, nvmet_addr_family[i].name))
  76. goto found;
  77. }
  78. pr_err("Invalid value '%s' for adrfam\n", page);
  79. return -EINVAL;
  80. found:
  81. port->disc_addr.adrfam = nvmet_addr_family[i].type;
  82. return count;
  83. }
  84. CONFIGFS_ATTR(nvmet_, addr_adrfam);
  85. static ssize_t nvmet_addr_portid_show(struct config_item *item,
  86. char *page)
  87. {
  88. __le16 portid = to_nvmet_port(item)->disc_addr.portid;
  89. return snprintf(page, PAGE_SIZE, "%d\n", le16_to_cpu(portid));
  90. }
  91. static ssize_t nvmet_addr_portid_store(struct config_item *item,
  92. const char *page, size_t count)
  93. {
  94. struct nvmet_port *port = to_nvmet_port(item);
  95. u16 portid = 0;
  96. if (kstrtou16(page, 0, &portid)) {
  97. pr_err("Invalid value '%s' for portid\n", page);
  98. return -EINVAL;
  99. }
  100. if (nvmet_is_port_enabled(port, __func__))
  101. return -EACCES;
  102. port->disc_addr.portid = cpu_to_le16(portid);
  103. return count;
  104. }
  105. CONFIGFS_ATTR(nvmet_, addr_portid);
  106. static ssize_t nvmet_addr_traddr_show(struct config_item *item,
  107. char *page)
  108. {
  109. struct nvmet_port *port = to_nvmet_port(item);
  110. return snprintf(page, PAGE_SIZE, "%s\n", port->disc_addr.traddr);
  111. }
  112. static ssize_t nvmet_addr_traddr_store(struct config_item *item,
  113. const char *page, size_t count)
  114. {
  115. struct nvmet_port *port = to_nvmet_port(item);
  116. if (count > NVMF_TRADDR_SIZE) {
  117. pr_err("Invalid value '%s' for traddr\n", page);
  118. return -EINVAL;
  119. }
  120. if (nvmet_is_port_enabled(port, __func__))
  121. return -EACCES;
  122. if (sscanf(page, "%s\n", port->disc_addr.traddr) != 1)
  123. return -EINVAL;
  124. return count;
  125. }
  126. CONFIGFS_ATTR(nvmet_, addr_traddr);
  127. static const struct nvmet_type_name_map nvmet_addr_treq[] = {
  128. { NVMF_TREQ_NOT_SPECIFIED, "not specified" },
  129. { NVMF_TREQ_REQUIRED, "required" },
  130. { NVMF_TREQ_NOT_REQUIRED, "not required" },
  131. };
  132. static inline u8 nvmet_port_disc_addr_treq_mask(struct nvmet_port *port)
  133. {
  134. return (port->disc_addr.treq & ~NVME_TREQ_SECURE_CHANNEL_MASK);
  135. }
  136. static ssize_t nvmet_addr_treq_show(struct config_item *item, char *page)
  137. {
  138. u8 treq = nvmet_port_disc_addr_treq_secure_channel(to_nvmet_port(item));
  139. int i;
  140. for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
  141. if (treq == nvmet_addr_treq[i].type)
  142. return snprintf(page, PAGE_SIZE, "%s\n",
  143. nvmet_addr_treq[i].name);
  144. }
  145. return snprintf(page, PAGE_SIZE, "\n");
  146. }
  147. static ssize_t nvmet_addr_treq_store(struct config_item *item,
  148. const char *page, size_t count)
  149. {
  150. struct nvmet_port *port = to_nvmet_port(item);
  151. u8 treq = nvmet_port_disc_addr_treq_mask(port);
  152. int i;
  153. if (nvmet_is_port_enabled(port, __func__))
  154. return -EACCES;
  155. for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
  156. if (sysfs_streq(page, nvmet_addr_treq[i].name))
  157. goto found;
  158. }
  159. pr_err("Invalid value '%s' for treq\n", page);
  160. return -EINVAL;
  161. found:
  162. if (port->disc_addr.trtype == NVMF_TRTYPE_TCP &&
  163. port->disc_addr.tsas.tcp.sectype == NVMF_TCP_SECTYPE_TLS13) {
  164. switch (nvmet_addr_treq[i].type) {
  165. case NVMF_TREQ_NOT_SPECIFIED:
  166. pr_debug("treq '%s' not allowed for TLS1.3\n",
  167. nvmet_addr_treq[i].name);
  168. return -EINVAL;
  169. case NVMF_TREQ_NOT_REQUIRED:
  170. pr_warn("Allow non-TLS connections while TLS1.3 is enabled\n");
  171. break;
  172. default:
  173. break;
  174. }
  175. }
  176. treq |= nvmet_addr_treq[i].type;
  177. port->disc_addr.treq = treq;
  178. return count;
  179. }
  180. CONFIGFS_ATTR(nvmet_, addr_treq);
  181. static ssize_t nvmet_addr_trsvcid_show(struct config_item *item,
  182. char *page)
  183. {
  184. struct nvmet_port *port = to_nvmet_port(item);
  185. return snprintf(page, PAGE_SIZE, "%s\n", port->disc_addr.trsvcid);
  186. }
  187. static ssize_t nvmet_addr_trsvcid_store(struct config_item *item,
  188. const char *page, size_t count)
  189. {
  190. struct nvmet_port *port = to_nvmet_port(item);
  191. if (count > NVMF_TRSVCID_SIZE) {
  192. pr_err("Invalid value '%s' for trsvcid\n", page);
  193. return -EINVAL;
  194. }
  195. if (nvmet_is_port_enabled(port, __func__))
  196. return -EACCES;
  197. if (sscanf(page, "%s\n", port->disc_addr.trsvcid) != 1)
  198. return -EINVAL;
  199. return count;
  200. }
  201. CONFIGFS_ATTR(nvmet_, addr_trsvcid);
  202. static ssize_t nvmet_param_inline_data_size_show(struct config_item *item,
  203. char *page)
  204. {
  205. struct nvmet_port *port = to_nvmet_port(item);
  206. return snprintf(page, PAGE_SIZE, "%d\n", port->inline_data_size);
  207. }
  208. static ssize_t nvmet_param_inline_data_size_store(struct config_item *item,
  209. const char *page, size_t count)
  210. {
  211. struct nvmet_port *port = to_nvmet_port(item);
  212. int ret;
  213. if (nvmet_is_port_enabled(port, __func__))
  214. return -EACCES;
  215. ret = kstrtoint(page, 0, &port->inline_data_size);
  216. if (ret) {
  217. pr_err("Invalid value '%s' for inline_data_size\n", page);
  218. return -EINVAL;
  219. }
  220. return count;
  221. }
  222. CONFIGFS_ATTR(nvmet_, param_inline_data_size);
  223. static ssize_t nvmet_param_max_queue_size_show(struct config_item *item,
  224. char *page)
  225. {
  226. struct nvmet_port *port = to_nvmet_port(item);
  227. return snprintf(page, PAGE_SIZE, "%d\n", port->max_queue_size);
  228. }
  229. static ssize_t nvmet_param_max_queue_size_store(struct config_item *item,
  230. const char *page, size_t count)
  231. {
  232. struct nvmet_port *port = to_nvmet_port(item);
  233. int ret;
  234. if (nvmet_is_port_enabled(port, __func__))
  235. return -EACCES;
  236. ret = kstrtoint(page, 0, &port->max_queue_size);
  237. if (ret) {
  238. pr_err("Invalid value '%s' for max_queue_size\n", page);
  239. return -EINVAL;
  240. }
  241. return count;
  242. }
  243. CONFIGFS_ATTR(nvmet_, param_max_queue_size);
  244. #ifdef CONFIG_BLK_DEV_INTEGRITY
  245. static ssize_t nvmet_param_pi_enable_show(struct config_item *item,
  246. char *page)
  247. {
  248. struct nvmet_port *port = to_nvmet_port(item);
  249. return snprintf(page, PAGE_SIZE, "%d\n", port->pi_enable);
  250. }
  251. static ssize_t nvmet_param_pi_enable_store(struct config_item *item,
  252. const char *page, size_t count)
  253. {
  254. struct nvmet_port *port = to_nvmet_port(item);
  255. bool val;
  256. if (kstrtobool(page, &val))
  257. return -EINVAL;
  258. if (nvmet_is_port_enabled(port, __func__))
  259. return -EACCES;
  260. port->pi_enable = val;
  261. return count;
  262. }
  263. CONFIGFS_ATTR(nvmet_, param_pi_enable);
  264. #endif
  265. static ssize_t nvmet_addr_trtype_show(struct config_item *item,
  266. char *page)
  267. {
  268. struct nvmet_port *port = to_nvmet_port(item);
  269. int i;
  270. for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
  271. if (port->disc_addr.trtype == nvmet_transport[i].type)
  272. return snprintf(page, PAGE_SIZE,
  273. "%s\n", nvmet_transport[i].name);
  274. }
  275. return sprintf(page, "\n");
  276. }
  277. static void nvmet_port_init_tsas_rdma(struct nvmet_port *port)
  278. {
  279. port->disc_addr.tsas.rdma.qptype = NVMF_RDMA_QPTYPE_CONNECTED;
  280. port->disc_addr.tsas.rdma.prtype = NVMF_RDMA_PRTYPE_NOT_SPECIFIED;
  281. port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM;
  282. }
  283. static void nvmet_port_init_tsas_tcp(struct nvmet_port *port, int sectype)
  284. {
  285. port->disc_addr.tsas.tcp.sectype = sectype;
  286. }
  287. static ssize_t nvmet_addr_trtype_store(struct config_item *item,
  288. const char *page, size_t count)
  289. {
  290. struct nvmet_port *port = to_nvmet_port(item);
  291. int i;
  292. if (nvmet_is_port_enabled(port, __func__))
  293. return -EACCES;
  294. for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
  295. if (sysfs_streq(page, nvmet_transport[i].name))
  296. goto found;
  297. }
  298. pr_err("Invalid value '%s' for trtype\n", page);
  299. return -EINVAL;
  300. found:
  301. memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE);
  302. port->disc_addr.trtype = nvmet_transport[i].type;
  303. if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA)
  304. nvmet_port_init_tsas_rdma(port);
  305. else if (port->disc_addr.trtype == NVMF_TRTYPE_TCP)
  306. nvmet_port_init_tsas_tcp(port, NVMF_TCP_SECTYPE_NONE);
  307. return count;
  308. }
  309. CONFIGFS_ATTR(nvmet_, addr_trtype);
  310. static const struct nvmet_type_name_map nvmet_addr_tsas_tcp[] = {
  311. { NVMF_TCP_SECTYPE_NONE, "none" },
  312. { NVMF_TCP_SECTYPE_TLS13, "tls1.3" },
  313. };
  314. static const struct nvmet_type_name_map nvmet_addr_tsas_rdma[] = {
  315. { NVMF_RDMA_QPTYPE_CONNECTED, "connected" },
  316. { NVMF_RDMA_QPTYPE_DATAGRAM, "datagram" },
  317. };
  318. static ssize_t nvmet_addr_tsas_show(struct config_item *item,
  319. char *page)
  320. {
  321. struct nvmet_port *port = to_nvmet_port(item);
  322. int i;
  323. if (port->disc_addr.trtype == NVMF_TRTYPE_TCP) {
  324. for (i = 0; i < ARRAY_SIZE(nvmet_addr_tsas_tcp); i++) {
  325. if (port->disc_addr.tsas.tcp.sectype == nvmet_addr_tsas_tcp[i].type)
  326. return sprintf(page, "%s\n", nvmet_addr_tsas_tcp[i].name);
  327. }
  328. } else if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA) {
  329. for (i = 0; i < ARRAY_SIZE(nvmet_addr_tsas_rdma); i++) {
  330. if (port->disc_addr.tsas.rdma.qptype == nvmet_addr_tsas_rdma[i].type)
  331. return sprintf(page, "%s\n", nvmet_addr_tsas_rdma[i].name);
  332. }
  333. }
  334. return sprintf(page, "\n");
  335. }
  336. static u8 nvmet_addr_tsas_rdma_store(const char *page)
  337. {
  338. int i;
  339. for (i = 0; i < ARRAY_SIZE(nvmet_addr_tsas_rdma); i++) {
  340. if (sysfs_streq(page, nvmet_addr_tsas_rdma[i].name))
  341. return nvmet_addr_tsas_rdma[i].type;
  342. }
  343. return NVMF_RDMA_QPTYPE_INVALID;
  344. }
  345. static u8 nvmet_addr_tsas_tcp_store(const char *page)
  346. {
  347. int i;
  348. for (i = 0; i < ARRAY_SIZE(nvmet_addr_tsas_tcp); i++) {
  349. if (sysfs_streq(page, nvmet_addr_tsas_tcp[i].name))
  350. return nvmet_addr_tsas_tcp[i].type;
  351. }
  352. return NVMF_TCP_SECTYPE_INVALID;
  353. }
  354. static ssize_t nvmet_addr_tsas_store(struct config_item *item,
  355. const char *page, size_t count)
  356. {
  357. struct nvmet_port *port = to_nvmet_port(item);
  358. u8 treq = nvmet_port_disc_addr_treq_mask(port);
  359. u8 sectype, qptype;
  360. if (nvmet_is_port_enabled(port, __func__))
  361. return -EACCES;
  362. if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA) {
  363. qptype = nvmet_addr_tsas_rdma_store(page);
  364. if (qptype == port->disc_addr.tsas.rdma.qptype)
  365. return count;
  366. } else if (port->disc_addr.trtype == NVMF_TRTYPE_TCP) {
  367. sectype = nvmet_addr_tsas_tcp_store(page);
  368. if (sectype != NVMF_TCP_SECTYPE_INVALID)
  369. goto found;
  370. }
  371. pr_err("Invalid value '%s' for tsas\n", page);
  372. return -EINVAL;
  373. found:
  374. if (sectype == NVMF_TCP_SECTYPE_TLS13) {
  375. if (!IS_ENABLED(CONFIG_NVME_TARGET_TCP_TLS)) {
  376. pr_err("TLS is not supported\n");
  377. return -EINVAL;
  378. }
  379. if (!port->keyring) {
  380. pr_err("TLS keyring not configured\n");
  381. return -EINVAL;
  382. }
  383. }
  384. nvmet_port_init_tsas_tcp(port, sectype);
  385. /*
  386. * If TLS is enabled TREQ should be set to 'required' per default
  387. */
  388. if (sectype == NVMF_TCP_SECTYPE_TLS13) {
  389. u8 sc = nvmet_port_disc_addr_treq_secure_channel(port);
  390. if (sc == NVMF_TREQ_NOT_SPECIFIED)
  391. treq |= NVMF_TREQ_REQUIRED;
  392. else
  393. treq |= sc;
  394. } else {
  395. treq |= NVMF_TREQ_NOT_SPECIFIED;
  396. }
  397. port->disc_addr.treq = treq;
  398. return count;
  399. }
  400. CONFIGFS_ATTR(nvmet_, addr_tsas);
  401. /*
  402. * Namespace structures & file operation functions below
  403. */
  404. static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page)
  405. {
  406. return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path);
  407. }
  408. static ssize_t nvmet_ns_device_path_store(struct config_item *item,
  409. const char *page, size_t count)
  410. {
  411. struct nvmet_ns *ns = to_nvmet_ns(item);
  412. struct nvmet_subsys *subsys = ns->subsys;
  413. size_t len;
  414. int ret;
  415. mutex_lock(&subsys->lock);
  416. ret = -EBUSY;
  417. if (ns->enabled)
  418. goto out_unlock;
  419. ret = -EINVAL;
  420. len = strcspn(page, "\n");
  421. if (!len)
  422. goto out_unlock;
  423. kfree(ns->device_path);
  424. ret = -ENOMEM;
  425. ns->device_path = kmemdup_nul(page, len, GFP_KERNEL);
  426. if (!ns->device_path)
  427. goto out_unlock;
  428. mutex_unlock(&subsys->lock);
  429. return count;
  430. out_unlock:
  431. mutex_unlock(&subsys->lock);
  432. return ret;
  433. }
  434. CONFIGFS_ATTR(nvmet_ns_, device_path);
  435. #ifdef CONFIG_PCI_P2PDMA
  436. static ssize_t nvmet_ns_p2pmem_show(struct config_item *item, char *page)
  437. {
  438. struct nvmet_ns *ns = to_nvmet_ns(item);
  439. return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
  440. }
  441. static ssize_t nvmet_ns_p2pmem_store(struct config_item *item,
  442. const char *page, size_t count)
  443. {
  444. struct nvmet_ns *ns = to_nvmet_ns(item);
  445. struct pci_dev *p2p_dev = NULL;
  446. bool use_p2pmem;
  447. int ret = count;
  448. int error;
  449. mutex_lock(&ns->subsys->lock);
  450. if (ns->enabled) {
  451. ret = -EBUSY;
  452. goto out_unlock;
  453. }
  454. error = pci_p2pdma_enable_store(page, &p2p_dev, &use_p2pmem);
  455. if (error) {
  456. ret = error;
  457. goto out_unlock;
  458. }
  459. ns->use_p2pmem = use_p2pmem;
  460. pci_dev_put(ns->p2p_dev);
  461. ns->p2p_dev = p2p_dev;
  462. out_unlock:
  463. mutex_unlock(&ns->subsys->lock);
  464. return ret;
  465. }
  466. CONFIGFS_ATTR(nvmet_ns_, p2pmem);
  467. #endif /* CONFIG_PCI_P2PDMA */
  468. static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page)
  469. {
  470. return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid);
  471. }
  472. static ssize_t nvmet_ns_device_uuid_store(struct config_item *item,
  473. const char *page, size_t count)
  474. {
  475. struct nvmet_ns *ns = to_nvmet_ns(item);
  476. struct nvmet_subsys *subsys = ns->subsys;
  477. int ret = 0;
  478. mutex_lock(&subsys->lock);
  479. if (ns->enabled) {
  480. ret = -EBUSY;
  481. goto out_unlock;
  482. }
  483. if (uuid_parse(page, &ns->uuid))
  484. ret = -EINVAL;
  485. out_unlock:
  486. mutex_unlock(&subsys->lock);
  487. return ret ? ret : count;
  488. }
  489. CONFIGFS_ATTR(nvmet_ns_, device_uuid);
  490. static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page)
  491. {
  492. return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid);
  493. }
  494. static ssize_t nvmet_ns_device_nguid_store(struct config_item *item,
  495. const char *page, size_t count)
  496. {
  497. struct nvmet_ns *ns = to_nvmet_ns(item);
  498. struct nvmet_subsys *subsys = ns->subsys;
  499. u8 nguid[16];
  500. const char *p = page;
  501. int i;
  502. int ret = 0;
  503. mutex_lock(&subsys->lock);
  504. if (ns->enabled) {
  505. ret = -EBUSY;
  506. goto out_unlock;
  507. }
  508. for (i = 0; i < 16; i++) {
  509. if (p + 2 > page + count) {
  510. ret = -EINVAL;
  511. goto out_unlock;
  512. }
  513. if (!isxdigit(p[0]) || !isxdigit(p[1])) {
  514. ret = -EINVAL;
  515. goto out_unlock;
  516. }
  517. nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]);
  518. p += 2;
  519. if (*p == '-' || *p == ':')
  520. p++;
  521. }
  522. memcpy(&ns->nguid, nguid, sizeof(nguid));
  523. out_unlock:
  524. mutex_unlock(&subsys->lock);
  525. return ret ? ret : count;
  526. }
  527. CONFIGFS_ATTR(nvmet_ns_, device_nguid);
  528. static ssize_t nvmet_ns_ana_grpid_show(struct config_item *item, char *page)
  529. {
  530. return sprintf(page, "%u\n", to_nvmet_ns(item)->anagrpid);
  531. }
  532. static ssize_t nvmet_ns_ana_grpid_store(struct config_item *item,
  533. const char *page, size_t count)
  534. {
  535. struct nvmet_ns *ns = to_nvmet_ns(item);
  536. u32 oldgrpid, newgrpid;
  537. int ret;
  538. ret = kstrtou32(page, 0, &newgrpid);
  539. if (ret)
  540. return ret;
  541. if (newgrpid < 1 || newgrpid > NVMET_MAX_ANAGRPS)
  542. return -EINVAL;
  543. down_write(&nvmet_ana_sem);
  544. oldgrpid = ns->anagrpid;
  545. newgrpid = array_index_nospec(newgrpid, NVMET_MAX_ANAGRPS);
  546. nvmet_ana_group_enabled[newgrpid]++;
  547. ns->anagrpid = newgrpid;
  548. nvmet_ana_group_enabled[oldgrpid]--;
  549. nvmet_ana_chgcnt++;
  550. up_write(&nvmet_ana_sem);
  551. nvmet_send_ana_event(ns->subsys, NULL);
  552. return count;
  553. }
  554. CONFIGFS_ATTR(nvmet_ns_, ana_grpid);
  555. static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page)
  556. {
  557. return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled);
  558. }
  559. static ssize_t nvmet_ns_enable_store(struct config_item *item,
  560. const char *page, size_t count)
  561. {
  562. struct nvmet_ns *ns = to_nvmet_ns(item);
  563. bool enable;
  564. int ret = 0;
  565. if (kstrtobool(page, &enable))
  566. return -EINVAL;
  567. /*
  568. * take a global nvmet_config_sem because the disable routine has a
  569. * window where it releases the subsys-lock, giving a chance to
  570. * a parallel enable to concurrently execute causing the disable to
  571. * have a misaccounting of the ns percpu_ref.
  572. */
  573. down_write(&nvmet_config_sem);
  574. if (enable)
  575. ret = nvmet_ns_enable(ns);
  576. else
  577. nvmet_ns_disable(ns);
  578. up_write(&nvmet_config_sem);
  579. return ret ? ret : count;
  580. }
  581. CONFIGFS_ATTR(nvmet_ns_, enable);
  582. static ssize_t nvmet_ns_buffered_io_show(struct config_item *item, char *page)
  583. {
  584. return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io);
  585. }
  586. static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
  587. const char *page, size_t count)
  588. {
  589. struct nvmet_ns *ns = to_nvmet_ns(item);
  590. bool val;
  591. if (kstrtobool(page, &val))
  592. return -EINVAL;
  593. mutex_lock(&ns->subsys->lock);
  594. if (ns->enabled) {
  595. pr_err("disable ns before setting buffered_io value.\n");
  596. mutex_unlock(&ns->subsys->lock);
  597. return -EINVAL;
  598. }
  599. ns->buffered_io = val;
  600. mutex_unlock(&ns->subsys->lock);
  601. return count;
  602. }
  603. CONFIGFS_ATTR(nvmet_ns_, buffered_io);
  604. static ssize_t nvmet_ns_revalidate_size_store(struct config_item *item,
  605. const char *page, size_t count)
  606. {
  607. struct nvmet_ns *ns = to_nvmet_ns(item);
  608. bool val;
  609. if (kstrtobool(page, &val))
  610. return -EINVAL;
  611. if (!val)
  612. return -EINVAL;
  613. mutex_lock(&ns->subsys->lock);
  614. if (!ns->enabled) {
  615. pr_err("enable ns before revalidate.\n");
  616. mutex_unlock(&ns->subsys->lock);
  617. return -EINVAL;
  618. }
  619. if (nvmet_ns_revalidate(ns))
  620. nvmet_ns_changed(ns->subsys, ns->nsid);
  621. mutex_unlock(&ns->subsys->lock);
  622. return count;
  623. }
  624. CONFIGFS_ATTR_WO(nvmet_ns_, revalidate_size);
  625. static struct configfs_attribute *nvmet_ns_attrs[] = {
  626. &nvmet_ns_attr_device_path,
  627. &nvmet_ns_attr_device_nguid,
  628. &nvmet_ns_attr_device_uuid,
  629. &nvmet_ns_attr_ana_grpid,
  630. &nvmet_ns_attr_enable,
  631. &nvmet_ns_attr_buffered_io,
  632. &nvmet_ns_attr_revalidate_size,
  633. #ifdef CONFIG_PCI_P2PDMA
  634. &nvmet_ns_attr_p2pmem,
  635. #endif
  636. NULL,
  637. };
  638. bool nvmet_subsys_nsid_exists(struct nvmet_subsys *subsys, u32 nsid)
  639. {
  640. struct config_item *ns_item;
  641. char name[12];
  642. snprintf(name, sizeof(name), "%u", nsid);
  643. mutex_lock(&subsys->namespaces_group.cg_subsys->su_mutex);
  644. ns_item = config_group_find_item(&subsys->namespaces_group, name);
  645. mutex_unlock(&subsys->namespaces_group.cg_subsys->su_mutex);
  646. return ns_item != NULL;
  647. }
  648. static void nvmet_ns_release(struct config_item *item)
  649. {
  650. struct nvmet_ns *ns = to_nvmet_ns(item);
  651. nvmet_ns_free(ns);
  652. }
  653. static struct configfs_item_operations nvmet_ns_item_ops = {
  654. .release = nvmet_ns_release,
  655. };
  656. static const struct config_item_type nvmet_ns_type = {
  657. .ct_item_ops = &nvmet_ns_item_ops,
  658. .ct_attrs = nvmet_ns_attrs,
  659. .ct_owner = THIS_MODULE,
  660. };
  661. static struct config_group *nvmet_ns_make(struct config_group *group,
  662. const char *name)
  663. {
  664. struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item);
  665. struct nvmet_ns *ns;
  666. int ret;
  667. u32 nsid;
  668. ret = kstrtou32(name, 0, &nsid);
  669. if (ret)
  670. goto out;
  671. ret = -EINVAL;
  672. if (nsid == 0 || nsid == NVME_NSID_ALL) {
  673. pr_err("invalid nsid %#x", nsid);
  674. goto out;
  675. }
  676. ret = -ENOMEM;
  677. ns = nvmet_ns_alloc(subsys, nsid);
  678. if (!ns)
  679. goto out;
  680. config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
  681. pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn);
  682. return &ns->group;
  683. out:
  684. return ERR_PTR(ret);
  685. }
  686. static struct configfs_group_operations nvmet_namespaces_group_ops = {
  687. .make_group = nvmet_ns_make,
  688. };
  689. static const struct config_item_type nvmet_namespaces_type = {
  690. .ct_group_ops = &nvmet_namespaces_group_ops,
  691. .ct_owner = THIS_MODULE,
  692. };
  693. #ifdef CONFIG_NVME_TARGET_PASSTHRU
  694. static ssize_t nvmet_passthru_device_path_show(struct config_item *item,
  695. char *page)
  696. {
  697. struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
  698. return snprintf(page, PAGE_SIZE, "%s\n", subsys->passthru_ctrl_path);
  699. }
  700. static ssize_t nvmet_passthru_device_path_store(struct config_item *item,
  701. const char *page, size_t count)
  702. {
  703. struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
  704. size_t len;
  705. int ret;
  706. mutex_lock(&subsys->lock);
  707. ret = -EBUSY;
  708. if (subsys->passthru_ctrl)
  709. goto out_unlock;
  710. ret = -EINVAL;
  711. len = strcspn(page, "\n");
  712. if (!len)
  713. goto out_unlock;
  714. kfree(subsys->passthru_ctrl_path);
  715. ret = -ENOMEM;
  716. subsys->passthru_ctrl_path = kstrndup(page, len, GFP_KERNEL);
  717. if (!subsys->passthru_ctrl_path)
  718. goto out_unlock;
  719. mutex_unlock(&subsys->lock);
  720. return count;
  721. out_unlock:
  722. mutex_unlock(&subsys->lock);
  723. return ret;
  724. }
  725. CONFIGFS_ATTR(nvmet_passthru_, device_path);
  726. static ssize_t nvmet_passthru_enable_show(struct config_item *item,
  727. char *page)
  728. {
  729. struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
  730. return sprintf(page, "%d\n", subsys->passthru_ctrl ? 1 : 0);
  731. }
  732. static ssize_t nvmet_passthru_enable_store(struct config_item *item,
  733. const char *page, size_t count)
  734. {
  735. struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
  736. bool enable;
  737. int ret = 0;
  738. if (kstrtobool(page, &enable))
  739. return -EINVAL;
  740. if (enable)
  741. ret = nvmet_passthru_ctrl_enable(subsys);
  742. else
  743. nvmet_passthru_ctrl_disable(subsys);
  744. return ret ? ret : count;
  745. }
  746. CONFIGFS_ATTR(nvmet_passthru_, enable);
  747. static ssize_t nvmet_passthru_admin_timeout_show(struct config_item *item,
  748. char *page)
  749. {
  750. return sprintf(page, "%u\n", to_subsys(item->ci_parent)->admin_timeout);
  751. }
  752. static ssize_t nvmet_passthru_admin_timeout_store(struct config_item *item,
  753. const char *page, size_t count)
  754. {
  755. struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
  756. unsigned int timeout;
  757. if (kstrtouint(page, 0, &timeout))
  758. return -EINVAL;
  759. subsys->admin_timeout = timeout;
  760. return count;
  761. }
  762. CONFIGFS_ATTR(nvmet_passthru_, admin_timeout);
  763. static ssize_t nvmet_passthru_io_timeout_show(struct config_item *item,
  764. char *page)
  765. {
  766. return sprintf(page, "%u\n", to_subsys(item->ci_parent)->io_timeout);
  767. }
  768. static ssize_t nvmet_passthru_io_timeout_store(struct config_item *item,
  769. const char *page, size_t count)
  770. {
  771. struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
  772. unsigned int timeout;
  773. if (kstrtouint(page, 0, &timeout))
  774. return -EINVAL;
  775. subsys->io_timeout = timeout;
  776. return count;
  777. }
  778. CONFIGFS_ATTR(nvmet_passthru_, io_timeout);
  779. static ssize_t nvmet_passthru_clear_ids_show(struct config_item *item,
  780. char *page)
  781. {
  782. return sprintf(page, "%u\n", to_subsys(item->ci_parent)->clear_ids);
  783. }
  784. static ssize_t nvmet_passthru_clear_ids_store(struct config_item *item,
  785. const char *page, size_t count)
  786. {
  787. struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
  788. unsigned int clear_ids;
  789. if (kstrtouint(page, 0, &clear_ids))
  790. return -EINVAL;
  791. subsys->clear_ids = clear_ids;
  792. return count;
  793. }
  794. CONFIGFS_ATTR(nvmet_passthru_, clear_ids);
  795. static struct configfs_attribute *nvmet_passthru_attrs[] = {
  796. &nvmet_passthru_attr_device_path,
  797. &nvmet_passthru_attr_enable,
  798. &nvmet_passthru_attr_admin_timeout,
  799. &nvmet_passthru_attr_io_timeout,
  800. &nvmet_passthru_attr_clear_ids,
  801. NULL,
  802. };
  803. static const struct config_item_type nvmet_passthru_type = {
  804. .ct_attrs = nvmet_passthru_attrs,
  805. .ct_owner = THIS_MODULE,
  806. };
  807. static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
  808. {
  809. config_group_init_type_name(&subsys->passthru_group,
  810. "passthru", &nvmet_passthru_type);
  811. configfs_add_default_group(&subsys->passthru_group,
  812. &subsys->group);
  813. }
  814. #else /* CONFIG_NVME_TARGET_PASSTHRU */
  815. static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
  816. {
  817. }
  818. #endif /* CONFIG_NVME_TARGET_PASSTHRU */
  819. static int nvmet_port_subsys_allow_link(struct config_item *parent,
  820. struct config_item *target)
  821. {
  822. struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
  823. struct nvmet_subsys *subsys;
  824. struct nvmet_subsys_link *link, *p;
  825. int ret;
  826. if (target->ci_type != &nvmet_subsys_type) {
  827. pr_err("can only link subsystems into the subsystems dir.!\n");
  828. return -EINVAL;
  829. }
  830. subsys = to_subsys(target);
  831. link = kmalloc(sizeof(*link), GFP_KERNEL);
  832. if (!link)
  833. return -ENOMEM;
  834. link->subsys = subsys;
  835. down_write(&nvmet_config_sem);
  836. ret = -EEXIST;
  837. list_for_each_entry(p, &port->subsystems, entry) {
  838. if (p->subsys == subsys)
  839. goto out_free_link;
  840. }
  841. if (list_empty(&port->subsystems)) {
  842. ret = nvmet_enable_port(port);
  843. if (ret)
  844. goto out_free_link;
  845. }
  846. list_add_tail(&link->entry, &port->subsystems);
  847. nvmet_port_disc_changed(port, subsys);
  848. up_write(&nvmet_config_sem);
  849. return 0;
  850. out_free_link:
  851. up_write(&nvmet_config_sem);
  852. kfree(link);
  853. return ret;
  854. }
  855. static void nvmet_port_subsys_drop_link(struct config_item *parent,
  856. struct config_item *target)
  857. {
  858. struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
  859. struct nvmet_subsys *subsys = to_subsys(target);
  860. struct nvmet_subsys_link *p;
  861. down_write(&nvmet_config_sem);
  862. list_for_each_entry(p, &port->subsystems, entry) {
  863. if (p->subsys == subsys)
  864. goto found;
  865. }
  866. up_write(&nvmet_config_sem);
  867. return;
  868. found:
  869. list_del(&p->entry);
  870. nvmet_port_del_ctrls(port, subsys);
  871. nvmet_port_disc_changed(port, subsys);
  872. if (list_empty(&port->subsystems))
  873. nvmet_disable_port(port);
  874. up_write(&nvmet_config_sem);
  875. kfree(p);
  876. }
  877. static struct configfs_item_operations nvmet_port_subsys_item_ops = {
  878. .allow_link = nvmet_port_subsys_allow_link,
  879. .drop_link = nvmet_port_subsys_drop_link,
  880. };
  881. static const struct config_item_type nvmet_port_subsys_type = {
  882. .ct_item_ops = &nvmet_port_subsys_item_ops,
  883. .ct_owner = THIS_MODULE,
  884. };
  885. static int nvmet_allowed_hosts_allow_link(struct config_item *parent,
  886. struct config_item *target)
  887. {
  888. struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
  889. struct nvmet_host *host;
  890. struct nvmet_host_link *link, *p;
  891. int ret;
  892. if (target->ci_type != &nvmet_host_type) {
  893. pr_err("can only link hosts into the allowed_hosts directory!\n");
  894. return -EINVAL;
  895. }
  896. host = to_host(target);
  897. link = kmalloc(sizeof(*link), GFP_KERNEL);
  898. if (!link)
  899. return -ENOMEM;
  900. link->host = host;
  901. down_write(&nvmet_config_sem);
  902. ret = -EINVAL;
  903. if (subsys->allow_any_host) {
  904. pr_err("can't add hosts when allow_any_host is set!\n");
  905. goto out_free_link;
  906. }
  907. ret = -EEXIST;
  908. list_for_each_entry(p, &subsys->hosts, entry) {
  909. if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
  910. goto out_free_link;
  911. }
  912. list_add_tail(&link->entry, &subsys->hosts);
  913. nvmet_subsys_disc_changed(subsys, host);
  914. up_write(&nvmet_config_sem);
  915. return 0;
  916. out_free_link:
  917. up_write(&nvmet_config_sem);
  918. kfree(link);
  919. return ret;
  920. }
  921. static void nvmet_allowed_hosts_drop_link(struct config_item *parent,
  922. struct config_item *target)
  923. {
  924. struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
  925. struct nvmet_host *host = to_host(target);
  926. struct nvmet_host_link *p;
  927. down_write(&nvmet_config_sem);
  928. list_for_each_entry(p, &subsys->hosts, entry) {
  929. if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
  930. goto found;
  931. }
  932. up_write(&nvmet_config_sem);
  933. return;
  934. found:
  935. list_del(&p->entry);
  936. nvmet_subsys_disc_changed(subsys, host);
  937. up_write(&nvmet_config_sem);
  938. kfree(p);
  939. }
  940. static struct configfs_item_operations nvmet_allowed_hosts_item_ops = {
  941. .allow_link = nvmet_allowed_hosts_allow_link,
  942. .drop_link = nvmet_allowed_hosts_drop_link,
  943. };
  944. static const struct config_item_type nvmet_allowed_hosts_type = {
  945. .ct_item_ops = &nvmet_allowed_hosts_item_ops,
  946. .ct_owner = THIS_MODULE,
  947. };
  948. static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item,
  949. char *page)
  950. {
  951. return snprintf(page, PAGE_SIZE, "%d\n",
  952. to_subsys(item)->allow_any_host);
  953. }
  954. static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item,
  955. const char *page, size_t count)
  956. {
  957. struct nvmet_subsys *subsys = to_subsys(item);
  958. bool allow_any_host;
  959. int ret = 0;
  960. if (kstrtobool(page, &allow_any_host))
  961. return -EINVAL;
  962. down_write(&nvmet_config_sem);
  963. if (allow_any_host && !list_empty(&subsys->hosts)) {
  964. pr_err("Can't set allow_any_host when explicit hosts are set!\n");
  965. ret = -EINVAL;
  966. goto out_unlock;
  967. }
  968. if (subsys->allow_any_host != allow_any_host) {
  969. subsys->allow_any_host = allow_any_host;
  970. nvmet_subsys_disc_changed(subsys, NULL);
  971. }
  972. out_unlock:
  973. up_write(&nvmet_config_sem);
  974. return ret ? ret : count;
  975. }
  976. CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host);
  977. static ssize_t nvmet_subsys_attr_version_show(struct config_item *item,
  978. char *page)
  979. {
  980. struct nvmet_subsys *subsys = to_subsys(item);
  981. if (NVME_TERTIARY(subsys->ver))
  982. return snprintf(page, PAGE_SIZE, "%llu.%llu.%llu\n",
  983. NVME_MAJOR(subsys->ver),
  984. NVME_MINOR(subsys->ver),
  985. NVME_TERTIARY(subsys->ver));
  986. return snprintf(page, PAGE_SIZE, "%llu.%llu\n",
  987. NVME_MAJOR(subsys->ver),
  988. NVME_MINOR(subsys->ver));
  989. }
  990. static ssize_t
  991. nvmet_subsys_attr_version_store_locked(struct nvmet_subsys *subsys,
  992. const char *page, size_t count)
  993. {
  994. int major, minor, tertiary = 0;
  995. int ret;
  996. if (subsys->subsys_discovered) {
  997. if (NVME_TERTIARY(subsys->ver))
  998. pr_err("Can't set version number. %llu.%llu.%llu is already assigned\n",
  999. NVME_MAJOR(subsys->ver),
  1000. NVME_MINOR(subsys->ver),
  1001. NVME_TERTIARY(subsys->ver));
  1002. else
  1003. pr_err("Can't set version number. %llu.%llu is already assigned\n",
  1004. NVME_MAJOR(subsys->ver),
  1005. NVME_MINOR(subsys->ver));
  1006. return -EINVAL;
  1007. }
  1008. /* passthru subsystems use the underlying controller's version */
  1009. if (nvmet_is_passthru_subsys(subsys))
  1010. return -EINVAL;
  1011. ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary);
  1012. if (ret != 2 && ret != 3)
  1013. return -EINVAL;
  1014. subsys->ver = NVME_VS(major, minor, tertiary);
  1015. return count;
  1016. }
  1017. static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,
  1018. const char *page, size_t count)
  1019. {
  1020. struct nvmet_subsys *subsys = to_subsys(item);
  1021. ssize_t ret;
  1022. down_write(&nvmet_config_sem);
  1023. mutex_lock(&subsys->lock);
  1024. ret = nvmet_subsys_attr_version_store_locked(subsys, page, count);
  1025. mutex_unlock(&subsys->lock);
  1026. up_write(&nvmet_config_sem);
  1027. return ret;
  1028. }
  1029. CONFIGFS_ATTR(nvmet_subsys_, attr_version);
  1030. /* See Section 1.5 of NVMe 1.4 */
  1031. static bool nvmet_is_ascii(const char c)
  1032. {
  1033. return c >= 0x20 && c <= 0x7e;
  1034. }
  1035. static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
  1036. char *page)
  1037. {
  1038. struct nvmet_subsys *subsys = to_subsys(item);
  1039. return snprintf(page, PAGE_SIZE, "%.*s\n",
  1040. NVMET_SN_MAX_SIZE, subsys->serial);
  1041. }
  1042. static ssize_t
  1043. nvmet_subsys_attr_serial_store_locked(struct nvmet_subsys *subsys,
  1044. const char *page, size_t count)
  1045. {
  1046. int pos, len = strcspn(page, "\n");
  1047. if (subsys->subsys_discovered) {
  1048. pr_err("Can't set serial number. %s is already assigned\n",
  1049. subsys->serial);
  1050. return -EINVAL;
  1051. }
  1052. if (!len || len > NVMET_SN_MAX_SIZE) {
  1053. pr_err("Serial Number can not be empty or exceed %d Bytes\n",
  1054. NVMET_SN_MAX_SIZE);
  1055. return -EINVAL;
  1056. }
  1057. for (pos = 0; pos < len; pos++) {
  1058. if (!nvmet_is_ascii(page[pos])) {
  1059. pr_err("Serial Number must contain only ASCII strings\n");
  1060. return -EINVAL;
  1061. }
  1062. }
  1063. memcpy_and_pad(subsys->serial, NVMET_SN_MAX_SIZE, page, len, ' ');
  1064. return count;
  1065. }
  1066. static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item,
  1067. const char *page, size_t count)
  1068. {
  1069. struct nvmet_subsys *subsys = to_subsys(item);
  1070. ssize_t ret;
  1071. down_write(&nvmet_config_sem);
  1072. mutex_lock(&subsys->lock);
  1073. ret = nvmet_subsys_attr_serial_store_locked(subsys, page, count);
  1074. mutex_unlock(&subsys->lock);
  1075. up_write(&nvmet_config_sem);
  1076. return ret;
  1077. }
  1078. CONFIGFS_ATTR(nvmet_subsys_, attr_serial);
  1079. static ssize_t nvmet_subsys_attr_cntlid_min_show(struct config_item *item,
  1080. char *page)
  1081. {
  1082. return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_min);
  1083. }
  1084. static ssize_t nvmet_subsys_attr_cntlid_min_store(struct config_item *item,
  1085. const char *page, size_t cnt)
  1086. {
  1087. u16 cntlid_min;
  1088. if (sscanf(page, "%hu\n", &cntlid_min) != 1)
  1089. return -EINVAL;
  1090. if (cntlid_min == 0)
  1091. return -EINVAL;
  1092. down_write(&nvmet_config_sem);
  1093. if (cntlid_min > to_subsys(item)->cntlid_max)
  1094. goto out_unlock;
  1095. to_subsys(item)->cntlid_min = cntlid_min;
  1096. up_write(&nvmet_config_sem);
  1097. return cnt;
  1098. out_unlock:
  1099. up_write(&nvmet_config_sem);
  1100. return -EINVAL;
  1101. }
  1102. CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_min);
  1103. static ssize_t nvmet_subsys_attr_cntlid_max_show(struct config_item *item,
  1104. char *page)
  1105. {
  1106. return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_max);
  1107. }
  1108. static ssize_t nvmet_subsys_attr_cntlid_max_store(struct config_item *item,
  1109. const char *page, size_t cnt)
  1110. {
  1111. u16 cntlid_max;
  1112. if (sscanf(page, "%hu\n", &cntlid_max) != 1)
  1113. return -EINVAL;
  1114. if (cntlid_max == 0)
  1115. return -EINVAL;
  1116. down_write(&nvmet_config_sem);
  1117. if (cntlid_max < to_subsys(item)->cntlid_min)
  1118. goto out_unlock;
  1119. to_subsys(item)->cntlid_max = cntlid_max;
  1120. up_write(&nvmet_config_sem);
  1121. return cnt;
  1122. out_unlock:
  1123. up_write(&nvmet_config_sem);
  1124. return -EINVAL;
  1125. }
  1126. CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_max);
  1127. static ssize_t nvmet_subsys_attr_model_show(struct config_item *item,
  1128. char *page)
  1129. {
  1130. struct nvmet_subsys *subsys = to_subsys(item);
  1131. return snprintf(page, PAGE_SIZE, "%s\n", subsys->model_number);
  1132. }
  1133. static ssize_t nvmet_subsys_attr_model_store_locked(struct nvmet_subsys *subsys,
  1134. const char *page, size_t count)
  1135. {
  1136. int pos = 0, len;
  1137. char *val;
  1138. if (subsys->subsys_discovered) {
  1139. pr_err("Can't set model number. %s is already assigned\n",
  1140. subsys->model_number);
  1141. return -EINVAL;
  1142. }
  1143. len = strcspn(page, "\n");
  1144. if (!len)
  1145. return -EINVAL;
  1146. if (len > NVMET_MN_MAX_SIZE) {
  1147. pr_err("Model number size can not exceed %d Bytes\n",
  1148. NVMET_MN_MAX_SIZE);
  1149. return -EINVAL;
  1150. }
  1151. for (pos = 0; pos < len; pos++) {
  1152. if (!nvmet_is_ascii(page[pos]))
  1153. return -EINVAL;
  1154. }
  1155. val = kmemdup_nul(page, len, GFP_KERNEL);
  1156. if (!val)
  1157. return -ENOMEM;
  1158. kfree(subsys->model_number);
  1159. subsys->model_number = val;
  1160. return count;
  1161. }
  1162. static ssize_t nvmet_subsys_attr_model_store(struct config_item *item,
  1163. const char *page, size_t count)
  1164. {
  1165. struct nvmet_subsys *subsys = to_subsys(item);
  1166. ssize_t ret;
  1167. down_write(&nvmet_config_sem);
  1168. mutex_lock(&subsys->lock);
  1169. ret = nvmet_subsys_attr_model_store_locked(subsys, page, count);
  1170. mutex_unlock(&subsys->lock);
  1171. up_write(&nvmet_config_sem);
  1172. return ret;
  1173. }
  1174. CONFIGFS_ATTR(nvmet_subsys_, attr_model);
  1175. static ssize_t nvmet_subsys_attr_ieee_oui_show(struct config_item *item,
  1176. char *page)
  1177. {
  1178. struct nvmet_subsys *subsys = to_subsys(item);
  1179. return sysfs_emit(page, "0x%06x\n", subsys->ieee_oui);
  1180. }
  1181. static ssize_t nvmet_subsys_attr_ieee_oui_store_locked(struct nvmet_subsys *subsys,
  1182. const char *page, size_t count)
  1183. {
  1184. uint32_t val = 0;
  1185. int ret;
  1186. if (subsys->subsys_discovered) {
  1187. pr_err("Can't set IEEE OUI. 0x%06x is already assigned\n",
  1188. subsys->ieee_oui);
  1189. return -EINVAL;
  1190. }
  1191. ret = kstrtou32(page, 0, &val);
  1192. if (ret < 0)
  1193. return ret;
  1194. if (val >= 0x1000000)
  1195. return -EINVAL;
  1196. subsys->ieee_oui = val;
  1197. return count;
  1198. }
  1199. static ssize_t nvmet_subsys_attr_ieee_oui_store(struct config_item *item,
  1200. const char *page, size_t count)
  1201. {
  1202. struct nvmet_subsys *subsys = to_subsys(item);
  1203. ssize_t ret;
  1204. down_write(&nvmet_config_sem);
  1205. mutex_lock(&subsys->lock);
  1206. ret = nvmet_subsys_attr_ieee_oui_store_locked(subsys, page, count);
  1207. mutex_unlock(&subsys->lock);
  1208. up_write(&nvmet_config_sem);
  1209. return ret;
  1210. }
  1211. CONFIGFS_ATTR(nvmet_subsys_, attr_ieee_oui);
  1212. static ssize_t nvmet_subsys_attr_firmware_show(struct config_item *item,
  1213. char *page)
  1214. {
  1215. struct nvmet_subsys *subsys = to_subsys(item);
  1216. return sysfs_emit(page, "%s\n", subsys->firmware_rev);
  1217. }
  1218. static ssize_t nvmet_subsys_attr_firmware_store_locked(struct nvmet_subsys *subsys,
  1219. const char *page, size_t count)
  1220. {
  1221. int pos = 0, len;
  1222. char *val;
  1223. if (subsys->subsys_discovered) {
  1224. pr_err("Can't set firmware revision. %s is already assigned\n",
  1225. subsys->firmware_rev);
  1226. return -EINVAL;
  1227. }
  1228. len = strcspn(page, "\n");
  1229. if (!len)
  1230. return -EINVAL;
  1231. if (len > NVMET_FR_MAX_SIZE) {
  1232. pr_err("Firmware revision size can not exceed %d Bytes\n",
  1233. NVMET_FR_MAX_SIZE);
  1234. return -EINVAL;
  1235. }
  1236. for (pos = 0; pos < len; pos++) {
  1237. if (!nvmet_is_ascii(page[pos]))
  1238. return -EINVAL;
  1239. }
  1240. val = kmemdup_nul(page, len, GFP_KERNEL);
  1241. if (!val)
  1242. return -ENOMEM;
  1243. kfree(subsys->firmware_rev);
  1244. subsys->firmware_rev = val;
  1245. return count;
  1246. }
  1247. static ssize_t nvmet_subsys_attr_firmware_store(struct config_item *item,
  1248. const char *page, size_t count)
  1249. {
  1250. struct nvmet_subsys *subsys = to_subsys(item);
  1251. ssize_t ret;
  1252. down_write(&nvmet_config_sem);
  1253. mutex_lock(&subsys->lock);
  1254. ret = nvmet_subsys_attr_firmware_store_locked(subsys, page, count);
  1255. mutex_unlock(&subsys->lock);
  1256. up_write(&nvmet_config_sem);
  1257. return ret;
  1258. }
  1259. CONFIGFS_ATTR(nvmet_subsys_, attr_firmware);
  1260. #ifdef CONFIG_BLK_DEV_INTEGRITY
  1261. static ssize_t nvmet_subsys_attr_pi_enable_show(struct config_item *item,
  1262. char *page)
  1263. {
  1264. return snprintf(page, PAGE_SIZE, "%d\n", to_subsys(item)->pi_support);
  1265. }
  1266. static ssize_t nvmet_subsys_attr_pi_enable_store(struct config_item *item,
  1267. const char *page, size_t count)
  1268. {
  1269. struct nvmet_subsys *subsys = to_subsys(item);
  1270. bool pi_enable;
  1271. if (kstrtobool(page, &pi_enable))
  1272. return -EINVAL;
  1273. subsys->pi_support = pi_enable;
  1274. return count;
  1275. }
  1276. CONFIGFS_ATTR(nvmet_subsys_, attr_pi_enable);
  1277. #endif
  1278. static ssize_t nvmet_subsys_attr_qid_max_show(struct config_item *item,
  1279. char *page)
  1280. {
  1281. return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->max_qid);
  1282. }
  1283. static ssize_t nvmet_subsys_attr_qid_max_store(struct config_item *item,
  1284. const char *page, size_t cnt)
  1285. {
  1286. struct nvmet_subsys *subsys = to_subsys(item);
  1287. struct nvmet_ctrl *ctrl;
  1288. u16 qid_max;
  1289. if (sscanf(page, "%hu\n", &qid_max) != 1)
  1290. return -EINVAL;
  1291. if (qid_max < 1 || qid_max > NVMET_NR_QUEUES)
  1292. return -EINVAL;
  1293. down_write(&nvmet_config_sem);
  1294. subsys->max_qid = qid_max;
  1295. /* Force reconnect */
  1296. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
  1297. ctrl->ops->delete_ctrl(ctrl);
  1298. up_write(&nvmet_config_sem);
  1299. return cnt;
  1300. }
  1301. CONFIGFS_ATTR(nvmet_subsys_, attr_qid_max);
  1302. static struct configfs_attribute *nvmet_subsys_attrs[] = {
  1303. &nvmet_subsys_attr_attr_allow_any_host,
  1304. &nvmet_subsys_attr_attr_version,
  1305. &nvmet_subsys_attr_attr_serial,
  1306. &nvmet_subsys_attr_attr_cntlid_min,
  1307. &nvmet_subsys_attr_attr_cntlid_max,
  1308. &nvmet_subsys_attr_attr_model,
  1309. &nvmet_subsys_attr_attr_qid_max,
  1310. &nvmet_subsys_attr_attr_ieee_oui,
  1311. &nvmet_subsys_attr_attr_firmware,
  1312. #ifdef CONFIG_BLK_DEV_INTEGRITY
  1313. &nvmet_subsys_attr_attr_pi_enable,
  1314. #endif
  1315. NULL,
  1316. };
  1317. /*
  1318. * Subsystem structures & folder operation functions below
  1319. */
  1320. static void nvmet_subsys_release(struct config_item *item)
  1321. {
  1322. struct nvmet_subsys *subsys = to_subsys(item);
  1323. nvmet_subsys_del_ctrls(subsys);
  1324. nvmet_subsys_put(subsys);
  1325. }
  1326. static struct configfs_item_operations nvmet_subsys_item_ops = {
  1327. .release = nvmet_subsys_release,
  1328. };
  1329. static const struct config_item_type nvmet_subsys_type = {
  1330. .ct_item_ops = &nvmet_subsys_item_ops,
  1331. .ct_attrs = nvmet_subsys_attrs,
  1332. .ct_owner = THIS_MODULE,
  1333. };
  1334. static struct config_group *nvmet_subsys_make(struct config_group *group,
  1335. const char *name)
  1336. {
  1337. struct nvmet_subsys *subsys;
  1338. if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) {
  1339. pr_err("can't create discovery subsystem through configfs\n");
  1340. return ERR_PTR(-EINVAL);
  1341. }
  1342. if (sysfs_streq(name, nvmet_disc_subsys->subsysnqn)) {
  1343. pr_err("can't create subsystem using unique discovery NQN\n");
  1344. return ERR_PTR(-EINVAL);
  1345. }
  1346. subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME);
  1347. if (IS_ERR(subsys))
  1348. return ERR_CAST(subsys);
  1349. config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type);
  1350. config_group_init_type_name(&subsys->namespaces_group,
  1351. "namespaces", &nvmet_namespaces_type);
  1352. configfs_add_default_group(&subsys->namespaces_group, &subsys->group);
  1353. config_group_init_type_name(&subsys->allowed_hosts_group,
  1354. "allowed_hosts", &nvmet_allowed_hosts_type);
  1355. configfs_add_default_group(&subsys->allowed_hosts_group,
  1356. &subsys->group);
  1357. nvmet_add_passthru_group(subsys);
  1358. return &subsys->group;
  1359. }
  1360. static struct configfs_group_operations nvmet_subsystems_group_ops = {
  1361. .make_group = nvmet_subsys_make,
  1362. };
  1363. static const struct config_item_type nvmet_subsystems_type = {
  1364. .ct_group_ops = &nvmet_subsystems_group_ops,
  1365. .ct_owner = THIS_MODULE,
  1366. };
  1367. static ssize_t nvmet_referral_enable_show(struct config_item *item,
  1368. char *page)
  1369. {
  1370. return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled);
  1371. }
  1372. static ssize_t nvmet_referral_enable_store(struct config_item *item,
  1373. const char *page, size_t count)
  1374. {
  1375. struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
  1376. struct nvmet_port *port = to_nvmet_port(item);
  1377. bool enable;
  1378. if (kstrtobool(page, &enable))
  1379. goto inval;
  1380. if (enable)
  1381. nvmet_referral_enable(parent, port);
  1382. else
  1383. nvmet_referral_disable(parent, port);
  1384. return count;
  1385. inval:
  1386. pr_err("Invalid value '%s' for enable\n", page);
  1387. return -EINVAL;
  1388. }
  1389. CONFIGFS_ATTR(nvmet_referral_, enable);
  1390. /*
  1391. * Discovery Service subsystem definitions
  1392. */
  1393. static struct configfs_attribute *nvmet_referral_attrs[] = {
  1394. &nvmet_attr_addr_adrfam,
  1395. &nvmet_attr_addr_portid,
  1396. &nvmet_attr_addr_treq,
  1397. &nvmet_attr_addr_traddr,
  1398. &nvmet_attr_addr_trsvcid,
  1399. &nvmet_attr_addr_trtype,
  1400. &nvmet_referral_attr_enable,
  1401. NULL,
  1402. };
  1403. static void nvmet_referral_notify(struct config_group *group,
  1404. struct config_item *item)
  1405. {
  1406. struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
  1407. struct nvmet_port *port = to_nvmet_port(item);
  1408. nvmet_referral_disable(parent, port);
  1409. }
  1410. static void nvmet_referral_release(struct config_item *item)
  1411. {
  1412. struct nvmet_port *port = to_nvmet_port(item);
  1413. kfree(port);
  1414. }
  1415. static struct configfs_item_operations nvmet_referral_item_ops = {
  1416. .release = nvmet_referral_release,
  1417. };
  1418. static const struct config_item_type nvmet_referral_type = {
  1419. .ct_owner = THIS_MODULE,
  1420. .ct_attrs = nvmet_referral_attrs,
  1421. .ct_item_ops = &nvmet_referral_item_ops,
  1422. };
  1423. static struct config_group *nvmet_referral_make(
  1424. struct config_group *group, const char *name)
  1425. {
  1426. struct nvmet_port *port;
  1427. port = kzalloc(sizeof(*port), GFP_KERNEL);
  1428. if (!port)
  1429. return ERR_PTR(-ENOMEM);
  1430. INIT_LIST_HEAD(&port->entry);
  1431. config_group_init_type_name(&port->group, name, &nvmet_referral_type);
  1432. return &port->group;
  1433. }
  1434. static struct configfs_group_operations nvmet_referral_group_ops = {
  1435. .make_group = nvmet_referral_make,
  1436. .disconnect_notify = nvmet_referral_notify,
  1437. };
  1438. static const struct config_item_type nvmet_referrals_type = {
  1439. .ct_owner = THIS_MODULE,
  1440. .ct_group_ops = &nvmet_referral_group_ops,
  1441. };
  1442. static struct nvmet_type_name_map nvmet_ana_state[] = {
  1443. { NVME_ANA_OPTIMIZED, "optimized" },
  1444. { NVME_ANA_NONOPTIMIZED, "non-optimized" },
  1445. { NVME_ANA_INACCESSIBLE, "inaccessible" },
  1446. { NVME_ANA_PERSISTENT_LOSS, "persistent-loss" },
  1447. { NVME_ANA_CHANGE, "change" },
  1448. };
  1449. static ssize_t nvmet_ana_group_ana_state_show(struct config_item *item,
  1450. char *page)
  1451. {
  1452. struct nvmet_ana_group *grp = to_ana_group(item);
  1453. enum nvme_ana_state state = grp->port->ana_state[grp->grpid];
  1454. int i;
  1455. for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
  1456. if (state == nvmet_ana_state[i].type)
  1457. return sprintf(page, "%s\n", nvmet_ana_state[i].name);
  1458. }
  1459. return sprintf(page, "\n");
  1460. }
  1461. static ssize_t nvmet_ana_group_ana_state_store(struct config_item *item,
  1462. const char *page, size_t count)
  1463. {
  1464. struct nvmet_ana_group *grp = to_ana_group(item);
  1465. enum nvme_ana_state *ana_state = grp->port->ana_state;
  1466. int i;
  1467. for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
  1468. if (sysfs_streq(page, nvmet_ana_state[i].name))
  1469. goto found;
  1470. }
  1471. pr_err("Invalid value '%s' for ana_state\n", page);
  1472. return -EINVAL;
  1473. found:
  1474. down_write(&nvmet_ana_sem);
  1475. ana_state[grp->grpid] = (enum nvme_ana_state) nvmet_ana_state[i].type;
  1476. nvmet_ana_chgcnt++;
  1477. up_write(&nvmet_ana_sem);
  1478. nvmet_port_send_ana_event(grp->port);
  1479. return count;
  1480. }
  1481. CONFIGFS_ATTR(nvmet_ana_group_, ana_state);
  1482. static struct configfs_attribute *nvmet_ana_group_attrs[] = {
  1483. &nvmet_ana_group_attr_ana_state,
  1484. NULL,
  1485. };
  1486. static void nvmet_ana_group_release(struct config_item *item)
  1487. {
  1488. struct nvmet_ana_group *grp = to_ana_group(item);
  1489. if (grp == &grp->port->ana_default_group)
  1490. return;
  1491. down_write(&nvmet_ana_sem);
  1492. grp->port->ana_state[grp->grpid] = NVME_ANA_INACCESSIBLE;
  1493. nvmet_ana_group_enabled[grp->grpid]--;
  1494. up_write(&nvmet_ana_sem);
  1495. nvmet_port_send_ana_event(grp->port);
  1496. kfree(grp);
  1497. }
  1498. static struct configfs_item_operations nvmet_ana_group_item_ops = {
  1499. .release = nvmet_ana_group_release,
  1500. };
  1501. static const struct config_item_type nvmet_ana_group_type = {
  1502. .ct_item_ops = &nvmet_ana_group_item_ops,
  1503. .ct_attrs = nvmet_ana_group_attrs,
  1504. .ct_owner = THIS_MODULE,
  1505. };
  1506. static struct config_group *nvmet_ana_groups_make_group(
  1507. struct config_group *group, const char *name)
  1508. {
  1509. struct nvmet_port *port = ana_groups_to_port(&group->cg_item);
  1510. struct nvmet_ana_group *grp;
  1511. u32 grpid;
  1512. int ret;
  1513. ret = kstrtou32(name, 0, &grpid);
  1514. if (ret)
  1515. goto out;
  1516. ret = -EINVAL;
  1517. if (grpid <= 1 || grpid > NVMET_MAX_ANAGRPS)
  1518. goto out;
  1519. ret = -ENOMEM;
  1520. grp = kzalloc(sizeof(*grp), GFP_KERNEL);
  1521. if (!grp)
  1522. goto out;
  1523. grp->port = port;
  1524. grp->grpid = grpid;
  1525. down_write(&nvmet_ana_sem);
  1526. grpid = array_index_nospec(grpid, NVMET_MAX_ANAGRPS);
  1527. nvmet_ana_group_enabled[grpid]++;
  1528. up_write(&nvmet_ana_sem);
  1529. nvmet_port_send_ana_event(grp->port);
  1530. config_group_init_type_name(&grp->group, name, &nvmet_ana_group_type);
  1531. return &grp->group;
  1532. out:
  1533. return ERR_PTR(ret);
  1534. }
  1535. static struct configfs_group_operations nvmet_ana_groups_group_ops = {
  1536. .make_group = nvmet_ana_groups_make_group,
  1537. };
  1538. static const struct config_item_type nvmet_ana_groups_type = {
  1539. .ct_group_ops = &nvmet_ana_groups_group_ops,
  1540. .ct_owner = THIS_MODULE,
  1541. };
  1542. /*
  1543. * Ports definitions.
  1544. */
  1545. static void nvmet_port_release(struct config_item *item)
  1546. {
  1547. struct nvmet_port *port = to_nvmet_port(item);
  1548. /* Let inflight controllers teardown complete */
  1549. flush_workqueue(nvmet_wq);
  1550. list_del(&port->global_entry);
  1551. key_put(port->keyring);
  1552. kfree(port->ana_state);
  1553. kfree(port);
  1554. }
  1555. static struct configfs_attribute *nvmet_port_attrs[] = {
  1556. &nvmet_attr_addr_adrfam,
  1557. &nvmet_attr_addr_treq,
  1558. &nvmet_attr_addr_traddr,
  1559. &nvmet_attr_addr_trsvcid,
  1560. &nvmet_attr_addr_trtype,
  1561. &nvmet_attr_addr_tsas,
  1562. &nvmet_attr_param_inline_data_size,
  1563. &nvmet_attr_param_max_queue_size,
  1564. #ifdef CONFIG_BLK_DEV_INTEGRITY
  1565. &nvmet_attr_param_pi_enable,
  1566. #endif
  1567. NULL,
  1568. };
  1569. static struct configfs_item_operations nvmet_port_item_ops = {
  1570. .release = nvmet_port_release,
  1571. };
  1572. static const struct config_item_type nvmet_port_type = {
  1573. .ct_attrs = nvmet_port_attrs,
  1574. .ct_item_ops = &nvmet_port_item_ops,
  1575. .ct_owner = THIS_MODULE,
  1576. };
  1577. static struct config_group *nvmet_ports_make(struct config_group *group,
  1578. const char *name)
  1579. {
  1580. struct nvmet_port *port;
  1581. u16 portid;
  1582. u32 i;
  1583. if (kstrtou16(name, 0, &portid))
  1584. return ERR_PTR(-EINVAL);
  1585. port = kzalloc(sizeof(*port), GFP_KERNEL);
  1586. if (!port)
  1587. return ERR_PTR(-ENOMEM);
  1588. port->ana_state = kcalloc(NVMET_MAX_ANAGRPS + 1,
  1589. sizeof(*port->ana_state), GFP_KERNEL);
  1590. if (!port->ana_state) {
  1591. kfree(port);
  1592. return ERR_PTR(-ENOMEM);
  1593. }
  1594. if (IS_ENABLED(CONFIG_NVME_TARGET_TCP_TLS) && nvme_keyring_id()) {
  1595. port->keyring = key_lookup(nvme_keyring_id());
  1596. if (IS_ERR(port->keyring)) {
  1597. pr_warn("NVMe keyring not available, disabling TLS\n");
  1598. port->keyring = NULL;
  1599. }
  1600. }
  1601. for (i = 1; i <= NVMET_MAX_ANAGRPS; i++) {
  1602. if (i == NVMET_DEFAULT_ANA_GRPID)
  1603. port->ana_state[1] = NVME_ANA_OPTIMIZED;
  1604. else
  1605. port->ana_state[i] = NVME_ANA_INACCESSIBLE;
  1606. }
  1607. list_add(&port->global_entry, &nvmet_ports_list);
  1608. INIT_LIST_HEAD(&port->entry);
  1609. INIT_LIST_HEAD(&port->subsystems);
  1610. INIT_LIST_HEAD(&port->referrals);
  1611. port->inline_data_size = -1; /* < 0 == let the transport choose */
  1612. port->max_queue_size = -1; /* < 0 == let the transport choose */
  1613. port->disc_addr.portid = cpu_to_le16(portid);
  1614. port->disc_addr.adrfam = NVMF_ADDR_FAMILY_MAX;
  1615. port->disc_addr.treq = NVMF_TREQ_DISABLE_SQFLOW;
  1616. config_group_init_type_name(&port->group, name, &nvmet_port_type);
  1617. config_group_init_type_name(&port->subsys_group,
  1618. "subsystems", &nvmet_port_subsys_type);
  1619. configfs_add_default_group(&port->subsys_group, &port->group);
  1620. config_group_init_type_name(&port->referrals_group,
  1621. "referrals", &nvmet_referrals_type);
  1622. configfs_add_default_group(&port->referrals_group, &port->group);
  1623. config_group_init_type_name(&port->ana_groups_group,
  1624. "ana_groups", &nvmet_ana_groups_type);
  1625. configfs_add_default_group(&port->ana_groups_group, &port->group);
  1626. port->ana_default_group.port = port;
  1627. port->ana_default_group.grpid = NVMET_DEFAULT_ANA_GRPID;
  1628. config_group_init_type_name(&port->ana_default_group.group,
  1629. __stringify(NVMET_DEFAULT_ANA_GRPID),
  1630. &nvmet_ana_group_type);
  1631. configfs_add_default_group(&port->ana_default_group.group,
  1632. &port->ana_groups_group);
  1633. return &port->group;
  1634. }
  1635. static struct configfs_group_operations nvmet_ports_group_ops = {
  1636. .make_group = nvmet_ports_make,
  1637. };
  1638. static const struct config_item_type nvmet_ports_type = {
  1639. .ct_group_ops = &nvmet_ports_group_ops,
  1640. .ct_owner = THIS_MODULE,
  1641. };
  1642. static struct config_group nvmet_subsystems_group;
  1643. static struct config_group nvmet_ports_group;
  1644. #ifdef CONFIG_NVME_TARGET_AUTH
  1645. static ssize_t nvmet_host_dhchap_key_show(struct config_item *item,
  1646. char *page)
  1647. {
  1648. u8 *dhchap_secret;
  1649. ssize_t ret;
  1650. down_read(&nvmet_config_sem);
  1651. dhchap_secret = to_host(item)->dhchap_secret;
  1652. if (!dhchap_secret)
  1653. ret = sprintf(page, "\n");
  1654. else
  1655. ret = sprintf(page, "%s\n", dhchap_secret);
  1656. up_read(&nvmet_config_sem);
  1657. return ret;
  1658. }
  1659. static ssize_t nvmet_host_dhchap_key_store(struct config_item *item,
  1660. const char *page, size_t count)
  1661. {
  1662. struct nvmet_host *host = to_host(item);
  1663. int ret;
  1664. ret = nvmet_auth_set_key(host, page, false);
  1665. /*
  1666. * Re-authentication is a soft state, so keep the
  1667. * current authentication valid until the host
  1668. * requests re-authentication.
  1669. */
  1670. return ret < 0 ? ret : count;
  1671. }
  1672. CONFIGFS_ATTR(nvmet_host_, dhchap_key);
  1673. static ssize_t nvmet_host_dhchap_ctrl_key_show(struct config_item *item,
  1674. char *page)
  1675. {
  1676. u8 *dhchap_secret = to_host(item)->dhchap_ctrl_secret;
  1677. ssize_t ret;
  1678. down_read(&nvmet_config_sem);
  1679. dhchap_secret = to_host(item)->dhchap_ctrl_secret;
  1680. if (!dhchap_secret)
  1681. ret = sprintf(page, "\n");
  1682. else
  1683. ret = sprintf(page, "%s\n", dhchap_secret);
  1684. up_read(&nvmet_config_sem);
  1685. return ret;
  1686. }
  1687. static ssize_t nvmet_host_dhchap_ctrl_key_store(struct config_item *item,
  1688. const char *page, size_t count)
  1689. {
  1690. struct nvmet_host *host = to_host(item);
  1691. int ret;
  1692. ret = nvmet_auth_set_key(host, page, true);
  1693. /*
  1694. * Re-authentication is a soft state, so keep the
  1695. * current authentication valid until the host
  1696. * requests re-authentication.
  1697. */
  1698. return ret < 0 ? ret : count;
  1699. }
  1700. CONFIGFS_ATTR(nvmet_host_, dhchap_ctrl_key);
  1701. static ssize_t nvmet_host_dhchap_hash_show(struct config_item *item,
  1702. char *page)
  1703. {
  1704. struct nvmet_host *host = to_host(item);
  1705. const char *hash_name = nvme_auth_hmac_name(host->dhchap_hash_id);
  1706. return sprintf(page, "%s\n", hash_name ? hash_name : "none");
  1707. }
  1708. static ssize_t nvmet_host_dhchap_hash_store(struct config_item *item,
  1709. const char *page, size_t count)
  1710. {
  1711. struct nvmet_host *host = to_host(item);
  1712. u8 hmac_id;
  1713. hmac_id = nvme_auth_hmac_id(page);
  1714. if (hmac_id == NVME_AUTH_HASH_INVALID)
  1715. return -EINVAL;
  1716. if (!crypto_has_shash(nvme_auth_hmac_name(hmac_id), 0, 0))
  1717. return -ENOTSUPP;
  1718. host->dhchap_hash_id = hmac_id;
  1719. return count;
  1720. }
  1721. CONFIGFS_ATTR(nvmet_host_, dhchap_hash);
  1722. static ssize_t nvmet_host_dhchap_dhgroup_show(struct config_item *item,
  1723. char *page)
  1724. {
  1725. struct nvmet_host *host = to_host(item);
  1726. const char *dhgroup = nvme_auth_dhgroup_name(host->dhchap_dhgroup_id);
  1727. return sprintf(page, "%s\n", dhgroup ? dhgroup : "none");
  1728. }
  1729. static ssize_t nvmet_host_dhchap_dhgroup_store(struct config_item *item,
  1730. const char *page, size_t count)
  1731. {
  1732. struct nvmet_host *host = to_host(item);
  1733. int dhgroup_id;
  1734. dhgroup_id = nvme_auth_dhgroup_id(page);
  1735. if (dhgroup_id == NVME_AUTH_DHGROUP_INVALID)
  1736. return -EINVAL;
  1737. if (dhgroup_id != NVME_AUTH_DHGROUP_NULL) {
  1738. const char *kpp = nvme_auth_dhgroup_kpp(dhgroup_id);
  1739. if (!crypto_has_kpp(kpp, 0, 0))
  1740. return -EINVAL;
  1741. }
  1742. host->dhchap_dhgroup_id = dhgroup_id;
  1743. return count;
  1744. }
  1745. CONFIGFS_ATTR(nvmet_host_, dhchap_dhgroup);
  1746. static struct configfs_attribute *nvmet_host_attrs[] = {
  1747. &nvmet_host_attr_dhchap_key,
  1748. &nvmet_host_attr_dhchap_ctrl_key,
  1749. &nvmet_host_attr_dhchap_hash,
  1750. &nvmet_host_attr_dhchap_dhgroup,
  1751. NULL,
  1752. };
  1753. #endif /* CONFIG_NVME_TARGET_AUTH */
  1754. static void nvmet_host_release(struct config_item *item)
  1755. {
  1756. struct nvmet_host *host = to_host(item);
  1757. #ifdef CONFIG_NVME_TARGET_AUTH
  1758. kfree(host->dhchap_secret);
  1759. kfree(host->dhchap_ctrl_secret);
  1760. #endif
  1761. kfree(host);
  1762. }
  1763. static struct configfs_item_operations nvmet_host_item_ops = {
  1764. .release = nvmet_host_release,
  1765. };
  1766. static const struct config_item_type nvmet_host_type = {
  1767. .ct_item_ops = &nvmet_host_item_ops,
  1768. #ifdef CONFIG_NVME_TARGET_AUTH
  1769. .ct_attrs = nvmet_host_attrs,
  1770. #endif
  1771. .ct_owner = THIS_MODULE,
  1772. };
  1773. static struct config_group *nvmet_hosts_make_group(struct config_group *group,
  1774. const char *name)
  1775. {
  1776. struct nvmet_host *host;
  1777. host = kzalloc(sizeof(*host), GFP_KERNEL);
  1778. if (!host)
  1779. return ERR_PTR(-ENOMEM);
  1780. #ifdef CONFIG_NVME_TARGET_AUTH
  1781. /* Default to SHA256 */
  1782. host->dhchap_hash_id = NVME_AUTH_HASH_SHA256;
  1783. #endif
  1784. config_group_init_type_name(&host->group, name, &nvmet_host_type);
  1785. return &host->group;
  1786. }
  1787. static struct configfs_group_operations nvmet_hosts_group_ops = {
  1788. .make_group = nvmet_hosts_make_group,
  1789. };
  1790. static const struct config_item_type nvmet_hosts_type = {
  1791. .ct_group_ops = &nvmet_hosts_group_ops,
  1792. .ct_owner = THIS_MODULE,
  1793. };
  1794. static struct config_group nvmet_hosts_group;
  1795. static ssize_t nvmet_root_discovery_nqn_show(struct config_item *item,
  1796. char *page)
  1797. {
  1798. return snprintf(page, PAGE_SIZE, "%s\n", nvmet_disc_subsys->subsysnqn);
  1799. }
  1800. static ssize_t nvmet_root_discovery_nqn_store(struct config_item *item,
  1801. const char *page, size_t count)
  1802. {
  1803. struct list_head *entry;
  1804. char *old_nqn, *new_nqn;
  1805. size_t len;
  1806. len = strcspn(page, "\n");
  1807. if (!len || len > NVMF_NQN_FIELD_LEN - 1)
  1808. return -EINVAL;
  1809. new_nqn = kstrndup(page, len, GFP_KERNEL);
  1810. if (!new_nqn)
  1811. return -ENOMEM;
  1812. down_write(&nvmet_config_sem);
  1813. list_for_each(entry, &nvmet_subsystems_group.cg_children) {
  1814. struct config_item *item =
  1815. container_of(entry, struct config_item, ci_entry);
  1816. if (!strncmp(config_item_name(item), page, len)) {
  1817. pr_err("duplicate NQN %s\n", config_item_name(item));
  1818. up_write(&nvmet_config_sem);
  1819. kfree(new_nqn);
  1820. return -EINVAL;
  1821. }
  1822. }
  1823. old_nqn = nvmet_disc_subsys->subsysnqn;
  1824. nvmet_disc_subsys->subsysnqn = new_nqn;
  1825. up_write(&nvmet_config_sem);
  1826. kfree(old_nqn);
  1827. return len;
  1828. }
  1829. CONFIGFS_ATTR(nvmet_root_, discovery_nqn);
  1830. static struct configfs_attribute *nvmet_root_attrs[] = {
  1831. &nvmet_root_attr_discovery_nqn,
  1832. NULL,
  1833. };
  1834. static const struct config_item_type nvmet_root_type = {
  1835. .ct_attrs = nvmet_root_attrs,
  1836. .ct_owner = THIS_MODULE,
  1837. };
  1838. static struct configfs_subsystem nvmet_configfs_subsystem = {
  1839. .su_group = {
  1840. .cg_item = {
  1841. .ci_namebuf = "nvmet",
  1842. .ci_type = &nvmet_root_type,
  1843. },
  1844. },
  1845. };
  1846. int __init nvmet_init_configfs(void)
  1847. {
  1848. int ret;
  1849. config_group_init(&nvmet_configfs_subsystem.su_group);
  1850. mutex_init(&nvmet_configfs_subsystem.su_mutex);
  1851. config_group_init_type_name(&nvmet_subsystems_group,
  1852. "subsystems", &nvmet_subsystems_type);
  1853. configfs_add_default_group(&nvmet_subsystems_group,
  1854. &nvmet_configfs_subsystem.su_group);
  1855. config_group_init_type_name(&nvmet_ports_group,
  1856. "ports", &nvmet_ports_type);
  1857. configfs_add_default_group(&nvmet_ports_group,
  1858. &nvmet_configfs_subsystem.su_group);
  1859. config_group_init_type_name(&nvmet_hosts_group,
  1860. "hosts", &nvmet_hosts_type);
  1861. configfs_add_default_group(&nvmet_hosts_group,
  1862. &nvmet_configfs_subsystem.su_group);
  1863. ret = configfs_register_subsystem(&nvmet_configfs_subsystem);
  1864. if (ret) {
  1865. pr_err("configfs_register_subsystem: %d\n", ret);
  1866. return ret;
  1867. }
  1868. return 0;
  1869. }
  1870. void __exit nvmet_exit_configfs(void)
  1871. {
  1872. configfs_unregister_subsystem(&nvmet_configfs_subsystem);
  1873. }