core.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  1. /*
  2. * NVM Express device driver
  3. * Copyright (c) 2011-2014, Intel Corporation.
  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. #include <linux/blkdev.h>
  15. #include <linux/blk-mq.h>
  16. #include <linux/delay.h>
  17. #include <linux/errno.h>
  18. #include <linux/hdreg.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/list_sort.h>
  22. #include <linux/slab.h>
  23. #include <linux/types.h>
  24. #include <linux/pr.h>
  25. #include <linux/ptrace.h>
  26. #include <linux/nvme_ioctl.h>
  27. #include <linux/t10-pi.h>
  28. #include <scsi/sg.h>
  29. #include <asm/unaligned.h>
  30. #include "nvme.h"
  31. #define NVME_MINORS (1U << MINORBITS)
  32. unsigned char admin_timeout = 60;
  33. module_param(admin_timeout, byte, 0644);
  34. MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
  35. EXPORT_SYMBOL_GPL(admin_timeout);
  36. unsigned char nvme_io_timeout = 30;
  37. module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
  38. MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
  39. EXPORT_SYMBOL_GPL(nvme_io_timeout);
  40. unsigned char shutdown_timeout = 5;
  41. module_param(shutdown_timeout, byte, 0644);
  42. MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
  43. static int nvme_major;
  44. module_param(nvme_major, int, 0);
  45. static int nvme_char_major;
  46. module_param(nvme_char_major, int, 0);
  47. static LIST_HEAD(nvme_ctrl_list);
  48. static DEFINE_SPINLOCK(dev_list_lock);
  49. static struct class *nvme_class;
  50. static void nvme_free_ns(struct kref *kref)
  51. {
  52. struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
  53. if (ns->type == NVME_NS_LIGHTNVM)
  54. nvme_nvm_unregister(ns->queue, ns->disk->disk_name);
  55. spin_lock(&dev_list_lock);
  56. ns->disk->private_data = NULL;
  57. spin_unlock(&dev_list_lock);
  58. put_disk(ns->disk);
  59. ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
  60. nvme_put_ctrl(ns->ctrl);
  61. kfree(ns);
  62. }
  63. static void nvme_put_ns(struct nvme_ns *ns)
  64. {
  65. kref_put(&ns->kref, nvme_free_ns);
  66. }
  67. static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk)
  68. {
  69. struct nvme_ns *ns;
  70. spin_lock(&dev_list_lock);
  71. ns = disk->private_data;
  72. if (ns) {
  73. if (!kref_get_unless_zero(&ns->kref))
  74. goto fail;
  75. if (!try_module_get(ns->ctrl->ops->module))
  76. goto fail_put_ns;
  77. }
  78. spin_unlock(&dev_list_lock);
  79. return ns;
  80. fail_put_ns:
  81. kref_put(&ns->kref, nvme_free_ns);
  82. fail:
  83. spin_unlock(&dev_list_lock);
  84. return NULL;
  85. }
  86. void nvme_requeue_req(struct request *req)
  87. {
  88. unsigned long flags;
  89. blk_mq_requeue_request(req);
  90. spin_lock_irqsave(req->q->queue_lock, flags);
  91. if (!blk_queue_stopped(req->q))
  92. blk_mq_kick_requeue_list(req->q);
  93. spin_unlock_irqrestore(req->q->queue_lock, flags);
  94. }
  95. EXPORT_SYMBOL_GPL(nvme_requeue_req);
  96. struct request *nvme_alloc_request(struct request_queue *q,
  97. struct nvme_command *cmd, unsigned int flags)
  98. {
  99. bool write = cmd->common.opcode & 1;
  100. struct request *req;
  101. req = blk_mq_alloc_request(q, write, flags);
  102. if (IS_ERR(req))
  103. return req;
  104. req->cmd_type = REQ_TYPE_DRV_PRIV;
  105. req->cmd_flags |= REQ_FAILFAST_DRIVER;
  106. req->__data_len = 0;
  107. req->__sector = (sector_t) -1;
  108. req->bio = req->biotail = NULL;
  109. req->cmd = (unsigned char *)cmd;
  110. req->cmd_len = sizeof(struct nvme_command);
  111. return req;
  112. }
  113. EXPORT_SYMBOL_GPL(nvme_alloc_request);
  114. /*
  115. * Returns 0 on success. If the result is negative, it's a Linux error code;
  116. * if the result is positive, it's an NVM Express status code
  117. */
  118. int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  119. struct nvme_completion *cqe, void *buffer, unsigned bufflen,
  120. unsigned timeout)
  121. {
  122. struct request *req;
  123. int ret;
  124. req = nvme_alloc_request(q, cmd, 0);
  125. if (IS_ERR(req))
  126. return PTR_ERR(req);
  127. req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
  128. req->special = cqe;
  129. if (buffer && bufflen) {
  130. ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
  131. if (ret)
  132. goto out;
  133. }
  134. blk_execute_rq(req->q, NULL, req, 0);
  135. ret = req->errors;
  136. out:
  137. blk_mq_free_request(req);
  138. return ret;
  139. }
  140. int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  141. void *buffer, unsigned bufflen)
  142. {
  143. return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0);
  144. }
  145. EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
  146. int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
  147. void __user *ubuffer, unsigned bufflen,
  148. void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
  149. u32 *result, unsigned timeout)
  150. {
  151. bool write = cmd->common.opcode & 1;
  152. struct nvme_completion cqe;
  153. struct nvme_ns *ns = q->queuedata;
  154. struct gendisk *disk = ns ? ns->disk : NULL;
  155. struct request *req;
  156. struct bio *bio = NULL;
  157. void *meta = NULL;
  158. int ret;
  159. req = nvme_alloc_request(q, cmd, 0);
  160. if (IS_ERR(req))
  161. return PTR_ERR(req);
  162. req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
  163. req->special = &cqe;
  164. if (ubuffer && bufflen) {
  165. ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
  166. GFP_KERNEL);
  167. if (ret)
  168. goto out;
  169. bio = req->bio;
  170. if (!disk)
  171. goto submit;
  172. bio->bi_bdev = bdget_disk(disk, 0);
  173. if (!bio->bi_bdev) {
  174. ret = -ENODEV;
  175. goto out_unmap;
  176. }
  177. if (meta_buffer && meta_len) {
  178. struct bio_integrity_payload *bip;
  179. meta = kmalloc(meta_len, GFP_KERNEL);
  180. if (!meta) {
  181. ret = -ENOMEM;
  182. goto out_unmap;
  183. }
  184. if (write) {
  185. if (copy_from_user(meta, meta_buffer,
  186. meta_len)) {
  187. ret = -EFAULT;
  188. goto out_free_meta;
  189. }
  190. }
  191. bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
  192. if (IS_ERR(bip)) {
  193. ret = PTR_ERR(bip);
  194. goto out_free_meta;
  195. }
  196. bip->bip_iter.bi_size = meta_len;
  197. bip->bip_iter.bi_sector = meta_seed;
  198. ret = bio_integrity_add_page(bio, virt_to_page(meta),
  199. meta_len, offset_in_page(meta));
  200. if (ret != meta_len) {
  201. ret = -ENOMEM;
  202. goto out_free_meta;
  203. }
  204. }
  205. }
  206. submit:
  207. blk_execute_rq(req->q, disk, req, 0);
  208. ret = req->errors;
  209. if (result)
  210. *result = le32_to_cpu(cqe.result);
  211. if (meta && !ret && !write) {
  212. if (copy_to_user(meta_buffer, meta, meta_len))
  213. ret = -EFAULT;
  214. }
  215. out_free_meta:
  216. kfree(meta);
  217. out_unmap:
  218. if (bio) {
  219. if (disk && bio->bi_bdev)
  220. bdput(bio->bi_bdev);
  221. blk_rq_unmap_user(bio);
  222. }
  223. out:
  224. blk_mq_free_request(req);
  225. return ret;
  226. }
  227. int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
  228. void __user *ubuffer, unsigned bufflen, u32 *result,
  229. unsigned timeout)
  230. {
  231. return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0,
  232. result, timeout);
  233. }
  234. int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
  235. {
  236. struct nvme_command c = { };
  237. int error;
  238. /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
  239. c.identify.opcode = nvme_admin_identify;
  240. c.identify.cns = cpu_to_le32(1);
  241. *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
  242. if (!*id)
  243. return -ENOMEM;
  244. error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
  245. sizeof(struct nvme_id_ctrl));
  246. if (error)
  247. kfree(*id);
  248. return error;
  249. }
  250. static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
  251. {
  252. struct nvme_command c = { };
  253. c.identify.opcode = nvme_admin_identify;
  254. c.identify.cns = cpu_to_le32(2);
  255. c.identify.nsid = cpu_to_le32(nsid);
  256. return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
  257. }
  258. int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
  259. struct nvme_id_ns **id)
  260. {
  261. struct nvme_command c = { };
  262. int error;
  263. /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
  264. c.identify.opcode = nvme_admin_identify,
  265. c.identify.nsid = cpu_to_le32(nsid),
  266. *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
  267. if (!*id)
  268. return -ENOMEM;
  269. error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
  270. sizeof(struct nvme_id_ns));
  271. if (error)
  272. kfree(*id);
  273. return error;
  274. }
  275. int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid,
  276. dma_addr_t dma_addr, u32 *result)
  277. {
  278. struct nvme_command c;
  279. struct nvme_completion cqe;
  280. int ret;
  281. memset(&c, 0, sizeof(c));
  282. c.features.opcode = nvme_admin_get_features;
  283. c.features.nsid = cpu_to_le32(nsid);
  284. c.features.prp1 = cpu_to_le64(dma_addr);
  285. c.features.fid = cpu_to_le32(fid);
  286. ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &cqe, NULL, 0, 0);
  287. if (ret >= 0)
  288. *result = le32_to_cpu(cqe.result);
  289. return ret;
  290. }
  291. int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
  292. dma_addr_t dma_addr, u32 *result)
  293. {
  294. struct nvme_command c;
  295. struct nvme_completion cqe;
  296. int ret;
  297. memset(&c, 0, sizeof(c));
  298. c.features.opcode = nvme_admin_set_features;
  299. c.features.prp1 = cpu_to_le64(dma_addr);
  300. c.features.fid = cpu_to_le32(fid);
  301. c.features.dword11 = cpu_to_le32(dword11);
  302. ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &cqe, NULL, 0, 0);
  303. if (ret >= 0)
  304. *result = le32_to_cpu(cqe.result);
  305. return ret;
  306. }
  307. int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log)
  308. {
  309. struct nvme_command c = { };
  310. int error;
  311. c.common.opcode = nvme_admin_get_log_page,
  312. c.common.nsid = cpu_to_le32(0xFFFFFFFF),
  313. c.common.cdw10[0] = cpu_to_le32(
  314. (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) |
  315. NVME_LOG_SMART),
  316. *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL);
  317. if (!*log)
  318. return -ENOMEM;
  319. error = nvme_submit_sync_cmd(dev->admin_q, &c, *log,
  320. sizeof(struct nvme_smart_log));
  321. if (error)
  322. kfree(*log);
  323. return error;
  324. }
  325. int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
  326. {
  327. u32 q_count = (*count - 1) | ((*count - 1) << 16);
  328. u32 result;
  329. int status, nr_io_queues;
  330. status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, 0,
  331. &result);
  332. if (status)
  333. return status;
  334. nr_io_queues = min(result & 0xffff, result >> 16) + 1;
  335. *count = min(*count, nr_io_queues);
  336. return 0;
  337. }
  338. EXPORT_SYMBOL_GPL(nvme_set_queue_count);
  339. static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
  340. {
  341. struct nvme_user_io io;
  342. struct nvme_command c;
  343. unsigned length, meta_len;
  344. void __user *metadata;
  345. if (copy_from_user(&io, uio, sizeof(io)))
  346. return -EFAULT;
  347. if (io.flags)
  348. return -EINVAL;
  349. switch (io.opcode) {
  350. case nvme_cmd_write:
  351. case nvme_cmd_read:
  352. case nvme_cmd_compare:
  353. break;
  354. default:
  355. return -EINVAL;
  356. }
  357. length = (io.nblocks + 1) << ns->lba_shift;
  358. meta_len = (io.nblocks + 1) * ns->ms;
  359. metadata = (void __user *)(uintptr_t)io.metadata;
  360. if (ns->ext) {
  361. length += meta_len;
  362. meta_len = 0;
  363. } else if (meta_len) {
  364. if ((io.metadata & 3) || !io.metadata)
  365. return -EINVAL;
  366. }
  367. memset(&c, 0, sizeof(c));
  368. c.rw.opcode = io.opcode;
  369. c.rw.flags = io.flags;
  370. c.rw.nsid = cpu_to_le32(ns->ns_id);
  371. c.rw.slba = cpu_to_le64(io.slba);
  372. c.rw.length = cpu_to_le16(io.nblocks);
  373. c.rw.control = cpu_to_le16(io.control);
  374. c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
  375. c.rw.reftag = cpu_to_le32(io.reftag);
  376. c.rw.apptag = cpu_to_le16(io.apptag);
  377. c.rw.appmask = cpu_to_le16(io.appmask);
  378. return __nvme_submit_user_cmd(ns->queue, &c,
  379. (void __user *)(uintptr_t)io.addr, length,
  380. metadata, meta_len, io.slba, NULL, 0);
  381. }
  382. static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
  383. struct nvme_passthru_cmd __user *ucmd)
  384. {
  385. struct nvme_passthru_cmd cmd;
  386. struct nvme_command c;
  387. unsigned timeout = 0;
  388. int status;
  389. if (!capable(CAP_SYS_ADMIN))
  390. return -EACCES;
  391. if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
  392. return -EFAULT;
  393. if (cmd.flags)
  394. return -EINVAL;
  395. memset(&c, 0, sizeof(c));
  396. c.common.opcode = cmd.opcode;
  397. c.common.flags = cmd.flags;
  398. c.common.nsid = cpu_to_le32(cmd.nsid);
  399. c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
  400. c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
  401. c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
  402. c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
  403. c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
  404. c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
  405. c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
  406. c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
  407. if (cmd.timeout_ms)
  408. timeout = msecs_to_jiffies(cmd.timeout_ms);
  409. status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
  410. (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
  411. &cmd.result, timeout);
  412. if (status >= 0) {
  413. if (put_user(cmd.result, &ucmd->result))
  414. return -EFAULT;
  415. }
  416. return status;
  417. }
  418. static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
  419. unsigned int cmd, unsigned long arg)
  420. {
  421. struct nvme_ns *ns = bdev->bd_disk->private_data;
  422. switch (cmd) {
  423. case NVME_IOCTL_ID:
  424. force_successful_syscall_return();
  425. return ns->ns_id;
  426. case NVME_IOCTL_ADMIN_CMD:
  427. return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
  428. case NVME_IOCTL_IO_CMD:
  429. return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
  430. case NVME_IOCTL_SUBMIT_IO:
  431. return nvme_submit_io(ns, (void __user *)arg);
  432. #ifdef CONFIG_BLK_DEV_NVME_SCSI
  433. case SG_GET_VERSION_NUM:
  434. return nvme_sg_get_version_num((void __user *)arg);
  435. case SG_IO:
  436. return nvme_sg_io(ns, (void __user *)arg);
  437. #endif
  438. default:
  439. return -ENOTTY;
  440. }
  441. }
  442. #ifdef CONFIG_COMPAT
  443. static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
  444. unsigned int cmd, unsigned long arg)
  445. {
  446. switch (cmd) {
  447. case SG_IO:
  448. return -ENOIOCTLCMD;
  449. }
  450. return nvme_ioctl(bdev, mode, cmd, arg);
  451. }
  452. #else
  453. #define nvme_compat_ioctl NULL
  454. #endif
  455. static int nvme_open(struct block_device *bdev, fmode_t mode)
  456. {
  457. return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO;
  458. }
  459. static void nvme_release(struct gendisk *disk, fmode_t mode)
  460. {
  461. struct nvme_ns *ns = disk->private_data;
  462. module_put(ns->ctrl->ops->module);
  463. nvme_put_ns(ns);
  464. }
  465. static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  466. {
  467. /* some standard values */
  468. geo->heads = 1 << 6;
  469. geo->sectors = 1 << 5;
  470. geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
  471. return 0;
  472. }
  473. #ifdef CONFIG_BLK_DEV_INTEGRITY
  474. static void nvme_init_integrity(struct nvme_ns *ns)
  475. {
  476. struct blk_integrity integrity;
  477. switch (ns->pi_type) {
  478. case NVME_NS_DPS_PI_TYPE3:
  479. integrity.profile = &t10_pi_type3_crc;
  480. break;
  481. case NVME_NS_DPS_PI_TYPE1:
  482. case NVME_NS_DPS_PI_TYPE2:
  483. integrity.profile = &t10_pi_type1_crc;
  484. break;
  485. default:
  486. integrity.profile = NULL;
  487. break;
  488. }
  489. integrity.tuple_size = ns->ms;
  490. blk_integrity_register(ns->disk, &integrity);
  491. blk_queue_max_integrity_segments(ns->queue, 1);
  492. }
  493. #else
  494. static void nvme_init_integrity(struct nvme_ns *ns)
  495. {
  496. }
  497. #endif /* CONFIG_BLK_DEV_INTEGRITY */
  498. static void nvme_config_discard(struct nvme_ns *ns)
  499. {
  500. struct nvme_ctrl *ctrl = ns->ctrl;
  501. u32 logical_block_size = queue_logical_block_size(ns->queue);
  502. if (ctrl->quirks & NVME_QUIRK_DISCARD_ZEROES)
  503. ns->queue->limits.discard_zeroes_data = 1;
  504. else
  505. ns->queue->limits.discard_zeroes_data = 0;
  506. ns->queue->limits.discard_alignment = logical_block_size;
  507. ns->queue->limits.discard_granularity = logical_block_size;
  508. blk_queue_max_discard_sectors(ns->queue, 0xffffffff);
  509. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
  510. }
  511. static int nvme_revalidate_disk(struct gendisk *disk)
  512. {
  513. struct nvme_ns *ns = disk->private_data;
  514. struct nvme_id_ns *id;
  515. u8 lbaf, pi_type;
  516. u16 old_ms;
  517. unsigned short bs;
  518. if (test_bit(NVME_NS_DEAD, &ns->flags)) {
  519. set_capacity(disk, 0);
  520. return -ENODEV;
  521. }
  522. if (nvme_identify_ns(ns->ctrl, ns->ns_id, &id)) {
  523. dev_warn(disk_to_dev(ns->disk), "%s: Identify failure\n",
  524. __func__);
  525. return -ENODEV;
  526. }
  527. if (id->ncap == 0) {
  528. kfree(id);
  529. return -ENODEV;
  530. }
  531. if (nvme_nvm_ns_supported(ns, id) && ns->type != NVME_NS_LIGHTNVM) {
  532. if (nvme_nvm_register(ns->queue, disk->disk_name)) {
  533. dev_warn(disk_to_dev(ns->disk),
  534. "%s: LightNVM init failure\n", __func__);
  535. kfree(id);
  536. return -ENODEV;
  537. }
  538. ns->type = NVME_NS_LIGHTNVM;
  539. }
  540. if (ns->ctrl->vs >= NVME_VS(1, 1))
  541. memcpy(ns->eui, id->eui64, sizeof(ns->eui));
  542. if (ns->ctrl->vs >= NVME_VS(1, 2))
  543. memcpy(ns->uuid, id->nguid, sizeof(ns->uuid));
  544. old_ms = ns->ms;
  545. lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK;
  546. ns->lba_shift = id->lbaf[lbaf].ds;
  547. ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
  548. ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
  549. /*
  550. * If identify namespace failed, use default 512 byte block size so
  551. * block layer can use before failing read/write for 0 capacity.
  552. */
  553. if (ns->lba_shift == 0)
  554. ns->lba_shift = 9;
  555. bs = 1 << ns->lba_shift;
  556. /* XXX: PI implementation requires metadata equal t10 pi tuple size */
  557. pi_type = ns->ms == sizeof(struct t10_pi_tuple) ?
  558. id->dps & NVME_NS_DPS_PI_MASK : 0;
  559. blk_mq_freeze_queue(disk->queue);
  560. if (blk_get_integrity(disk) && (ns->pi_type != pi_type ||
  561. ns->ms != old_ms ||
  562. bs != queue_logical_block_size(disk->queue) ||
  563. (ns->ms && ns->ext)))
  564. blk_integrity_unregister(disk);
  565. ns->pi_type = pi_type;
  566. blk_queue_logical_block_size(ns->queue, bs);
  567. if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
  568. nvme_init_integrity(ns);
  569. if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
  570. set_capacity(disk, 0);
  571. else
  572. set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
  573. if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
  574. nvme_config_discard(ns);
  575. blk_mq_unfreeze_queue(disk->queue);
  576. kfree(id);
  577. return 0;
  578. }
  579. static char nvme_pr_type(enum pr_type type)
  580. {
  581. switch (type) {
  582. case PR_WRITE_EXCLUSIVE:
  583. return 1;
  584. case PR_EXCLUSIVE_ACCESS:
  585. return 2;
  586. case PR_WRITE_EXCLUSIVE_REG_ONLY:
  587. return 3;
  588. case PR_EXCLUSIVE_ACCESS_REG_ONLY:
  589. return 4;
  590. case PR_WRITE_EXCLUSIVE_ALL_REGS:
  591. return 5;
  592. case PR_EXCLUSIVE_ACCESS_ALL_REGS:
  593. return 6;
  594. default:
  595. return 0;
  596. }
  597. };
  598. static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
  599. u64 key, u64 sa_key, u8 op)
  600. {
  601. struct nvme_ns *ns = bdev->bd_disk->private_data;
  602. struct nvme_command c;
  603. u8 data[16] = { 0, };
  604. put_unaligned_le64(key, &data[0]);
  605. put_unaligned_le64(sa_key, &data[8]);
  606. memset(&c, 0, sizeof(c));
  607. c.common.opcode = op;
  608. c.common.nsid = cpu_to_le32(ns->ns_id);
  609. c.common.cdw10[0] = cpu_to_le32(cdw10);
  610. return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
  611. }
  612. static int nvme_pr_register(struct block_device *bdev, u64 old,
  613. u64 new, unsigned flags)
  614. {
  615. u32 cdw10;
  616. if (flags & ~PR_FL_IGNORE_KEY)
  617. return -EOPNOTSUPP;
  618. cdw10 = old ? 2 : 0;
  619. cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
  620. cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
  621. return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
  622. }
  623. static int nvme_pr_reserve(struct block_device *bdev, u64 key,
  624. enum pr_type type, unsigned flags)
  625. {
  626. u32 cdw10;
  627. if (flags & ~PR_FL_IGNORE_KEY)
  628. return -EOPNOTSUPP;
  629. cdw10 = nvme_pr_type(type) << 8;
  630. cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
  631. return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
  632. }
  633. static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
  634. enum pr_type type, bool abort)
  635. {
  636. u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
  637. return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
  638. }
  639. static int nvme_pr_clear(struct block_device *bdev, u64 key)
  640. {
  641. u32 cdw10 = 1 | (key ? 1 << 3 : 0);
  642. return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
  643. }
  644. static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
  645. {
  646. u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
  647. return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
  648. }
  649. static const struct pr_ops nvme_pr_ops = {
  650. .pr_register = nvme_pr_register,
  651. .pr_reserve = nvme_pr_reserve,
  652. .pr_release = nvme_pr_release,
  653. .pr_preempt = nvme_pr_preempt,
  654. .pr_clear = nvme_pr_clear,
  655. };
  656. static const struct block_device_operations nvme_fops = {
  657. .owner = THIS_MODULE,
  658. .ioctl = nvme_ioctl,
  659. .compat_ioctl = nvme_compat_ioctl,
  660. .open = nvme_open,
  661. .release = nvme_release,
  662. .getgeo = nvme_getgeo,
  663. .revalidate_disk= nvme_revalidate_disk,
  664. .pr_ops = &nvme_pr_ops,
  665. };
  666. static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
  667. {
  668. unsigned long timeout =
  669. ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
  670. u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
  671. int ret;
  672. while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
  673. if ((csts & NVME_CSTS_RDY) == bit)
  674. break;
  675. msleep(100);
  676. if (fatal_signal_pending(current))
  677. return -EINTR;
  678. if (time_after(jiffies, timeout)) {
  679. dev_err(ctrl->device,
  680. "Device not ready; aborting %s\n", enabled ?
  681. "initialisation" : "reset");
  682. return -ENODEV;
  683. }
  684. }
  685. return ret;
  686. }
  687. /*
  688. * If the device has been passed off to us in an enabled state, just clear
  689. * the enabled bit. The spec says we should set the 'shutdown notification
  690. * bits', but doing so may cause the device to complete commands to the
  691. * admin queue ... and we don't know what memory that might be pointing at!
  692. */
  693. int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
  694. {
  695. int ret;
  696. ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
  697. ctrl->ctrl_config &= ~NVME_CC_ENABLE;
  698. ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
  699. if (ret)
  700. return ret;
  701. return nvme_wait_ready(ctrl, cap, false);
  702. }
  703. EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
  704. int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
  705. {
  706. /*
  707. * Default to a 4K page size, with the intention to update this
  708. * path in the future to accomodate architectures with differing
  709. * kernel and IO page sizes.
  710. */
  711. unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
  712. int ret;
  713. if (page_shift < dev_page_min) {
  714. dev_err(ctrl->device,
  715. "Minimum device page size %u too large for host (%u)\n",
  716. 1 << dev_page_min, 1 << page_shift);
  717. return -ENODEV;
  718. }
  719. ctrl->page_size = 1 << page_shift;
  720. ctrl->ctrl_config = NVME_CC_CSS_NVM;
  721. ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
  722. ctrl->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
  723. ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
  724. ctrl->ctrl_config |= NVME_CC_ENABLE;
  725. ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
  726. if (ret)
  727. return ret;
  728. return nvme_wait_ready(ctrl, cap, true);
  729. }
  730. EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
  731. int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
  732. {
  733. unsigned long timeout = SHUTDOWN_TIMEOUT + jiffies;
  734. u32 csts;
  735. int ret;
  736. ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
  737. ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
  738. ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
  739. if (ret)
  740. return ret;
  741. while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
  742. if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
  743. break;
  744. msleep(100);
  745. if (fatal_signal_pending(current))
  746. return -EINTR;
  747. if (time_after(jiffies, timeout)) {
  748. dev_err(ctrl->device,
  749. "Device shutdown incomplete; abort shutdown\n");
  750. return -ENODEV;
  751. }
  752. }
  753. return ret;
  754. }
  755. EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
  756. static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
  757. struct request_queue *q)
  758. {
  759. if (ctrl->max_hw_sectors) {
  760. u32 max_segments =
  761. (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
  762. blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
  763. blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
  764. }
  765. if (ctrl->stripe_size)
  766. blk_queue_chunk_sectors(q, ctrl->stripe_size >> 9);
  767. if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
  768. blk_queue_flush(q, REQ_FLUSH | REQ_FUA);
  769. blk_queue_virt_boundary(q, ctrl->page_size - 1);
  770. }
  771. /*
  772. * Initialize the cached copies of the Identify data and various controller
  773. * register in our nvme_ctrl structure. This should be called as soon as
  774. * the admin queue is fully up and running.
  775. */
  776. int nvme_init_identify(struct nvme_ctrl *ctrl)
  777. {
  778. struct nvme_id_ctrl *id;
  779. u64 cap;
  780. int ret, page_shift;
  781. ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
  782. if (ret) {
  783. dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
  784. return ret;
  785. }
  786. ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
  787. if (ret) {
  788. dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
  789. return ret;
  790. }
  791. page_shift = NVME_CAP_MPSMIN(cap) + 12;
  792. if (ctrl->vs >= NVME_VS(1, 1))
  793. ctrl->subsystem = NVME_CAP_NSSRC(cap);
  794. ret = nvme_identify_ctrl(ctrl, &id);
  795. if (ret) {
  796. dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
  797. return -EIO;
  798. }
  799. ctrl->vid = le16_to_cpu(id->vid);
  800. ctrl->oncs = le16_to_cpup(&id->oncs);
  801. atomic_set(&ctrl->abort_limit, id->acl + 1);
  802. ctrl->vwc = id->vwc;
  803. ctrl->cntlid = le16_to_cpup(&id->cntlid);
  804. memcpy(ctrl->serial, id->sn, sizeof(id->sn));
  805. memcpy(ctrl->model, id->mn, sizeof(id->mn));
  806. memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
  807. if (id->mdts)
  808. ctrl->max_hw_sectors = 1 << (id->mdts + page_shift - 9);
  809. else
  810. ctrl->max_hw_sectors = UINT_MAX;
  811. if ((ctrl->quirks & NVME_QUIRK_STRIPE_SIZE) && id->vs[3]) {
  812. unsigned int max_hw_sectors;
  813. ctrl->stripe_size = 1 << (id->vs[3] + page_shift);
  814. max_hw_sectors = ctrl->stripe_size >> (page_shift - 9);
  815. if (ctrl->max_hw_sectors) {
  816. ctrl->max_hw_sectors = min(max_hw_sectors,
  817. ctrl->max_hw_sectors);
  818. } else {
  819. ctrl->max_hw_sectors = max_hw_sectors;
  820. }
  821. }
  822. nvme_set_queue_limits(ctrl, ctrl->admin_q);
  823. kfree(id);
  824. return 0;
  825. }
  826. EXPORT_SYMBOL_GPL(nvme_init_identify);
  827. static int nvme_dev_open(struct inode *inode, struct file *file)
  828. {
  829. struct nvme_ctrl *ctrl;
  830. int instance = iminor(inode);
  831. int ret = -ENODEV;
  832. spin_lock(&dev_list_lock);
  833. list_for_each_entry(ctrl, &nvme_ctrl_list, node) {
  834. if (ctrl->instance != instance)
  835. continue;
  836. if (!ctrl->admin_q) {
  837. ret = -EWOULDBLOCK;
  838. break;
  839. }
  840. if (!kref_get_unless_zero(&ctrl->kref))
  841. break;
  842. file->private_data = ctrl;
  843. ret = 0;
  844. break;
  845. }
  846. spin_unlock(&dev_list_lock);
  847. return ret;
  848. }
  849. static int nvme_dev_release(struct inode *inode, struct file *file)
  850. {
  851. nvme_put_ctrl(file->private_data);
  852. return 0;
  853. }
  854. static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
  855. {
  856. struct nvme_ns *ns;
  857. int ret;
  858. mutex_lock(&ctrl->namespaces_mutex);
  859. if (list_empty(&ctrl->namespaces)) {
  860. ret = -ENOTTY;
  861. goto out_unlock;
  862. }
  863. ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
  864. if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
  865. dev_warn(ctrl->device,
  866. "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
  867. ret = -EINVAL;
  868. goto out_unlock;
  869. }
  870. dev_warn(ctrl->device,
  871. "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
  872. kref_get(&ns->kref);
  873. mutex_unlock(&ctrl->namespaces_mutex);
  874. ret = nvme_user_cmd(ctrl, ns, argp);
  875. nvme_put_ns(ns);
  876. return ret;
  877. out_unlock:
  878. mutex_unlock(&ctrl->namespaces_mutex);
  879. return ret;
  880. }
  881. static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
  882. unsigned long arg)
  883. {
  884. struct nvme_ctrl *ctrl = file->private_data;
  885. void __user *argp = (void __user *)arg;
  886. switch (cmd) {
  887. case NVME_IOCTL_ADMIN_CMD:
  888. return nvme_user_cmd(ctrl, NULL, argp);
  889. case NVME_IOCTL_IO_CMD:
  890. return nvme_dev_user_cmd(ctrl, argp);
  891. case NVME_IOCTL_RESET:
  892. dev_warn(ctrl->device, "resetting controller\n");
  893. return ctrl->ops->reset_ctrl(ctrl);
  894. case NVME_IOCTL_SUBSYS_RESET:
  895. return nvme_reset_subsystem(ctrl);
  896. default:
  897. return -ENOTTY;
  898. }
  899. }
  900. static const struct file_operations nvme_dev_fops = {
  901. .owner = THIS_MODULE,
  902. .open = nvme_dev_open,
  903. .release = nvme_dev_release,
  904. .unlocked_ioctl = nvme_dev_ioctl,
  905. .compat_ioctl = nvme_dev_ioctl,
  906. };
  907. static ssize_t nvme_sysfs_reset(struct device *dev,
  908. struct device_attribute *attr, const char *buf,
  909. size_t count)
  910. {
  911. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  912. int ret;
  913. ret = ctrl->ops->reset_ctrl(ctrl);
  914. if (ret < 0)
  915. return ret;
  916. return count;
  917. }
  918. static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
  919. static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
  920. char *buf)
  921. {
  922. struct nvme_ns *ns = dev_to_disk(dev)->private_data;
  923. struct nvme_ctrl *ctrl = ns->ctrl;
  924. int serial_len = sizeof(ctrl->serial);
  925. int model_len = sizeof(ctrl->model);
  926. if (memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
  927. return sprintf(buf, "eui.%16phN\n", ns->uuid);
  928. if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
  929. return sprintf(buf, "eui.%8phN\n", ns->eui);
  930. while (ctrl->serial[serial_len - 1] == ' ')
  931. serial_len--;
  932. while (ctrl->model[model_len - 1] == ' ')
  933. model_len--;
  934. return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
  935. serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
  936. }
  937. static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
  938. static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
  939. char *buf)
  940. {
  941. struct nvme_ns *ns = dev_to_disk(dev)->private_data;
  942. return sprintf(buf, "%pU\n", ns->uuid);
  943. }
  944. static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
  945. static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
  946. char *buf)
  947. {
  948. struct nvme_ns *ns = dev_to_disk(dev)->private_data;
  949. return sprintf(buf, "%8phd\n", ns->eui);
  950. }
  951. static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
  952. static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
  953. char *buf)
  954. {
  955. struct nvme_ns *ns = dev_to_disk(dev)->private_data;
  956. return sprintf(buf, "%d\n", ns->ns_id);
  957. }
  958. static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
  959. static struct attribute *nvme_ns_attrs[] = {
  960. &dev_attr_wwid.attr,
  961. &dev_attr_uuid.attr,
  962. &dev_attr_eui.attr,
  963. &dev_attr_nsid.attr,
  964. NULL,
  965. };
  966. static umode_t nvme_attrs_are_visible(struct kobject *kobj,
  967. struct attribute *a, int n)
  968. {
  969. struct device *dev = container_of(kobj, struct device, kobj);
  970. struct nvme_ns *ns = dev_to_disk(dev)->private_data;
  971. if (a == &dev_attr_uuid.attr) {
  972. if (!memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
  973. return 0;
  974. }
  975. if (a == &dev_attr_eui.attr) {
  976. if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
  977. return 0;
  978. }
  979. return a->mode;
  980. }
  981. static const struct attribute_group nvme_ns_attr_group = {
  982. .attrs = nvme_ns_attrs,
  983. .is_visible = nvme_attrs_are_visible,
  984. };
  985. #define nvme_show_str_function(field) \
  986. static ssize_t field##_show(struct device *dev, \
  987. struct device_attribute *attr, char *buf) \
  988. { \
  989. struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
  990. return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
  991. } \
  992. static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
  993. #define nvme_show_int_function(field) \
  994. static ssize_t field##_show(struct device *dev, \
  995. struct device_attribute *attr, char *buf) \
  996. { \
  997. struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
  998. return sprintf(buf, "%d\n", ctrl->field); \
  999. } \
  1000. static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
  1001. nvme_show_str_function(model);
  1002. nvme_show_str_function(serial);
  1003. nvme_show_str_function(firmware_rev);
  1004. nvme_show_int_function(cntlid);
  1005. static struct attribute *nvme_dev_attrs[] = {
  1006. &dev_attr_reset_controller.attr,
  1007. &dev_attr_model.attr,
  1008. &dev_attr_serial.attr,
  1009. &dev_attr_firmware_rev.attr,
  1010. &dev_attr_cntlid.attr,
  1011. NULL
  1012. };
  1013. static struct attribute_group nvme_dev_attrs_group = {
  1014. .attrs = nvme_dev_attrs,
  1015. };
  1016. static const struct attribute_group *nvme_dev_attr_groups[] = {
  1017. &nvme_dev_attrs_group,
  1018. NULL,
  1019. };
  1020. static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
  1021. {
  1022. struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
  1023. struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
  1024. return nsa->ns_id - nsb->ns_id;
  1025. }
  1026. static struct nvme_ns *nvme_find_ns(struct nvme_ctrl *ctrl, unsigned nsid)
  1027. {
  1028. struct nvme_ns *ns;
  1029. lockdep_assert_held(&ctrl->namespaces_mutex);
  1030. list_for_each_entry(ns, &ctrl->namespaces, list) {
  1031. if (ns->ns_id == nsid)
  1032. return ns;
  1033. if (ns->ns_id > nsid)
  1034. break;
  1035. }
  1036. return NULL;
  1037. }
  1038. static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
  1039. {
  1040. struct nvme_ns *ns;
  1041. struct gendisk *disk;
  1042. int node = dev_to_node(ctrl->dev);
  1043. lockdep_assert_held(&ctrl->namespaces_mutex);
  1044. ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
  1045. if (!ns)
  1046. return;
  1047. ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
  1048. if (ns->instance < 0)
  1049. goto out_free_ns;
  1050. ns->queue = blk_mq_init_queue(ctrl->tagset);
  1051. if (IS_ERR(ns->queue))
  1052. goto out_release_instance;
  1053. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
  1054. ns->queue->queuedata = ns;
  1055. ns->ctrl = ctrl;
  1056. disk = alloc_disk_node(0, node);
  1057. if (!disk)
  1058. goto out_free_queue;
  1059. kref_init(&ns->kref);
  1060. ns->ns_id = nsid;
  1061. ns->disk = disk;
  1062. ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
  1063. blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
  1064. nvme_set_queue_limits(ctrl, ns->queue);
  1065. disk->major = nvme_major;
  1066. disk->first_minor = 0;
  1067. disk->fops = &nvme_fops;
  1068. disk->private_data = ns;
  1069. disk->queue = ns->queue;
  1070. disk->driverfs_dev = ctrl->device;
  1071. disk->flags = GENHD_FL_EXT_DEVT;
  1072. sprintf(disk->disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
  1073. if (nvme_revalidate_disk(ns->disk))
  1074. goto out_free_disk;
  1075. list_add_tail(&ns->list, &ctrl->namespaces);
  1076. kref_get(&ctrl->kref);
  1077. if (ns->type == NVME_NS_LIGHTNVM)
  1078. return;
  1079. add_disk(ns->disk);
  1080. if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
  1081. &nvme_ns_attr_group))
  1082. pr_warn("%s: failed to create sysfs group for identification\n",
  1083. ns->disk->disk_name);
  1084. return;
  1085. out_free_disk:
  1086. kfree(disk);
  1087. out_free_queue:
  1088. blk_cleanup_queue(ns->queue);
  1089. out_release_instance:
  1090. ida_simple_remove(&ctrl->ns_ida, ns->instance);
  1091. out_free_ns:
  1092. kfree(ns);
  1093. }
  1094. static void nvme_ns_remove(struct nvme_ns *ns)
  1095. {
  1096. if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
  1097. return;
  1098. if (ns->disk->flags & GENHD_FL_UP) {
  1099. if (blk_get_integrity(ns->disk))
  1100. blk_integrity_unregister(ns->disk);
  1101. sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
  1102. &nvme_ns_attr_group);
  1103. del_gendisk(ns->disk);
  1104. blk_mq_abort_requeue_list(ns->queue);
  1105. blk_cleanup_queue(ns->queue);
  1106. }
  1107. mutex_lock(&ns->ctrl->namespaces_mutex);
  1108. list_del_init(&ns->list);
  1109. mutex_unlock(&ns->ctrl->namespaces_mutex);
  1110. nvme_put_ns(ns);
  1111. }
  1112. static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
  1113. {
  1114. struct nvme_ns *ns;
  1115. ns = nvme_find_ns(ctrl, nsid);
  1116. if (ns) {
  1117. if (revalidate_disk(ns->disk))
  1118. nvme_ns_remove(ns);
  1119. } else
  1120. nvme_alloc_ns(ctrl, nsid);
  1121. }
  1122. static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
  1123. {
  1124. struct nvme_ns *ns;
  1125. __le32 *ns_list;
  1126. unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
  1127. int ret = 0;
  1128. ns_list = kzalloc(0x1000, GFP_KERNEL);
  1129. if (!ns_list)
  1130. return -ENOMEM;
  1131. for (i = 0; i < num_lists; i++) {
  1132. ret = nvme_identify_ns_list(ctrl, prev, ns_list);
  1133. if (ret)
  1134. goto out;
  1135. for (j = 0; j < min(nn, 1024U); j++) {
  1136. nsid = le32_to_cpu(ns_list[j]);
  1137. if (!nsid)
  1138. goto out;
  1139. nvme_validate_ns(ctrl, nsid);
  1140. while (++prev < nsid) {
  1141. ns = nvme_find_ns(ctrl, prev);
  1142. if (ns)
  1143. nvme_ns_remove(ns);
  1144. }
  1145. }
  1146. nn -= j;
  1147. }
  1148. out:
  1149. kfree(ns_list);
  1150. return ret;
  1151. }
  1152. static void __nvme_scan_namespaces(struct nvme_ctrl *ctrl, unsigned nn)
  1153. {
  1154. struct nvme_ns *ns, *next;
  1155. unsigned i;
  1156. lockdep_assert_held(&ctrl->namespaces_mutex);
  1157. for (i = 1; i <= nn; i++)
  1158. nvme_validate_ns(ctrl, i);
  1159. list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
  1160. if (ns->ns_id > nn)
  1161. nvme_ns_remove(ns);
  1162. }
  1163. }
  1164. void nvme_scan_namespaces(struct nvme_ctrl *ctrl)
  1165. {
  1166. struct nvme_id_ctrl *id;
  1167. unsigned nn;
  1168. if (nvme_identify_ctrl(ctrl, &id))
  1169. return;
  1170. mutex_lock(&ctrl->namespaces_mutex);
  1171. nn = le32_to_cpu(id->nn);
  1172. if (ctrl->vs >= NVME_VS(1, 1) &&
  1173. !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
  1174. if (!nvme_scan_ns_list(ctrl, nn))
  1175. goto done;
  1176. }
  1177. __nvme_scan_namespaces(ctrl, le32_to_cpup(&id->nn));
  1178. done:
  1179. list_sort(NULL, &ctrl->namespaces, ns_cmp);
  1180. mutex_unlock(&ctrl->namespaces_mutex);
  1181. kfree(id);
  1182. }
  1183. EXPORT_SYMBOL_GPL(nvme_scan_namespaces);
  1184. void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
  1185. {
  1186. struct nvme_ns *ns, *next;
  1187. list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
  1188. nvme_ns_remove(ns);
  1189. }
  1190. EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
  1191. static DEFINE_IDA(nvme_instance_ida);
  1192. static int nvme_set_instance(struct nvme_ctrl *ctrl)
  1193. {
  1194. int instance, error;
  1195. do {
  1196. if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
  1197. return -ENODEV;
  1198. spin_lock(&dev_list_lock);
  1199. error = ida_get_new(&nvme_instance_ida, &instance);
  1200. spin_unlock(&dev_list_lock);
  1201. } while (error == -EAGAIN);
  1202. if (error)
  1203. return -ENODEV;
  1204. ctrl->instance = instance;
  1205. return 0;
  1206. }
  1207. static void nvme_release_instance(struct nvme_ctrl *ctrl)
  1208. {
  1209. spin_lock(&dev_list_lock);
  1210. ida_remove(&nvme_instance_ida, ctrl->instance);
  1211. spin_unlock(&dev_list_lock);
  1212. }
  1213. void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
  1214. {
  1215. device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
  1216. spin_lock(&dev_list_lock);
  1217. list_del(&ctrl->node);
  1218. spin_unlock(&dev_list_lock);
  1219. }
  1220. EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
  1221. static void nvme_free_ctrl(struct kref *kref)
  1222. {
  1223. struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref);
  1224. put_device(ctrl->device);
  1225. nvme_release_instance(ctrl);
  1226. ida_destroy(&ctrl->ns_ida);
  1227. ctrl->ops->free_ctrl(ctrl);
  1228. }
  1229. void nvme_put_ctrl(struct nvme_ctrl *ctrl)
  1230. {
  1231. kref_put(&ctrl->kref, nvme_free_ctrl);
  1232. }
  1233. EXPORT_SYMBOL_GPL(nvme_put_ctrl);
  1234. /*
  1235. * Initialize a NVMe controller structures. This needs to be called during
  1236. * earliest initialization so that we have the initialized structured around
  1237. * during probing.
  1238. */
  1239. int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
  1240. const struct nvme_ctrl_ops *ops, unsigned long quirks)
  1241. {
  1242. int ret;
  1243. INIT_LIST_HEAD(&ctrl->namespaces);
  1244. mutex_init(&ctrl->namespaces_mutex);
  1245. kref_init(&ctrl->kref);
  1246. ctrl->dev = dev;
  1247. ctrl->ops = ops;
  1248. ctrl->quirks = quirks;
  1249. ret = nvme_set_instance(ctrl);
  1250. if (ret)
  1251. goto out;
  1252. ctrl->device = device_create_with_groups(nvme_class, ctrl->dev,
  1253. MKDEV(nvme_char_major, ctrl->instance),
  1254. ctrl, nvme_dev_attr_groups,
  1255. "nvme%d", ctrl->instance);
  1256. if (IS_ERR(ctrl->device)) {
  1257. ret = PTR_ERR(ctrl->device);
  1258. goto out_release_instance;
  1259. }
  1260. get_device(ctrl->device);
  1261. ida_init(&ctrl->ns_ida);
  1262. spin_lock(&dev_list_lock);
  1263. list_add_tail(&ctrl->node, &nvme_ctrl_list);
  1264. spin_unlock(&dev_list_lock);
  1265. return 0;
  1266. out_release_instance:
  1267. nvme_release_instance(ctrl);
  1268. out:
  1269. return ret;
  1270. }
  1271. EXPORT_SYMBOL_GPL(nvme_init_ctrl);
  1272. /**
  1273. * nvme_kill_queues(): Ends all namespace queues
  1274. * @ctrl: the dead controller that needs to end
  1275. *
  1276. * Call this function when the driver determines it is unable to get the
  1277. * controller in a state capable of servicing IO.
  1278. */
  1279. void nvme_kill_queues(struct nvme_ctrl *ctrl)
  1280. {
  1281. struct nvme_ns *ns;
  1282. mutex_lock(&ctrl->namespaces_mutex);
  1283. list_for_each_entry(ns, &ctrl->namespaces, list) {
  1284. if (!kref_get_unless_zero(&ns->kref))
  1285. continue;
  1286. /*
  1287. * Revalidating a dead namespace sets capacity to 0. This will
  1288. * end buffered writers dirtying pages that can't be synced.
  1289. */
  1290. if (!test_and_set_bit(NVME_NS_DEAD, &ns->flags))
  1291. revalidate_disk(ns->disk);
  1292. blk_set_queue_dying(ns->queue);
  1293. blk_mq_abort_requeue_list(ns->queue);
  1294. blk_mq_start_stopped_hw_queues(ns->queue, true);
  1295. nvme_put_ns(ns);
  1296. }
  1297. mutex_unlock(&ctrl->namespaces_mutex);
  1298. }
  1299. EXPORT_SYMBOL_GPL(nvme_kill_queues);
  1300. void nvme_stop_queues(struct nvme_ctrl *ctrl)
  1301. {
  1302. struct nvme_ns *ns;
  1303. mutex_lock(&ctrl->namespaces_mutex);
  1304. list_for_each_entry(ns, &ctrl->namespaces, list) {
  1305. spin_lock_irq(ns->queue->queue_lock);
  1306. queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
  1307. spin_unlock_irq(ns->queue->queue_lock);
  1308. blk_mq_cancel_requeue_work(ns->queue);
  1309. blk_mq_stop_hw_queues(ns->queue);
  1310. }
  1311. mutex_unlock(&ctrl->namespaces_mutex);
  1312. }
  1313. EXPORT_SYMBOL_GPL(nvme_stop_queues);
  1314. void nvme_start_queues(struct nvme_ctrl *ctrl)
  1315. {
  1316. struct nvme_ns *ns;
  1317. mutex_lock(&ctrl->namespaces_mutex);
  1318. list_for_each_entry(ns, &ctrl->namespaces, list) {
  1319. queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
  1320. blk_mq_start_stopped_hw_queues(ns->queue, true);
  1321. blk_mq_kick_requeue_list(ns->queue);
  1322. }
  1323. mutex_unlock(&ctrl->namespaces_mutex);
  1324. }
  1325. EXPORT_SYMBOL_GPL(nvme_start_queues);
  1326. int __init nvme_core_init(void)
  1327. {
  1328. int result;
  1329. result = register_blkdev(nvme_major, "nvme");
  1330. if (result < 0)
  1331. return result;
  1332. else if (result > 0)
  1333. nvme_major = result;
  1334. result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
  1335. &nvme_dev_fops);
  1336. if (result < 0)
  1337. goto unregister_blkdev;
  1338. else if (result > 0)
  1339. nvme_char_major = result;
  1340. nvme_class = class_create(THIS_MODULE, "nvme");
  1341. if (IS_ERR(nvme_class)) {
  1342. result = PTR_ERR(nvme_class);
  1343. goto unregister_chrdev;
  1344. }
  1345. return 0;
  1346. unregister_chrdev:
  1347. __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
  1348. unregister_blkdev:
  1349. unregister_blkdev(nvme_major, "nvme");
  1350. return result;
  1351. }
  1352. void nvme_core_exit(void)
  1353. {
  1354. unregister_blkdev(nvme_major, "nvme");
  1355. class_destroy(nvme_class);
  1356. __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
  1357. }
  1358. MODULE_LICENSE("GPL");
  1359. MODULE_VERSION("1.0");
  1360. module_init(nvme_core_init);
  1361. module_exit(nvme_core_exit);