core.c 36 KB

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