core.c 52 KB

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