core.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105
  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. return -ENOTTY;
  664. }
  665. }
  666. #ifdef CONFIG_COMPAT
  667. static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
  668. unsigned int cmd, unsigned long arg)
  669. {
  670. switch (cmd) {
  671. case SG_IO:
  672. return -ENOIOCTLCMD;
  673. }
  674. return nvme_ioctl(bdev, mode, cmd, arg);
  675. }
  676. #else
  677. #define nvme_compat_ioctl NULL
  678. #endif
  679. static int nvme_open(struct block_device *bdev, fmode_t mode)
  680. {
  681. return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO;
  682. }
  683. static void nvme_release(struct gendisk *disk, fmode_t mode)
  684. {
  685. struct nvme_ns *ns = disk->private_data;
  686. module_put(ns->ctrl->ops->module);
  687. nvme_put_ns(ns);
  688. }
  689. static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  690. {
  691. /* some standard values */
  692. geo->heads = 1 << 6;
  693. geo->sectors = 1 << 5;
  694. geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
  695. return 0;
  696. }
  697. #ifdef CONFIG_BLK_DEV_INTEGRITY
  698. static void nvme_init_integrity(struct nvme_ns *ns)
  699. {
  700. struct blk_integrity integrity;
  701. memset(&integrity, 0, sizeof(integrity));
  702. switch (ns->pi_type) {
  703. case NVME_NS_DPS_PI_TYPE3:
  704. integrity.profile = &t10_pi_type3_crc;
  705. integrity.tag_size = sizeof(u16) + sizeof(u32);
  706. integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
  707. break;
  708. case NVME_NS_DPS_PI_TYPE1:
  709. case NVME_NS_DPS_PI_TYPE2:
  710. integrity.profile = &t10_pi_type1_crc;
  711. integrity.tag_size = sizeof(u16);
  712. integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
  713. break;
  714. default:
  715. integrity.profile = NULL;
  716. break;
  717. }
  718. integrity.tuple_size = ns->ms;
  719. blk_integrity_register(ns->disk, &integrity);
  720. blk_queue_max_integrity_segments(ns->queue, 1);
  721. }
  722. #else
  723. static void nvme_init_integrity(struct nvme_ns *ns)
  724. {
  725. }
  726. #endif /* CONFIG_BLK_DEV_INTEGRITY */
  727. static void nvme_config_discard(struct nvme_ns *ns)
  728. {
  729. struct nvme_ctrl *ctrl = ns->ctrl;
  730. u32 logical_block_size = queue_logical_block_size(ns->queue);
  731. if (ctrl->quirks & NVME_QUIRK_DISCARD_ZEROES)
  732. ns->queue->limits.discard_zeroes_data = 1;
  733. else
  734. ns->queue->limits.discard_zeroes_data = 0;
  735. ns->queue->limits.discard_alignment = logical_block_size;
  736. ns->queue->limits.discard_granularity = logical_block_size;
  737. blk_queue_max_discard_sectors(ns->queue, UINT_MAX);
  738. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
  739. }
  740. static int nvme_revalidate_ns(struct nvme_ns *ns, struct nvme_id_ns **id)
  741. {
  742. if (nvme_identify_ns(ns->ctrl, ns->ns_id, id)) {
  743. dev_warn(ns->ctrl->dev, "%s: Identify failure\n", __func__);
  744. return -ENODEV;
  745. }
  746. if ((*id)->ncap == 0) {
  747. kfree(*id);
  748. return -ENODEV;
  749. }
  750. if (ns->ctrl->vs >= NVME_VS(1, 1, 0))
  751. memcpy(ns->eui, (*id)->eui64, sizeof(ns->eui));
  752. if (ns->ctrl->vs >= NVME_VS(1, 2, 0))
  753. memcpy(ns->uuid, (*id)->nguid, sizeof(ns->uuid));
  754. return 0;
  755. }
  756. static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
  757. {
  758. struct nvme_ns *ns = disk->private_data;
  759. u8 lbaf, pi_type;
  760. u16 old_ms;
  761. unsigned short bs;
  762. old_ms = ns->ms;
  763. lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK;
  764. ns->lba_shift = id->lbaf[lbaf].ds;
  765. ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
  766. ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
  767. /*
  768. * If identify namespace failed, use default 512 byte block size so
  769. * block layer can use before failing read/write for 0 capacity.
  770. */
  771. if (ns->lba_shift == 0)
  772. ns->lba_shift = 9;
  773. bs = 1 << ns->lba_shift;
  774. /* XXX: PI implementation requires metadata equal t10 pi tuple size */
  775. pi_type = ns->ms == sizeof(struct t10_pi_tuple) ?
  776. id->dps & NVME_NS_DPS_PI_MASK : 0;
  777. blk_mq_freeze_queue(disk->queue);
  778. if (blk_get_integrity(disk) && (ns->pi_type != pi_type ||
  779. ns->ms != old_ms ||
  780. bs != queue_logical_block_size(disk->queue) ||
  781. (ns->ms && ns->ext)))
  782. blk_integrity_unregister(disk);
  783. ns->pi_type = pi_type;
  784. blk_queue_logical_block_size(ns->queue, bs);
  785. if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
  786. nvme_init_integrity(ns);
  787. if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
  788. set_capacity(disk, 0);
  789. else
  790. set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
  791. if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
  792. nvme_config_discard(ns);
  793. blk_mq_unfreeze_queue(disk->queue);
  794. }
  795. static int nvme_revalidate_disk(struct gendisk *disk)
  796. {
  797. struct nvme_ns *ns = disk->private_data;
  798. struct nvme_id_ns *id = NULL;
  799. int ret;
  800. if (test_bit(NVME_NS_DEAD, &ns->flags)) {
  801. set_capacity(disk, 0);
  802. return -ENODEV;
  803. }
  804. ret = nvme_revalidate_ns(ns, &id);
  805. if (ret)
  806. return ret;
  807. __nvme_revalidate_disk(disk, id);
  808. kfree(id);
  809. return 0;
  810. }
  811. static char nvme_pr_type(enum pr_type type)
  812. {
  813. switch (type) {
  814. case PR_WRITE_EXCLUSIVE:
  815. return 1;
  816. case PR_EXCLUSIVE_ACCESS:
  817. return 2;
  818. case PR_WRITE_EXCLUSIVE_REG_ONLY:
  819. return 3;
  820. case PR_EXCLUSIVE_ACCESS_REG_ONLY:
  821. return 4;
  822. case PR_WRITE_EXCLUSIVE_ALL_REGS:
  823. return 5;
  824. case PR_EXCLUSIVE_ACCESS_ALL_REGS:
  825. return 6;
  826. default:
  827. return 0;
  828. }
  829. };
  830. static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
  831. u64 key, u64 sa_key, u8 op)
  832. {
  833. struct nvme_ns *ns = bdev->bd_disk->private_data;
  834. struct nvme_command c;
  835. u8 data[16] = { 0, };
  836. put_unaligned_le64(key, &data[0]);
  837. put_unaligned_le64(sa_key, &data[8]);
  838. memset(&c, 0, sizeof(c));
  839. c.common.opcode = op;
  840. c.common.nsid = cpu_to_le32(ns->ns_id);
  841. c.common.cdw10[0] = cpu_to_le32(cdw10);
  842. return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
  843. }
  844. static int nvme_pr_register(struct block_device *bdev, u64 old,
  845. u64 new, unsigned flags)
  846. {
  847. u32 cdw10;
  848. if (flags & ~PR_FL_IGNORE_KEY)
  849. return -EOPNOTSUPP;
  850. cdw10 = old ? 2 : 0;
  851. cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
  852. cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
  853. return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
  854. }
  855. static int nvme_pr_reserve(struct block_device *bdev, u64 key,
  856. enum pr_type type, unsigned flags)
  857. {
  858. u32 cdw10;
  859. if (flags & ~PR_FL_IGNORE_KEY)
  860. return -EOPNOTSUPP;
  861. cdw10 = nvme_pr_type(type) << 8;
  862. cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
  863. return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
  864. }
  865. static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
  866. enum pr_type type, bool abort)
  867. {
  868. u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
  869. return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
  870. }
  871. static int nvme_pr_clear(struct block_device *bdev, u64 key)
  872. {
  873. u32 cdw10 = 1 | (key ? 1 << 3 : 0);
  874. return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
  875. }
  876. static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
  877. {
  878. u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
  879. return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
  880. }
  881. static const struct pr_ops nvme_pr_ops = {
  882. .pr_register = nvme_pr_register,
  883. .pr_reserve = nvme_pr_reserve,
  884. .pr_release = nvme_pr_release,
  885. .pr_preempt = nvme_pr_preempt,
  886. .pr_clear = nvme_pr_clear,
  887. };
  888. static const struct block_device_operations nvme_fops = {
  889. .owner = THIS_MODULE,
  890. .ioctl = nvme_ioctl,
  891. .compat_ioctl = nvme_compat_ioctl,
  892. .open = nvme_open,
  893. .release = nvme_release,
  894. .getgeo = nvme_getgeo,
  895. .revalidate_disk= nvme_revalidate_disk,
  896. .pr_ops = &nvme_pr_ops,
  897. };
  898. static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
  899. {
  900. unsigned long timeout =
  901. ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
  902. u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
  903. int ret;
  904. while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
  905. if (csts == ~0)
  906. return -ENODEV;
  907. if ((csts & NVME_CSTS_RDY) == bit)
  908. break;
  909. msleep(100);
  910. if (fatal_signal_pending(current))
  911. return -EINTR;
  912. if (time_after(jiffies, timeout)) {
  913. dev_err(ctrl->device,
  914. "Device not ready; aborting %s\n", enabled ?
  915. "initialisation" : "reset");
  916. return -ENODEV;
  917. }
  918. }
  919. return ret;
  920. }
  921. /*
  922. * If the device has been passed off to us in an enabled state, just clear
  923. * the enabled bit. The spec says we should set the 'shutdown notification
  924. * bits', but doing so may cause the device to complete commands to the
  925. * admin queue ... and we don't know what memory that might be pointing at!
  926. */
  927. int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
  928. {
  929. int ret;
  930. ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
  931. ctrl->ctrl_config &= ~NVME_CC_ENABLE;
  932. ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
  933. if (ret)
  934. return ret;
  935. if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
  936. msleep(NVME_QUIRK_DELAY_AMOUNT);
  937. return nvme_wait_ready(ctrl, cap, false);
  938. }
  939. EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
  940. int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
  941. {
  942. /*
  943. * Default to a 4K page size, with the intention to update this
  944. * path in the future to accomodate architectures with differing
  945. * kernel and IO page sizes.
  946. */
  947. unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
  948. int ret;
  949. if (page_shift < dev_page_min) {
  950. dev_err(ctrl->device,
  951. "Minimum device page size %u too large for host (%u)\n",
  952. 1 << dev_page_min, 1 << page_shift);
  953. return -ENODEV;
  954. }
  955. ctrl->page_size = 1 << page_shift;
  956. ctrl->ctrl_config = NVME_CC_CSS_NVM;
  957. ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
  958. ctrl->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
  959. ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
  960. ctrl->ctrl_config |= NVME_CC_ENABLE;
  961. ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
  962. if (ret)
  963. return ret;
  964. return nvme_wait_ready(ctrl, cap, true);
  965. }
  966. EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
  967. int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
  968. {
  969. unsigned long timeout = SHUTDOWN_TIMEOUT + jiffies;
  970. u32 csts;
  971. int ret;
  972. ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
  973. ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
  974. ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
  975. if (ret)
  976. return ret;
  977. while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
  978. if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
  979. break;
  980. msleep(100);
  981. if (fatal_signal_pending(current))
  982. return -EINTR;
  983. if (time_after(jiffies, timeout)) {
  984. dev_err(ctrl->device,
  985. "Device shutdown incomplete; abort shutdown\n");
  986. return -ENODEV;
  987. }
  988. }
  989. return ret;
  990. }
  991. EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
  992. static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
  993. struct request_queue *q)
  994. {
  995. bool vwc = false;
  996. if (ctrl->max_hw_sectors) {
  997. u32 max_segments =
  998. (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
  999. blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
  1000. blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
  1001. }
  1002. if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
  1003. blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
  1004. blk_queue_virt_boundary(q, ctrl->page_size - 1);
  1005. if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
  1006. vwc = true;
  1007. blk_queue_write_cache(q, vwc, vwc);
  1008. }
  1009. /*
  1010. * Initialize the cached copies of the Identify data and various controller
  1011. * register in our nvme_ctrl structure. This should be called as soon as
  1012. * the admin queue is fully up and running.
  1013. */
  1014. int nvme_init_identify(struct nvme_ctrl *ctrl)
  1015. {
  1016. struct nvme_id_ctrl *id;
  1017. u64 cap;
  1018. int ret, page_shift;
  1019. u32 max_hw_sectors;
  1020. ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
  1021. if (ret) {
  1022. dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
  1023. return ret;
  1024. }
  1025. ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
  1026. if (ret) {
  1027. dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
  1028. return ret;
  1029. }
  1030. page_shift = NVME_CAP_MPSMIN(cap) + 12;
  1031. if (ctrl->vs >= NVME_VS(1, 1, 0))
  1032. ctrl->subsystem = NVME_CAP_NSSRC(cap);
  1033. ret = nvme_identify_ctrl(ctrl, &id);
  1034. if (ret) {
  1035. dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
  1036. return -EIO;
  1037. }
  1038. ctrl->vid = le16_to_cpu(id->vid);
  1039. ctrl->oncs = le16_to_cpup(&id->oncs);
  1040. atomic_set(&ctrl->abort_limit, id->acl + 1);
  1041. ctrl->vwc = id->vwc;
  1042. ctrl->cntlid = le16_to_cpup(&id->cntlid);
  1043. memcpy(ctrl->serial, id->sn, sizeof(id->sn));
  1044. memcpy(ctrl->model, id->mn, sizeof(id->mn));
  1045. memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
  1046. if (id->mdts)
  1047. max_hw_sectors = 1 << (id->mdts + page_shift - 9);
  1048. else
  1049. max_hw_sectors = UINT_MAX;
  1050. ctrl->max_hw_sectors =
  1051. min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
  1052. nvme_set_queue_limits(ctrl, ctrl->admin_q);
  1053. ctrl->sgls = le32_to_cpu(id->sgls);
  1054. ctrl->kas = le16_to_cpu(id->kas);
  1055. if (ctrl->ops->is_fabrics) {
  1056. ctrl->icdoff = le16_to_cpu(id->icdoff);
  1057. ctrl->ioccsz = le32_to_cpu(id->ioccsz);
  1058. ctrl->iorcsz = le32_to_cpu(id->iorcsz);
  1059. ctrl->maxcmd = le16_to_cpu(id->maxcmd);
  1060. /*
  1061. * In fabrics we need to verify the cntlid matches the
  1062. * admin connect
  1063. */
  1064. if (ctrl->cntlid != le16_to_cpu(id->cntlid))
  1065. ret = -EINVAL;
  1066. if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
  1067. dev_err(ctrl->dev,
  1068. "keep-alive support is mandatory for fabrics\n");
  1069. ret = -EINVAL;
  1070. }
  1071. } else {
  1072. ctrl->cntlid = le16_to_cpu(id->cntlid);
  1073. }
  1074. kfree(id);
  1075. return ret;
  1076. }
  1077. EXPORT_SYMBOL_GPL(nvme_init_identify);
  1078. static int nvme_dev_open(struct inode *inode, struct file *file)
  1079. {
  1080. struct nvme_ctrl *ctrl;
  1081. int instance = iminor(inode);
  1082. int ret = -ENODEV;
  1083. spin_lock(&dev_list_lock);
  1084. list_for_each_entry(ctrl, &nvme_ctrl_list, node) {
  1085. if (ctrl->instance != instance)
  1086. continue;
  1087. if (!ctrl->admin_q) {
  1088. ret = -EWOULDBLOCK;
  1089. break;
  1090. }
  1091. if (!kref_get_unless_zero(&ctrl->kref))
  1092. break;
  1093. file->private_data = ctrl;
  1094. ret = 0;
  1095. break;
  1096. }
  1097. spin_unlock(&dev_list_lock);
  1098. return ret;
  1099. }
  1100. static int nvme_dev_release(struct inode *inode, struct file *file)
  1101. {
  1102. nvme_put_ctrl(file->private_data);
  1103. return 0;
  1104. }
  1105. static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
  1106. {
  1107. struct nvme_ns *ns;
  1108. int ret;
  1109. mutex_lock(&ctrl->namespaces_mutex);
  1110. if (list_empty(&ctrl->namespaces)) {
  1111. ret = -ENOTTY;
  1112. goto out_unlock;
  1113. }
  1114. ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
  1115. if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
  1116. dev_warn(ctrl->device,
  1117. "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
  1118. ret = -EINVAL;
  1119. goto out_unlock;
  1120. }
  1121. dev_warn(ctrl->device,
  1122. "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
  1123. kref_get(&ns->kref);
  1124. mutex_unlock(&ctrl->namespaces_mutex);
  1125. ret = nvme_user_cmd(ctrl, ns, argp);
  1126. nvme_put_ns(ns);
  1127. return ret;
  1128. out_unlock:
  1129. mutex_unlock(&ctrl->namespaces_mutex);
  1130. return ret;
  1131. }
  1132. static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
  1133. unsigned long arg)
  1134. {
  1135. struct nvme_ctrl *ctrl = file->private_data;
  1136. void __user *argp = (void __user *)arg;
  1137. switch (cmd) {
  1138. case NVME_IOCTL_ADMIN_CMD:
  1139. return nvme_user_cmd(ctrl, NULL, argp);
  1140. case NVME_IOCTL_IO_CMD:
  1141. return nvme_dev_user_cmd(ctrl, argp);
  1142. case NVME_IOCTL_RESET:
  1143. dev_warn(ctrl->device, "resetting controller\n");
  1144. return ctrl->ops->reset_ctrl(ctrl);
  1145. case NVME_IOCTL_SUBSYS_RESET:
  1146. return nvme_reset_subsystem(ctrl);
  1147. case NVME_IOCTL_RESCAN:
  1148. nvme_queue_scan(ctrl);
  1149. return 0;
  1150. default:
  1151. return -ENOTTY;
  1152. }
  1153. }
  1154. static const struct file_operations nvme_dev_fops = {
  1155. .owner = THIS_MODULE,
  1156. .open = nvme_dev_open,
  1157. .release = nvme_dev_release,
  1158. .unlocked_ioctl = nvme_dev_ioctl,
  1159. .compat_ioctl = nvme_dev_ioctl,
  1160. };
  1161. static ssize_t nvme_sysfs_reset(struct device *dev,
  1162. struct device_attribute *attr, const char *buf,
  1163. size_t count)
  1164. {
  1165. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  1166. int ret;
  1167. ret = ctrl->ops->reset_ctrl(ctrl);
  1168. if (ret < 0)
  1169. return ret;
  1170. return count;
  1171. }
  1172. static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
  1173. static ssize_t nvme_sysfs_rescan(struct device *dev,
  1174. struct device_attribute *attr, const char *buf,
  1175. size_t count)
  1176. {
  1177. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  1178. nvme_queue_scan(ctrl);
  1179. return count;
  1180. }
  1181. static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
  1182. static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
  1183. char *buf)
  1184. {
  1185. struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
  1186. struct nvme_ctrl *ctrl = ns->ctrl;
  1187. int serial_len = sizeof(ctrl->serial);
  1188. int model_len = sizeof(ctrl->model);
  1189. if (memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
  1190. return sprintf(buf, "eui.%16phN\n", ns->uuid);
  1191. if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
  1192. return sprintf(buf, "eui.%8phN\n", ns->eui);
  1193. while (ctrl->serial[serial_len - 1] == ' ')
  1194. serial_len--;
  1195. while (ctrl->model[model_len - 1] == ' ')
  1196. model_len--;
  1197. return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
  1198. serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
  1199. }
  1200. static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
  1201. static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
  1202. char *buf)
  1203. {
  1204. struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
  1205. return sprintf(buf, "%pU\n", ns->uuid);
  1206. }
  1207. static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
  1208. static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
  1209. char *buf)
  1210. {
  1211. struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
  1212. return sprintf(buf, "%8phd\n", ns->eui);
  1213. }
  1214. static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
  1215. static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
  1216. char *buf)
  1217. {
  1218. struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
  1219. return sprintf(buf, "%d\n", ns->ns_id);
  1220. }
  1221. static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
  1222. static struct attribute *nvme_ns_attrs[] = {
  1223. &dev_attr_wwid.attr,
  1224. &dev_attr_uuid.attr,
  1225. &dev_attr_eui.attr,
  1226. &dev_attr_nsid.attr,
  1227. NULL,
  1228. };
  1229. static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
  1230. struct attribute *a, int n)
  1231. {
  1232. struct device *dev = container_of(kobj, struct device, kobj);
  1233. struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
  1234. if (a == &dev_attr_uuid.attr) {
  1235. if (!memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
  1236. return 0;
  1237. }
  1238. if (a == &dev_attr_eui.attr) {
  1239. if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
  1240. return 0;
  1241. }
  1242. return a->mode;
  1243. }
  1244. static const struct attribute_group nvme_ns_attr_group = {
  1245. .attrs = nvme_ns_attrs,
  1246. .is_visible = nvme_ns_attrs_are_visible,
  1247. };
  1248. #define nvme_show_str_function(field) \
  1249. static ssize_t field##_show(struct device *dev, \
  1250. struct device_attribute *attr, char *buf) \
  1251. { \
  1252. struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
  1253. return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
  1254. } \
  1255. static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
  1256. #define nvme_show_int_function(field) \
  1257. static ssize_t field##_show(struct device *dev, \
  1258. struct device_attribute *attr, char *buf) \
  1259. { \
  1260. struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
  1261. return sprintf(buf, "%d\n", ctrl->field); \
  1262. } \
  1263. static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
  1264. nvme_show_str_function(model);
  1265. nvme_show_str_function(serial);
  1266. nvme_show_str_function(firmware_rev);
  1267. nvme_show_int_function(cntlid);
  1268. static ssize_t nvme_sysfs_delete(struct device *dev,
  1269. struct device_attribute *attr, const char *buf,
  1270. size_t count)
  1271. {
  1272. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  1273. if (device_remove_file_self(dev, attr))
  1274. ctrl->ops->delete_ctrl(ctrl);
  1275. return count;
  1276. }
  1277. static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
  1278. static ssize_t nvme_sysfs_show_transport(struct device *dev,
  1279. struct device_attribute *attr,
  1280. char *buf)
  1281. {
  1282. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  1283. return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
  1284. }
  1285. static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
  1286. static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
  1287. struct device_attribute *attr,
  1288. char *buf)
  1289. {
  1290. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  1291. return snprintf(buf, PAGE_SIZE, "%s\n",
  1292. ctrl->ops->get_subsysnqn(ctrl));
  1293. }
  1294. static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
  1295. static ssize_t nvme_sysfs_show_address(struct device *dev,
  1296. struct device_attribute *attr,
  1297. char *buf)
  1298. {
  1299. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  1300. return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
  1301. }
  1302. static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
  1303. static struct attribute *nvme_dev_attrs[] = {
  1304. &dev_attr_reset_controller.attr,
  1305. &dev_attr_rescan_controller.attr,
  1306. &dev_attr_model.attr,
  1307. &dev_attr_serial.attr,
  1308. &dev_attr_firmware_rev.attr,
  1309. &dev_attr_cntlid.attr,
  1310. &dev_attr_delete_controller.attr,
  1311. &dev_attr_transport.attr,
  1312. &dev_attr_subsysnqn.attr,
  1313. &dev_attr_address.attr,
  1314. NULL
  1315. };
  1316. #define CHECK_ATTR(ctrl, a, name) \
  1317. if ((a) == &dev_attr_##name.attr && \
  1318. !(ctrl)->ops->get_##name) \
  1319. return 0
  1320. static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
  1321. struct attribute *a, int n)
  1322. {
  1323. struct device *dev = container_of(kobj, struct device, kobj);
  1324. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  1325. if (a == &dev_attr_delete_controller.attr) {
  1326. if (!ctrl->ops->delete_ctrl)
  1327. return 0;
  1328. }
  1329. CHECK_ATTR(ctrl, a, subsysnqn);
  1330. CHECK_ATTR(ctrl, a, address);
  1331. return a->mode;
  1332. }
  1333. static struct attribute_group nvme_dev_attrs_group = {
  1334. .attrs = nvme_dev_attrs,
  1335. .is_visible = nvme_dev_attrs_are_visible,
  1336. };
  1337. static const struct attribute_group *nvme_dev_attr_groups[] = {
  1338. &nvme_dev_attrs_group,
  1339. NULL,
  1340. };
  1341. static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
  1342. {
  1343. struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
  1344. struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
  1345. return nsa->ns_id - nsb->ns_id;
  1346. }
  1347. static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
  1348. {
  1349. struct nvme_ns *ns, *ret = NULL;
  1350. mutex_lock(&ctrl->namespaces_mutex);
  1351. list_for_each_entry(ns, &ctrl->namespaces, list) {
  1352. if (ns->ns_id == nsid) {
  1353. kref_get(&ns->kref);
  1354. ret = ns;
  1355. break;
  1356. }
  1357. if (ns->ns_id > nsid)
  1358. break;
  1359. }
  1360. mutex_unlock(&ctrl->namespaces_mutex);
  1361. return ret;
  1362. }
  1363. static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
  1364. {
  1365. struct nvme_ns *ns;
  1366. struct gendisk *disk;
  1367. struct nvme_id_ns *id;
  1368. char disk_name[DISK_NAME_LEN];
  1369. int node = dev_to_node(ctrl->dev);
  1370. ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
  1371. if (!ns)
  1372. return;
  1373. ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
  1374. if (ns->instance < 0)
  1375. goto out_free_ns;
  1376. ns->queue = blk_mq_init_queue(ctrl->tagset);
  1377. if (IS_ERR(ns->queue))
  1378. goto out_release_instance;
  1379. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
  1380. ns->queue->queuedata = ns;
  1381. ns->ctrl = ctrl;
  1382. kref_init(&ns->kref);
  1383. ns->ns_id = nsid;
  1384. ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
  1385. blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
  1386. nvme_set_queue_limits(ctrl, ns->queue);
  1387. sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
  1388. if (nvme_revalidate_ns(ns, &id))
  1389. goto out_free_queue;
  1390. if (nvme_nvm_ns_supported(ns, id) &&
  1391. nvme_nvm_register(ns, disk_name, node)) {
  1392. dev_warn(ctrl->dev, "%s: LightNVM init failure\n", __func__);
  1393. goto out_free_id;
  1394. }
  1395. disk = alloc_disk_node(0, node);
  1396. if (!disk)
  1397. goto out_free_id;
  1398. disk->fops = &nvme_fops;
  1399. disk->private_data = ns;
  1400. disk->queue = ns->queue;
  1401. disk->flags = GENHD_FL_EXT_DEVT;
  1402. memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
  1403. ns->disk = disk;
  1404. __nvme_revalidate_disk(disk, id);
  1405. mutex_lock(&ctrl->namespaces_mutex);
  1406. list_add_tail(&ns->list, &ctrl->namespaces);
  1407. mutex_unlock(&ctrl->namespaces_mutex);
  1408. kref_get(&ctrl->kref);
  1409. kfree(id);
  1410. device_add_disk(ctrl->device, ns->disk);
  1411. if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
  1412. &nvme_ns_attr_group))
  1413. pr_warn("%s: failed to create sysfs group for identification\n",
  1414. ns->disk->disk_name);
  1415. if (ns->ndev && nvme_nvm_register_sysfs(ns))
  1416. pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
  1417. ns->disk->disk_name);
  1418. return;
  1419. out_free_id:
  1420. kfree(id);
  1421. out_free_queue:
  1422. blk_cleanup_queue(ns->queue);
  1423. out_release_instance:
  1424. ida_simple_remove(&ctrl->ns_ida, ns->instance);
  1425. out_free_ns:
  1426. kfree(ns);
  1427. }
  1428. static void nvme_ns_remove(struct nvme_ns *ns)
  1429. {
  1430. if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
  1431. return;
  1432. if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
  1433. if (blk_get_integrity(ns->disk))
  1434. blk_integrity_unregister(ns->disk);
  1435. sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
  1436. &nvme_ns_attr_group);
  1437. if (ns->ndev)
  1438. nvme_nvm_unregister_sysfs(ns);
  1439. del_gendisk(ns->disk);
  1440. blk_mq_abort_requeue_list(ns->queue);
  1441. blk_cleanup_queue(ns->queue);
  1442. }
  1443. mutex_lock(&ns->ctrl->namespaces_mutex);
  1444. list_del_init(&ns->list);
  1445. mutex_unlock(&ns->ctrl->namespaces_mutex);
  1446. nvme_put_ns(ns);
  1447. }
  1448. static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
  1449. {
  1450. struct nvme_ns *ns;
  1451. ns = nvme_find_get_ns(ctrl, nsid);
  1452. if (ns) {
  1453. if (ns->disk && revalidate_disk(ns->disk))
  1454. nvme_ns_remove(ns);
  1455. nvme_put_ns(ns);
  1456. } else
  1457. nvme_alloc_ns(ctrl, nsid);
  1458. }
  1459. static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
  1460. unsigned nsid)
  1461. {
  1462. struct nvme_ns *ns, *next;
  1463. list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
  1464. if (ns->ns_id > nsid)
  1465. nvme_ns_remove(ns);
  1466. }
  1467. }
  1468. static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
  1469. {
  1470. struct nvme_ns *ns;
  1471. __le32 *ns_list;
  1472. unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
  1473. int ret = 0;
  1474. ns_list = kzalloc(0x1000, GFP_KERNEL);
  1475. if (!ns_list)
  1476. return -ENOMEM;
  1477. for (i = 0; i < num_lists; i++) {
  1478. ret = nvme_identify_ns_list(ctrl, prev, ns_list);
  1479. if (ret)
  1480. goto free;
  1481. for (j = 0; j < min(nn, 1024U); j++) {
  1482. nsid = le32_to_cpu(ns_list[j]);
  1483. if (!nsid)
  1484. goto out;
  1485. nvme_validate_ns(ctrl, nsid);
  1486. while (++prev < nsid) {
  1487. ns = nvme_find_get_ns(ctrl, prev);
  1488. if (ns) {
  1489. nvme_ns_remove(ns);
  1490. nvme_put_ns(ns);
  1491. }
  1492. }
  1493. }
  1494. nn -= j;
  1495. }
  1496. out:
  1497. nvme_remove_invalid_namespaces(ctrl, prev);
  1498. free:
  1499. kfree(ns_list);
  1500. return ret;
  1501. }
  1502. static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
  1503. {
  1504. unsigned i;
  1505. for (i = 1; i <= nn; i++)
  1506. nvme_validate_ns(ctrl, i);
  1507. nvme_remove_invalid_namespaces(ctrl, nn);
  1508. }
  1509. static void nvme_scan_work(struct work_struct *work)
  1510. {
  1511. struct nvme_ctrl *ctrl =
  1512. container_of(work, struct nvme_ctrl, scan_work);
  1513. struct nvme_id_ctrl *id;
  1514. unsigned nn;
  1515. if (ctrl->state != NVME_CTRL_LIVE)
  1516. return;
  1517. if (nvme_identify_ctrl(ctrl, &id))
  1518. return;
  1519. nn = le32_to_cpu(id->nn);
  1520. if (ctrl->vs >= NVME_VS(1, 1, 0) &&
  1521. !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
  1522. if (!nvme_scan_ns_list(ctrl, nn))
  1523. goto done;
  1524. }
  1525. nvme_scan_ns_sequential(ctrl, nn);
  1526. done:
  1527. mutex_lock(&ctrl->namespaces_mutex);
  1528. list_sort(NULL, &ctrl->namespaces, ns_cmp);
  1529. mutex_unlock(&ctrl->namespaces_mutex);
  1530. kfree(id);
  1531. }
  1532. void nvme_queue_scan(struct nvme_ctrl *ctrl)
  1533. {
  1534. /*
  1535. * Do not queue new scan work when a controller is reset during
  1536. * removal.
  1537. */
  1538. if (ctrl->state == NVME_CTRL_LIVE)
  1539. schedule_work(&ctrl->scan_work);
  1540. }
  1541. EXPORT_SYMBOL_GPL(nvme_queue_scan);
  1542. /*
  1543. * This function iterates the namespace list unlocked to allow recovery from
  1544. * controller failure. It is up to the caller to ensure the namespace list is
  1545. * not modified by scan work while this function is executing.
  1546. */
  1547. void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
  1548. {
  1549. struct nvme_ns *ns, *next;
  1550. /*
  1551. * The dead states indicates the controller was not gracefully
  1552. * disconnected. In that case, we won't be able to flush any data while
  1553. * removing the namespaces' disks; fail all the queues now to avoid
  1554. * potentially having to clean up the failed sync later.
  1555. */
  1556. if (ctrl->state == NVME_CTRL_DEAD)
  1557. nvme_kill_queues(ctrl);
  1558. list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
  1559. nvme_ns_remove(ns);
  1560. }
  1561. EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
  1562. static void nvme_async_event_work(struct work_struct *work)
  1563. {
  1564. struct nvme_ctrl *ctrl =
  1565. container_of(work, struct nvme_ctrl, async_event_work);
  1566. spin_lock_irq(&ctrl->lock);
  1567. while (ctrl->event_limit > 0) {
  1568. int aer_idx = --ctrl->event_limit;
  1569. spin_unlock_irq(&ctrl->lock);
  1570. ctrl->ops->submit_async_event(ctrl, aer_idx);
  1571. spin_lock_irq(&ctrl->lock);
  1572. }
  1573. spin_unlock_irq(&ctrl->lock);
  1574. }
  1575. void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
  1576. union nvme_result *res)
  1577. {
  1578. u32 result = le32_to_cpu(res->u32);
  1579. bool done = true;
  1580. switch (le16_to_cpu(status) >> 1) {
  1581. case NVME_SC_SUCCESS:
  1582. done = false;
  1583. /*FALLTHRU*/
  1584. case NVME_SC_ABORT_REQ:
  1585. ++ctrl->event_limit;
  1586. schedule_work(&ctrl->async_event_work);
  1587. break;
  1588. default:
  1589. break;
  1590. }
  1591. if (done)
  1592. return;
  1593. switch (result & 0xff07) {
  1594. case NVME_AER_NOTICE_NS_CHANGED:
  1595. dev_info(ctrl->device, "rescanning\n");
  1596. nvme_queue_scan(ctrl);
  1597. break;
  1598. default:
  1599. dev_warn(ctrl->device, "async event result %08x\n", result);
  1600. }
  1601. }
  1602. EXPORT_SYMBOL_GPL(nvme_complete_async_event);
  1603. void nvme_queue_async_events(struct nvme_ctrl *ctrl)
  1604. {
  1605. ctrl->event_limit = NVME_NR_AERS;
  1606. schedule_work(&ctrl->async_event_work);
  1607. }
  1608. EXPORT_SYMBOL_GPL(nvme_queue_async_events);
  1609. static DEFINE_IDA(nvme_instance_ida);
  1610. static int nvme_set_instance(struct nvme_ctrl *ctrl)
  1611. {
  1612. int instance, error;
  1613. do {
  1614. if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
  1615. return -ENODEV;
  1616. spin_lock(&dev_list_lock);
  1617. error = ida_get_new(&nvme_instance_ida, &instance);
  1618. spin_unlock(&dev_list_lock);
  1619. } while (error == -EAGAIN);
  1620. if (error)
  1621. return -ENODEV;
  1622. ctrl->instance = instance;
  1623. return 0;
  1624. }
  1625. static void nvme_release_instance(struct nvme_ctrl *ctrl)
  1626. {
  1627. spin_lock(&dev_list_lock);
  1628. ida_remove(&nvme_instance_ida, ctrl->instance);
  1629. spin_unlock(&dev_list_lock);
  1630. }
  1631. void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
  1632. {
  1633. flush_work(&ctrl->async_event_work);
  1634. flush_work(&ctrl->scan_work);
  1635. nvme_remove_namespaces(ctrl);
  1636. device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
  1637. spin_lock(&dev_list_lock);
  1638. list_del(&ctrl->node);
  1639. spin_unlock(&dev_list_lock);
  1640. }
  1641. EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
  1642. static void nvme_free_ctrl(struct kref *kref)
  1643. {
  1644. struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref);
  1645. put_device(ctrl->device);
  1646. nvme_release_instance(ctrl);
  1647. ida_destroy(&ctrl->ns_ida);
  1648. ctrl->ops->free_ctrl(ctrl);
  1649. }
  1650. void nvme_put_ctrl(struct nvme_ctrl *ctrl)
  1651. {
  1652. kref_put(&ctrl->kref, nvme_free_ctrl);
  1653. }
  1654. EXPORT_SYMBOL_GPL(nvme_put_ctrl);
  1655. /*
  1656. * Initialize a NVMe controller structures. This needs to be called during
  1657. * earliest initialization so that we have the initialized structured around
  1658. * during probing.
  1659. */
  1660. int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
  1661. const struct nvme_ctrl_ops *ops, unsigned long quirks)
  1662. {
  1663. int ret;
  1664. ctrl->state = NVME_CTRL_NEW;
  1665. spin_lock_init(&ctrl->lock);
  1666. INIT_LIST_HEAD(&ctrl->namespaces);
  1667. mutex_init(&ctrl->namespaces_mutex);
  1668. kref_init(&ctrl->kref);
  1669. ctrl->dev = dev;
  1670. ctrl->ops = ops;
  1671. ctrl->quirks = quirks;
  1672. INIT_WORK(&ctrl->scan_work, nvme_scan_work);
  1673. INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
  1674. ret = nvme_set_instance(ctrl);
  1675. if (ret)
  1676. goto out;
  1677. ctrl->device = device_create_with_groups(nvme_class, ctrl->dev,
  1678. MKDEV(nvme_char_major, ctrl->instance),
  1679. ctrl, nvme_dev_attr_groups,
  1680. "nvme%d", ctrl->instance);
  1681. if (IS_ERR(ctrl->device)) {
  1682. ret = PTR_ERR(ctrl->device);
  1683. goto out_release_instance;
  1684. }
  1685. get_device(ctrl->device);
  1686. ida_init(&ctrl->ns_ida);
  1687. spin_lock(&dev_list_lock);
  1688. list_add_tail(&ctrl->node, &nvme_ctrl_list);
  1689. spin_unlock(&dev_list_lock);
  1690. return 0;
  1691. out_release_instance:
  1692. nvme_release_instance(ctrl);
  1693. out:
  1694. return ret;
  1695. }
  1696. EXPORT_SYMBOL_GPL(nvme_init_ctrl);
  1697. /**
  1698. * nvme_kill_queues(): Ends all namespace queues
  1699. * @ctrl: the dead controller that needs to end
  1700. *
  1701. * Call this function when the driver determines it is unable to get the
  1702. * controller in a state capable of servicing IO.
  1703. */
  1704. void nvme_kill_queues(struct nvme_ctrl *ctrl)
  1705. {
  1706. struct nvme_ns *ns;
  1707. mutex_lock(&ctrl->namespaces_mutex);
  1708. list_for_each_entry(ns, &ctrl->namespaces, list) {
  1709. /*
  1710. * Revalidating a dead namespace sets capacity to 0. This will
  1711. * end buffered writers dirtying pages that can't be synced.
  1712. */
  1713. if (ns->disk && !test_and_set_bit(NVME_NS_DEAD, &ns->flags))
  1714. revalidate_disk(ns->disk);
  1715. blk_set_queue_dying(ns->queue);
  1716. blk_mq_abort_requeue_list(ns->queue);
  1717. blk_mq_start_stopped_hw_queues(ns->queue, true);
  1718. }
  1719. mutex_unlock(&ctrl->namespaces_mutex);
  1720. }
  1721. EXPORT_SYMBOL_GPL(nvme_kill_queues);
  1722. void nvme_stop_queues(struct nvme_ctrl *ctrl)
  1723. {
  1724. struct nvme_ns *ns;
  1725. mutex_lock(&ctrl->namespaces_mutex);
  1726. list_for_each_entry(ns, &ctrl->namespaces, list)
  1727. blk_mq_quiesce_queue(ns->queue);
  1728. mutex_unlock(&ctrl->namespaces_mutex);
  1729. }
  1730. EXPORT_SYMBOL_GPL(nvme_stop_queues);
  1731. void nvme_start_queues(struct nvme_ctrl *ctrl)
  1732. {
  1733. struct nvme_ns *ns;
  1734. mutex_lock(&ctrl->namespaces_mutex);
  1735. list_for_each_entry(ns, &ctrl->namespaces, list) {
  1736. blk_mq_start_stopped_hw_queues(ns->queue, true);
  1737. blk_mq_kick_requeue_list(ns->queue);
  1738. }
  1739. mutex_unlock(&ctrl->namespaces_mutex);
  1740. }
  1741. EXPORT_SYMBOL_GPL(nvme_start_queues);
  1742. int __init nvme_core_init(void)
  1743. {
  1744. int result;
  1745. result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
  1746. &nvme_dev_fops);
  1747. if (result < 0)
  1748. return result;
  1749. else if (result > 0)
  1750. nvme_char_major = result;
  1751. nvme_class = class_create(THIS_MODULE, "nvme");
  1752. if (IS_ERR(nvme_class)) {
  1753. result = PTR_ERR(nvme_class);
  1754. goto unregister_chrdev;
  1755. }
  1756. return 0;
  1757. unregister_chrdev:
  1758. __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
  1759. return result;
  1760. }
  1761. void nvme_core_exit(void)
  1762. {
  1763. class_destroy(nvme_class);
  1764. __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
  1765. }
  1766. MODULE_LICENSE("GPL");
  1767. MODULE_VERSION("1.0");
  1768. module_init(nvme_core_init);
  1769. module_exit(nvme_core_exit);