core.c 22 KB

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