core.c 23 KB

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