core.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Common code 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/module.h>
  8. #include <linux/random.h>
  9. #include <linux/rculist.h>
  10. #include <linux/pci-p2pdma.h>
  11. #include <linux/scatterlist.h>
  12. #include <generated/utsrelease.h>
  13. #define CREATE_TRACE_POINTS
  14. #include "trace.h"
  15. #include "nvmet.h"
  16. #include "debugfs.h"
  17. struct kmem_cache *nvmet_bvec_cache;
  18. struct workqueue_struct *buffered_io_wq;
  19. struct workqueue_struct *zbd_wq;
  20. static const struct nvmet_fabrics_ops *nvmet_transports[NVMF_TRTYPE_MAX];
  21. static DEFINE_IDA(cntlid_ida);
  22. struct workqueue_struct *nvmet_wq;
  23. EXPORT_SYMBOL_GPL(nvmet_wq);
  24. /*
  25. * This read/write semaphore is used to synchronize access to configuration
  26. * information on a target system that will result in discovery log page
  27. * information change for at least one host.
  28. * The full list of resources to protected by this semaphore is:
  29. *
  30. * - subsystems list
  31. * - per-subsystem allowed hosts list
  32. * - allow_any_host subsystem attribute
  33. * - nvmet_genctr
  34. * - the nvmet_transports array
  35. *
  36. * When updating any of those lists/structures write lock should be obtained,
  37. * while when reading (popolating discovery log page or checking host-subsystem
  38. * link) read lock is obtained to allow concurrent reads.
  39. */
  40. DECLARE_RWSEM(nvmet_config_sem);
  41. u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1];
  42. u64 nvmet_ana_chgcnt;
  43. DECLARE_RWSEM(nvmet_ana_sem);
  44. inline u16 errno_to_nvme_status(struct nvmet_req *req, int errno)
  45. {
  46. switch (errno) {
  47. case 0:
  48. return NVME_SC_SUCCESS;
  49. case -ENOSPC:
  50. req->error_loc = offsetof(struct nvme_rw_command, length);
  51. return NVME_SC_CAP_EXCEEDED | NVME_STATUS_DNR;
  52. case -EREMOTEIO:
  53. req->error_loc = offsetof(struct nvme_rw_command, slba);
  54. return NVME_SC_LBA_RANGE | NVME_STATUS_DNR;
  55. case -EOPNOTSUPP:
  56. req->error_loc = offsetof(struct nvme_common_command, opcode);
  57. return NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR;
  58. case -ENODATA:
  59. req->error_loc = offsetof(struct nvme_rw_command, nsid);
  60. return NVME_SC_ACCESS_DENIED;
  61. case -EIO:
  62. fallthrough;
  63. default:
  64. req->error_loc = offsetof(struct nvme_common_command, opcode);
  65. return NVME_SC_INTERNAL | NVME_STATUS_DNR;
  66. }
  67. }
  68. u16 nvmet_report_invalid_opcode(struct nvmet_req *req)
  69. {
  70. pr_debug("unhandled cmd %d on qid %d\n", req->cmd->common.opcode,
  71. req->sq->qid);
  72. req->error_loc = offsetof(struct nvme_common_command, opcode);
  73. return NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR;
  74. }
  75. static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
  76. const char *subsysnqn);
  77. u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
  78. size_t len)
  79. {
  80. if (sg_pcopy_from_buffer(req->sg, req->sg_cnt, buf, len, off) != len) {
  81. req->error_loc = offsetof(struct nvme_common_command, dptr);
  82. return NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR;
  83. }
  84. return 0;
  85. }
  86. u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf, size_t len)
  87. {
  88. if (sg_pcopy_to_buffer(req->sg, req->sg_cnt, buf, len, off) != len) {
  89. req->error_loc = offsetof(struct nvme_common_command, dptr);
  90. return NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR;
  91. }
  92. return 0;
  93. }
  94. u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len)
  95. {
  96. if (sg_zero_buffer(req->sg, req->sg_cnt, len, off) != len) {
  97. req->error_loc = offsetof(struct nvme_common_command, dptr);
  98. return NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR;
  99. }
  100. return 0;
  101. }
  102. static u32 nvmet_max_nsid(struct nvmet_subsys *subsys)
  103. {
  104. struct nvmet_ns *cur;
  105. unsigned long idx;
  106. u32 nsid = 0;
  107. xa_for_each(&subsys->namespaces, idx, cur)
  108. nsid = cur->nsid;
  109. return nsid;
  110. }
  111. static u32 nvmet_async_event_result(struct nvmet_async_event *aen)
  112. {
  113. return aen->event_type | (aen->event_info << 8) | (aen->log_page << 16);
  114. }
  115. static void nvmet_async_events_failall(struct nvmet_ctrl *ctrl)
  116. {
  117. struct nvmet_req *req;
  118. mutex_lock(&ctrl->lock);
  119. while (ctrl->nr_async_event_cmds) {
  120. req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds];
  121. mutex_unlock(&ctrl->lock);
  122. nvmet_req_complete(req, NVME_SC_INTERNAL | NVME_STATUS_DNR);
  123. mutex_lock(&ctrl->lock);
  124. }
  125. mutex_unlock(&ctrl->lock);
  126. }
  127. static void nvmet_async_events_process(struct nvmet_ctrl *ctrl)
  128. {
  129. struct nvmet_async_event *aen;
  130. struct nvmet_req *req;
  131. mutex_lock(&ctrl->lock);
  132. while (ctrl->nr_async_event_cmds && !list_empty(&ctrl->async_events)) {
  133. aen = list_first_entry(&ctrl->async_events,
  134. struct nvmet_async_event, entry);
  135. req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds];
  136. nvmet_set_result(req, nvmet_async_event_result(aen));
  137. list_del(&aen->entry);
  138. kfree(aen);
  139. mutex_unlock(&ctrl->lock);
  140. trace_nvmet_async_event(ctrl, req->cqe->result.u32);
  141. nvmet_req_complete(req, 0);
  142. mutex_lock(&ctrl->lock);
  143. }
  144. mutex_unlock(&ctrl->lock);
  145. }
  146. static void nvmet_async_events_free(struct nvmet_ctrl *ctrl)
  147. {
  148. struct nvmet_async_event *aen, *tmp;
  149. mutex_lock(&ctrl->lock);
  150. list_for_each_entry_safe(aen, tmp, &ctrl->async_events, entry) {
  151. list_del(&aen->entry);
  152. kfree(aen);
  153. }
  154. mutex_unlock(&ctrl->lock);
  155. }
  156. static void nvmet_async_event_work(struct work_struct *work)
  157. {
  158. struct nvmet_ctrl *ctrl =
  159. container_of(work, struct nvmet_ctrl, async_event_work);
  160. nvmet_async_events_process(ctrl);
  161. }
  162. void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
  163. u8 event_info, u8 log_page)
  164. {
  165. struct nvmet_async_event *aen;
  166. aen = kmalloc(sizeof(*aen), GFP_KERNEL);
  167. if (!aen)
  168. return;
  169. aen->event_type = event_type;
  170. aen->event_info = event_info;
  171. aen->log_page = log_page;
  172. mutex_lock(&ctrl->lock);
  173. list_add_tail(&aen->entry, &ctrl->async_events);
  174. mutex_unlock(&ctrl->lock);
  175. queue_work(nvmet_wq, &ctrl->async_event_work);
  176. }
  177. static void nvmet_add_to_changed_ns_log(struct nvmet_ctrl *ctrl, __le32 nsid)
  178. {
  179. u32 i;
  180. mutex_lock(&ctrl->lock);
  181. if (ctrl->nr_changed_ns > NVME_MAX_CHANGED_NAMESPACES)
  182. goto out_unlock;
  183. for (i = 0; i < ctrl->nr_changed_ns; i++) {
  184. if (ctrl->changed_ns_list[i] == nsid)
  185. goto out_unlock;
  186. }
  187. if (ctrl->nr_changed_ns == NVME_MAX_CHANGED_NAMESPACES) {
  188. ctrl->changed_ns_list[0] = cpu_to_le32(0xffffffff);
  189. ctrl->nr_changed_ns = U32_MAX;
  190. goto out_unlock;
  191. }
  192. ctrl->changed_ns_list[ctrl->nr_changed_ns++] = nsid;
  193. out_unlock:
  194. mutex_unlock(&ctrl->lock);
  195. }
  196. void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid)
  197. {
  198. struct nvmet_ctrl *ctrl;
  199. lockdep_assert_held(&subsys->lock);
  200. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
  201. nvmet_add_to_changed_ns_log(ctrl, cpu_to_le32(nsid));
  202. if (nvmet_aen_bit_disabled(ctrl, NVME_AEN_BIT_NS_ATTR))
  203. continue;
  204. nvmet_add_async_event(ctrl, NVME_AER_NOTICE,
  205. NVME_AER_NOTICE_NS_CHANGED,
  206. NVME_LOG_CHANGED_NS);
  207. }
  208. }
  209. void nvmet_send_ana_event(struct nvmet_subsys *subsys,
  210. struct nvmet_port *port)
  211. {
  212. struct nvmet_ctrl *ctrl;
  213. mutex_lock(&subsys->lock);
  214. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
  215. if (port && ctrl->port != port)
  216. continue;
  217. if (nvmet_aen_bit_disabled(ctrl, NVME_AEN_BIT_ANA_CHANGE))
  218. continue;
  219. nvmet_add_async_event(ctrl, NVME_AER_NOTICE,
  220. NVME_AER_NOTICE_ANA, NVME_LOG_ANA);
  221. }
  222. mutex_unlock(&subsys->lock);
  223. }
  224. void nvmet_port_send_ana_event(struct nvmet_port *port)
  225. {
  226. struct nvmet_subsys_link *p;
  227. down_read(&nvmet_config_sem);
  228. list_for_each_entry(p, &port->subsystems, entry)
  229. nvmet_send_ana_event(p->subsys, port);
  230. up_read(&nvmet_config_sem);
  231. }
  232. int nvmet_register_transport(const struct nvmet_fabrics_ops *ops)
  233. {
  234. int ret = 0;
  235. down_write(&nvmet_config_sem);
  236. if (nvmet_transports[ops->type])
  237. ret = -EINVAL;
  238. else
  239. nvmet_transports[ops->type] = ops;
  240. up_write(&nvmet_config_sem);
  241. return ret;
  242. }
  243. EXPORT_SYMBOL_GPL(nvmet_register_transport);
  244. void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops)
  245. {
  246. down_write(&nvmet_config_sem);
  247. nvmet_transports[ops->type] = NULL;
  248. up_write(&nvmet_config_sem);
  249. }
  250. EXPORT_SYMBOL_GPL(nvmet_unregister_transport);
  251. void nvmet_port_del_ctrls(struct nvmet_port *port, struct nvmet_subsys *subsys)
  252. {
  253. struct nvmet_ctrl *ctrl;
  254. mutex_lock(&subsys->lock);
  255. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
  256. if (ctrl->port == port)
  257. ctrl->ops->delete_ctrl(ctrl);
  258. }
  259. mutex_unlock(&subsys->lock);
  260. }
  261. int nvmet_enable_port(struct nvmet_port *port)
  262. {
  263. const struct nvmet_fabrics_ops *ops;
  264. int ret;
  265. lockdep_assert_held(&nvmet_config_sem);
  266. ops = nvmet_transports[port->disc_addr.trtype];
  267. if (!ops) {
  268. up_write(&nvmet_config_sem);
  269. request_module("nvmet-transport-%d", port->disc_addr.trtype);
  270. down_write(&nvmet_config_sem);
  271. ops = nvmet_transports[port->disc_addr.trtype];
  272. if (!ops) {
  273. pr_err("transport type %d not supported\n",
  274. port->disc_addr.trtype);
  275. return -EINVAL;
  276. }
  277. }
  278. if (!try_module_get(ops->owner))
  279. return -EINVAL;
  280. /*
  281. * If the user requested PI support and the transport isn't pi capable,
  282. * don't enable the port.
  283. */
  284. if (port->pi_enable && !(ops->flags & NVMF_METADATA_SUPPORTED)) {
  285. pr_err("T10-PI is not supported by transport type %d\n",
  286. port->disc_addr.trtype);
  287. ret = -EINVAL;
  288. goto out_put;
  289. }
  290. ret = ops->add_port(port);
  291. if (ret)
  292. goto out_put;
  293. /* If the transport didn't set inline_data_size, then disable it. */
  294. if (port->inline_data_size < 0)
  295. port->inline_data_size = 0;
  296. /*
  297. * If the transport didn't set the max_queue_size properly, then clamp
  298. * it to the target limits. Also set default values in case the
  299. * transport didn't set it at all.
  300. */
  301. if (port->max_queue_size < 0)
  302. port->max_queue_size = NVMET_MAX_QUEUE_SIZE;
  303. else
  304. port->max_queue_size = clamp_t(int, port->max_queue_size,
  305. NVMET_MIN_QUEUE_SIZE,
  306. NVMET_MAX_QUEUE_SIZE);
  307. port->enabled = true;
  308. port->tr_ops = ops;
  309. return 0;
  310. out_put:
  311. module_put(ops->owner);
  312. return ret;
  313. }
  314. void nvmet_disable_port(struct nvmet_port *port)
  315. {
  316. const struct nvmet_fabrics_ops *ops;
  317. lockdep_assert_held(&nvmet_config_sem);
  318. port->enabled = false;
  319. port->tr_ops = NULL;
  320. ops = nvmet_transports[port->disc_addr.trtype];
  321. ops->remove_port(port);
  322. module_put(ops->owner);
  323. }
  324. static void nvmet_keep_alive_timer(struct work_struct *work)
  325. {
  326. struct nvmet_ctrl *ctrl = container_of(to_delayed_work(work),
  327. struct nvmet_ctrl, ka_work);
  328. bool reset_tbkas = ctrl->reset_tbkas;
  329. ctrl->reset_tbkas = false;
  330. if (reset_tbkas) {
  331. pr_debug("ctrl %d reschedule traffic based keep-alive timer\n",
  332. ctrl->cntlid);
  333. queue_delayed_work(nvmet_wq, &ctrl->ka_work, ctrl->kato * HZ);
  334. return;
  335. }
  336. pr_err("ctrl %d keep-alive timer (%d seconds) expired!\n",
  337. ctrl->cntlid, ctrl->kato);
  338. nvmet_ctrl_fatal_error(ctrl);
  339. }
  340. void nvmet_start_keep_alive_timer(struct nvmet_ctrl *ctrl)
  341. {
  342. if (unlikely(ctrl->kato == 0))
  343. return;
  344. pr_debug("ctrl %d start keep-alive timer for %d secs\n",
  345. ctrl->cntlid, ctrl->kato);
  346. queue_delayed_work(nvmet_wq, &ctrl->ka_work, ctrl->kato * HZ);
  347. }
  348. void nvmet_stop_keep_alive_timer(struct nvmet_ctrl *ctrl)
  349. {
  350. if (unlikely(ctrl->kato == 0))
  351. return;
  352. pr_debug("ctrl %d stop keep-alive\n", ctrl->cntlid);
  353. cancel_delayed_work_sync(&ctrl->ka_work);
  354. }
  355. u16 nvmet_req_find_ns(struct nvmet_req *req)
  356. {
  357. u32 nsid = le32_to_cpu(req->cmd->common.nsid);
  358. struct nvmet_subsys *subsys = nvmet_req_subsys(req);
  359. req->ns = xa_load(&subsys->namespaces, nsid);
  360. if (unlikely(!req->ns)) {
  361. req->error_loc = offsetof(struct nvme_common_command, nsid);
  362. if (nvmet_subsys_nsid_exists(subsys, nsid))
  363. return NVME_SC_INTERNAL_PATH_ERROR;
  364. return NVME_SC_INVALID_NS | NVME_STATUS_DNR;
  365. }
  366. percpu_ref_get(&req->ns->ref);
  367. return NVME_SC_SUCCESS;
  368. }
  369. static void nvmet_destroy_namespace(struct percpu_ref *ref)
  370. {
  371. struct nvmet_ns *ns = container_of(ref, struct nvmet_ns, ref);
  372. complete(&ns->disable_done);
  373. }
  374. void nvmet_put_namespace(struct nvmet_ns *ns)
  375. {
  376. percpu_ref_put(&ns->ref);
  377. }
  378. static void nvmet_ns_dev_disable(struct nvmet_ns *ns)
  379. {
  380. nvmet_bdev_ns_disable(ns);
  381. nvmet_file_ns_disable(ns);
  382. }
  383. static int nvmet_p2pmem_ns_enable(struct nvmet_ns *ns)
  384. {
  385. int ret;
  386. struct pci_dev *p2p_dev;
  387. if (!ns->use_p2pmem)
  388. return 0;
  389. if (!ns->bdev) {
  390. pr_err("peer-to-peer DMA is not supported by non-block device namespaces\n");
  391. return -EINVAL;
  392. }
  393. if (!blk_queue_pci_p2pdma(ns->bdev->bd_disk->queue)) {
  394. pr_err("peer-to-peer DMA is not supported by the driver of %s\n",
  395. ns->device_path);
  396. return -EINVAL;
  397. }
  398. if (ns->p2p_dev) {
  399. ret = pci_p2pdma_distance(ns->p2p_dev, nvmet_ns_dev(ns), true);
  400. if (ret < 0)
  401. return -EINVAL;
  402. } else {
  403. /*
  404. * Right now we just check that there is p2pmem available so
  405. * we can report an error to the user right away if there
  406. * is not. We'll find the actual device to use once we
  407. * setup the controller when the port's device is available.
  408. */
  409. p2p_dev = pci_p2pmem_find(nvmet_ns_dev(ns));
  410. if (!p2p_dev) {
  411. pr_err("no peer-to-peer memory is available for %s\n",
  412. ns->device_path);
  413. return -EINVAL;
  414. }
  415. pci_dev_put(p2p_dev);
  416. }
  417. return 0;
  418. }
  419. /*
  420. * Note: ctrl->subsys->lock should be held when calling this function
  421. */
  422. static void nvmet_p2pmem_ns_add_p2p(struct nvmet_ctrl *ctrl,
  423. struct nvmet_ns *ns)
  424. {
  425. struct device *clients[2];
  426. struct pci_dev *p2p_dev;
  427. int ret;
  428. if (!ctrl->p2p_client || !ns->use_p2pmem)
  429. return;
  430. if (ns->p2p_dev) {
  431. ret = pci_p2pdma_distance(ns->p2p_dev, ctrl->p2p_client, true);
  432. if (ret < 0)
  433. return;
  434. p2p_dev = pci_dev_get(ns->p2p_dev);
  435. } else {
  436. clients[0] = ctrl->p2p_client;
  437. clients[1] = nvmet_ns_dev(ns);
  438. p2p_dev = pci_p2pmem_find_many(clients, ARRAY_SIZE(clients));
  439. if (!p2p_dev) {
  440. pr_err("no peer-to-peer memory is available that's supported by %s and %s\n",
  441. dev_name(ctrl->p2p_client), ns->device_path);
  442. return;
  443. }
  444. }
  445. ret = radix_tree_insert(&ctrl->p2p_ns_map, ns->nsid, p2p_dev);
  446. if (ret < 0)
  447. pci_dev_put(p2p_dev);
  448. pr_info("using p2pmem on %s for nsid %d\n", pci_name(p2p_dev),
  449. ns->nsid);
  450. }
  451. bool nvmet_ns_revalidate(struct nvmet_ns *ns)
  452. {
  453. loff_t oldsize = ns->size;
  454. if (ns->bdev)
  455. nvmet_bdev_ns_revalidate(ns);
  456. else
  457. nvmet_file_ns_revalidate(ns);
  458. return oldsize != ns->size;
  459. }
  460. int nvmet_ns_enable(struct nvmet_ns *ns)
  461. {
  462. struct nvmet_subsys *subsys = ns->subsys;
  463. struct nvmet_ctrl *ctrl;
  464. int ret;
  465. mutex_lock(&subsys->lock);
  466. ret = 0;
  467. if (nvmet_is_passthru_subsys(subsys)) {
  468. pr_info("cannot enable both passthru and regular namespaces for a single subsystem");
  469. goto out_unlock;
  470. }
  471. if (ns->enabled)
  472. goto out_unlock;
  473. ret = -EMFILE;
  474. if (subsys->nr_namespaces == NVMET_MAX_NAMESPACES)
  475. goto out_unlock;
  476. ret = nvmet_bdev_ns_enable(ns);
  477. if (ret == -ENOTBLK)
  478. ret = nvmet_file_ns_enable(ns);
  479. if (ret)
  480. goto out_unlock;
  481. ret = nvmet_p2pmem_ns_enable(ns);
  482. if (ret)
  483. goto out_dev_disable;
  484. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
  485. nvmet_p2pmem_ns_add_p2p(ctrl, ns);
  486. ret = percpu_ref_init(&ns->ref, nvmet_destroy_namespace,
  487. 0, GFP_KERNEL);
  488. if (ret)
  489. goto out_dev_put;
  490. if (ns->nsid > subsys->max_nsid)
  491. subsys->max_nsid = ns->nsid;
  492. ret = xa_insert(&subsys->namespaces, ns->nsid, ns, GFP_KERNEL);
  493. if (ret)
  494. goto out_restore_subsys_maxnsid;
  495. subsys->nr_namespaces++;
  496. nvmet_ns_changed(subsys, ns->nsid);
  497. ns->enabled = true;
  498. ret = 0;
  499. out_unlock:
  500. mutex_unlock(&subsys->lock);
  501. return ret;
  502. out_restore_subsys_maxnsid:
  503. subsys->max_nsid = nvmet_max_nsid(subsys);
  504. percpu_ref_exit(&ns->ref);
  505. out_dev_put:
  506. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
  507. pci_dev_put(radix_tree_delete(&ctrl->p2p_ns_map, ns->nsid));
  508. out_dev_disable:
  509. nvmet_ns_dev_disable(ns);
  510. goto out_unlock;
  511. }
  512. void nvmet_ns_disable(struct nvmet_ns *ns)
  513. {
  514. struct nvmet_subsys *subsys = ns->subsys;
  515. struct nvmet_ctrl *ctrl;
  516. mutex_lock(&subsys->lock);
  517. if (!ns->enabled)
  518. goto out_unlock;
  519. ns->enabled = false;
  520. xa_erase(&ns->subsys->namespaces, ns->nsid);
  521. if (ns->nsid == subsys->max_nsid)
  522. subsys->max_nsid = nvmet_max_nsid(subsys);
  523. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
  524. pci_dev_put(radix_tree_delete(&ctrl->p2p_ns_map, ns->nsid));
  525. mutex_unlock(&subsys->lock);
  526. /*
  527. * Now that we removed the namespaces from the lookup list, we
  528. * can kill the per_cpu ref and wait for any remaining references
  529. * to be dropped, as well as a RCU grace period for anyone only
  530. * using the namepace under rcu_read_lock(). Note that we can't
  531. * use call_rcu here as we need to ensure the namespaces have
  532. * been fully destroyed before unloading the module.
  533. */
  534. percpu_ref_kill(&ns->ref);
  535. synchronize_rcu();
  536. wait_for_completion(&ns->disable_done);
  537. percpu_ref_exit(&ns->ref);
  538. mutex_lock(&subsys->lock);
  539. subsys->nr_namespaces--;
  540. nvmet_ns_changed(subsys, ns->nsid);
  541. nvmet_ns_dev_disable(ns);
  542. out_unlock:
  543. mutex_unlock(&subsys->lock);
  544. }
  545. void nvmet_ns_free(struct nvmet_ns *ns)
  546. {
  547. nvmet_ns_disable(ns);
  548. down_write(&nvmet_ana_sem);
  549. nvmet_ana_group_enabled[ns->anagrpid]--;
  550. up_write(&nvmet_ana_sem);
  551. kfree(ns->device_path);
  552. kfree(ns);
  553. }
  554. struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
  555. {
  556. struct nvmet_ns *ns;
  557. ns = kzalloc(sizeof(*ns), GFP_KERNEL);
  558. if (!ns)
  559. return NULL;
  560. init_completion(&ns->disable_done);
  561. ns->nsid = nsid;
  562. ns->subsys = subsys;
  563. down_write(&nvmet_ana_sem);
  564. ns->anagrpid = NVMET_DEFAULT_ANA_GRPID;
  565. nvmet_ana_group_enabled[ns->anagrpid]++;
  566. up_write(&nvmet_ana_sem);
  567. uuid_gen(&ns->uuid);
  568. ns->buffered_io = false;
  569. ns->csi = NVME_CSI_NVM;
  570. return ns;
  571. }
  572. static void nvmet_update_sq_head(struct nvmet_req *req)
  573. {
  574. if (req->sq->size) {
  575. u32 old_sqhd, new_sqhd;
  576. old_sqhd = READ_ONCE(req->sq->sqhd);
  577. do {
  578. new_sqhd = (old_sqhd + 1) % req->sq->size;
  579. } while (!try_cmpxchg(&req->sq->sqhd, &old_sqhd, new_sqhd));
  580. }
  581. req->cqe->sq_head = cpu_to_le16(req->sq->sqhd & 0x0000FFFF);
  582. }
  583. static void nvmet_set_error(struct nvmet_req *req, u16 status)
  584. {
  585. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  586. struct nvme_error_slot *new_error_slot;
  587. unsigned long flags;
  588. req->cqe->status = cpu_to_le16(status << 1);
  589. if (!ctrl || req->error_loc == NVMET_NO_ERROR_LOC)
  590. return;
  591. spin_lock_irqsave(&ctrl->error_lock, flags);
  592. ctrl->err_counter++;
  593. new_error_slot =
  594. &ctrl->slots[ctrl->err_counter % NVMET_ERROR_LOG_SLOTS];
  595. new_error_slot->error_count = cpu_to_le64(ctrl->err_counter);
  596. new_error_slot->sqid = cpu_to_le16(req->sq->qid);
  597. new_error_slot->cmdid = cpu_to_le16(req->cmd->common.command_id);
  598. new_error_slot->status_field = cpu_to_le16(status << 1);
  599. new_error_slot->param_error_location = cpu_to_le16(req->error_loc);
  600. new_error_slot->lba = cpu_to_le64(req->error_slba);
  601. new_error_slot->nsid = req->cmd->common.nsid;
  602. spin_unlock_irqrestore(&ctrl->error_lock, flags);
  603. /* set the more bit for this request */
  604. req->cqe->status |= cpu_to_le16(1 << 14);
  605. }
  606. static void __nvmet_req_complete(struct nvmet_req *req, u16 status)
  607. {
  608. struct nvmet_ns *ns = req->ns;
  609. if (!req->sq->sqhd_disabled)
  610. nvmet_update_sq_head(req);
  611. req->cqe->sq_id = cpu_to_le16(req->sq->qid);
  612. req->cqe->command_id = req->cmd->common.command_id;
  613. if (unlikely(status))
  614. nvmet_set_error(req, status);
  615. trace_nvmet_req_complete(req);
  616. req->ops->queue_response(req);
  617. if (ns)
  618. nvmet_put_namespace(ns);
  619. }
  620. void nvmet_req_complete(struct nvmet_req *req, u16 status)
  621. {
  622. struct nvmet_sq *sq = req->sq;
  623. __nvmet_req_complete(req, status);
  624. percpu_ref_put(&sq->ref);
  625. }
  626. EXPORT_SYMBOL_GPL(nvmet_req_complete);
  627. void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq,
  628. u16 qid, u16 size)
  629. {
  630. cq->qid = qid;
  631. cq->size = size;
  632. }
  633. void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq,
  634. u16 qid, u16 size)
  635. {
  636. sq->sqhd = 0;
  637. sq->qid = qid;
  638. sq->size = size;
  639. ctrl->sqs[qid] = sq;
  640. }
  641. static void nvmet_confirm_sq(struct percpu_ref *ref)
  642. {
  643. struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
  644. complete(&sq->confirm_done);
  645. }
  646. void nvmet_sq_destroy(struct nvmet_sq *sq)
  647. {
  648. struct nvmet_ctrl *ctrl = sq->ctrl;
  649. /*
  650. * If this is the admin queue, complete all AERs so that our
  651. * queue doesn't have outstanding requests on it.
  652. */
  653. if (ctrl && ctrl->sqs && ctrl->sqs[0] == sq)
  654. nvmet_async_events_failall(ctrl);
  655. percpu_ref_kill_and_confirm(&sq->ref, nvmet_confirm_sq);
  656. wait_for_completion(&sq->confirm_done);
  657. wait_for_completion(&sq->free_done);
  658. percpu_ref_exit(&sq->ref);
  659. nvmet_auth_sq_free(sq);
  660. /*
  661. * we must reference the ctrl again after waiting for inflight IO
  662. * to complete. Because admin connect may have sneaked in after we
  663. * store sq->ctrl locally, but before we killed the percpu_ref. the
  664. * admin connect allocates and assigns sq->ctrl, which now needs a
  665. * final ref put, as this ctrl is going away.
  666. */
  667. ctrl = sq->ctrl;
  668. if (ctrl) {
  669. /*
  670. * The teardown flow may take some time, and the host may not
  671. * send us keep-alive during this period, hence reset the
  672. * traffic based keep-alive timer so we don't trigger a
  673. * controller teardown as a result of a keep-alive expiration.
  674. */
  675. ctrl->reset_tbkas = true;
  676. sq->ctrl->sqs[sq->qid] = NULL;
  677. nvmet_ctrl_put(ctrl);
  678. sq->ctrl = NULL; /* allows reusing the queue later */
  679. }
  680. }
  681. EXPORT_SYMBOL_GPL(nvmet_sq_destroy);
  682. static void nvmet_sq_free(struct percpu_ref *ref)
  683. {
  684. struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
  685. complete(&sq->free_done);
  686. }
  687. int nvmet_sq_init(struct nvmet_sq *sq)
  688. {
  689. int ret;
  690. ret = percpu_ref_init(&sq->ref, nvmet_sq_free, 0, GFP_KERNEL);
  691. if (ret) {
  692. pr_err("percpu_ref init failed!\n");
  693. return ret;
  694. }
  695. init_completion(&sq->free_done);
  696. init_completion(&sq->confirm_done);
  697. nvmet_auth_sq_init(sq);
  698. return 0;
  699. }
  700. EXPORT_SYMBOL_GPL(nvmet_sq_init);
  701. static inline u16 nvmet_check_ana_state(struct nvmet_port *port,
  702. struct nvmet_ns *ns)
  703. {
  704. enum nvme_ana_state state = port->ana_state[ns->anagrpid];
  705. if (unlikely(state == NVME_ANA_INACCESSIBLE))
  706. return NVME_SC_ANA_INACCESSIBLE;
  707. if (unlikely(state == NVME_ANA_PERSISTENT_LOSS))
  708. return NVME_SC_ANA_PERSISTENT_LOSS;
  709. if (unlikely(state == NVME_ANA_CHANGE))
  710. return NVME_SC_ANA_TRANSITION;
  711. return 0;
  712. }
  713. static inline u16 nvmet_io_cmd_check_access(struct nvmet_req *req)
  714. {
  715. if (unlikely(req->ns->readonly)) {
  716. switch (req->cmd->common.opcode) {
  717. case nvme_cmd_read:
  718. case nvme_cmd_flush:
  719. break;
  720. default:
  721. return NVME_SC_NS_WRITE_PROTECTED;
  722. }
  723. }
  724. return 0;
  725. }
  726. static u16 nvmet_parse_io_cmd(struct nvmet_req *req)
  727. {
  728. struct nvme_command *cmd = req->cmd;
  729. u16 ret;
  730. if (nvme_is_fabrics(cmd))
  731. return nvmet_parse_fabrics_io_cmd(req);
  732. if (unlikely(!nvmet_check_auth_status(req)))
  733. return NVME_SC_AUTH_REQUIRED | NVME_STATUS_DNR;
  734. ret = nvmet_check_ctrl_status(req);
  735. if (unlikely(ret))
  736. return ret;
  737. if (nvmet_is_passthru_req(req))
  738. return nvmet_parse_passthru_io_cmd(req);
  739. ret = nvmet_req_find_ns(req);
  740. if (unlikely(ret))
  741. return ret;
  742. ret = nvmet_check_ana_state(req->port, req->ns);
  743. if (unlikely(ret)) {
  744. req->error_loc = offsetof(struct nvme_common_command, nsid);
  745. return ret;
  746. }
  747. ret = nvmet_io_cmd_check_access(req);
  748. if (unlikely(ret)) {
  749. req->error_loc = offsetof(struct nvme_common_command, nsid);
  750. return ret;
  751. }
  752. switch (req->ns->csi) {
  753. case NVME_CSI_NVM:
  754. if (req->ns->file)
  755. return nvmet_file_parse_io_cmd(req);
  756. return nvmet_bdev_parse_io_cmd(req);
  757. case NVME_CSI_ZNS:
  758. if (IS_ENABLED(CONFIG_BLK_DEV_ZONED))
  759. return nvmet_bdev_zns_parse_io_cmd(req);
  760. return NVME_SC_INVALID_IO_CMD_SET;
  761. default:
  762. return NVME_SC_INVALID_IO_CMD_SET;
  763. }
  764. }
  765. bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
  766. struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops)
  767. {
  768. u8 flags = req->cmd->common.flags;
  769. u16 status;
  770. req->cq = cq;
  771. req->sq = sq;
  772. req->ops = ops;
  773. req->sg = NULL;
  774. req->metadata_sg = NULL;
  775. req->sg_cnt = 0;
  776. req->metadata_sg_cnt = 0;
  777. req->transfer_len = 0;
  778. req->metadata_len = 0;
  779. req->cqe->result.u64 = 0;
  780. req->cqe->status = 0;
  781. req->cqe->sq_head = 0;
  782. req->ns = NULL;
  783. req->error_loc = NVMET_NO_ERROR_LOC;
  784. req->error_slba = 0;
  785. /* no support for fused commands yet */
  786. if (unlikely(flags & (NVME_CMD_FUSE_FIRST | NVME_CMD_FUSE_SECOND))) {
  787. req->error_loc = offsetof(struct nvme_common_command, flags);
  788. status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
  789. goto fail;
  790. }
  791. /*
  792. * For fabrics, PSDT field shall describe metadata pointer (MPTR) that
  793. * contains an address of a single contiguous physical buffer that is
  794. * byte aligned.
  795. */
  796. if (unlikely((flags & NVME_CMD_SGL_ALL) != NVME_CMD_SGL_METABUF)) {
  797. req->error_loc = offsetof(struct nvme_common_command, flags);
  798. status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
  799. goto fail;
  800. }
  801. if (unlikely(!req->sq->ctrl))
  802. /* will return an error for any non-connect command: */
  803. status = nvmet_parse_connect_cmd(req);
  804. else if (likely(req->sq->qid != 0))
  805. status = nvmet_parse_io_cmd(req);
  806. else
  807. status = nvmet_parse_admin_cmd(req);
  808. if (status)
  809. goto fail;
  810. trace_nvmet_req_init(req, req->cmd);
  811. if (unlikely(!percpu_ref_tryget_live(&sq->ref))) {
  812. status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
  813. goto fail;
  814. }
  815. if (sq->ctrl)
  816. sq->ctrl->reset_tbkas = true;
  817. return true;
  818. fail:
  819. __nvmet_req_complete(req, status);
  820. return false;
  821. }
  822. EXPORT_SYMBOL_GPL(nvmet_req_init);
  823. void nvmet_req_uninit(struct nvmet_req *req)
  824. {
  825. percpu_ref_put(&req->sq->ref);
  826. if (req->ns)
  827. nvmet_put_namespace(req->ns);
  828. }
  829. EXPORT_SYMBOL_GPL(nvmet_req_uninit);
  830. bool nvmet_check_transfer_len(struct nvmet_req *req, size_t len)
  831. {
  832. if (unlikely(len != req->transfer_len)) {
  833. req->error_loc = offsetof(struct nvme_common_command, dptr);
  834. nvmet_req_complete(req, NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR);
  835. return false;
  836. }
  837. return true;
  838. }
  839. EXPORT_SYMBOL_GPL(nvmet_check_transfer_len);
  840. bool nvmet_check_data_len_lte(struct nvmet_req *req, size_t data_len)
  841. {
  842. if (unlikely(data_len > req->transfer_len)) {
  843. req->error_loc = offsetof(struct nvme_common_command, dptr);
  844. nvmet_req_complete(req, NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR);
  845. return false;
  846. }
  847. return true;
  848. }
  849. static unsigned int nvmet_data_transfer_len(struct nvmet_req *req)
  850. {
  851. return req->transfer_len - req->metadata_len;
  852. }
  853. static int nvmet_req_alloc_p2pmem_sgls(struct pci_dev *p2p_dev,
  854. struct nvmet_req *req)
  855. {
  856. req->sg = pci_p2pmem_alloc_sgl(p2p_dev, &req->sg_cnt,
  857. nvmet_data_transfer_len(req));
  858. if (!req->sg)
  859. goto out_err;
  860. if (req->metadata_len) {
  861. req->metadata_sg = pci_p2pmem_alloc_sgl(p2p_dev,
  862. &req->metadata_sg_cnt, req->metadata_len);
  863. if (!req->metadata_sg)
  864. goto out_free_sg;
  865. }
  866. req->p2p_dev = p2p_dev;
  867. return 0;
  868. out_free_sg:
  869. pci_p2pmem_free_sgl(req->p2p_dev, req->sg);
  870. out_err:
  871. return -ENOMEM;
  872. }
  873. static struct pci_dev *nvmet_req_find_p2p_dev(struct nvmet_req *req)
  874. {
  875. if (!IS_ENABLED(CONFIG_PCI_P2PDMA) ||
  876. !req->sq->ctrl || !req->sq->qid || !req->ns)
  877. return NULL;
  878. return radix_tree_lookup(&req->sq->ctrl->p2p_ns_map, req->ns->nsid);
  879. }
  880. int nvmet_req_alloc_sgls(struct nvmet_req *req)
  881. {
  882. struct pci_dev *p2p_dev = nvmet_req_find_p2p_dev(req);
  883. if (p2p_dev && !nvmet_req_alloc_p2pmem_sgls(p2p_dev, req))
  884. return 0;
  885. req->sg = sgl_alloc(nvmet_data_transfer_len(req), GFP_KERNEL,
  886. &req->sg_cnt);
  887. if (unlikely(!req->sg))
  888. goto out;
  889. if (req->metadata_len) {
  890. req->metadata_sg = sgl_alloc(req->metadata_len, GFP_KERNEL,
  891. &req->metadata_sg_cnt);
  892. if (unlikely(!req->metadata_sg))
  893. goto out_free;
  894. }
  895. return 0;
  896. out_free:
  897. sgl_free(req->sg);
  898. out:
  899. return -ENOMEM;
  900. }
  901. EXPORT_SYMBOL_GPL(nvmet_req_alloc_sgls);
  902. void nvmet_req_free_sgls(struct nvmet_req *req)
  903. {
  904. if (req->p2p_dev) {
  905. pci_p2pmem_free_sgl(req->p2p_dev, req->sg);
  906. if (req->metadata_sg)
  907. pci_p2pmem_free_sgl(req->p2p_dev, req->metadata_sg);
  908. req->p2p_dev = NULL;
  909. } else {
  910. sgl_free(req->sg);
  911. if (req->metadata_sg)
  912. sgl_free(req->metadata_sg);
  913. }
  914. req->sg = NULL;
  915. req->metadata_sg = NULL;
  916. req->sg_cnt = 0;
  917. req->metadata_sg_cnt = 0;
  918. }
  919. EXPORT_SYMBOL_GPL(nvmet_req_free_sgls);
  920. static inline bool nvmet_cc_en(u32 cc)
  921. {
  922. return (cc >> NVME_CC_EN_SHIFT) & 0x1;
  923. }
  924. static inline u8 nvmet_cc_css(u32 cc)
  925. {
  926. return (cc >> NVME_CC_CSS_SHIFT) & 0x7;
  927. }
  928. static inline u8 nvmet_cc_mps(u32 cc)
  929. {
  930. return (cc >> NVME_CC_MPS_SHIFT) & 0xf;
  931. }
  932. static inline u8 nvmet_cc_ams(u32 cc)
  933. {
  934. return (cc >> NVME_CC_AMS_SHIFT) & 0x7;
  935. }
  936. static inline u8 nvmet_cc_shn(u32 cc)
  937. {
  938. return (cc >> NVME_CC_SHN_SHIFT) & 0x3;
  939. }
  940. static inline u8 nvmet_cc_iosqes(u32 cc)
  941. {
  942. return (cc >> NVME_CC_IOSQES_SHIFT) & 0xf;
  943. }
  944. static inline u8 nvmet_cc_iocqes(u32 cc)
  945. {
  946. return (cc >> NVME_CC_IOCQES_SHIFT) & 0xf;
  947. }
  948. static inline bool nvmet_css_supported(u8 cc_css)
  949. {
  950. switch (cc_css << NVME_CC_CSS_SHIFT) {
  951. case NVME_CC_CSS_NVM:
  952. case NVME_CC_CSS_CSI:
  953. return true;
  954. default:
  955. return false;
  956. }
  957. }
  958. static void nvmet_start_ctrl(struct nvmet_ctrl *ctrl)
  959. {
  960. lockdep_assert_held(&ctrl->lock);
  961. /*
  962. * Only I/O controllers should verify iosqes,iocqes.
  963. * Strictly speaking, the spec says a discovery controller
  964. * should verify iosqes,iocqes are zeroed, however that
  965. * would break backwards compatibility, so don't enforce it.
  966. */
  967. if (!nvmet_is_disc_subsys(ctrl->subsys) &&
  968. (nvmet_cc_iosqes(ctrl->cc) != NVME_NVM_IOSQES ||
  969. nvmet_cc_iocqes(ctrl->cc) != NVME_NVM_IOCQES)) {
  970. ctrl->csts = NVME_CSTS_CFS;
  971. return;
  972. }
  973. if (nvmet_cc_mps(ctrl->cc) != 0 ||
  974. nvmet_cc_ams(ctrl->cc) != 0 ||
  975. !nvmet_css_supported(nvmet_cc_css(ctrl->cc))) {
  976. ctrl->csts = NVME_CSTS_CFS;
  977. return;
  978. }
  979. ctrl->csts = NVME_CSTS_RDY;
  980. /*
  981. * Controllers that are not yet enabled should not really enforce the
  982. * keep alive timeout, but we still want to track a timeout and cleanup
  983. * in case a host died before it enabled the controller. Hence, simply
  984. * reset the keep alive timer when the controller is enabled.
  985. */
  986. if (ctrl->kato)
  987. mod_delayed_work(nvmet_wq, &ctrl->ka_work, ctrl->kato * HZ);
  988. }
  989. static void nvmet_clear_ctrl(struct nvmet_ctrl *ctrl)
  990. {
  991. lockdep_assert_held(&ctrl->lock);
  992. /* XXX: tear down queues? */
  993. ctrl->csts &= ~NVME_CSTS_RDY;
  994. ctrl->cc = 0;
  995. }
  996. void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new)
  997. {
  998. u32 old;
  999. mutex_lock(&ctrl->lock);
  1000. old = ctrl->cc;
  1001. ctrl->cc = new;
  1002. if (nvmet_cc_en(new) && !nvmet_cc_en(old))
  1003. nvmet_start_ctrl(ctrl);
  1004. if (!nvmet_cc_en(new) && nvmet_cc_en(old))
  1005. nvmet_clear_ctrl(ctrl);
  1006. if (nvmet_cc_shn(new) && !nvmet_cc_shn(old)) {
  1007. nvmet_clear_ctrl(ctrl);
  1008. ctrl->csts |= NVME_CSTS_SHST_CMPLT;
  1009. }
  1010. if (!nvmet_cc_shn(new) && nvmet_cc_shn(old))
  1011. ctrl->csts &= ~NVME_CSTS_SHST_CMPLT;
  1012. mutex_unlock(&ctrl->lock);
  1013. }
  1014. static void nvmet_init_cap(struct nvmet_ctrl *ctrl)
  1015. {
  1016. /* command sets supported: NVMe command set: */
  1017. ctrl->cap = (1ULL << 37);
  1018. /* Controller supports one or more I/O Command Sets */
  1019. ctrl->cap |= (1ULL << 43);
  1020. /* CC.EN timeout in 500msec units: */
  1021. ctrl->cap |= (15ULL << 24);
  1022. /* maximum queue entries supported: */
  1023. if (ctrl->ops->get_max_queue_size)
  1024. ctrl->cap |= min_t(u16, ctrl->ops->get_max_queue_size(ctrl),
  1025. ctrl->port->max_queue_size) - 1;
  1026. else
  1027. ctrl->cap |= ctrl->port->max_queue_size - 1;
  1028. if (nvmet_is_passthru_subsys(ctrl->subsys))
  1029. nvmet_passthrough_override_cap(ctrl);
  1030. }
  1031. struct nvmet_ctrl *nvmet_ctrl_find_get(const char *subsysnqn,
  1032. const char *hostnqn, u16 cntlid,
  1033. struct nvmet_req *req)
  1034. {
  1035. struct nvmet_ctrl *ctrl = NULL;
  1036. struct nvmet_subsys *subsys;
  1037. subsys = nvmet_find_get_subsys(req->port, subsysnqn);
  1038. if (!subsys) {
  1039. pr_warn("connect request for invalid subsystem %s!\n",
  1040. subsysnqn);
  1041. req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
  1042. goto out;
  1043. }
  1044. mutex_lock(&subsys->lock);
  1045. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
  1046. if (ctrl->cntlid == cntlid) {
  1047. if (strncmp(hostnqn, ctrl->hostnqn, NVMF_NQN_SIZE)) {
  1048. pr_warn("hostnqn mismatch.\n");
  1049. continue;
  1050. }
  1051. if (!kref_get_unless_zero(&ctrl->ref))
  1052. continue;
  1053. /* ctrl found */
  1054. goto found;
  1055. }
  1056. }
  1057. ctrl = NULL; /* ctrl not found */
  1058. pr_warn("could not find controller %d for subsys %s / host %s\n",
  1059. cntlid, subsysnqn, hostnqn);
  1060. req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
  1061. found:
  1062. mutex_unlock(&subsys->lock);
  1063. nvmet_subsys_put(subsys);
  1064. out:
  1065. return ctrl;
  1066. }
  1067. u16 nvmet_check_ctrl_status(struct nvmet_req *req)
  1068. {
  1069. if (unlikely(!(req->sq->ctrl->cc & NVME_CC_ENABLE))) {
  1070. pr_err("got cmd %d while CC.EN == 0 on qid = %d\n",
  1071. req->cmd->common.opcode, req->sq->qid);
  1072. return NVME_SC_CMD_SEQ_ERROR | NVME_STATUS_DNR;
  1073. }
  1074. if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) {
  1075. pr_err("got cmd %d while CSTS.RDY == 0 on qid = %d\n",
  1076. req->cmd->common.opcode, req->sq->qid);
  1077. return NVME_SC_CMD_SEQ_ERROR | NVME_STATUS_DNR;
  1078. }
  1079. if (unlikely(!nvmet_check_auth_status(req))) {
  1080. pr_warn("qid %d not authenticated\n", req->sq->qid);
  1081. return NVME_SC_AUTH_REQUIRED | NVME_STATUS_DNR;
  1082. }
  1083. return 0;
  1084. }
  1085. bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn)
  1086. {
  1087. struct nvmet_host_link *p;
  1088. lockdep_assert_held(&nvmet_config_sem);
  1089. if (subsys->allow_any_host)
  1090. return true;
  1091. if (nvmet_is_disc_subsys(subsys)) /* allow all access to disc subsys */
  1092. return true;
  1093. list_for_each_entry(p, &subsys->hosts, entry) {
  1094. if (!strcmp(nvmet_host_name(p->host), hostnqn))
  1095. return true;
  1096. }
  1097. return false;
  1098. }
  1099. /*
  1100. * Note: ctrl->subsys->lock should be held when calling this function
  1101. */
  1102. static void nvmet_setup_p2p_ns_map(struct nvmet_ctrl *ctrl,
  1103. struct nvmet_req *req)
  1104. {
  1105. struct nvmet_ns *ns;
  1106. unsigned long idx;
  1107. if (!req->p2p_client)
  1108. return;
  1109. ctrl->p2p_client = get_device(req->p2p_client);
  1110. xa_for_each(&ctrl->subsys->namespaces, idx, ns)
  1111. nvmet_p2pmem_ns_add_p2p(ctrl, ns);
  1112. }
  1113. /*
  1114. * Note: ctrl->subsys->lock should be held when calling this function
  1115. */
  1116. static void nvmet_release_p2p_ns_map(struct nvmet_ctrl *ctrl)
  1117. {
  1118. struct radix_tree_iter iter;
  1119. void __rcu **slot;
  1120. radix_tree_for_each_slot(slot, &ctrl->p2p_ns_map, &iter, 0)
  1121. pci_dev_put(radix_tree_deref_slot(slot));
  1122. put_device(ctrl->p2p_client);
  1123. }
  1124. static void nvmet_fatal_error_handler(struct work_struct *work)
  1125. {
  1126. struct nvmet_ctrl *ctrl =
  1127. container_of(work, struct nvmet_ctrl, fatal_err_work);
  1128. pr_err("ctrl %d fatal error occurred!\n", ctrl->cntlid);
  1129. ctrl->ops->delete_ctrl(ctrl);
  1130. }
  1131. u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
  1132. struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp)
  1133. {
  1134. struct nvmet_subsys *subsys;
  1135. struct nvmet_ctrl *ctrl;
  1136. int ret;
  1137. u16 status;
  1138. status = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR;
  1139. subsys = nvmet_find_get_subsys(req->port, subsysnqn);
  1140. if (!subsys) {
  1141. pr_warn("connect request for invalid subsystem %s!\n",
  1142. subsysnqn);
  1143. req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
  1144. req->error_loc = offsetof(struct nvme_common_command, dptr);
  1145. goto out;
  1146. }
  1147. down_read(&nvmet_config_sem);
  1148. if (!nvmet_host_allowed(subsys, hostnqn)) {
  1149. pr_info("connect by host %s for subsystem %s not allowed\n",
  1150. hostnqn, subsysnqn);
  1151. req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(hostnqn);
  1152. up_read(&nvmet_config_sem);
  1153. status = NVME_SC_CONNECT_INVALID_HOST | NVME_STATUS_DNR;
  1154. req->error_loc = offsetof(struct nvme_common_command, dptr);
  1155. goto out_put_subsystem;
  1156. }
  1157. up_read(&nvmet_config_sem);
  1158. status = NVME_SC_INTERNAL;
  1159. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  1160. if (!ctrl)
  1161. goto out_put_subsystem;
  1162. mutex_init(&ctrl->lock);
  1163. ctrl->port = req->port;
  1164. ctrl->ops = req->ops;
  1165. #ifdef CONFIG_NVME_TARGET_PASSTHRU
  1166. /* By default, set loop targets to clear IDS by default */
  1167. if (ctrl->port->disc_addr.trtype == NVMF_TRTYPE_LOOP)
  1168. subsys->clear_ids = 1;
  1169. #endif
  1170. INIT_WORK(&ctrl->async_event_work, nvmet_async_event_work);
  1171. INIT_LIST_HEAD(&ctrl->async_events);
  1172. INIT_RADIX_TREE(&ctrl->p2p_ns_map, GFP_KERNEL);
  1173. INIT_WORK(&ctrl->fatal_err_work, nvmet_fatal_error_handler);
  1174. INIT_DELAYED_WORK(&ctrl->ka_work, nvmet_keep_alive_timer);
  1175. memcpy(ctrl->subsysnqn, subsysnqn, NVMF_NQN_SIZE);
  1176. memcpy(ctrl->hostnqn, hostnqn, NVMF_NQN_SIZE);
  1177. kref_init(&ctrl->ref);
  1178. ctrl->subsys = subsys;
  1179. ctrl->pi_support = ctrl->port->pi_enable && ctrl->subsys->pi_support;
  1180. nvmet_init_cap(ctrl);
  1181. WRITE_ONCE(ctrl->aen_enabled, NVMET_AEN_CFG_OPTIONAL);
  1182. ctrl->changed_ns_list = kmalloc_array(NVME_MAX_CHANGED_NAMESPACES,
  1183. sizeof(__le32), GFP_KERNEL);
  1184. if (!ctrl->changed_ns_list)
  1185. goto out_free_ctrl;
  1186. ctrl->sqs = kcalloc(subsys->max_qid + 1,
  1187. sizeof(struct nvmet_sq *),
  1188. GFP_KERNEL);
  1189. if (!ctrl->sqs)
  1190. goto out_free_changed_ns_list;
  1191. ret = ida_alloc_range(&cntlid_ida,
  1192. subsys->cntlid_min, subsys->cntlid_max,
  1193. GFP_KERNEL);
  1194. if (ret < 0) {
  1195. status = NVME_SC_CONNECT_CTRL_BUSY | NVME_STATUS_DNR;
  1196. goto out_free_sqs;
  1197. }
  1198. ctrl->cntlid = ret;
  1199. /*
  1200. * Discovery controllers may use some arbitrary high value
  1201. * in order to cleanup stale discovery sessions
  1202. */
  1203. if (nvmet_is_disc_subsys(ctrl->subsys) && !kato)
  1204. kato = NVMET_DISC_KATO_MS;
  1205. /* keep-alive timeout in seconds */
  1206. ctrl->kato = DIV_ROUND_UP(kato, 1000);
  1207. ctrl->err_counter = 0;
  1208. spin_lock_init(&ctrl->error_lock);
  1209. nvmet_start_keep_alive_timer(ctrl);
  1210. mutex_lock(&subsys->lock);
  1211. list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
  1212. nvmet_setup_p2p_ns_map(ctrl, req);
  1213. nvmet_debugfs_ctrl_setup(ctrl);
  1214. mutex_unlock(&subsys->lock);
  1215. *ctrlp = ctrl;
  1216. return 0;
  1217. out_free_sqs:
  1218. kfree(ctrl->sqs);
  1219. out_free_changed_ns_list:
  1220. kfree(ctrl->changed_ns_list);
  1221. out_free_ctrl:
  1222. kfree(ctrl);
  1223. out_put_subsystem:
  1224. nvmet_subsys_put(subsys);
  1225. out:
  1226. return status;
  1227. }
  1228. static void nvmet_ctrl_free(struct kref *ref)
  1229. {
  1230. struct nvmet_ctrl *ctrl = container_of(ref, struct nvmet_ctrl, ref);
  1231. struct nvmet_subsys *subsys = ctrl->subsys;
  1232. mutex_lock(&subsys->lock);
  1233. nvmet_release_p2p_ns_map(ctrl);
  1234. list_del(&ctrl->subsys_entry);
  1235. mutex_unlock(&subsys->lock);
  1236. nvmet_stop_keep_alive_timer(ctrl);
  1237. flush_work(&ctrl->async_event_work);
  1238. cancel_work_sync(&ctrl->fatal_err_work);
  1239. nvmet_destroy_auth(ctrl);
  1240. nvmet_debugfs_ctrl_free(ctrl);
  1241. ida_free(&cntlid_ida, ctrl->cntlid);
  1242. nvmet_async_events_free(ctrl);
  1243. kfree(ctrl->sqs);
  1244. kfree(ctrl->changed_ns_list);
  1245. kfree(ctrl);
  1246. nvmet_subsys_put(subsys);
  1247. }
  1248. void nvmet_ctrl_put(struct nvmet_ctrl *ctrl)
  1249. {
  1250. kref_put(&ctrl->ref, nvmet_ctrl_free);
  1251. }
  1252. void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl)
  1253. {
  1254. mutex_lock(&ctrl->lock);
  1255. if (!(ctrl->csts & NVME_CSTS_CFS)) {
  1256. ctrl->csts |= NVME_CSTS_CFS;
  1257. queue_work(nvmet_wq, &ctrl->fatal_err_work);
  1258. }
  1259. mutex_unlock(&ctrl->lock);
  1260. }
  1261. EXPORT_SYMBOL_GPL(nvmet_ctrl_fatal_error);
  1262. ssize_t nvmet_ctrl_host_traddr(struct nvmet_ctrl *ctrl,
  1263. char *traddr, size_t traddr_len)
  1264. {
  1265. if (!ctrl->ops->host_traddr)
  1266. return -EOPNOTSUPP;
  1267. return ctrl->ops->host_traddr(ctrl, traddr, traddr_len);
  1268. }
  1269. static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
  1270. const char *subsysnqn)
  1271. {
  1272. struct nvmet_subsys_link *p;
  1273. if (!port)
  1274. return NULL;
  1275. if (!strcmp(NVME_DISC_SUBSYS_NAME, subsysnqn)) {
  1276. if (!kref_get_unless_zero(&nvmet_disc_subsys->ref))
  1277. return NULL;
  1278. return nvmet_disc_subsys;
  1279. }
  1280. down_read(&nvmet_config_sem);
  1281. if (!strncmp(nvmet_disc_subsys->subsysnqn, subsysnqn,
  1282. NVMF_NQN_SIZE)) {
  1283. if (kref_get_unless_zero(&nvmet_disc_subsys->ref)) {
  1284. up_read(&nvmet_config_sem);
  1285. return nvmet_disc_subsys;
  1286. }
  1287. }
  1288. list_for_each_entry(p, &port->subsystems, entry) {
  1289. if (!strncmp(p->subsys->subsysnqn, subsysnqn,
  1290. NVMF_NQN_SIZE)) {
  1291. if (!kref_get_unless_zero(&p->subsys->ref))
  1292. break;
  1293. up_read(&nvmet_config_sem);
  1294. return p->subsys;
  1295. }
  1296. }
  1297. up_read(&nvmet_config_sem);
  1298. return NULL;
  1299. }
  1300. struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
  1301. enum nvme_subsys_type type)
  1302. {
  1303. struct nvmet_subsys *subsys;
  1304. char serial[NVMET_SN_MAX_SIZE / 2];
  1305. int ret;
  1306. subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
  1307. if (!subsys)
  1308. return ERR_PTR(-ENOMEM);
  1309. subsys->ver = NVMET_DEFAULT_VS;
  1310. /* generate a random serial number as our controllers are ephemeral: */
  1311. get_random_bytes(&serial, sizeof(serial));
  1312. bin2hex(subsys->serial, &serial, sizeof(serial));
  1313. subsys->model_number = kstrdup(NVMET_DEFAULT_CTRL_MODEL, GFP_KERNEL);
  1314. if (!subsys->model_number) {
  1315. ret = -ENOMEM;
  1316. goto free_subsys;
  1317. }
  1318. subsys->ieee_oui = 0;
  1319. subsys->firmware_rev = kstrndup(UTS_RELEASE, NVMET_FR_MAX_SIZE, GFP_KERNEL);
  1320. if (!subsys->firmware_rev) {
  1321. ret = -ENOMEM;
  1322. goto free_mn;
  1323. }
  1324. switch (type) {
  1325. case NVME_NQN_NVME:
  1326. subsys->max_qid = NVMET_NR_QUEUES;
  1327. break;
  1328. case NVME_NQN_DISC:
  1329. case NVME_NQN_CURR:
  1330. subsys->max_qid = 0;
  1331. break;
  1332. default:
  1333. pr_err("%s: Unknown Subsystem type - %d\n", __func__, type);
  1334. ret = -EINVAL;
  1335. goto free_fr;
  1336. }
  1337. subsys->type = type;
  1338. subsys->subsysnqn = kstrndup(subsysnqn, NVMF_NQN_SIZE,
  1339. GFP_KERNEL);
  1340. if (!subsys->subsysnqn) {
  1341. ret = -ENOMEM;
  1342. goto free_fr;
  1343. }
  1344. subsys->cntlid_min = NVME_CNTLID_MIN;
  1345. subsys->cntlid_max = NVME_CNTLID_MAX;
  1346. kref_init(&subsys->ref);
  1347. mutex_init(&subsys->lock);
  1348. xa_init(&subsys->namespaces);
  1349. INIT_LIST_HEAD(&subsys->ctrls);
  1350. INIT_LIST_HEAD(&subsys->hosts);
  1351. ret = nvmet_debugfs_subsys_setup(subsys);
  1352. if (ret)
  1353. goto free_subsysnqn;
  1354. return subsys;
  1355. free_subsysnqn:
  1356. kfree(subsys->subsysnqn);
  1357. free_fr:
  1358. kfree(subsys->firmware_rev);
  1359. free_mn:
  1360. kfree(subsys->model_number);
  1361. free_subsys:
  1362. kfree(subsys);
  1363. return ERR_PTR(ret);
  1364. }
  1365. static void nvmet_subsys_free(struct kref *ref)
  1366. {
  1367. struct nvmet_subsys *subsys =
  1368. container_of(ref, struct nvmet_subsys, ref);
  1369. WARN_ON_ONCE(!xa_empty(&subsys->namespaces));
  1370. nvmet_debugfs_subsys_free(subsys);
  1371. xa_destroy(&subsys->namespaces);
  1372. nvmet_passthru_subsys_free(subsys);
  1373. kfree(subsys->subsysnqn);
  1374. kfree(subsys->model_number);
  1375. kfree(subsys->firmware_rev);
  1376. kfree(subsys);
  1377. }
  1378. void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys)
  1379. {
  1380. struct nvmet_ctrl *ctrl;
  1381. mutex_lock(&subsys->lock);
  1382. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
  1383. ctrl->ops->delete_ctrl(ctrl);
  1384. mutex_unlock(&subsys->lock);
  1385. }
  1386. void nvmet_subsys_put(struct nvmet_subsys *subsys)
  1387. {
  1388. kref_put(&subsys->ref, nvmet_subsys_free);
  1389. }
  1390. static int __init nvmet_init(void)
  1391. {
  1392. int error = -ENOMEM;
  1393. nvmet_ana_group_enabled[NVMET_DEFAULT_ANA_GRPID] = 1;
  1394. nvmet_bvec_cache = kmem_cache_create("nvmet-bvec",
  1395. NVMET_MAX_MPOOL_BVEC * sizeof(struct bio_vec), 0,
  1396. SLAB_HWCACHE_ALIGN, NULL);
  1397. if (!nvmet_bvec_cache)
  1398. return -ENOMEM;
  1399. zbd_wq = alloc_workqueue("nvmet-zbd-wq", WQ_MEM_RECLAIM, 0);
  1400. if (!zbd_wq)
  1401. goto out_destroy_bvec_cache;
  1402. buffered_io_wq = alloc_workqueue("nvmet-buffered-io-wq",
  1403. WQ_MEM_RECLAIM, 0);
  1404. if (!buffered_io_wq)
  1405. goto out_free_zbd_work_queue;
  1406. nvmet_wq = alloc_workqueue("nvmet-wq",
  1407. WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
  1408. if (!nvmet_wq)
  1409. goto out_free_buffered_work_queue;
  1410. error = nvmet_init_debugfs();
  1411. if (error)
  1412. goto out_free_nvmet_work_queue;
  1413. error = nvmet_init_discovery();
  1414. if (error)
  1415. goto out_exit_debugfs;
  1416. error = nvmet_init_configfs();
  1417. if (error)
  1418. goto out_exit_discovery;
  1419. return 0;
  1420. out_exit_discovery:
  1421. nvmet_exit_discovery();
  1422. out_exit_debugfs:
  1423. nvmet_exit_debugfs();
  1424. out_free_nvmet_work_queue:
  1425. destroy_workqueue(nvmet_wq);
  1426. out_free_buffered_work_queue:
  1427. destroy_workqueue(buffered_io_wq);
  1428. out_free_zbd_work_queue:
  1429. destroy_workqueue(zbd_wq);
  1430. out_destroy_bvec_cache:
  1431. kmem_cache_destroy(nvmet_bvec_cache);
  1432. return error;
  1433. }
  1434. static void __exit nvmet_exit(void)
  1435. {
  1436. nvmet_exit_configfs();
  1437. nvmet_exit_discovery();
  1438. nvmet_exit_debugfs();
  1439. ida_destroy(&cntlid_ida);
  1440. destroy_workqueue(nvmet_wq);
  1441. destroy_workqueue(buffered_io_wq);
  1442. destroy_workqueue(zbd_wq);
  1443. kmem_cache_destroy(nvmet_bvec_cache);
  1444. BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_entry) != 1024);
  1445. BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_hdr) != 1024);
  1446. }
  1447. module_init(nvmet_init);
  1448. module_exit(nvmet_exit);
  1449. MODULE_DESCRIPTION("NVMe target core framework");
  1450. MODULE_LICENSE("GPL v2");