core.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /*
  2. * Common code for the NVMe target.
  3. * Copyright (c) 2015-2016 HGST, a Western Digital Company.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/module.h>
  16. #include <linux/random.h>
  17. #include <linux/rculist.h>
  18. #include "nvmet.h"
  19. static struct nvmet_fabrics_ops *nvmet_transports[NVMF_TRTYPE_MAX];
  20. static DEFINE_IDA(cntlid_ida);
  21. /*
  22. * This read/write semaphore is used to synchronize access to configuration
  23. * information on a target system that will result in discovery log page
  24. * information change for at least one host.
  25. * The full list of resources to protected by this semaphore is:
  26. *
  27. * - subsystems list
  28. * - per-subsystem allowed hosts list
  29. * - allow_any_host subsystem attribute
  30. * - nvmet_genctr
  31. * - the nvmet_transports array
  32. *
  33. * When updating any of those lists/structures write lock should be obtained,
  34. * while when reading (popolating discovery log page or checking host-subsystem
  35. * link) read lock is obtained to allow concurrent reads.
  36. */
  37. DECLARE_RWSEM(nvmet_config_sem);
  38. static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
  39. const char *subsysnqn);
  40. u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
  41. size_t len)
  42. {
  43. if (sg_pcopy_from_buffer(req->sg, req->sg_cnt, buf, len, off) != len)
  44. return NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR;
  45. return 0;
  46. }
  47. u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf, size_t len)
  48. {
  49. if (sg_pcopy_to_buffer(req->sg, req->sg_cnt, buf, len, off) != len)
  50. return NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR;
  51. return 0;
  52. }
  53. static u32 nvmet_async_event_result(struct nvmet_async_event *aen)
  54. {
  55. return aen->event_type | (aen->event_info << 8) | (aen->log_page << 16);
  56. }
  57. static void nvmet_async_events_free(struct nvmet_ctrl *ctrl)
  58. {
  59. struct nvmet_req *req;
  60. while (1) {
  61. mutex_lock(&ctrl->lock);
  62. if (!ctrl->nr_async_event_cmds) {
  63. mutex_unlock(&ctrl->lock);
  64. return;
  65. }
  66. req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds];
  67. mutex_unlock(&ctrl->lock);
  68. nvmet_req_complete(req, NVME_SC_INTERNAL | NVME_SC_DNR);
  69. }
  70. }
  71. static void nvmet_async_event_work(struct work_struct *work)
  72. {
  73. struct nvmet_ctrl *ctrl =
  74. container_of(work, struct nvmet_ctrl, async_event_work);
  75. struct nvmet_async_event *aen;
  76. struct nvmet_req *req;
  77. while (1) {
  78. mutex_lock(&ctrl->lock);
  79. aen = list_first_entry_or_null(&ctrl->async_events,
  80. struct nvmet_async_event, entry);
  81. if (!aen || !ctrl->nr_async_event_cmds) {
  82. mutex_unlock(&ctrl->lock);
  83. return;
  84. }
  85. req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds];
  86. nvmet_set_result(req, nvmet_async_event_result(aen));
  87. list_del(&aen->entry);
  88. kfree(aen);
  89. mutex_unlock(&ctrl->lock);
  90. nvmet_req_complete(req, 0);
  91. }
  92. }
  93. static void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
  94. u8 event_info, u8 log_page)
  95. {
  96. struct nvmet_async_event *aen;
  97. aen = kmalloc(sizeof(*aen), GFP_KERNEL);
  98. if (!aen)
  99. return;
  100. aen->event_type = event_type;
  101. aen->event_info = event_info;
  102. aen->log_page = log_page;
  103. mutex_lock(&ctrl->lock);
  104. list_add_tail(&aen->entry, &ctrl->async_events);
  105. mutex_unlock(&ctrl->lock);
  106. schedule_work(&ctrl->async_event_work);
  107. }
  108. int nvmet_register_transport(struct nvmet_fabrics_ops *ops)
  109. {
  110. int ret = 0;
  111. down_write(&nvmet_config_sem);
  112. if (nvmet_transports[ops->type])
  113. ret = -EINVAL;
  114. else
  115. nvmet_transports[ops->type] = ops;
  116. up_write(&nvmet_config_sem);
  117. return ret;
  118. }
  119. EXPORT_SYMBOL_GPL(nvmet_register_transport);
  120. void nvmet_unregister_transport(struct nvmet_fabrics_ops *ops)
  121. {
  122. down_write(&nvmet_config_sem);
  123. nvmet_transports[ops->type] = NULL;
  124. up_write(&nvmet_config_sem);
  125. }
  126. EXPORT_SYMBOL_GPL(nvmet_unregister_transport);
  127. int nvmet_enable_port(struct nvmet_port *port)
  128. {
  129. struct nvmet_fabrics_ops *ops;
  130. int ret;
  131. lockdep_assert_held(&nvmet_config_sem);
  132. ops = nvmet_transports[port->disc_addr.trtype];
  133. if (!ops) {
  134. up_write(&nvmet_config_sem);
  135. request_module("nvmet-transport-%d", port->disc_addr.trtype);
  136. down_write(&nvmet_config_sem);
  137. ops = nvmet_transports[port->disc_addr.trtype];
  138. if (!ops) {
  139. pr_err("transport type %d not supported\n",
  140. port->disc_addr.trtype);
  141. return -EINVAL;
  142. }
  143. }
  144. if (!try_module_get(ops->owner))
  145. return -EINVAL;
  146. ret = ops->add_port(port);
  147. if (ret) {
  148. module_put(ops->owner);
  149. return ret;
  150. }
  151. port->enabled = true;
  152. return 0;
  153. }
  154. void nvmet_disable_port(struct nvmet_port *port)
  155. {
  156. struct nvmet_fabrics_ops *ops;
  157. lockdep_assert_held(&nvmet_config_sem);
  158. port->enabled = false;
  159. ops = nvmet_transports[port->disc_addr.trtype];
  160. ops->remove_port(port);
  161. module_put(ops->owner);
  162. }
  163. static void nvmet_keep_alive_timer(struct work_struct *work)
  164. {
  165. struct nvmet_ctrl *ctrl = container_of(to_delayed_work(work),
  166. struct nvmet_ctrl, ka_work);
  167. pr_err("ctrl %d keep-alive timer (%d seconds) expired!\n",
  168. ctrl->cntlid, ctrl->kato);
  169. nvmet_ctrl_fatal_error(ctrl);
  170. }
  171. static void nvmet_start_keep_alive_timer(struct nvmet_ctrl *ctrl)
  172. {
  173. if (unlikely(ctrl->kato == 0))
  174. return;
  175. pr_debug("ctrl %d start keep-alive timer for %d secs\n",
  176. ctrl->cntlid, ctrl->kato);
  177. INIT_DELAYED_WORK(&ctrl->ka_work, nvmet_keep_alive_timer);
  178. schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
  179. }
  180. static void nvmet_stop_keep_alive_timer(struct nvmet_ctrl *ctrl)
  181. {
  182. if (unlikely(ctrl->kato == 0))
  183. return;
  184. pr_debug("ctrl %d stop keep-alive\n", ctrl->cntlid);
  185. cancel_delayed_work_sync(&ctrl->ka_work);
  186. }
  187. static struct nvmet_ns *__nvmet_find_namespace(struct nvmet_ctrl *ctrl,
  188. __le32 nsid)
  189. {
  190. struct nvmet_ns *ns;
  191. list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) {
  192. if (ns->nsid == le32_to_cpu(nsid))
  193. return ns;
  194. }
  195. return NULL;
  196. }
  197. struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid)
  198. {
  199. struct nvmet_ns *ns;
  200. rcu_read_lock();
  201. ns = __nvmet_find_namespace(ctrl, nsid);
  202. if (ns)
  203. percpu_ref_get(&ns->ref);
  204. rcu_read_unlock();
  205. return ns;
  206. }
  207. static void nvmet_destroy_namespace(struct percpu_ref *ref)
  208. {
  209. struct nvmet_ns *ns = container_of(ref, struct nvmet_ns, ref);
  210. complete(&ns->disable_done);
  211. }
  212. void nvmet_put_namespace(struct nvmet_ns *ns)
  213. {
  214. percpu_ref_put(&ns->ref);
  215. }
  216. int nvmet_ns_enable(struct nvmet_ns *ns)
  217. {
  218. struct nvmet_subsys *subsys = ns->subsys;
  219. struct nvmet_ctrl *ctrl;
  220. int ret = 0;
  221. mutex_lock(&subsys->lock);
  222. if (ns->enabled)
  223. goto out_unlock;
  224. ns->bdev = blkdev_get_by_path(ns->device_path, FMODE_READ | FMODE_WRITE,
  225. NULL);
  226. if (IS_ERR(ns->bdev)) {
  227. pr_err("failed to open block device %s: (%ld)\n",
  228. ns->device_path, PTR_ERR(ns->bdev));
  229. ret = PTR_ERR(ns->bdev);
  230. ns->bdev = NULL;
  231. goto out_unlock;
  232. }
  233. ns->size = i_size_read(ns->bdev->bd_inode);
  234. ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
  235. ret = percpu_ref_init(&ns->ref, nvmet_destroy_namespace,
  236. 0, GFP_KERNEL);
  237. if (ret)
  238. goto out_blkdev_put;
  239. if (ns->nsid > subsys->max_nsid)
  240. subsys->max_nsid = ns->nsid;
  241. /*
  242. * The namespaces list needs to be sorted to simplify the implementation
  243. * of the Identify Namepace List subcommand.
  244. */
  245. if (list_empty(&subsys->namespaces)) {
  246. list_add_tail_rcu(&ns->dev_link, &subsys->namespaces);
  247. } else {
  248. struct nvmet_ns *old;
  249. list_for_each_entry_rcu(old, &subsys->namespaces, dev_link) {
  250. BUG_ON(ns->nsid == old->nsid);
  251. if (ns->nsid < old->nsid)
  252. break;
  253. }
  254. list_add_tail_rcu(&ns->dev_link, &old->dev_link);
  255. }
  256. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
  257. nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE, 0, 0);
  258. ns->enabled = true;
  259. ret = 0;
  260. out_unlock:
  261. mutex_unlock(&subsys->lock);
  262. return ret;
  263. out_blkdev_put:
  264. blkdev_put(ns->bdev, FMODE_WRITE|FMODE_READ);
  265. ns->bdev = NULL;
  266. goto out_unlock;
  267. }
  268. void nvmet_ns_disable(struct nvmet_ns *ns)
  269. {
  270. struct nvmet_subsys *subsys = ns->subsys;
  271. struct nvmet_ctrl *ctrl;
  272. mutex_lock(&subsys->lock);
  273. if (!ns->enabled)
  274. goto out_unlock;
  275. ns->enabled = false;
  276. list_del_rcu(&ns->dev_link);
  277. mutex_unlock(&subsys->lock);
  278. /*
  279. * Now that we removed the namespaces from the lookup list, we
  280. * can kill the per_cpu ref and wait for any remaining references
  281. * to be dropped, as well as a RCU grace period for anyone only
  282. * using the namepace under rcu_read_lock(). Note that we can't
  283. * use call_rcu here as we need to ensure the namespaces have
  284. * been fully destroyed before unloading the module.
  285. */
  286. percpu_ref_kill(&ns->ref);
  287. synchronize_rcu();
  288. wait_for_completion(&ns->disable_done);
  289. percpu_ref_exit(&ns->ref);
  290. mutex_lock(&subsys->lock);
  291. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
  292. nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE, 0, 0);
  293. if (ns->bdev)
  294. blkdev_put(ns->bdev, FMODE_WRITE|FMODE_READ);
  295. out_unlock:
  296. mutex_unlock(&subsys->lock);
  297. }
  298. void nvmet_ns_free(struct nvmet_ns *ns)
  299. {
  300. nvmet_ns_disable(ns);
  301. kfree(ns->device_path);
  302. kfree(ns);
  303. }
  304. struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
  305. {
  306. struct nvmet_ns *ns;
  307. ns = kzalloc(sizeof(*ns), GFP_KERNEL);
  308. if (!ns)
  309. return NULL;
  310. INIT_LIST_HEAD(&ns->dev_link);
  311. init_completion(&ns->disable_done);
  312. ns->nsid = nsid;
  313. ns->subsys = subsys;
  314. uuid_gen(&ns->uuid);
  315. return ns;
  316. }
  317. static void __nvmet_req_complete(struct nvmet_req *req, u16 status)
  318. {
  319. u32 old_sqhd, new_sqhd;
  320. u16 sqhd;
  321. if (status)
  322. nvmet_set_status(req, status);
  323. if (req->sq->size) {
  324. do {
  325. old_sqhd = req->sq->sqhd;
  326. new_sqhd = (old_sqhd + 1) % req->sq->size;
  327. } while (cmpxchg(&req->sq->sqhd, old_sqhd, new_sqhd) !=
  328. old_sqhd);
  329. }
  330. sqhd = req->sq->sqhd & 0x0000FFFF;
  331. req->rsp->sq_head = cpu_to_le16(sqhd);
  332. req->rsp->sq_id = cpu_to_le16(req->sq->qid);
  333. req->rsp->command_id = req->cmd->common.command_id;
  334. if (req->ns)
  335. nvmet_put_namespace(req->ns);
  336. req->ops->queue_response(req);
  337. }
  338. void nvmet_req_complete(struct nvmet_req *req, u16 status)
  339. {
  340. __nvmet_req_complete(req, status);
  341. percpu_ref_put(&req->sq->ref);
  342. }
  343. EXPORT_SYMBOL_GPL(nvmet_req_complete);
  344. void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq,
  345. u16 qid, u16 size)
  346. {
  347. cq->qid = qid;
  348. cq->size = size;
  349. ctrl->cqs[qid] = cq;
  350. }
  351. void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq,
  352. u16 qid, u16 size)
  353. {
  354. sq->sqhd = 0;
  355. sq->qid = qid;
  356. sq->size = size;
  357. ctrl->sqs[qid] = sq;
  358. }
  359. static void nvmet_confirm_sq(struct percpu_ref *ref)
  360. {
  361. struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
  362. complete(&sq->confirm_done);
  363. }
  364. void nvmet_sq_destroy(struct nvmet_sq *sq)
  365. {
  366. /*
  367. * If this is the admin queue, complete all AERs so that our
  368. * queue doesn't have outstanding requests on it.
  369. */
  370. if (sq->ctrl && sq->ctrl->sqs && sq->ctrl->sqs[0] == sq)
  371. nvmet_async_events_free(sq->ctrl);
  372. percpu_ref_kill_and_confirm(&sq->ref, nvmet_confirm_sq);
  373. wait_for_completion(&sq->confirm_done);
  374. wait_for_completion(&sq->free_done);
  375. percpu_ref_exit(&sq->ref);
  376. if (sq->ctrl) {
  377. nvmet_ctrl_put(sq->ctrl);
  378. sq->ctrl = NULL; /* allows reusing the queue later */
  379. }
  380. }
  381. EXPORT_SYMBOL_GPL(nvmet_sq_destroy);
  382. static void nvmet_sq_free(struct percpu_ref *ref)
  383. {
  384. struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
  385. complete(&sq->free_done);
  386. }
  387. int nvmet_sq_init(struct nvmet_sq *sq)
  388. {
  389. int ret;
  390. ret = percpu_ref_init(&sq->ref, nvmet_sq_free, 0, GFP_KERNEL);
  391. if (ret) {
  392. pr_err("percpu_ref init failed!\n");
  393. return ret;
  394. }
  395. init_completion(&sq->free_done);
  396. init_completion(&sq->confirm_done);
  397. return 0;
  398. }
  399. EXPORT_SYMBOL_GPL(nvmet_sq_init);
  400. bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
  401. struct nvmet_sq *sq, struct nvmet_fabrics_ops *ops)
  402. {
  403. u8 flags = req->cmd->common.flags;
  404. u16 status;
  405. req->cq = cq;
  406. req->sq = sq;
  407. req->ops = ops;
  408. req->sg = NULL;
  409. req->sg_cnt = 0;
  410. req->rsp->status = 0;
  411. /* no support for fused commands yet */
  412. if (unlikely(flags & (NVME_CMD_FUSE_FIRST | NVME_CMD_FUSE_SECOND))) {
  413. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  414. goto fail;
  415. }
  416. /*
  417. * For fabrics, PSDT field shall describe metadata pointer (MPTR) that
  418. * contains an address of a single contiguous physical buffer that is
  419. * byte aligned.
  420. */
  421. if (unlikely((flags & NVME_CMD_SGL_ALL) != NVME_CMD_SGL_METABUF)) {
  422. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  423. goto fail;
  424. }
  425. if (unlikely(!req->sq->ctrl))
  426. /* will return an error for any Non-connect command: */
  427. status = nvmet_parse_connect_cmd(req);
  428. else if (likely(req->sq->qid != 0))
  429. status = nvmet_parse_io_cmd(req);
  430. else if (req->cmd->common.opcode == nvme_fabrics_command)
  431. status = nvmet_parse_fabrics_cmd(req);
  432. else if (req->sq->ctrl->subsys->type == NVME_NQN_DISC)
  433. status = nvmet_parse_discovery_cmd(req);
  434. else
  435. status = nvmet_parse_admin_cmd(req);
  436. if (status)
  437. goto fail;
  438. if (unlikely(!percpu_ref_tryget_live(&sq->ref))) {
  439. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  440. goto fail;
  441. }
  442. return true;
  443. fail:
  444. __nvmet_req_complete(req, status);
  445. return false;
  446. }
  447. EXPORT_SYMBOL_GPL(nvmet_req_init);
  448. void nvmet_req_uninit(struct nvmet_req *req)
  449. {
  450. percpu_ref_put(&req->sq->ref);
  451. }
  452. EXPORT_SYMBOL_GPL(nvmet_req_uninit);
  453. static inline bool nvmet_cc_en(u32 cc)
  454. {
  455. return (cc >> NVME_CC_EN_SHIFT) & 0x1;
  456. }
  457. static inline u8 nvmet_cc_css(u32 cc)
  458. {
  459. return (cc >> NVME_CC_CSS_SHIFT) & 0x7;
  460. }
  461. static inline u8 nvmet_cc_mps(u32 cc)
  462. {
  463. return (cc >> NVME_CC_MPS_SHIFT) & 0xf;
  464. }
  465. static inline u8 nvmet_cc_ams(u32 cc)
  466. {
  467. return (cc >> NVME_CC_AMS_SHIFT) & 0x7;
  468. }
  469. static inline u8 nvmet_cc_shn(u32 cc)
  470. {
  471. return (cc >> NVME_CC_SHN_SHIFT) & 0x3;
  472. }
  473. static inline u8 nvmet_cc_iosqes(u32 cc)
  474. {
  475. return (cc >> NVME_CC_IOSQES_SHIFT) & 0xf;
  476. }
  477. static inline u8 nvmet_cc_iocqes(u32 cc)
  478. {
  479. return (cc >> NVME_CC_IOCQES_SHIFT) & 0xf;
  480. }
  481. static void nvmet_start_ctrl(struct nvmet_ctrl *ctrl)
  482. {
  483. lockdep_assert_held(&ctrl->lock);
  484. /*
  485. * Only I/O controllers should verify iosqes,iocqes.
  486. * Strictly speaking, the spec says a discovery controller
  487. * should verify iosqes,iocqes are zeroed, however that
  488. * would break backwards compatibility, so don't enforce it.
  489. */
  490. if (ctrl->subsys->type != NVME_NQN_DISC &&
  491. (nvmet_cc_iosqes(ctrl->cc) != NVME_NVM_IOSQES ||
  492. nvmet_cc_iocqes(ctrl->cc) != NVME_NVM_IOCQES)) {
  493. ctrl->csts = NVME_CSTS_CFS;
  494. return;
  495. }
  496. if (nvmet_cc_mps(ctrl->cc) != 0 ||
  497. nvmet_cc_ams(ctrl->cc) != 0 ||
  498. nvmet_cc_css(ctrl->cc) != 0) {
  499. ctrl->csts = NVME_CSTS_CFS;
  500. return;
  501. }
  502. ctrl->csts = NVME_CSTS_RDY;
  503. /*
  504. * Controllers that are not yet enabled should not really enforce the
  505. * keep alive timeout, but we still want to track a timeout and cleanup
  506. * in case a host died before it enabled the controller. Hence, simply
  507. * reset the keep alive timer when the controller is enabled.
  508. */
  509. if (ctrl->kato)
  510. mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ);
  511. }
  512. static void nvmet_clear_ctrl(struct nvmet_ctrl *ctrl)
  513. {
  514. lockdep_assert_held(&ctrl->lock);
  515. /* XXX: tear down queues? */
  516. ctrl->csts &= ~NVME_CSTS_RDY;
  517. ctrl->cc = 0;
  518. }
  519. void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new)
  520. {
  521. u32 old;
  522. mutex_lock(&ctrl->lock);
  523. old = ctrl->cc;
  524. ctrl->cc = new;
  525. if (nvmet_cc_en(new) && !nvmet_cc_en(old))
  526. nvmet_start_ctrl(ctrl);
  527. if (!nvmet_cc_en(new) && nvmet_cc_en(old))
  528. nvmet_clear_ctrl(ctrl);
  529. if (nvmet_cc_shn(new) && !nvmet_cc_shn(old)) {
  530. nvmet_clear_ctrl(ctrl);
  531. ctrl->csts |= NVME_CSTS_SHST_CMPLT;
  532. }
  533. if (!nvmet_cc_shn(new) && nvmet_cc_shn(old))
  534. ctrl->csts &= ~NVME_CSTS_SHST_CMPLT;
  535. mutex_unlock(&ctrl->lock);
  536. }
  537. static void nvmet_init_cap(struct nvmet_ctrl *ctrl)
  538. {
  539. /* command sets supported: NVMe command set: */
  540. ctrl->cap = (1ULL << 37);
  541. /* CC.EN timeout in 500msec units: */
  542. ctrl->cap |= (15ULL << 24);
  543. /* maximum queue entries supported: */
  544. ctrl->cap |= NVMET_QUEUE_SIZE - 1;
  545. }
  546. u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid,
  547. struct nvmet_req *req, struct nvmet_ctrl **ret)
  548. {
  549. struct nvmet_subsys *subsys;
  550. struct nvmet_ctrl *ctrl;
  551. u16 status = 0;
  552. subsys = nvmet_find_get_subsys(req->port, subsysnqn);
  553. if (!subsys) {
  554. pr_warn("connect request for invalid subsystem %s!\n",
  555. subsysnqn);
  556. req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
  557. return NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
  558. }
  559. mutex_lock(&subsys->lock);
  560. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
  561. if (ctrl->cntlid == cntlid) {
  562. if (strncmp(hostnqn, ctrl->hostnqn, NVMF_NQN_SIZE)) {
  563. pr_warn("hostnqn mismatch.\n");
  564. continue;
  565. }
  566. if (!kref_get_unless_zero(&ctrl->ref))
  567. continue;
  568. *ret = ctrl;
  569. goto out;
  570. }
  571. }
  572. pr_warn("could not find controller %d for subsys %s / host %s\n",
  573. cntlid, subsysnqn, hostnqn);
  574. req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
  575. status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
  576. out:
  577. mutex_unlock(&subsys->lock);
  578. nvmet_subsys_put(subsys);
  579. return status;
  580. }
  581. u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd)
  582. {
  583. if (unlikely(!(req->sq->ctrl->cc & NVME_CC_ENABLE))) {
  584. pr_err("got io cmd %d while CC.EN == 0 on qid = %d\n",
  585. cmd->common.opcode, req->sq->qid);
  586. return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
  587. }
  588. if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) {
  589. pr_err("got io cmd %d while CSTS.RDY == 0 on qid = %d\n",
  590. cmd->common.opcode, req->sq->qid);
  591. req->ns = NULL;
  592. return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
  593. }
  594. return 0;
  595. }
  596. static bool __nvmet_host_allowed(struct nvmet_subsys *subsys,
  597. const char *hostnqn)
  598. {
  599. struct nvmet_host_link *p;
  600. if (subsys->allow_any_host)
  601. return true;
  602. list_for_each_entry(p, &subsys->hosts, entry) {
  603. if (!strcmp(nvmet_host_name(p->host), hostnqn))
  604. return true;
  605. }
  606. return false;
  607. }
  608. static bool nvmet_host_discovery_allowed(struct nvmet_req *req,
  609. const char *hostnqn)
  610. {
  611. struct nvmet_subsys_link *s;
  612. list_for_each_entry(s, &req->port->subsystems, entry) {
  613. if (__nvmet_host_allowed(s->subsys, hostnqn))
  614. return true;
  615. }
  616. return false;
  617. }
  618. bool nvmet_host_allowed(struct nvmet_req *req, struct nvmet_subsys *subsys,
  619. const char *hostnqn)
  620. {
  621. lockdep_assert_held(&nvmet_config_sem);
  622. if (subsys->type == NVME_NQN_DISC)
  623. return nvmet_host_discovery_allowed(req, hostnqn);
  624. else
  625. return __nvmet_host_allowed(subsys, hostnqn);
  626. }
  627. static void nvmet_fatal_error_handler(struct work_struct *work)
  628. {
  629. struct nvmet_ctrl *ctrl =
  630. container_of(work, struct nvmet_ctrl, fatal_err_work);
  631. pr_err("ctrl %d fatal error occurred!\n", ctrl->cntlid);
  632. ctrl->ops->delete_ctrl(ctrl);
  633. }
  634. u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
  635. struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp)
  636. {
  637. struct nvmet_subsys *subsys;
  638. struct nvmet_ctrl *ctrl;
  639. int ret;
  640. u16 status;
  641. status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
  642. subsys = nvmet_find_get_subsys(req->port, subsysnqn);
  643. if (!subsys) {
  644. pr_warn("connect request for invalid subsystem %s!\n",
  645. subsysnqn);
  646. req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
  647. goto out;
  648. }
  649. status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
  650. down_read(&nvmet_config_sem);
  651. if (!nvmet_host_allowed(req, subsys, hostnqn)) {
  652. pr_info("connect by host %s for subsystem %s not allowed\n",
  653. hostnqn, subsysnqn);
  654. req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(hostnqn);
  655. up_read(&nvmet_config_sem);
  656. status = NVME_SC_CONNECT_INVALID_HOST | NVME_SC_DNR;
  657. goto out_put_subsystem;
  658. }
  659. up_read(&nvmet_config_sem);
  660. status = NVME_SC_INTERNAL;
  661. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  662. if (!ctrl)
  663. goto out_put_subsystem;
  664. mutex_init(&ctrl->lock);
  665. nvmet_init_cap(ctrl);
  666. INIT_WORK(&ctrl->async_event_work, nvmet_async_event_work);
  667. INIT_LIST_HEAD(&ctrl->async_events);
  668. INIT_WORK(&ctrl->fatal_err_work, nvmet_fatal_error_handler);
  669. memcpy(ctrl->subsysnqn, subsysnqn, NVMF_NQN_SIZE);
  670. memcpy(ctrl->hostnqn, hostnqn, NVMF_NQN_SIZE);
  671. kref_init(&ctrl->ref);
  672. ctrl->subsys = subsys;
  673. ctrl->cqs = kcalloc(subsys->max_qid + 1,
  674. sizeof(struct nvmet_cq *),
  675. GFP_KERNEL);
  676. if (!ctrl->cqs)
  677. goto out_free_ctrl;
  678. ctrl->sqs = kcalloc(subsys->max_qid + 1,
  679. sizeof(struct nvmet_sq *),
  680. GFP_KERNEL);
  681. if (!ctrl->sqs)
  682. goto out_free_cqs;
  683. ret = ida_simple_get(&cntlid_ida,
  684. NVME_CNTLID_MIN, NVME_CNTLID_MAX,
  685. GFP_KERNEL);
  686. if (ret < 0) {
  687. status = NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR;
  688. goto out_free_sqs;
  689. }
  690. ctrl->cntlid = ret;
  691. ctrl->ops = req->ops;
  692. if (ctrl->subsys->type == NVME_NQN_DISC) {
  693. /* Don't accept keep-alive timeout for discovery controllers */
  694. if (kato) {
  695. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  696. goto out_free_sqs;
  697. }
  698. /*
  699. * Discovery controllers use some arbitrary high value in order
  700. * to cleanup stale discovery sessions
  701. *
  702. * From the latest base diff RC:
  703. * "The Keep Alive command is not supported by
  704. * Discovery controllers. A transport may specify a
  705. * fixed Discovery controller activity timeout value
  706. * (e.g., 2 minutes). If no commands are received
  707. * by a Discovery controller within that time
  708. * period, the controller may perform the
  709. * actions for Keep Alive Timer expiration".
  710. */
  711. ctrl->kato = NVMET_DISC_KATO;
  712. } else {
  713. /* keep-alive timeout in seconds */
  714. ctrl->kato = DIV_ROUND_UP(kato, 1000);
  715. }
  716. nvmet_start_keep_alive_timer(ctrl);
  717. mutex_lock(&subsys->lock);
  718. list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
  719. mutex_unlock(&subsys->lock);
  720. *ctrlp = ctrl;
  721. return 0;
  722. out_free_sqs:
  723. kfree(ctrl->sqs);
  724. out_free_cqs:
  725. kfree(ctrl->cqs);
  726. out_free_ctrl:
  727. kfree(ctrl);
  728. out_put_subsystem:
  729. nvmet_subsys_put(subsys);
  730. out:
  731. return status;
  732. }
  733. static void nvmet_ctrl_free(struct kref *ref)
  734. {
  735. struct nvmet_ctrl *ctrl = container_of(ref, struct nvmet_ctrl, ref);
  736. struct nvmet_subsys *subsys = ctrl->subsys;
  737. nvmet_stop_keep_alive_timer(ctrl);
  738. mutex_lock(&subsys->lock);
  739. list_del(&ctrl->subsys_entry);
  740. mutex_unlock(&subsys->lock);
  741. flush_work(&ctrl->async_event_work);
  742. cancel_work_sync(&ctrl->fatal_err_work);
  743. ida_simple_remove(&cntlid_ida, ctrl->cntlid);
  744. nvmet_subsys_put(subsys);
  745. kfree(ctrl->sqs);
  746. kfree(ctrl->cqs);
  747. kfree(ctrl);
  748. }
  749. void nvmet_ctrl_put(struct nvmet_ctrl *ctrl)
  750. {
  751. kref_put(&ctrl->ref, nvmet_ctrl_free);
  752. }
  753. void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl)
  754. {
  755. mutex_lock(&ctrl->lock);
  756. if (!(ctrl->csts & NVME_CSTS_CFS)) {
  757. ctrl->csts |= NVME_CSTS_CFS;
  758. schedule_work(&ctrl->fatal_err_work);
  759. }
  760. mutex_unlock(&ctrl->lock);
  761. }
  762. EXPORT_SYMBOL_GPL(nvmet_ctrl_fatal_error);
  763. static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
  764. const char *subsysnqn)
  765. {
  766. struct nvmet_subsys_link *p;
  767. if (!port)
  768. return NULL;
  769. if (!strncmp(NVME_DISC_SUBSYS_NAME, subsysnqn,
  770. NVMF_NQN_SIZE)) {
  771. if (!kref_get_unless_zero(&nvmet_disc_subsys->ref))
  772. return NULL;
  773. return nvmet_disc_subsys;
  774. }
  775. down_read(&nvmet_config_sem);
  776. list_for_each_entry(p, &port->subsystems, entry) {
  777. if (!strncmp(p->subsys->subsysnqn, subsysnqn,
  778. NVMF_NQN_SIZE)) {
  779. if (!kref_get_unless_zero(&p->subsys->ref))
  780. break;
  781. up_read(&nvmet_config_sem);
  782. return p->subsys;
  783. }
  784. }
  785. up_read(&nvmet_config_sem);
  786. return NULL;
  787. }
  788. struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
  789. enum nvme_subsys_type type)
  790. {
  791. struct nvmet_subsys *subsys;
  792. subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
  793. if (!subsys)
  794. return NULL;
  795. subsys->ver = NVME_VS(1, 3, 0); /* NVMe 1.3.0 */
  796. /* generate a random serial number as our controllers are ephemeral: */
  797. get_random_bytes(&subsys->serial, sizeof(subsys->serial));
  798. switch (type) {
  799. case NVME_NQN_NVME:
  800. subsys->max_qid = NVMET_NR_QUEUES;
  801. break;
  802. case NVME_NQN_DISC:
  803. subsys->max_qid = 0;
  804. break;
  805. default:
  806. pr_err("%s: Unknown Subsystem type - %d\n", __func__, type);
  807. kfree(subsys);
  808. return NULL;
  809. }
  810. subsys->type = type;
  811. subsys->subsysnqn = kstrndup(subsysnqn, NVMF_NQN_SIZE,
  812. GFP_KERNEL);
  813. if (!subsys->subsysnqn) {
  814. kfree(subsys);
  815. return NULL;
  816. }
  817. kref_init(&subsys->ref);
  818. mutex_init(&subsys->lock);
  819. INIT_LIST_HEAD(&subsys->namespaces);
  820. INIT_LIST_HEAD(&subsys->ctrls);
  821. INIT_LIST_HEAD(&subsys->hosts);
  822. return subsys;
  823. }
  824. static void nvmet_subsys_free(struct kref *ref)
  825. {
  826. struct nvmet_subsys *subsys =
  827. container_of(ref, struct nvmet_subsys, ref);
  828. WARN_ON_ONCE(!list_empty(&subsys->namespaces));
  829. kfree(subsys->subsysnqn);
  830. kfree(subsys);
  831. }
  832. void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys)
  833. {
  834. struct nvmet_ctrl *ctrl;
  835. mutex_lock(&subsys->lock);
  836. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
  837. ctrl->ops->delete_ctrl(ctrl);
  838. mutex_unlock(&subsys->lock);
  839. }
  840. void nvmet_subsys_put(struct nvmet_subsys *subsys)
  841. {
  842. kref_put(&subsys->ref, nvmet_subsys_free);
  843. }
  844. static int __init nvmet_init(void)
  845. {
  846. int error;
  847. error = nvmet_init_discovery();
  848. if (error)
  849. goto out;
  850. error = nvmet_init_configfs();
  851. if (error)
  852. goto out_exit_discovery;
  853. return 0;
  854. out_exit_discovery:
  855. nvmet_exit_discovery();
  856. out:
  857. return error;
  858. }
  859. static void __exit nvmet_exit(void)
  860. {
  861. nvmet_exit_configfs();
  862. nvmet_exit_discovery();
  863. ida_destroy(&cntlid_ida);
  864. BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_entry) != 1024);
  865. BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_hdr) != 1024);
  866. }
  867. module_init(nvmet_init);
  868. module_exit(nvmet_exit);
  869. MODULE_LICENSE("GPL v2");