core.c 25 KB

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