core.c 24 KB

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