core.c 22 KB

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