core.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137
  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. #include "fabrics.h"
  32. #define NVME_MINORS (1U << MINORBITS)
  33. unsigned char admin_timeout = 60;
  34. module_param(admin_timeout, byte, 0644);
  35. MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
  36. EXPORT_SYMBOL_GPL(admin_timeout);
  37. unsigned char nvme_io_timeout = 30;
  38. module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
  39. MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
  40. EXPORT_SYMBOL_GPL(nvme_io_timeout);
  41. unsigned char shutdown_timeout = 5;
  42. module_param(shutdown_timeout, byte, 0644);
  43. MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
  44. unsigned int nvme_max_retries = 5;
  45. module_param_named(max_retries, nvme_max_retries, uint, 0644);
  46. MODULE_PARM_DESC(max_retries, "max number of retries a command may have");
  47. EXPORT_SYMBOL_GPL(nvme_max_retries);
  48. static int nvme_char_major;
  49. module_param(nvme_char_major, int, 0);
  50. static LIST_HEAD(nvme_ctrl_list);
  51. static DEFINE_SPINLOCK(dev_list_lock);
  52. static struct class *nvme_class;
  53. void nvme_cancel_request(struct request *req, void *data, bool reserved)
  54. {
  55. int status;
  56. if (!blk_mq_request_started(req))
  57. return;
  58. dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
  59. "Cancelling I/O %d", req->tag);
  60. status = NVME_SC_ABORT_REQ;
  61. if (blk_queue_dying(req->q))
  62. status |= NVME_SC_DNR;
  63. blk_mq_complete_request(req, status);
  64. }
  65. EXPORT_SYMBOL_GPL(nvme_cancel_request);
  66. bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
  67. enum nvme_ctrl_state new_state)
  68. {
  69. enum nvme_ctrl_state old_state;
  70. bool changed = false;
  71. spin_lock_irq(&ctrl->lock);
  72. old_state = ctrl->state;
  73. switch (new_state) {
  74. case NVME_CTRL_LIVE:
  75. switch (old_state) {
  76. case NVME_CTRL_NEW:
  77. case NVME_CTRL_RESETTING:
  78. case NVME_CTRL_RECONNECTING:
  79. changed = true;
  80. /* FALLTHRU */
  81. default:
  82. break;
  83. }
  84. break;
  85. case NVME_CTRL_RESETTING:
  86. switch (old_state) {
  87. case NVME_CTRL_NEW:
  88. case NVME_CTRL_LIVE:
  89. case NVME_CTRL_RECONNECTING:
  90. changed = true;
  91. /* FALLTHRU */
  92. default:
  93. break;
  94. }
  95. break;
  96. case NVME_CTRL_RECONNECTING:
  97. switch (old_state) {
  98. case NVME_CTRL_LIVE:
  99. changed = true;
  100. /* FALLTHRU */
  101. default:
  102. break;
  103. }
  104. break;
  105. case NVME_CTRL_DELETING:
  106. switch (old_state) {
  107. case NVME_CTRL_LIVE:
  108. case NVME_CTRL_RESETTING:
  109. case NVME_CTRL_RECONNECTING:
  110. changed = true;
  111. /* FALLTHRU */
  112. default:
  113. break;
  114. }
  115. break;
  116. case NVME_CTRL_DEAD:
  117. switch (old_state) {
  118. case NVME_CTRL_DELETING:
  119. changed = true;
  120. /* FALLTHRU */
  121. default:
  122. break;
  123. }
  124. break;
  125. default:
  126. break;
  127. }
  128. if (changed)
  129. ctrl->state = new_state;
  130. spin_unlock_irq(&ctrl->lock);
  131. return changed;
  132. }
  133. EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
  134. static void nvme_free_ns(struct kref *kref)
  135. {
  136. struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
  137. if (ns->ndev)
  138. nvme_nvm_unregister(ns);
  139. if (ns->disk) {
  140. spin_lock(&dev_list_lock);
  141. ns->disk->private_data = NULL;
  142. spin_unlock(&dev_list_lock);
  143. }
  144. put_disk(ns->disk);
  145. ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
  146. nvme_put_ctrl(ns->ctrl);
  147. kfree(ns);
  148. }
  149. static void nvme_put_ns(struct nvme_ns *ns)
  150. {
  151. kref_put(&ns->kref, nvme_free_ns);
  152. }
  153. static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk)
  154. {
  155. struct nvme_ns *ns;
  156. spin_lock(&dev_list_lock);
  157. ns = disk->private_data;
  158. if (ns) {
  159. if (!kref_get_unless_zero(&ns->kref))
  160. goto fail;
  161. if (!try_module_get(ns->ctrl->ops->module))
  162. goto fail_put_ns;
  163. }
  164. spin_unlock(&dev_list_lock);
  165. return ns;
  166. fail_put_ns:
  167. kref_put(&ns->kref, nvme_free_ns);
  168. fail:
  169. spin_unlock(&dev_list_lock);
  170. return NULL;
  171. }
  172. void nvme_requeue_req(struct request *req)
  173. {
  174. unsigned long flags;
  175. blk_mq_requeue_request(req);
  176. spin_lock_irqsave(req->q->queue_lock, flags);
  177. if (!blk_queue_stopped(req->q))
  178. blk_mq_kick_requeue_list(req->q);
  179. spin_unlock_irqrestore(req->q->queue_lock, flags);
  180. }
  181. EXPORT_SYMBOL_GPL(nvme_requeue_req);
  182. struct request *nvme_alloc_request(struct request_queue *q,
  183. struct nvme_command *cmd, unsigned int flags, int qid)
  184. {
  185. struct request *req;
  186. if (qid == NVME_QID_ANY) {
  187. req = blk_mq_alloc_request(q, nvme_is_write(cmd), flags);
  188. } else {
  189. req = blk_mq_alloc_request_hctx(q, nvme_is_write(cmd), flags,
  190. qid ? qid - 1 : 0);
  191. }
  192. if (IS_ERR(req))
  193. return req;
  194. req->cmd_type = REQ_TYPE_DRV_PRIV;
  195. req->cmd_flags |= REQ_FAILFAST_DRIVER;
  196. req->cmd = (unsigned char *)cmd;
  197. req->cmd_len = sizeof(struct nvme_command);
  198. return req;
  199. }
  200. EXPORT_SYMBOL_GPL(nvme_alloc_request);
  201. static inline void nvme_setup_flush(struct nvme_ns *ns,
  202. struct nvme_command *cmnd)
  203. {
  204. memset(cmnd, 0, sizeof(*cmnd));
  205. cmnd->common.opcode = nvme_cmd_flush;
  206. cmnd->common.nsid = cpu_to_le32(ns->ns_id);
  207. }
  208. static inline int nvme_setup_discard(struct nvme_ns *ns, struct request *req,
  209. struct nvme_command *cmnd)
  210. {
  211. struct nvme_dsm_range *range;
  212. struct page *page;
  213. int offset;
  214. unsigned int nr_bytes = blk_rq_bytes(req);
  215. range = kmalloc(sizeof(*range), GFP_ATOMIC);
  216. if (!range)
  217. return BLK_MQ_RQ_QUEUE_BUSY;
  218. range->cattr = cpu_to_le32(0);
  219. range->nlb = cpu_to_le32(nr_bytes >> ns->lba_shift);
  220. range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
  221. memset(cmnd, 0, sizeof(*cmnd));
  222. cmnd->dsm.opcode = nvme_cmd_dsm;
  223. cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
  224. cmnd->dsm.nr = 0;
  225. cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
  226. req->completion_data = range;
  227. page = virt_to_page(range);
  228. offset = offset_in_page(range);
  229. blk_add_request_payload(req, page, offset, sizeof(*range));
  230. /*
  231. * we set __data_len back to the size of the area to be discarded
  232. * on disk. This allows us to report completion on the full amount
  233. * of blocks described by the request.
  234. */
  235. req->__data_len = nr_bytes;
  236. return 0;
  237. }
  238. static inline void nvme_setup_rw(struct nvme_ns *ns, struct request *req,
  239. struct nvme_command *cmnd)
  240. {
  241. u16 control = 0;
  242. u32 dsmgmt = 0;
  243. if (req->cmd_flags & REQ_FUA)
  244. control |= NVME_RW_FUA;
  245. if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
  246. control |= NVME_RW_LR;
  247. if (req->cmd_flags & REQ_RAHEAD)
  248. dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
  249. memset(cmnd, 0, sizeof(*cmnd));
  250. cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
  251. cmnd->rw.command_id = req->tag;
  252. cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
  253. cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
  254. cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
  255. if (ns->ms) {
  256. switch (ns->pi_type) {
  257. case NVME_NS_DPS_PI_TYPE3:
  258. control |= NVME_RW_PRINFO_PRCHK_GUARD;
  259. break;
  260. case NVME_NS_DPS_PI_TYPE1:
  261. case NVME_NS_DPS_PI_TYPE2:
  262. control |= NVME_RW_PRINFO_PRCHK_GUARD |
  263. NVME_RW_PRINFO_PRCHK_REF;
  264. cmnd->rw.reftag = cpu_to_le32(
  265. nvme_block_nr(ns, blk_rq_pos(req)));
  266. break;
  267. }
  268. if (!blk_integrity_rq(req))
  269. control |= NVME_RW_PRINFO_PRACT;
  270. }
  271. cmnd->rw.control = cpu_to_le16(control);
  272. cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
  273. }
  274. int nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
  275. struct nvme_command *cmd)
  276. {
  277. int ret = 0;
  278. if (req->cmd_type == REQ_TYPE_DRV_PRIV)
  279. memcpy(cmd, req->cmd, sizeof(*cmd));
  280. else if (req_op(req) == REQ_OP_FLUSH)
  281. nvme_setup_flush(ns, cmd);
  282. else if (req_op(req) == REQ_OP_DISCARD)
  283. ret = nvme_setup_discard(ns, req, cmd);
  284. else
  285. nvme_setup_rw(ns, req, cmd);
  286. return ret;
  287. }
  288. EXPORT_SYMBOL_GPL(nvme_setup_cmd);
  289. /*
  290. * Returns 0 on success. If the result is negative, it's a Linux error code;
  291. * if the result is positive, it's an NVM Express status code
  292. */
  293. int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  294. struct nvme_completion *cqe, void *buffer, unsigned bufflen,
  295. unsigned timeout, int qid, int at_head, int flags)
  296. {
  297. struct request *req;
  298. int ret;
  299. req = nvme_alloc_request(q, cmd, flags, qid);
  300. if (IS_ERR(req))
  301. return PTR_ERR(req);
  302. req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
  303. req->special = cqe;
  304. if (buffer && bufflen) {
  305. ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
  306. if (ret)
  307. goto out;
  308. }
  309. blk_execute_rq(req->q, NULL, req, at_head);
  310. ret = req->errors;
  311. out:
  312. blk_mq_free_request(req);
  313. return ret;
  314. }
  315. EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
  316. int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  317. void *buffer, unsigned bufflen)
  318. {
  319. return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
  320. NVME_QID_ANY, 0, 0);
  321. }
  322. EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
  323. int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
  324. void __user *ubuffer, unsigned bufflen,
  325. void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
  326. u32 *result, unsigned timeout)
  327. {
  328. bool write = nvme_is_write(cmd);
  329. struct nvme_completion cqe;
  330. struct nvme_ns *ns = q->queuedata;
  331. struct gendisk *disk = ns ? ns->disk : NULL;
  332. struct request *req;
  333. struct bio *bio = NULL;
  334. void *meta = NULL;
  335. int ret;
  336. req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
  337. if (IS_ERR(req))
  338. return PTR_ERR(req);
  339. req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
  340. req->special = &cqe;
  341. if (ubuffer && bufflen) {
  342. ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
  343. GFP_KERNEL);
  344. if (ret)
  345. goto out;
  346. bio = req->bio;
  347. if (!disk)
  348. goto submit;
  349. bio->bi_bdev = bdget_disk(disk, 0);
  350. if (!bio->bi_bdev) {
  351. ret = -ENODEV;
  352. goto out_unmap;
  353. }
  354. if (meta_buffer && meta_len) {
  355. struct bio_integrity_payload *bip;
  356. meta = kmalloc(meta_len, GFP_KERNEL);
  357. if (!meta) {
  358. ret = -ENOMEM;
  359. goto out_unmap;
  360. }
  361. if (write) {
  362. if (copy_from_user(meta, meta_buffer,
  363. meta_len)) {
  364. ret = -EFAULT;
  365. goto out_free_meta;
  366. }
  367. }
  368. bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
  369. if (IS_ERR(bip)) {
  370. ret = PTR_ERR(bip);
  371. goto out_free_meta;
  372. }
  373. bip->bip_iter.bi_size = meta_len;
  374. bip->bip_iter.bi_sector = meta_seed;
  375. ret = bio_integrity_add_page(bio, virt_to_page(meta),
  376. meta_len, offset_in_page(meta));
  377. if (ret != meta_len) {
  378. ret = -ENOMEM;
  379. goto out_free_meta;
  380. }
  381. }
  382. }
  383. submit:
  384. blk_execute_rq(req->q, disk, req, 0);
  385. ret = req->errors;
  386. if (result)
  387. *result = le32_to_cpu(cqe.result);
  388. if (meta && !ret && !write) {
  389. if (copy_to_user(meta_buffer, meta, meta_len))
  390. ret = -EFAULT;
  391. }
  392. out_free_meta:
  393. kfree(meta);
  394. out_unmap:
  395. if (bio) {
  396. if (disk && bio->bi_bdev)
  397. bdput(bio->bi_bdev);
  398. blk_rq_unmap_user(bio);
  399. }
  400. out:
  401. blk_mq_free_request(req);
  402. return ret;
  403. }
  404. int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
  405. void __user *ubuffer, unsigned bufflen, u32 *result,
  406. unsigned timeout)
  407. {
  408. return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0,
  409. result, timeout);
  410. }
  411. static void nvme_keep_alive_end_io(struct request *rq, int error)
  412. {
  413. struct nvme_ctrl *ctrl = rq->end_io_data;
  414. blk_mq_free_request(rq);
  415. if (error) {
  416. dev_err(ctrl->device,
  417. "failed nvme_keep_alive_end_io error=%d\n", error);
  418. return;
  419. }
  420. schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
  421. }
  422. static int nvme_keep_alive(struct nvme_ctrl *ctrl)
  423. {
  424. struct nvme_command c;
  425. struct request *rq;
  426. memset(&c, 0, sizeof(c));
  427. c.common.opcode = nvme_admin_keep_alive;
  428. rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
  429. NVME_QID_ANY);
  430. if (IS_ERR(rq))
  431. return PTR_ERR(rq);
  432. rq->timeout = ctrl->kato * HZ;
  433. rq->end_io_data = ctrl;
  434. blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
  435. return 0;
  436. }
  437. static void nvme_keep_alive_work(struct work_struct *work)
  438. {
  439. struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
  440. struct nvme_ctrl, ka_work);
  441. if (nvme_keep_alive(ctrl)) {
  442. /* allocation failure, reset the controller */
  443. dev_err(ctrl->device, "keep-alive failed\n");
  444. ctrl->ops->reset_ctrl(ctrl);
  445. return;
  446. }
  447. }
  448. void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
  449. {
  450. if (unlikely(ctrl->kato == 0))
  451. return;
  452. INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
  453. schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
  454. }
  455. EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
  456. void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
  457. {
  458. if (unlikely(ctrl->kato == 0))
  459. return;
  460. cancel_delayed_work_sync(&ctrl->ka_work);
  461. }
  462. EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
  463. int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
  464. {
  465. struct nvme_command c = { };
  466. int error;
  467. /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
  468. c.identify.opcode = nvme_admin_identify;
  469. c.identify.cns = cpu_to_le32(NVME_ID_CNS_CTRL);
  470. *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
  471. if (!*id)
  472. return -ENOMEM;
  473. error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
  474. sizeof(struct nvme_id_ctrl));
  475. if (error)
  476. kfree(*id);
  477. return error;
  478. }
  479. static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
  480. {
  481. struct nvme_command c = { };
  482. c.identify.opcode = nvme_admin_identify;
  483. c.identify.cns = cpu_to_le32(NVME_ID_CNS_NS_ACTIVE_LIST);
  484. c.identify.nsid = cpu_to_le32(nsid);
  485. return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
  486. }
  487. int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
  488. struct nvme_id_ns **id)
  489. {
  490. struct nvme_command c = { };
  491. int error;
  492. /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
  493. c.identify.opcode = nvme_admin_identify,
  494. c.identify.nsid = cpu_to_le32(nsid),
  495. *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
  496. if (!*id)
  497. return -ENOMEM;
  498. error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
  499. sizeof(struct nvme_id_ns));
  500. if (error)
  501. kfree(*id);
  502. return error;
  503. }
  504. int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid,
  505. void *buffer, size_t buflen, u32 *result)
  506. {
  507. struct nvme_command c;
  508. struct nvme_completion cqe;
  509. int ret;
  510. memset(&c, 0, sizeof(c));
  511. c.features.opcode = nvme_admin_get_features;
  512. c.features.nsid = cpu_to_le32(nsid);
  513. c.features.fid = cpu_to_le32(fid);
  514. ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &cqe, buffer, buflen, 0,
  515. NVME_QID_ANY, 0, 0);
  516. if (ret >= 0 && result)
  517. *result = le32_to_cpu(cqe.result);
  518. return ret;
  519. }
  520. int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
  521. void *buffer, size_t buflen, u32 *result)
  522. {
  523. struct nvme_command c;
  524. struct nvme_completion cqe;
  525. int ret;
  526. memset(&c, 0, sizeof(c));
  527. c.features.opcode = nvme_admin_set_features;
  528. c.features.fid = cpu_to_le32(fid);
  529. c.features.dword11 = cpu_to_le32(dword11);
  530. ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &cqe,
  531. buffer, buflen, 0, NVME_QID_ANY, 0, 0);
  532. if (ret >= 0 && result)
  533. *result = le32_to_cpu(cqe.result);
  534. return ret;
  535. }
  536. int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log)
  537. {
  538. struct nvme_command c = { };
  539. int error;
  540. c.common.opcode = nvme_admin_get_log_page,
  541. c.common.nsid = cpu_to_le32(0xFFFFFFFF),
  542. c.common.cdw10[0] = cpu_to_le32(
  543. (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) |
  544. NVME_LOG_SMART),
  545. *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL);
  546. if (!*log)
  547. return -ENOMEM;
  548. error = nvme_submit_sync_cmd(dev->admin_q, &c, *log,
  549. sizeof(struct nvme_smart_log));
  550. if (error)
  551. kfree(*log);
  552. return error;
  553. }
  554. int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
  555. {
  556. u32 q_count = (*count - 1) | ((*count - 1) << 16);
  557. u32 result;
  558. int status, nr_io_queues;
  559. status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
  560. &result);
  561. if (status < 0)
  562. return status;
  563. /*
  564. * Degraded controllers might return an error when setting the queue
  565. * count. We still want to be able to bring them online and offer
  566. * access to the admin queue, as that might be only way to fix them up.
  567. */
  568. if (status > 0) {
  569. dev_err(ctrl->dev, "Could not set queue count (%d)\n", status);
  570. *count = 0;
  571. } else {
  572. nr_io_queues = min(result & 0xffff, result >> 16) + 1;
  573. *count = min(*count, nr_io_queues);
  574. }
  575. return 0;
  576. }
  577. EXPORT_SYMBOL_GPL(nvme_set_queue_count);
  578. static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
  579. {
  580. struct nvme_user_io io;
  581. struct nvme_command c;
  582. unsigned length, meta_len;
  583. void __user *metadata;
  584. if (copy_from_user(&io, uio, sizeof(io)))
  585. return -EFAULT;
  586. if (io.flags)
  587. return -EINVAL;
  588. switch (io.opcode) {
  589. case nvme_cmd_write:
  590. case nvme_cmd_read:
  591. case nvme_cmd_compare:
  592. break;
  593. default:
  594. return -EINVAL;
  595. }
  596. length = (io.nblocks + 1) << ns->lba_shift;
  597. meta_len = (io.nblocks + 1) * ns->ms;
  598. metadata = (void __user *)(uintptr_t)io.metadata;
  599. if (ns->ext) {
  600. length += meta_len;
  601. meta_len = 0;
  602. } else if (meta_len) {
  603. if ((io.metadata & 3) || !io.metadata)
  604. return -EINVAL;
  605. }
  606. memset(&c, 0, sizeof(c));
  607. c.rw.opcode = io.opcode;
  608. c.rw.flags = io.flags;
  609. c.rw.nsid = cpu_to_le32(ns->ns_id);
  610. c.rw.slba = cpu_to_le64(io.slba);
  611. c.rw.length = cpu_to_le16(io.nblocks);
  612. c.rw.control = cpu_to_le16(io.control);
  613. c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
  614. c.rw.reftag = cpu_to_le32(io.reftag);
  615. c.rw.apptag = cpu_to_le16(io.apptag);
  616. c.rw.appmask = cpu_to_le16(io.appmask);
  617. return __nvme_submit_user_cmd(ns->queue, &c,
  618. (void __user *)(uintptr_t)io.addr, length,
  619. metadata, meta_len, io.slba, NULL, 0);
  620. }
  621. static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
  622. struct nvme_passthru_cmd __user *ucmd)
  623. {
  624. struct nvme_passthru_cmd cmd;
  625. struct nvme_command c;
  626. unsigned timeout = 0;
  627. int status;
  628. if (!capable(CAP_SYS_ADMIN))
  629. return -EACCES;
  630. if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
  631. return -EFAULT;
  632. if (cmd.flags)
  633. return -EINVAL;
  634. memset(&c, 0, sizeof(c));
  635. c.common.opcode = cmd.opcode;
  636. c.common.flags = cmd.flags;
  637. c.common.nsid = cpu_to_le32(cmd.nsid);
  638. c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
  639. c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
  640. c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
  641. c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
  642. c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
  643. c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
  644. c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
  645. c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
  646. if (cmd.timeout_ms)
  647. timeout = msecs_to_jiffies(cmd.timeout_ms);
  648. status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
  649. (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
  650. &cmd.result, timeout);
  651. if (status >= 0) {
  652. if (put_user(cmd.result, &ucmd->result))
  653. return -EFAULT;
  654. }
  655. return status;
  656. }
  657. static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
  658. unsigned int cmd, unsigned long arg)
  659. {
  660. struct nvme_ns *ns = bdev->bd_disk->private_data;
  661. switch (cmd) {
  662. case NVME_IOCTL_ID:
  663. force_successful_syscall_return();
  664. return ns->ns_id;
  665. case NVME_IOCTL_ADMIN_CMD:
  666. return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
  667. case NVME_IOCTL_IO_CMD:
  668. return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
  669. case NVME_IOCTL_SUBMIT_IO:
  670. return nvme_submit_io(ns, (void __user *)arg);
  671. #ifdef CONFIG_BLK_DEV_NVME_SCSI
  672. case SG_GET_VERSION_NUM:
  673. return nvme_sg_get_version_num((void __user *)arg);
  674. case SG_IO:
  675. return nvme_sg_io(ns, (void __user *)arg);
  676. #endif
  677. default:
  678. return -ENOTTY;
  679. }
  680. }
  681. #ifdef CONFIG_COMPAT
  682. static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
  683. unsigned int cmd, unsigned long arg)
  684. {
  685. switch (cmd) {
  686. case SG_IO:
  687. return -ENOIOCTLCMD;
  688. }
  689. return nvme_ioctl(bdev, mode, cmd, arg);
  690. }
  691. #else
  692. #define nvme_compat_ioctl NULL
  693. #endif
  694. static int nvme_open(struct block_device *bdev, fmode_t mode)
  695. {
  696. return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO;
  697. }
  698. static void nvme_release(struct gendisk *disk, fmode_t mode)
  699. {
  700. struct nvme_ns *ns = disk->private_data;
  701. module_put(ns->ctrl->ops->module);
  702. nvme_put_ns(ns);
  703. }
  704. static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  705. {
  706. /* some standard values */
  707. geo->heads = 1 << 6;
  708. geo->sectors = 1 << 5;
  709. geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
  710. return 0;
  711. }
  712. #ifdef CONFIG_BLK_DEV_INTEGRITY
  713. static void nvme_init_integrity(struct nvme_ns *ns)
  714. {
  715. struct blk_integrity integrity;
  716. memset(&integrity, 0, sizeof(integrity));
  717. switch (ns->pi_type) {
  718. case NVME_NS_DPS_PI_TYPE3:
  719. integrity.profile = &t10_pi_type3_crc;
  720. integrity.tag_size = sizeof(u16) + sizeof(u32);
  721. integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
  722. break;
  723. case NVME_NS_DPS_PI_TYPE1:
  724. case NVME_NS_DPS_PI_TYPE2:
  725. integrity.profile = &t10_pi_type1_crc;
  726. integrity.tag_size = sizeof(u16);
  727. integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
  728. break;
  729. default:
  730. integrity.profile = NULL;
  731. break;
  732. }
  733. integrity.tuple_size = ns->ms;
  734. blk_integrity_register(ns->disk, &integrity);
  735. blk_queue_max_integrity_segments(ns->queue, 1);
  736. }
  737. #else
  738. static void nvme_init_integrity(struct nvme_ns *ns)
  739. {
  740. }
  741. #endif /* CONFIG_BLK_DEV_INTEGRITY */
  742. static void nvme_config_discard(struct nvme_ns *ns)
  743. {
  744. struct nvme_ctrl *ctrl = ns->ctrl;
  745. u32 logical_block_size = queue_logical_block_size(ns->queue);
  746. if (ctrl->quirks & NVME_QUIRK_DISCARD_ZEROES)
  747. ns->queue->limits.discard_zeroes_data = 1;
  748. else
  749. ns->queue->limits.discard_zeroes_data = 0;
  750. ns->queue->limits.discard_alignment = logical_block_size;
  751. ns->queue->limits.discard_granularity = logical_block_size;
  752. blk_queue_max_discard_sectors(ns->queue, UINT_MAX);
  753. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
  754. }
  755. static int nvme_revalidate_ns(struct nvme_ns *ns, struct nvme_id_ns **id)
  756. {
  757. if (nvme_identify_ns(ns->ctrl, ns->ns_id, id)) {
  758. dev_warn(ns->ctrl->dev, "%s: Identify failure\n", __func__);
  759. return -ENODEV;
  760. }
  761. if ((*id)->ncap == 0) {
  762. kfree(*id);
  763. return -ENODEV;
  764. }
  765. if (ns->ctrl->vs >= NVME_VS(1, 1, 0))
  766. memcpy(ns->eui, (*id)->eui64, sizeof(ns->eui));
  767. if (ns->ctrl->vs >= NVME_VS(1, 2, 0))
  768. memcpy(ns->uuid, (*id)->nguid, sizeof(ns->uuid));
  769. return 0;
  770. }
  771. static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
  772. {
  773. struct nvme_ns *ns = disk->private_data;
  774. u8 lbaf, pi_type;
  775. u16 old_ms;
  776. unsigned short bs;
  777. old_ms = ns->ms;
  778. lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK;
  779. ns->lba_shift = id->lbaf[lbaf].ds;
  780. ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
  781. ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
  782. /*
  783. * If identify namespace failed, use default 512 byte block size so
  784. * block layer can use before failing read/write for 0 capacity.
  785. */
  786. if (ns->lba_shift == 0)
  787. ns->lba_shift = 9;
  788. bs = 1 << ns->lba_shift;
  789. /* XXX: PI implementation requires metadata equal t10 pi tuple size */
  790. pi_type = ns->ms == sizeof(struct t10_pi_tuple) ?
  791. id->dps & NVME_NS_DPS_PI_MASK : 0;
  792. blk_mq_freeze_queue(disk->queue);
  793. if (blk_get_integrity(disk) && (ns->pi_type != pi_type ||
  794. ns->ms != old_ms ||
  795. bs != queue_logical_block_size(disk->queue) ||
  796. (ns->ms && ns->ext)))
  797. blk_integrity_unregister(disk);
  798. ns->pi_type = pi_type;
  799. blk_queue_logical_block_size(ns->queue, bs);
  800. if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
  801. nvme_init_integrity(ns);
  802. if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
  803. set_capacity(disk, 0);
  804. else
  805. set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
  806. if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
  807. nvme_config_discard(ns);
  808. blk_mq_unfreeze_queue(disk->queue);
  809. }
  810. static int nvme_revalidate_disk(struct gendisk *disk)
  811. {
  812. struct nvme_ns *ns = disk->private_data;
  813. struct nvme_id_ns *id = NULL;
  814. int ret;
  815. if (test_bit(NVME_NS_DEAD, &ns->flags)) {
  816. set_capacity(disk, 0);
  817. return -ENODEV;
  818. }
  819. ret = nvme_revalidate_ns(ns, &id);
  820. if (ret)
  821. return ret;
  822. __nvme_revalidate_disk(disk, id);
  823. kfree(id);
  824. return 0;
  825. }
  826. static char nvme_pr_type(enum pr_type type)
  827. {
  828. switch (type) {
  829. case PR_WRITE_EXCLUSIVE:
  830. return 1;
  831. case PR_EXCLUSIVE_ACCESS:
  832. return 2;
  833. case PR_WRITE_EXCLUSIVE_REG_ONLY:
  834. return 3;
  835. case PR_EXCLUSIVE_ACCESS_REG_ONLY:
  836. return 4;
  837. case PR_WRITE_EXCLUSIVE_ALL_REGS:
  838. return 5;
  839. case PR_EXCLUSIVE_ACCESS_ALL_REGS:
  840. return 6;
  841. default:
  842. return 0;
  843. }
  844. };
  845. static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
  846. u64 key, u64 sa_key, u8 op)
  847. {
  848. struct nvme_ns *ns = bdev->bd_disk->private_data;
  849. struct nvme_command c;
  850. u8 data[16] = { 0, };
  851. put_unaligned_le64(key, &data[0]);
  852. put_unaligned_le64(sa_key, &data[8]);
  853. memset(&c, 0, sizeof(c));
  854. c.common.opcode = op;
  855. c.common.nsid = cpu_to_le32(ns->ns_id);
  856. c.common.cdw10[0] = cpu_to_le32(cdw10);
  857. return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
  858. }
  859. static int nvme_pr_register(struct block_device *bdev, u64 old,
  860. u64 new, unsigned flags)
  861. {
  862. u32 cdw10;
  863. if (flags & ~PR_FL_IGNORE_KEY)
  864. return -EOPNOTSUPP;
  865. cdw10 = old ? 2 : 0;
  866. cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
  867. cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
  868. return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
  869. }
  870. static int nvme_pr_reserve(struct block_device *bdev, u64 key,
  871. enum pr_type type, unsigned flags)
  872. {
  873. u32 cdw10;
  874. if (flags & ~PR_FL_IGNORE_KEY)
  875. return -EOPNOTSUPP;
  876. cdw10 = nvme_pr_type(type) << 8;
  877. cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
  878. return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
  879. }
  880. static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
  881. enum pr_type type, bool abort)
  882. {
  883. u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
  884. return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
  885. }
  886. static int nvme_pr_clear(struct block_device *bdev, u64 key)
  887. {
  888. u32 cdw10 = 1 | (key ? 1 << 3 : 0);
  889. return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
  890. }
  891. static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
  892. {
  893. u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
  894. return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
  895. }
  896. static const struct pr_ops nvme_pr_ops = {
  897. .pr_register = nvme_pr_register,
  898. .pr_reserve = nvme_pr_reserve,
  899. .pr_release = nvme_pr_release,
  900. .pr_preempt = nvme_pr_preempt,
  901. .pr_clear = nvme_pr_clear,
  902. };
  903. static const struct block_device_operations nvme_fops = {
  904. .owner = THIS_MODULE,
  905. .ioctl = nvme_ioctl,
  906. .compat_ioctl = nvme_compat_ioctl,
  907. .open = nvme_open,
  908. .release = nvme_release,
  909. .getgeo = nvme_getgeo,
  910. .revalidate_disk= nvme_revalidate_disk,
  911. .pr_ops = &nvme_pr_ops,
  912. };
  913. static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
  914. {
  915. unsigned long timeout =
  916. ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
  917. u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
  918. int ret;
  919. while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
  920. if (csts == ~0)
  921. return -ENODEV;
  922. if ((csts & NVME_CSTS_RDY) == bit)
  923. break;
  924. msleep(100);
  925. if (fatal_signal_pending(current))
  926. return -EINTR;
  927. if (time_after(jiffies, timeout)) {
  928. dev_err(ctrl->device,
  929. "Device not ready; aborting %s\n", enabled ?
  930. "initialisation" : "reset");
  931. return -ENODEV;
  932. }
  933. }
  934. return ret;
  935. }
  936. /*
  937. * If the device has been passed off to us in an enabled state, just clear
  938. * the enabled bit. The spec says we should set the 'shutdown notification
  939. * bits', but doing so may cause the device to complete commands to the
  940. * admin queue ... and we don't know what memory that might be pointing at!
  941. */
  942. int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
  943. {
  944. int ret;
  945. ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
  946. ctrl->ctrl_config &= ~NVME_CC_ENABLE;
  947. ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
  948. if (ret)
  949. return ret;
  950. if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
  951. msleep(NVME_QUIRK_DELAY_AMOUNT);
  952. return nvme_wait_ready(ctrl, cap, false);
  953. }
  954. EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
  955. int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
  956. {
  957. /*
  958. * Default to a 4K page size, with the intention to update this
  959. * path in the future to accomodate architectures with differing
  960. * kernel and IO page sizes.
  961. */
  962. unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
  963. int ret;
  964. if (page_shift < dev_page_min) {
  965. dev_err(ctrl->device,
  966. "Minimum device page size %u too large for host (%u)\n",
  967. 1 << dev_page_min, 1 << page_shift);
  968. return -ENODEV;
  969. }
  970. ctrl->page_size = 1 << page_shift;
  971. ctrl->ctrl_config = NVME_CC_CSS_NVM;
  972. ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
  973. ctrl->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
  974. ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
  975. ctrl->ctrl_config |= NVME_CC_ENABLE;
  976. ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
  977. if (ret)
  978. return ret;
  979. return nvme_wait_ready(ctrl, cap, true);
  980. }
  981. EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
  982. int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
  983. {
  984. unsigned long timeout = SHUTDOWN_TIMEOUT + jiffies;
  985. u32 csts;
  986. int ret;
  987. ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
  988. ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
  989. ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
  990. if (ret)
  991. return ret;
  992. while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
  993. if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
  994. break;
  995. msleep(100);
  996. if (fatal_signal_pending(current))
  997. return -EINTR;
  998. if (time_after(jiffies, timeout)) {
  999. dev_err(ctrl->device,
  1000. "Device shutdown incomplete; abort shutdown\n");
  1001. return -ENODEV;
  1002. }
  1003. }
  1004. return ret;
  1005. }
  1006. EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
  1007. static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
  1008. struct request_queue *q)
  1009. {
  1010. bool vwc = false;
  1011. if (ctrl->max_hw_sectors) {
  1012. u32 max_segments =
  1013. (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
  1014. blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
  1015. blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
  1016. }
  1017. if ((ctrl->quirks & NVME_QUIRK_STRIPE_SIZE) &&
  1018. is_power_of_2(ctrl->max_hw_sectors))
  1019. blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
  1020. blk_queue_virt_boundary(q, ctrl->page_size - 1);
  1021. if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
  1022. vwc = true;
  1023. blk_queue_write_cache(q, vwc, vwc);
  1024. }
  1025. /*
  1026. * Initialize the cached copies of the Identify data and various controller
  1027. * register in our nvme_ctrl structure. This should be called as soon as
  1028. * the admin queue is fully up and running.
  1029. */
  1030. int nvme_init_identify(struct nvme_ctrl *ctrl)
  1031. {
  1032. struct nvme_id_ctrl *id;
  1033. u64 cap;
  1034. int ret, page_shift;
  1035. u32 max_hw_sectors;
  1036. ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
  1037. if (ret) {
  1038. dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
  1039. return ret;
  1040. }
  1041. ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
  1042. if (ret) {
  1043. dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
  1044. return ret;
  1045. }
  1046. page_shift = NVME_CAP_MPSMIN(cap) + 12;
  1047. if (ctrl->vs >= NVME_VS(1, 1, 0))
  1048. ctrl->subsystem = NVME_CAP_NSSRC(cap);
  1049. ret = nvme_identify_ctrl(ctrl, &id);
  1050. if (ret) {
  1051. dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
  1052. return -EIO;
  1053. }
  1054. ctrl->vid = le16_to_cpu(id->vid);
  1055. ctrl->oncs = le16_to_cpup(&id->oncs);
  1056. atomic_set(&ctrl->abort_limit, id->acl + 1);
  1057. ctrl->vwc = id->vwc;
  1058. ctrl->cntlid = le16_to_cpup(&id->cntlid);
  1059. memcpy(ctrl->serial, id->sn, sizeof(id->sn));
  1060. memcpy(ctrl->model, id->mn, sizeof(id->mn));
  1061. memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
  1062. if (id->mdts)
  1063. max_hw_sectors = 1 << (id->mdts + page_shift - 9);
  1064. else
  1065. max_hw_sectors = UINT_MAX;
  1066. ctrl->max_hw_sectors =
  1067. min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
  1068. nvme_set_queue_limits(ctrl, ctrl->admin_q);
  1069. ctrl->sgls = le32_to_cpu(id->sgls);
  1070. ctrl->kas = le16_to_cpu(id->kas);
  1071. if (ctrl->ops->is_fabrics) {
  1072. ctrl->icdoff = le16_to_cpu(id->icdoff);
  1073. ctrl->ioccsz = le32_to_cpu(id->ioccsz);
  1074. ctrl->iorcsz = le32_to_cpu(id->iorcsz);
  1075. ctrl->maxcmd = le16_to_cpu(id->maxcmd);
  1076. /*
  1077. * In fabrics we need to verify the cntlid matches the
  1078. * admin connect
  1079. */
  1080. if (ctrl->cntlid != le16_to_cpu(id->cntlid))
  1081. ret = -EINVAL;
  1082. if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
  1083. dev_err(ctrl->dev,
  1084. "keep-alive support is mandatory for fabrics\n");
  1085. ret = -EINVAL;
  1086. }
  1087. } else {
  1088. ctrl->cntlid = le16_to_cpu(id->cntlid);
  1089. }
  1090. kfree(id);
  1091. return ret;
  1092. }
  1093. EXPORT_SYMBOL_GPL(nvme_init_identify);
  1094. static int nvme_dev_open(struct inode *inode, struct file *file)
  1095. {
  1096. struct nvme_ctrl *ctrl;
  1097. int instance = iminor(inode);
  1098. int ret = -ENODEV;
  1099. spin_lock(&dev_list_lock);
  1100. list_for_each_entry(ctrl, &nvme_ctrl_list, node) {
  1101. if (ctrl->instance != instance)
  1102. continue;
  1103. if (!ctrl->admin_q) {
  1104. ret = -EWOULDBLOCK;
  1105. break;
  1106. }
  1107. if (!kref_get_unless_zero(&ctrl->kref))
  1108. break;
  1109. file->private_data = ctrl;
  1110. ret = 0;
  1111. break;
  1112. }
  1113. spin_unlock(&dev_list_lock);
  1114. return ret;
  1115. }
  1116. static int nvme_dev_release(struct inode *inode, struct file *file)
  1117. {
  1118. nvme_put_ctrl(file->private_data);
  1119. return 0;
  1120. }
  1121. static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
  1122. {
  1123. struct nvme_ns *ns;
  1124. int ret;
  1125. mutex_lock(&ctrl->namespaces_mutex);
  1126. if (list_empty(&ctrl->namespaces)) {
  1127. ret = -ENOTTY;
  1128. goto out_unlock;
  1129. }
  1130. ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
  1131. if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
  1132. dev_warn(ctrl->device,
  1133. "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
  1134. ret = -EINVAL;
  1135. goto out_unlock;
  1136. }
  1137. dev_warn(ctrl->device,
  1138. "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
  1139. kref_get(&ns->kref);
  1140. mutex_unlock(&ctrl->namespaces_mutex);
  1141. ret = nvme_user_cmd(ctrl, ns, argp);
  1142. nvme_put_ns(ns);
  1143. return ret;
  1144. out_unlock:
  1145. mutex_unlock(&ctrl->namespaces_mutex);
  1146. return ret;
  1147. }
  1148. static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
  1149. unsigned long arg)
  1150. {
  1151. struct nvme_ctrl *ctrl = file->private_data;
  1152. void __user *argp = (void __user *)arg;
  1153. switch (cmd) {
  1154. case NVME_IOCTL_ADMIN_CMD:
  1155. return nvme_user_cmd(ctrl, NULL, argp);
  1156. case NVME_IOCTL_IO_CMD:
  1157. return nvme_dev_user_cmd(ctrl, argp);
  1158. case NVME_IOCTL_RESET:
  1159. dev_warn(ctrl->device, "resetting controller\n");
  1160. return ctrl->ops->reset_ctrl(ctrl);
  1161. case NVME_IOCTL_SUBSYS_RESET:
  1162. return nvme_reset_subsystem(ctrl);
  1163. case NVME_IOCTL_RESCAN:
  1164. nvme_queue_scan(ctrl);
  1165. return 0;
  1166. default:
  1167. return -ENOTTY;
  1168. }
  1169. }
  1170. static const struct file_operations nvme_dev_fops = {
  1171. .owner = THIS_MODULE,
  1172. .open = nvme_dev_open,
  1173. .release = nvme_dev_release,
  1174. .unlocked_ioctl = nvme_dev_ioctl,
  1175. .compat_ioctl = nvme_dev_ioctl,
  1176. };
  1177. static ssize_t nvme_sysfs_reset(struct device *dev,
  1178. struct device_attribute *attr, const char *buf,
  1179. size_t count)
  1180. {
  1181. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  1182. int ret;
  1183. ret = ctrl->ops->reset_ctrl(ctrl);
  1184. if (ret < 0)
  1185. return ret;
  1186. return count;
  1187. }
  1188. static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
  1189. static ssize_t nvme_sysfs_rescan(struct device *dev,
  1190. struct device_attribute *attr, const char *buf,
  1191. size_t count)
  1192. {
  1193. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  1194. nvme_queue_scan(ctrl);
  1195. return count;
  1196. }
  1197. static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
  1198. static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
  1199. char *buf)
  1200. {
  1201. struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
  1202. struct nvme_ctrl *ctrl = ns->ctrl;
  1203. int serial_len = sizeof(ctrl->serial);
  1204. int model_len = sizeof(ctrl->model);
  1205. if (memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
  1206. return sprintf(buf, "eui.%16phN\n", ns->uuid);
  1207. if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
  1208. return sprintf(buf, "eui.%8phN\n", ns->eui);
  1209. while (ctrl->serial[serial_len - 1] == ' ')
  1210. serial_len--;
  1211. while (ctrl->model[model_len - 1] == ' ')
  1212. model_len--;
  1213. return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
  1214. serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
  1215. }
  1216. static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
  1217. static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
  1218. char *buf)
  1219. {
  1220. struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
  1221. return sprintf(buf, "%pU\n", ns->uuid);
  1222. }
  1223. static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
  1224. static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
  1225. char *buf)
  1226. {
  1227. struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
  1228. return sprintf(buf, "%8phd\n", ns->eui);
  1229. }
  1230. static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
  1231. static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
  1232. char *buf)
  1233. {
  1234. struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
  1235. return sprintf(buf, "%d\n", ns->ns_id);
  1236. }
  1237. static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
  1238. static struct attribute *nvme_ns_attrs[] = {
  1239. &dev_attr_wwid.attr,
  1240. &dev_attr_uuid.attr,
  1241. &dev_attr_eui.attr,
  1242. &dev_attr_nsid.attr,
  1243. NULL,
  1244. };
  1245. static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
  1246. struct attribute *a, int n)
  1247. {
  1248. struct device *dev = container_of(kobj, struct device, kobj);
  1249. struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
  1250. if (a == &dev_attr_uuid.attr) {
  1251. if (!memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
  1252. return 0;
  1253. }
  1254. if (a == &dev_attr_eui.attr) {
  1255. if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
  1256. return 0;
  1257. }
  1258. return a->mode;
  1259. }
  1260. static const struct attribute_group nvme_ns_attr_group = {
  1261. .attrs = nvme_ns_attrs,
  1262. .is_visible = nvme_ns_attrs_are_visible,
  1263. };
  1264. #define nvme_show_str_function(field) \
  1265. static ssize_t field##_show(struct device *dev, \
  1266. struct device_attribute *attr, char *buf) \
  1267. { \
  1268. struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
  1269. return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
  1270. } \
  1271. static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
  1272. #define nvme_show_int_function(field) \
  1273. static ssize_t field##_show(struct device *dev, \
  1274. struct device_attribute *attr, char *buf) \
  1275. { \
  1276. struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
  1277. return sprintf(buf, "%d\n", ctrl->field); \
  1278. } \
  1279. static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
  1280. nvme_show_str_function(model);
  1281. nvme_show_str_function(serial);
  1282. nvme_show_str_function(firmware_rev);
  1283. nvme_show_int_function(cntlid);
  1284. static ssize_t nvme_sysfs_delete(struct device *dev,
  1285. struct device_attribute *attr, const char *buf,
  1286. size_t count)
  1287. {
  1288. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  1289. if (device_remove_file_self(dev, attr))
  1290. ctrl->ops->delete_ctrl(ctrl);
  1291. return count;
  1292. }
  1293. static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
  1294. static ssize_t nvme_sysfs_show_transport(struct device *dev,
  1295. struct device_attribute *attr,
  1296. char *buf)
  1297. {
  1298. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  1299. return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
  1300. }
  1301. static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
  1302. static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
  1303. struct device_attribute *attr,
  1304. char *buf)
  1305. {
  1306. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  1307. return snprintf(buf, PAGE_SIZE, "%s\n",
  1308. ctrl->ops->get_subsysnqn(ctrl));
  1309. }
  1310. static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
  1311. static ssize_t nvme_sysfs_show_address(struct device *dev,
  1312. struct device_attribute *attr,
  1313. char *buf)
  1314. {
  1315. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  1316. return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
  1317. }
  1318. static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
  1319. static struct attribute *nvme_dev_attrs[] = {
  1320. &dev_attr_reset_controller.attr,
  1321. &dev_attr_rescan_controller.attr,
  1322. &dev_attr_model.attr,
  1323. &dev_attr_serial.attr,
  1324. &dev_attr_firmware_rev.attr,
  1325. &dev_attr_cntlid.attr,
  1326. &dev_attr_delete_controller.attr,
  1327. &dev_attr_transport.attr,
  1328. &dev_attr_subsysnqn.attr,
  1329. &dev_attr_address.attr,
  1330. NULL
  1331. };
  1332. #define CHECK_ATTR(ctrl, a, name) \
  1333. if ((a) == &dev_attr_##name.attr && \
  1334. !(ctrl)->ops->get_##name) \
  1335. return 0
  1336. static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
  1337. struct attribute *a, int n)
  1338. {
  1339. struct device *dev = container_of(kobj, struct device, kobj);
  1340. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  1341. if (a == &dev_attr_delete_controller.attr) {
  1342. if (!ctrl->ops->delete_ctrl)
  1343. return 0;
  1344. }
  1345. CHECK_ATTR(ctrl, a, subsysnqn);
  1346. CHECK_ATTR(ctrl, a, address);
  1347. return a->mode;
  1348. }
  1349. static struct attribute_group nvme_dev_attrs_group = {
  1350. .attrs = nvme_dev_attrs,
  1351. .is_visible = nvme_dev_attrs_are_visible,
  1352. };
  1353. static const struct attribute_group *nvme_dev_attr_groups[] = {
  1354. &nvme_dev_attrs_group,
  1355. NULL,
  1356. };
  1357. static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
  1358. {
  1359. struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
  1360. struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
  1361. return nsa->ns_id - nsb->ns_id;
  1362. }
  1363. static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
  1364. {
  1365. struct nvme_ns *ns, *ret = NULL;
  1366. mutex_lock(&ctrl->namespaces_mutex);
  1367. list_for_each_entry(ns, &ctrl->namespaces, list) {
  1368. if (ns->ns_id == nsid) {
  1369. if (!kref_get_unless_zero(&ns->kref))
  1370. continue;
  1371. ret = ns;
  1372. break;
  1373. }
  1374. if (ns->ns_id > nsid)
  1375. break;
  1376. }
  1377. mutex_unlock(&ctrl->namespaces_mutex);
  1378. return ret;
  1379. }
  1380. static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
  1381. {
  1382. struct nvme_ns *ns;
  1383. struct gendisk *disk;
  1384. struct nvme_id_ns *id;
  1385. char disk_name[DISK_NAME_LEN];
  1386. int node = dev_to_node(ctrl->dev);
  1387. ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
  1388. if (!ns)
  1389. return;
  1390. ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
  1391. if (ns->instance < 0)
  1392. goto out_free_ns;
  1393. ns->queue = blk_mq_init_queue(ctrl->tagset);
  1394. if (IS_ERR(ns->queue))
  1395. goto out_release_instance;
  1396. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
  1397. ns->queue->queuedata = ns;
  1398. ns->ctrl = ctrl;
  1399. kref_init(&ns->kref);
  1400. ns->ns_id = nsid;
  1401. ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
  1402. blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
  1403. nvme_set_queue_limits(ctrl, ns->queue);
  1404. sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
  1405. if (nvme_revalidate_ns(ns, &id))
  1406. goto out_free_queue;
  1407. if (nvme_nvm_ns_supported(ns, id)) {
  1408. if (nvme_nvm_register(ns, disk_name, node,
  1409. &nvme_ns_attr_group)) {
  1410. dev_warn(ctrl->dev, "%s: LightNVM init failure\n",
  1411. __func__);
  1412. goto out_free_id;
  1413. }
  1414. } else {
  1415. disk = alloc_disk_node(0, node);
  1416. if (!disk)
  1417. goto out_free_id;
  1418. disk->fops = &nvme_fops;
  1419. disk->private_data = ns;
  1420. disk->queue = ns->queue;
  1421. disk->flags = GENHD_FL_EXT_DEVT;
  1422. memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
  1423. ns->disk = disk;
  1424. __nvme_revalidate_disk(disk, id);
  1425. }
  1426. mutex_lock(&ctrl->namespaces_mutex);
  1427. list_add_tail(&ns->list, &ctrl->namespaces);
  1428. mutex_unlock(&ctrl->namespaces_mutex);
  1429. kref_get(&ctrl->kref);
  1430. kfree(id);
  1431. if (ns->ndev)
  1432. return;
  1433. device_add_disk(ctrl->device, ns->disk);
  1434. if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
  1435. &nvme_ns_attr_group))
  1436. pr_warn("%s: failed to create sysfs group for identification\n",
  1437. ns->disk->disk_name);
  1438. return;
  1439. out_free_id:
  1440. kfree(id);
  1441. out_free_queue:
  1442. blk_cleanup_queue(ns->queue);
  1443. out_release_instance:
  1444. ida_simple_remove(&ctrl->ns_ida, ns->instance);
  1445. out_free_ns:
  1446. kfree(ns);
  1447. }
  1448. static void nvme_ns_remove(struct nvme_ns *ns)
  1449. {
  1450. if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
  1451. return;
  1452. if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
  1453. if (blk_get_integrity(ns->disk))
  1454. blk_integrity_unregister(ns->disk);
  1455. sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
  1456. &nvme_ns_attr_group);
  1457. del_gendisk(ns->disk);
  1458. blk_cleanup_queue(ns->queue);
  1459. }
  1460. mutex_lock(&ns->ctrl->namespaces_mutex);
  1461. list_del_init(&ns->list);
  1462. mutex_unlock(&ns->ctrl->namespaces_mutex);
  1463. nvme_put_ns(ns);
  1464. }
  1465. static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
  1466. {
  1467. struct nvme_ns *ns;
  1468. ns = nvme_find_get_ns(ctrl, nsid);
  1469. if (ns) {
  1470. if (ns->disk && revalidate_disk(ns->disk))
  1471. nvme_ns_remove(ns);
  1472. nvme_put_ns(ns);
  1473. } else
  1474. nvme_alloc_ns(ctrl, nsid);
  1475. }
  1476. static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
  1477. unsigned nsid)
  1478. {
  1479. struct nvme_ns *ns, *next;
  1480. list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
  1481. if (ns->ns_id > nsid)
  1482. nvme_ns_remove(ns);
  1483. }
  1484. }
  1485. static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
  1486. {
  1487. struct nvme_ns *ns;
  1488. __le32 *ns_list;
  1489. unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
  1490. int ret = 0;
  1491. ns_list = kzalloc(0x1000, GFP_KERNEL);
  1492. if (!ns_list)
  1493. return -ENOMEM;
  1494. for (i = 0; i < num_lists; i++) {
  1495. ret = nvme_identify_ns_list(ctrl, prev, ns_list);
  1496. if (ret)
  1497. goto free;
  1498. for (j = 0; j < min(nn, 1024U); j++) {
  1499. nsid = le32_to_cpu(ns_list[j]);
  1500. if (!nsid)
  1501. goto out;
  1502. nvme_validate_ns(ctrl, nsid);
  1503. while (++prev < nsid) {
  1504. ns = nvme_find_get_ns(ctrl, prev);
  1505. if (ns) {
  1506. nvme_ns_remove(ns);
  1507. nvme_put_ns(ns);
  1508. }
  1509. }
  1510. }
  1511. nn -= j;
  1512. }
  1513. out:
  1514. nvme_remove_invalid_namespaces(ctrl, prev);
  1515. free:
  1516. kfree(ns_list);
  1517. return ret;
  1518. }
  1519. static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
  1520. {
  1521. unsigned i;
  1522. for (i = 1; i <= nn; i++)
  1523. nvme_validate_ns(ctrl, i);
  1524. nvme_remove_invalid_namespaces(ctrl, nn);
  1525. }
  1526. static void nvme_scan_work(struct work_struct *work)
  1527. {
  1528. struct nvme_ctrl *ctrl =
  1529. container_of(work, struct nvme_ctrl, scan_work);
  1530. struct nvme_id_ctrl *id;
  1531. unsigned nn;
  1532. if (ctrl->state != NVME_CTRL_LIVE)
  1533. return;
  1534. if (nvme_identify_ctrl(ctrl, &id))
  1535. return;
  1536. nn = le32_to_cpu(id->nn);
  1537. if (ctrl->vs >= NVME_VS(1, 1, 0) &&
  1538. !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
  1539. if (!nvme_scan_ns_list(ctrl, nn))
  1540. goto done;
  1541. }
  1542. nvme_scan_ns_sequential(ctrl, nn);
  1543. done:
  1544. mutex_lock(&ctrl->namespaces_mutex);
  1545. list_sort(NULL, &ctrl->namespaces, ns_cmp);
  1546. mutex_unlock(&ctrl->namespaces_mutex);
  1547. kfree(id);
  1548. }
  1549. void nvme_queue_scan(struct nvme_ctrl *ctrl)
  1550. {
  1551. /*
  1552. * Do not queue new scan work when a controller is reset during
  1553. * removal.
  1554. */
  1555. if (ctrl->state == NVME_CTRL_LIVE)
  1556. schedule_work(&ctrl->scan_work);
  1557. }
  1558. EXPORT_SYMBOL_GPL(nvme_queue_scan);
  1559. /*
  1560. * This function iterates the namespace list unlocked to allow recovery from
  1561. * controller failure. It is up to the caller to ensure the namespace list is
  1562. * not modified by scan work while this function is executing.
  1563. */
  1564. void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
  1565. {
  1566. struct nvme_ns *ns, *next;
  1567. /*
  1568. * The dead states indicates the controller was not gracefully
  1569. * disconnected. In that case, we won't be able to flush any data while
  1570. * removing the namespaces' disks; fail all the queues now to avoid
  1571. * potentially having to clean up the failed sync later.
  1572. */
  1573. if (ctrl->state == NVME_CTRL_DEAD)
  1574. nvme_kill_queues(ctrl);
  1575. list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
  1576. nvme_ns_remove(ns);
  1577. }
  1578. EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
  1579. static void nvme_async_event_work(struct work_struct *work)
  1580. {
  1581. struct nvme_ctrl *ctrl =
  1582. container_of(work, struct nvme_ctrl, async_event_work);
  1583. spin_lock_irq(&ctrl->lock);
  1584. while (ctrl->event_limit > 0) {
  1585. int aer_idx = --ctrl->event_limit;
  1586. spin_unlock_irq(&ctrl->lock);
  1587. ctrl->ops->submit_async_event(ctrl, aer_idx);
  1588. spin_lock_irq(&ctrl->lock);
  1589. }
  1590. spin_unlock_irq(&ctrl->lock);
  1591. }
  1592. void nvme_complete_async_event(struct nvme_ctrl *ctrl,
  1593. struct nvme_completion *cqe)
  1594. {
  1595. u16 status = le16_to_cpu(cqe->status) >> 1;
  1596. u32 result = le32_to_cpu(cqe->result);
  1597. if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ) {
  1598. ++ctrl->event_limit;
  1599. schedule_work(&ctrl->async_event_work);
  1600. }
  1601. if (status != NVME_SC_SUCCESS)
  1602. return;
  1603. switch (result & 0xff07) {
  1604. case NVME_AER_NOTICE_NS_CHANGED:
  1605. dev_info(ctrl->device, "rescanning\n");
  1606. nvme_queue_scan(ctrl);
  1607. break;
  1608. default:
  1609. dev_warn(ctrl->device, "async event result %08x\n", result);
  1610. }
  1611. }
  1612. EXPORT_SYMBOL_GPL(nvme_complete_async_event);
  1613. void nvme_queue_async_events(struct nvme_ctrl *ctrl)
  1614. {
  1615. ctrl->event_limit = NVME_NR_AERS;
  1616. schedule_work(&ctrl->async_event_work);
  1617. }
  1618. EXPORT_SYMBOL_GPL(nvme_queue_async_events);
  1619. static DEFINE_IDA(nvme_instance_ida);
  1620. static int nvme_set_instance(struct nvme_ctrl *ctrl)
  1621. {
  1622. int instance, error;
  1623. do {
  1624. if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
  1625. return -ENODEV;
  1626. spin_lock(&dev_list_lock);
  1627. error = ida_get_new(&nvme_instance_ida, &instance);
  1628. spin_unlock(&dev_list_lock);
  1629. } while (error == -EAGAIN);
  1630. if (error)
  1631. return -ENODEV;
  1632. ctrl->instance = instance;
  1633. return 0;
  1634. }
  1635. static void nvme_release_instance(struct nvme_ctrl *ctrl)
  1636. {
  1637. spin_lock(&dev_list_lock);
  1638. ida_remove(&nvme_instance_ida, ctrl->instance);
  1639. spin_unlock(&dev_list_lock);
  1640. }
  1641. void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
  1642. {
  1643. flush_work(&ctrl->async_event_work);
  1644. flush_work(&ctrl->scan_work);
  1645. nvme_remove_namespaces(ctrl);
  1646. device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
  1647. spin_lock(&dev_list_lock);
  1648. list_del(&ctrl->node);
  1649. spin_unlock(&dev_list_lock);
  1650. }
  1651. EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
  1652. static void nvme_free_ctrl(struct kref *kref)
  1653. {
  1654. struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref);
  1655. put_device(ctrl->device);
  1656. nvme_release_instance(ctrl);
  1657. ida_destroy(&ctrl->ns_ida);
  1658. ctrl->ops->free_ctrl(ctrl);
  1659. }
  1660. void nvme_put_ctrl(struct nvme_ctrl *ctrl)
  1661. {
  1662. kref_put(&ctrl->kref, nvme_free_ctrl);
  1663. }
  1664. EXPORT_SYMBOL_GPL(nvme_put_ctrl);
  1665. /*
  1666. * Initialize a NVMe controller structures. This needs to be called during
  1667. * earliest initialization so that we have the initialized structured around
  1668. * during probing.
  1669. */
  1670. int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
  1671. const struct nvme_ctrl_ops *ops, unsigned long quirks)
  1672. {
  1673. int ret;
  1674. ctrl->state = NVME_CTRL_NEW;
  1675. spin_lock_init(&ctrl->lock);
  1676. INIT_LIST_HEAD(&ctrl->namespaces);
  1677. mutex_init(&ctrl->namespaces_mutex);
  1678. kref_init(&ctrl->kref);
  1679. ctrl->dev = dev;
  1680. ctrl->ops = ops;
  1681. ctrl->quirks = quirks;
  1682. INIT_WORK(&ctrl->scan_work, nvme_scan_work);
  1683. INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
  1684. ret = nvme_set_instance(ctrl);
  1685. if (ret)
  1686. goto out;
  1687. ctrl->device = device_create_with_groups(nvme_class, ctrl->dev,
  1688. MKDEV(nvme_char_major, ctrl->instance),
  1689. ctrl, nvme_dev_attr_groups,
  1690. "nvme%d", ctrl->instance);
  1691. if (IS_ERR(ctrl->device)) {
  1692. ret = PTR_ERR(ctrl->device);
  1693. goto out_release_instance;
  1694. }
  1695. get_device(ctrl->device);
  1696. ida_init(&ctrl->ns_ida);
  1697. spin_lock(&dev_list_lock);
  1698. list_add_tail(&ctrl->node, &nvme_ctrl_list);
  1699. spin_unlock(&dev_list_lock);
  1700. return 0;
  1701. out_release_instance:
  1702. nvme_release_instance(ctrl);
  1703. out:
  1704. return ret;
  1705. }
  1706. EXPORT_SYMBOL_GPL(nvme_init_ctrl);
  1707. /**
  1708. * nvme_kill_queues(): Ends all namespace queues
  1709. * @ctrl: the dead controller that needs to end
  1710. *
  1711. * Call this function when the driver determines it is unable to get the
  1712. * controller in a state capable of servicing IO.
  1713. */
  1714. void nvme_kill_queues(struct nvme_ctrl *ctrl)
  1715. {
  1716. struct nvme_ns *ns;
  1717. mutex_lock(&ctrl->namespaces_mutex);
  1718. /* Forcibly start all queues to avoid having stuck requests */
  1719. if (ctrl->admin_q)
  1720. blk_mq_start_hw_queues(ctrl->admin_q);
  1721. list_for_each_entry(ns, &ctrl->namespaces, list) {
  1722. /*
  1723. * Revalidating a dead namespace sets capacity to 0. This will
  1724. * end buffered writers dirtying pages that can't be synced.
  1725. */
  1726. if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
  1727. continue;
  1728. revalidate_disk(ns->disk);
  1729. blk_set_queue_dying(ns->queue);
  1730. /*
  1731. * Forcibly start all queues to avoid having stuck requests.
  1732. * Note that we must ensure the queues are not stopped
  1733. * when the final removal happens.
  1734. */
  1735. blk_mq_start_hw_queues(ns->queue);
  1736. /* draining requests in requeue list */
  1737. blk_mq_kick_requeue_list(ns->queue);
  1738. }
  1739. mutex_unlock(&ctrl->namespaces_mutex);
  1740. }
  1741. EXPORT_SYMBOL_GPL(nvme_kill_queues);
  1742. void nvme_stop_queues(struct nvme_ctrl *ctrl)
  1743. {
  1744. struct nvme_ns *ns;
  1745. mutex_lock(&ctrl->namespaces_mutex);
  1746. list_for_each_entry(ns, &ctrl->namespaces, list) {
  1747. spin_lock_irq(ns->queue->queue_lock);
  1748. queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
  1749. spin_unlock_irq(ns->queue->queue_lock);
  1750. blk_mq_cancel_requeue_work(ns->queue);
  1751. blk_mq_stop_hw_queues(ns->queue);
  1752. }
  1753. mutex_unlock(&ctrl->namespaces_mutex);
  1754. }
  1755. EXPORT_SYMBOL_GPL(nvme_stop_queues);
  1756. void nvme_start_queues(struct nvme_ctrl *ctrl)
  1757. {
  1758. struct nvme_ns *ns;
  1759. mutex_lock(&ctrl->namespaces_mutex);
  1760. list_for_each_entry(ns, &ctrl->namespaces, list) {
  1761. queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
  1762. blk_mq_start_stopped_hw_queues(ns->queue, true);
  1763. blk_mq_kick_requeue_list(ns->queue);
  1764. }
  1765. mutex_unlock(&ctrl->namespaces_mutex);
  1766. }
  1767. EXPORT_SYMBOL_GPL(nvme_start_queues);
  1768. int __init nvme_core_init(void)
  1769. {
  1770. int result;
  1771. result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
  1772. &nvme_dev_fops);
  1773. if (result < 0)
  1774. return result;
  1775. else if (result > 0)
  1776. nvme_char_major = result;
  1777. nvme_class = class_create(THIS_MODULE, "nvme");
  1778. if (IS_ERR(nvme_class)) {
  1779. result = PTR_ERR(nvme_class);
  1780. goto unregister_chrdev;
  1781. }
  1782. return 0;
  1783. unregister_chrdev:
  1784. __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
  1785. return result;
  1786. }
  1787. void nvme_core_exit(void)
  1788. {
  1789. class_destroy(nvme_class);
  1790. __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
  1791. }
  1792. MODULE_LICENSE("GPL");
  1793. MODULE_VERSION("1.0");
  1794. module_init(nvme_core_init);
  1795. module_exit(nvme_core_exit);