core.c 24 KB

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