core.c 37 KB

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