core.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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. pr_debug("ctrl %d start keep-alive timer for %d secs\n",
  174. ctrl->cntlid, ctrl->kato);
  175. INIT_DELAYED_WORK(&ctrl->ka_work, nvmet_keep_alive_timer);
  176. schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
  177. }
  178. static void nvmet_stop_keep_alive_timer(struct nvmet_ctrl *ctrl)
  179. {
  180. pr_debug("ctrl %d stop keep-alive\n", ctrl->cntlid);
  181. cancel_delayed_work_sync(&ctrl->ka_work);
  182. }
  183. static struct nvmet_ns *__nvmet_find_namespace(struct nvmet_ctrl *ctrl,
  184. __le32 nsid)
  185. {
  186. struct nvmet_ns *ns;
  187. list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) {
  188. if (ns->nsid == le32_to_cpu(nsid))
  189. return ns;
  190. }
  191. return NULL;
  192. }
  193. struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid)
  194. {
  195. struct nvmet_ns *ns;
  196. rcu_read_lock();
  197. ns = __nvmet_find_namespace(ctrl, nsid);
  198. if (ns)
  199. percpu_ref_get(&ns->ref);
  200. rcu_read_unlock();
  201. return ns;
  202. }
  203. static void nvmet_destroy_namespace(struct percpu_ref *ref)
  204. {
  205. struct nvmet_ns *ns = container_of(ref, struct nvmet_ns, ref);
  206. complete(&ns->disable_done);
  207. }
  208. void nvmet_put_namespace(struct nvmet_ns *ns)
  209. {
  210. percpu_ref_put(&ns->ref);
  211. }
  212. int nvmet_ns_enable(struct nvmet_ns *ns)
  213. {
  214. struct nvmet_subsys *subsys = ns->subsys;
  215. struct nvmet_ctrl *ctrl;
  216. int ret = 0;
  217. mutex_lock(&subsys->lock);
  218. if (ns->enabled)
  219. goto out_unlock;
  220. ns->bdev = blkdev_get_by_path(ns->device_path, FMODE_READ | FMODE_WRITE,
  221. NULL);
  222. if (IS_ERR(ns->bdev)) {
  223. pr_err("nvmet: failed to open block device %s: (%ld)\n",
  224. ns->device_path, PTR_ERR(ns->bdev));
  225. ret = PTR_ERR(ns->bdev);
  226. ns->bdev = NULL;
  227. goto out_unlock;
  228. }
  229. ns->size = i_size_read(ns->bdev->bd_inode);
  230. ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
  231. ret = percpu_ref_init(&ns->ref, nvmet_destroy_namespace,
  232. 0, GFP_KERNEL);
  233. if (ret)
  234. goto out_blkdev_put;
  235. if (ns->nsid > subsys->max_nsid)
  236. subsys->max_nsid = ns->nsid;
  237. /*
  238. * The namespaces list needs to be sorted to simplify the implementation
  239. * of the Identify Namepace List subcommand.
  240. */
  241. if (list_empty(&subsys->namespaces)) {
  242. list_add_tail_rcu(&ns->dev_link, &subsys->namespaces);
  243. } else {
  244. struct nvmet_ns *old;
  245. list_for_each_entry_rcu(old, &subsys->namespaces, dev_link) {
  246. BUG_ON(ns->nsid == old->nsid);
  247. if (ns->nsid < old->nsid)
  248. break;
  249. }
  250. list_add_tail_rcu(&ns->dev_link, &old->dev_link);
  251. }
  252. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
  253. nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE, 0, 0);
  254. ns->enabled = true;
  255. ret = 0;
  256. out_unlock:
  257. mutex_unlock(&subsys->lock);
  258. return ret;
  259. out_blkdev_put:
  260. blkdev_put(ns->bdev, FMODE_WRITE|FMODE_READ);
  261. ns->bdev = NULL;
  262. goto out_unlock;
  263. }
  264. void nvmet_ns_disable(struct nvmet_ns *ns)
  265. {
  266. struct nvmet_subsys *subsys = ns->subsys;
  267. struct nvmet_ctrl *ctrl;
  268. mutex_lock(&subsys->lock);
  269. if (!ns->enabled)
  270. goto out_unlock;
  271. ns->enabled = false;
  272. list_del_rcu(&ns->dev_link);
  273. mutex_unlock(&subsys->lock);
  274. /*
  275. * Now that we removed the namespaces from the lookup list, we
  276. * can kill the per_cpu ref and wait for any remaining references
  277. * to be dropped, as well as a RCU grace period for anyone only
  278. * using the namepace under rcu_read_lock(). Note that we can't
  279. * use call_rcu here as we need to ensure the namespaces have
  280. * been fully destroyed before unloading the module.
  281. */
  282. percpu_ref_kill(&ns->ref);
  283. synchronize_rcu();
  284. wait_for_completion(&ns->disable_done);
  285. percpu_ref_exit(&ns->ref);
  286. mutex_lock(&subsys->lock);
  287. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
  288. nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE, 0, 0);
  289. if (ns->bdev)
  290. blkdev_put(ns->bdev, FMODE_WRITE|FMODE_READ);
  291. out_unlock:
  292. mutex_unlock(&subsys->lock);
  293. }
  294. void nvmet_ns_free(struct nvmet_ns *ns)
  295. {
  296. nvmet_ns_disable(ns);
  297. kfree(ns->device_path);
  298. kfree(ns);
  299. }
  300. struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
  301. {
  302. struct nvmet_ns *ns;
  303. ns = kzalloc(sizeof(*ns), GFP_KERNEL);
  304. if (!ns)
  305. return NULL;
  306. INIT_LIST_HEAD(&ns->dev_link);
  307. init_completion(&ns->disable_done);
  308. ns->nsid = nsid;
  309. ns->subsys = subsys;
  310. return ns;
  311. }
  312. static void __nvmet_req_complete(struct nvmet_req *req, u16 status)
  313. {
  314. if (status)
  315. nvmet_set_status(req, status);
  316. /* XXX: need to fill in something useful for sq_head */
  317. req->rsp->sq_head = 0;
  318. if (likely(req->sq)) /* may happen during early failure */
  319. req->rsp->sq_id = cpu_to_le16(req->sq->qid);
  320. req->rsp->command_id = req->cmd->common.command_id;
  321. if (req->ns)
  322. nvmet_put_namespace(req->ns);
  323. req->ops->queue_response(req);
  324. }
  325. void nvmet_req_complete(struct nvmet_req *req, u16 status)
  326. {
  327. __nvmet_req_complete(req, status);
  328. percpu_ref_put(&req->sq->ref);
  329. }
  330. EXPORT_SYMBOL_GPL(nvmet_req_complete);
  331. void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq,
  332. u16 qid, u16 size)
  333. {
  334. cq->qid = qid;
  335. cq->size = size;
  336. ctrl->cqs[qid] = cq;
  337. }
  338. void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq,
  339. u16 qid, u16 size)
  340. {
  341. sq->qid = qid;
  342. sq->size = size;
  343. ctrl->sqs[qid] = sq;
  344. }
  345. static void nvmet_confirm_sq(struct percpu_ref *ref)
  346. {
  347. struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
  348. complete(&sq->confirm_done);
  349. }
  350. void nvmet_sq_destroy(struct nvmet_sq *sq)
  351. {
  352. /*
  353. * If this is the admin queue, complete all AERs so that our
  354. * queue doesn't have outstanding requests on it.
  355. */
  356. if (sq->ctrl && sq->ctrl->sqs && sq->ctrl->sqs[0] == sq)
  357. nvmet_async_events_free(sq->ctrl);
  358. percpu_ref_kill_and_confirm(&sq->ref, nvmet_confirm_sq);
  359. wait_for_completion(&sq->confirm_done);
  360. wait_for_completion(&sq->free_done);
  361. percpu_ref_exit(&sq->ref);
  362. if (sq->ctrl) {
  363. nvmet_ctrl_put(sq->ctrl);
  364. sq->ctrl = NULL; /* allows reusing the queue later */
  365. }
  366. }
  367. EXPORT_SYMBOL_GPL(nvmet_sq_destroy);
  368. static void nvmet_sq_free(struct percpu_ref *ref)
  369. {
  370. struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
  371. complete(&sq->free_done);
  372. }
  373. int nvmet_sq_init(struct nvmet_sq *sq)
  374. {
  375. int ret;
  376. ret = percpu_ref_init(&sq->ref, nvmet_sq_free, 0, GFP_KERNEL);
  377. if (ret) {
  378. pr_err("percpu_ref init failed!\n");
  379. return ret;
  380. }
  381. init_completion(&sq->free_done);
  382. init_completion(&sq->confirm_done);
  383. return 0;
  384. }
  385. EXPORT_SYMBOL_GPL(nvmet_sq_init);
  386. bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
  387. struct nvmet_sq *sq, struct nvmet_fabrics_ops *ops)
  388. {
  389. u8 flags = req->cmd->common.flags;
  390. u16 status;
  391. req->cq = cq;
  392. req->sq = sq;
  393. req->ops = ops;
  394. req->sg = NULL;
  395. req->sg_cnt = 0;
  396. req->rsp->status = 0;
  397. /* no support for fused commands yet */
  398. if (unlikely(flags & (NVME_CMD_FUSE_FIRST | NVME_CMD_FUSE_SECOND))) {
  399. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  400. goto fail;
  401. }
  402. /* either variant of SGLs is fine, as we don't support metadata */
  403. if (unlikely((flags & NVME_CMD_SGL_ALL) != NVME_CMD_SGL_METABUF &&
  404. (flags & NVME_CMD_SGL_ALL) != NVME_CMD_SGL_METASEG)) {
  405. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  406. goto fail;
  407. }
  408. if (unlikely(!req->sq->ctrl))
  409. /* will return an error for any Non-connect command: */
  410. status = nvmet_parse_connect_cmd(req);
  411. else if (likely(req->sq->qid != 0))
  412. status = nvmet_parse_io_cmd(req);
  413. else if (req->cmd->common.opcode == nvme_fabrics_command)
  414. status = nvmet_parse_fabrics_cmd(req);
  415. else if (req->sq->ctrl->subsys->type == NVME_NQN_DISC)
  416. status = nvmet_parse_discovery_cmd(req);
  417. else
  418. status = nvmet_parse_admin_cmd(req);
  419. if (status)
  420. goto fail;
  421. if (unlikely(!percpu_ref_tryget_live(&sq->ref))) {
  422. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  423. goto fail;
  424. }
  425. return true;
  426. fail:
  427. __nvmet_req_complete(req, status);
  428. return false;
  429. }
  430. EXPORT_SYMBOL_GPL(nvmet_req_init);
  431. static inline bool nvmet_cc_en(u32 cc)
  432. {
  433. return cc & 0x1;
  434. }
  435. static inline u8 nvmet_cc_css(u32 cc)
  436. {
  437. return (cc >> 4) & 0x7;
  438. }
  439. static inline u8 nvmet_cc_mps(u32 cc)
  440. {
  441. return (cc >> 7) & 0xf;
  442. }
  443. static inline u8 nvmet_cc_ams(u32 cc)
  444. {
  445. return (cc >> 11) & 0x7;
  446. }
  447. static inline u8 nvmet_cc_shn(u32 cc)
  448. {
  449. return (cc >> 14) & 0x3;
  450. }
  451. static inline u8 nvmet_cc_iosqes(u32 cc)
  452. {
  453. return (cc >> 16) & 0xf;
  454. }
  455. static inline u8 nvmet_cc_iocqes(u32 cc)
  456. {
  457. return (cc >> 20) & 0xf;
  458. }
  459. static void nvmet_start_ctrl(struct nvmet_ctrl *ctrl)
  460. {
  461. lockdep_assert_held(&ctrl->lock);
  462. if (nvmet_cc_iosqes(ctrl->cc) != NVME_NVM_IOSQES ||
  463. nvmet_cc_iocqes(ctrl->cc) != NVME_NVM_IOCQES ||
  464. nvmet_cc_mps(ctrl->cc) != 0 ||
  465. nvmet_cc_ams(ctrl->cc) != 0 ||
  466. nvmet_cc_css(ctrl->cc) != 0) {
  467. ctrl->csts = NVME_CSTS_CFS;
  468. return;
  469. }
  470. ctrl->csts = NVME_CSTS_RDY;
  471. }
  472. static void nvmet_clear_ctrl(struct nvmet_ctrl *ctrl)
  473. {
  474. lockdep_assert_held(&ctrl->lock);
  475. /* XXX: tear down queues? */
  476. ctrl->csts &= ~NVME_CSTS_RDY;
  477. ctrl->cc = 0;
  478. }
  479. void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new)
  480. {
  481. u32 old;
  482. mutex_lock(&ctrl->lock);
  483. old = ctrl->cc;
  484. ctrl->cc = new;
  485. if (nvmet_cc_en(new) && !nvmet_cc_en(old))
  486. nvmet_start_ctrl(ctrl);
  487. if (!nvmet_cc_en(new) && nvmet_cc_en(old))
  488. nvmet_clear_ctrl(ctrl);
  489. if (nvmet_cc_shn(new) && !nvmet_cc_shn(old)) {
  490. nvmet_clear_ctrl(ctrl);
  491. ctrl->csts |= NVME_CSTS_SHST_CMPLT;
  492. }
  493. if (!nvmet_cc_shn(new) && nvmet_cc_shn(old))
  494. ctrl->csts &= ~NVME_CSTS_SHST_CMPLT;
  495. mutex_unlock(&ctrl->lock);
  496. }
  497. static void nvmet_init_cap(struct nvmet_ctrl *ctrl)
  498. {
  499. /* command sets supported: NVMe command set: */
  500. ctrl->cap = (1ULL << 37);
  501. /* CC.EN timeout in 500msec units: */
  502. ctrl->cap |= (15ULL << 24);
  503. /* maximum queue entries supported: */
  504. ctrl->cap |= NVMET_QUEUE_SIZE - 1;
  505. }
  506. u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid,
  507. struct nvmet_req *req, struct nvmet_ctrl **ret)
  508. {
  509. struct nvmet_subsys *subsys;
  510. struct nvmet_ctrl *ctrl;
  511. u16 status = 0;
  512. subsys = nvmet_find_get_subsys(req->port, subsysnqn);
  513. if (!subsys) {
  514. pr_warn("connect request for invalid subsystem %s!\n",
  515. subsysnqn);
  516. req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
  517. return NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
  518. }
  519. mutex_lock(&subsys->lock);
  520. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
  521. if (ctrl->cntlid == cntlid) {
  522. if (strncmp(hostnqn, ctrl->hostnqn, NVMF_NQN_SIZE)) {
  523. pr_warn("hostnqn mismatch.\n");
  524. continue;
  525. }
  526. if (!kref_get_unless_zero(&ctrl->ref))
  527. continue;
  528. *ret = ctrl;
  529. goto out;
  530. }
  531. }
  532. pr_warn("could not find controller %d for subsys %s / host %s\n",
  533. cntlid, subsysnqn, hostnqn);
  534. req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
  535. status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
  536. out:
  537. mutex_unlock(&subsys->lock);
  538. nvmet_subsys_put(subsys);
  539. return status;
  540. }
  541. static bool __nvmet_host_allowed(struct nvmet_subsys *subsys,
  542. const char *hostnqn)
  543. {
  544. struct nvmet_host_link *p;
  545. if (subsys->allow_any_host)
  546. return true;
  547. list_for_each_entry(p, &subsys->hosts, entry) {
  548. if (!strcmp(nvmet_host_name(p->host), hostnqn))
  549. return true;
  550. }
  551. return false;
  552. }
  553. static bool nvmet_host_discovery_allowed(struct nvmet_req *req,
  554. const char *hostnqn)
  555. {
  556. struct nvmet_subsys_link *s;
  557. list_for_each_entry(s, &req->port->subsystems, entry) {
  558. if (__nvmet_host_allowed(s->subsys, hostnqn))
  559. return true;
  560. }
  561. return false;
  562. }
  563. bool nvmet_host_allowed(struct nvmet_req *req, struct nvmet_subsys *subsys,
  564. const char *hostnqn)
  565. {
  566. lockdep_assert_held(&nvmet_config_sem);
  567. if (subsys->type == NVME_NQN_DISC)
  568. return nvmet_host_discovery_allowed(req, hostnqn);
  569. else
  570. return __nvmet_host_allowed(subsys, hostnqn);
  571. }
  572. u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
  573. struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp)
  574. {
  575. struct nvmet_subsys *subsys;
  576. struct nvmet_ctrl *ctrl;
  577. int ret;
  578. u16 status;
  579. status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
  580. subsys = nvmet_find_get_subsys(req->port, subsysnqn);
  581. if (!subsys) {
  582. pr_warn("connect request for invalid subsystem %s!\n",
  583. subsysnqn);
  584. req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
  585. goto out;
  586. }
  587. status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
  588. down_read(&nvmet_config_sem);
  589. if (!nvmet_host_allowed(req, subsys, hostnqn)) {
  590. pr_info("connect by host %s for subsystem %s not allowed\n",
  591. hostnqn, subsysnqn);
  592. req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(hostnqn);
  593. up_read(&nvmet_config_sem);
  594. goto out_put_subsystem;
  595. }
  596. up_read(&nvmet_config_sem);
  597. status = NVME_SC_INTERNAL;
  598. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  599. if (!ctrl)
  600. goto out_put_subsystem;
  601. mutex_init(&ctrl->lock);
  602. nvmet_init_cap(ctrl);
  603. INIT_WORK(&ctrl->async_event_work, nvmet_async_event_work);
  604. INIT_LIST_HEAD(&ctrl->async_events);
  605. memcpy(ctrl->subsysnqn, subsysnqn, NVMF_NQN_SIZE);
  606. memcpy(ctrl->hostnqn, hostnqn, NVMF_NQN_SIZE);
  607. /* generate a random serial number as our controllers are ephemeral: */
  608. get_random_bytes(&ctrl->serial, sizeof(ctrl->serial));
  609. kref_init(&ctrl->ref);
  610. ctrl->subsys = subsys;
  611. ctrl->cqs = kcalloc(subsys->max_qid + 1,
  612. sizeof(struct nvmet_cq *),
  613. GFP_KERNEL);
  614. if (!ctrl->cqs)
  615. goto out_free_ctrl;
  616. ctrl->sqs = kcalloc(subsys->max_qid + 1,
  617. sizeof(struct nvmet_sq *),
  618. GFP_KERNEL);
  619. if (!ctrl->sqs)
  620. goto out_free_cqs;
  621. ret = ida_simple_get(&cntlid_ida,
  622. NVME_CNTLID_MIN, NVME_CNTLID_MAX,
  623. GFP_KERNEL);
  624. if (ret < 0) {
  625. status = NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR;
  626. goto out_free_sqs;
  627. }
  628. ctrl->cntlid = ret;
  629. ctrl->ops = req->ops;
  630. if (ctrl->subsys->type == NVME_NQN_DISC) {
  631. /* Don't accept keep-alive timeout for discovery controllers */
  632. if (kato) {
  633. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  634. goto out_free_sqs;
  635. }
  636. /*
  637. * Discovery controllers use some arbitrary high value in order
  638. * to cleanup stale discovery sessions
  639. *
  640. * From the latest base diff RC:
  641. * "The Keep Alive command is not supported by
  642. * Discovery controllers. A transport may specify a
  643. * fixed Discovery controller activity timeout value
  644. * (e.g., 2 minutes). If no commands are received
  645. * by a Discovery controller within that time
  646. * period, the controller may perform the
  647. * actions for Keep Alive Timer expiration".
  648. */
  649. ctrl->kato = NVMET_DISC_KATO;
  650. } else {
  651. /* keep-alive timeout in seconds */
  652. ctrl->kato = DIV_ROUND_UP(kato, 1000);
  653. }
  654. nvmet_start_keep_alive_timer(ctrl);
  655. mutex_lock(&subsys->lock);
  656. list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
  657. mutex_unlock(&subsys->lock);
  658. *ctrlp = ctrl;
  659. return 0;
  660. out_free_sqs:
  661. kfree(ctrl->sqs);
  662. out_free_cqs:
  663. kfree(ctrl->cqs);
  664. out_free_ctrl:
  665. kfree(ctrl);
  666. out_put_subsystem:
  667. nvmet_subsys_put(subsys);
  668. out:
  669. return status;
  670. }
  671. static void nvmet_ctrl_free(struct kref *ref)
  672. {
  673. struct nvmet_ctrl *ctrl = container_of(ref, struct nvmet_ctrl, ref);
  674. struct nvmet_subsys *subsys = ctrl->subsys;
  675. nvmet_stop_keep_alive_timer(ctrl);
  676. mutex_lock(&subsys->lock);
  677. list_del(&ctrl->subsys_entry);
  678. mutex_unlock(&subsys->lock);
  679. flush_work(&ctrl->async_event_work);
  680. cancel_work_sync(&ctrl->fatal_err_work);
  681. ida_simple_remove(&cntlid_ida, ctrl->cntlid);
  682. nvmet_subsys_put(subsys);
  683. kfree(ctrl->sqs);
  684. kfree(ctrl->cqs);
  685. kfree(ctrl);
  686. }
  687. void nvmet_ctrl_put(struct nvmet_ctrl *ctrl)
  688. {
  689. kref_put(&ctrl->ref, nvmet_ctrl_free);
  690. }
  691. static void nvmet_fatal_error_handler(struct work_struct *work)
  692. {
  693. struct nvmet_ctrl *ctrl =
  694. container_of(work, struct nvmet_ctrl, fatal_err_work);
  695. pr_err("ctrl %d fatal error occurred!\n", ctrl->cntlid);
  696. ctrl->ops->delete_ctrl(ctrl);
  697. }
  698. void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl)
  699. {
  700. mutex_lock(&ctrl->lock);
  701. if (!(ctrl->csts & NVME_CSTS_CFS)) {
  702. ctrl->csts |= NVME_CSTS_CFS;
  703. INIT_WORK(&ctrl->fatal_err_work, nvmet_fatal_error_handler);
  704. schedule_work(&ctrl->fatal_err_work);
  705. }
  706. mutex_unlock(&ctrl->lock);
  707. }
  708. EXPORT_SYMBOL_GPL(nvmet_ctrl_fatal_error);
  709. static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
  710. const char *subsysnqn)
  711. {
  712. struct nvmet_subsys_link *p;
  713. if (!port)
  714. return NULL;
  715. if (!strncmp(NVME_DISC_SUBSYS_NAME, subsysnqn,
  716. NVMF_NQN_SIZE)) {
  717. if (!kref_get_unless_zero(&nvmet_disc_subsys->ref))
  718. return NULL;
  719. return nvmet_disc_subsys;
  720. }
  721. down_read(&nvmet_config_sem);
  722. list_for_each_entry(p, &port->subsystems, entry) {
  723. if (!strncmp(p->subsys->subsysnqn, subsysnqn,
  724. NVMF_NQN_SIZE)) {
  725. if (!kref_get_unless_zero(&p->subsys->ref))
  726. break;
  727. up_read(&nvmet_config_sem);
  728. return p->subsys;
  729. }
  730. }
  731. up_read(&nvmet_config_sem);
  732. return NULL;
  733. }
  734. struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
  735. enum nvme_subsys_type type)
  736. {
  737. struct nvmet_subsys *subsys;
  738. subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
  739. if (!subsys)
  740. return NULL;
  741. subsys->ver = NVME_VS(1, 2, 1); /* NVMe 1.2.1 */
  742. switch (type) {
  743. case NVME_NQN_NVME:
  744. subsys->max_qid = NVMET_NR_QUEUES;
  745. break;
  746. case NVME_NQN_DISC:
  747. subsys->max_qid = 0;
  748. break;
  749. default:
  750. pr_err("%s: Unknown Subsystem type - %d\n", __func__, type);
  751. kfree(subsys);
  752. return NULL;
  753. }
  754. subsys->type = type;
  755. subsys->subsysnqn = kstrndup(subsysnqn, NVMF_NQN_SIZE,
  756. GFP_KERNEL);
  757. if (!subsys->subsysnqn) {
  758. kfree(subsys);
  759. return NULL;
  760. }
  761. kref_init(&subsys->ref);
  762. mutex_init(&subsys->lock);
  763. INIT_LIST_HEAD(&subsys->namespaces);
  764. INIT_LIST_HEAD(&subsys->ctrls);
  765. INIT_LIST_HEAD(&subsys->hosts);
  766. return subsys;
  767. }
  768. static void nvmet_subsys_free(struct kref *ref)
  769. {
  770. struct nvmet_subsys *subsys =
  771. container_of(ref, struct nvmet_subsys, ref);
  772. WARN_ON_ONCE(!list_empty(&subsys->namespaces));
  773. kfree(subsys->subsysnqn);
  774. kfree(subsys);
  775. }
  776. void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys)
  777. {
  778. struct nvmet_ctrl *ctrl;
  779. mutex_lock(&subsys->lock);
  780. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
  781. ctrl->ops->delete_ctrl(ctrl);
  782. mutex_unlock(&subsys->lock);
  783. }
  784. void nvmet_subsys_put(struct nvmet_subsys *subsys)
  785. {
  786. kref_put(&subsys->ref, nvmet_subsys_free);
  787. }
  788. static int __init nvmet_init(void)
  789. {
  790. int error;
  791. error = nvmet_init_discovery();
  792. if (error)
  793. goto out;
  794. error = nvmet_init_configfs();
  795. if (error)
  796. goto out_exit_discovery;
  797. return 0;
  798. out_exit_discovery:
  799. nvmet_exit_discovery();
  800. out:
  801. return error;
  802. }
  803. static void __exit nvmet_exit(void)
  804. {
  805. nvmet_exit_configfs();
  806. nvmet_exit_discovery();
  807. ida_destroy(&cntlid_ida);
  808. BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_entry) != 1024);
  809. BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_hdr) != 1024);
  810. }
  811. module_init(nvmet_init);
  812. module_exit(nvmet_exit);
  813. MODULE_LICENSE("GPL v2");