nvme-core.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978
  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/nvme.h>
  15. #include <linux/bio.h>
  16. #include <linux/bitops.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/cpu.h>
  19. #include <linux/delay.h>
  20. #include <linux/errno.h>
  21. #include <linux/fs.h>
  22. #include <linux/genhd.h>
  23. #include <linux/hdreg.h>
  24. #include <linux/idr.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/io.h>
  28. #include <linux/kdev_t.h>
  29. #include <linux/kthread.h>
  30. #include <linux/kernel.h>
  31. #include <linux/mm.h>
  32. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/pci.h>
  35. #include <linux/percpu.h>
  36. #include <linux/poison.h>
  37. #include <linux/ptrace.h>
  38. #include <linux/sched.h>
  39. #include <linux/slab.h>
  40. #include <linux/types.h>
  41. #include <scsi/sg.h>
  42. #include <asm-generic/io-64-nonatomic-lo-hi.h>
  43. #include <trace/events/block.h>
  44. #define NVME_Q_DEPTH 1024
  45. #define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
  46. #define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
  47. #define ADMIN_TIMEOUT (admin_timeout * HZ)
  48. #define IOD_TIMEOUT (retry_time * HZ)
  49. static unsigned char admin_timeout = 60;
  50. module_param(admin_timeout, byte, 0644);
  51. MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
  52. unsigned char nvme_io_timeout = 30;
  53. module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
  54. MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
  55. static unsigned char retry_time = 30;
  56. module_param(retry_time, byte, 0644);
  57. MODULE_PARM_DESC(retry_time, "time in seconds to retry failed I/O");
  58. static int nvme_major;
  59. module_param(nvme_major, int, 0);
  60. static int use_threaded_interrupts;
  61. module_param(use_threaded_interrupts, int, 0);
  62. static DEFINE_SPINLOCK(dev_list_lock);
  63. static LIST_HEAD(dev_list);
  64. static struct task_struct *nvme_thread;
  65. static struct workqueue_struct *nvme_workq;
  66. static wait_queue_head_t nvme_kthread_wait;
  67. static struct notifier_block nvme_nb;
  68. static void nvme_reset_failed_dev(struct work_struct *ws);
  69. struct async_cmd_info {
  70. struct kthread_work work;
  71. struct kthread_worker *worker;
  72. u32 result;
  73. int status;
  74. void *ctx;
  75. };
  76. /*
  77. * An NVM Express queue. Each device has at least two (one for admin
  78. * commands and one for I/O commands).
  79. */
  80. struct nvme_queue {
  81. struct rcu_head r_head;
  82. struct device *q_dmadev;
  83. struct nvme_dev *dev;
  84. char irqname[24]; /* nvme4294967295-65535\0 */
  85. spinlock_t q_lock;
  86. struct nvme_command *sq_cmds;
  87. volatile struct nvme_completion *cqes;
  88. dma_addr_t sq_dma_addr;
  89. dma_addr_t cq_dma_addr;
  90. wait_queue_head_t sq_full;
  91. wait_queue_t sq_cong_wait;
  92. struct bio_list sq_cong;
  93. struct list_head iod_bio;
  94. u32 __iomem *q_db;
  95. u16 q_depth;
  96. u16 cq_vector;
  97. u16 sq_head;
  98. u16 sq_tail;
  99. u16 cq_head;
  100. u16 qid;
  101. u8 cq_phase;
  102. u8 cqe_seen;
  103. u8 q_suspended;
  104. cpumask_var_t cpu_mask;
  105. struct async_cmd_info cmdinfo;
  106. unsigned long cmdid_data[];
  107. };
  108. /*
  109. * Check we didin't inadvertently grow the command struct
  110. */
  111. static inline void _nvme_check_size(void)
  112. {
  113. BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
  114. BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
  115. BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
  116. BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
  117. BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
  118. BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
  119. BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
  120. BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
  121. BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
  122. BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
  123. BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
  124. BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
  125. }
  126. typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
  127. struct nvme_completion *);
  128. struct nvme_cmd_info {
  129. nvme_completion_fn fn;
  130. void *ctx;
  131. unsigned long timeout;
  132. int aborted;
  133. };
  134. static struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq)
  135. {
  136. return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)];
  137. }
  138. static unsigned nvme_queue_extra(int depth)
  139. {
  140. return DIV_ROUND_UP(depth, 8) + (depth * sizeof(struct nvme_cmd_info));
  141. }
  142. /**
  143. * alloc_cmdid() - Allocate a Command ID
  144. * @nvmeq: The queue that will be used for this command
  145. * @ctx: A pointer that will be passed to the handler
  146. * @handler: The function to call on completion
  147. *
  148. * Allocate a Command ID for a queue. The data passed in will
  149. * be passed to the completion handler. This is implemented by using
  150. * the bottom two bits of the ctx pointer to store the handler ID.
  151. * Passing in a pointer that's not 4-byte aligned will cause a BUG.
  152. * We can change this if it becomes a problem.
  153. *
  154. * May be called with local interrupts disabled and the q_lock held,
  155. * or with interrupts enabled and no locks held.
  156. */
  157. static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx,
  158. nvme_completion_fn handler, unsigned timeout)
  159. {
  160. int depth = nvmeq->q_depth - 1;
  161. struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
  162. int cmdid;
  163. do {
  164. cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
  165. if (cmdid >= depth)
  166. return -EBUSY;
  167. } while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
  168. info[cmdid].fn = handler;
  169. info[cmdid].ctx = ctx;
  170. info[cmdid].timeout = jiffies + timeout;
  171. info[cmdid].aborted = 0;
  172. return cmdid;
  173. }
  174. static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
  175. nvme_completion_fn handler, unsigned timeout)
  176. {
  177. int cmdid;
  178. wait_event_killable(nvmeq->sq_full,
  179. (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0);
  180. return (cmdid < 0) ? -EINTR : cmdid;
  181. }
  182. /* Special values must be less than 0x1000 */
  183. #define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
  184. #define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
  185. #define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
  186. #define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
  187. #define CMD_CTX_ABORT (0x318 + CMD_CTX_BASE)
  188. static void special_completion(struct nvme_queue *nvmeq, void *ctx,
  189. struct nvme_completion *cqe)
  190. {
  191. if (ctx == CMD_CTX_CANCELLED)
  192. return;
  193. if (ctx == CMD_CTX_ABORT) {
  194. ++nvmeq->dev->abort_limit;
  195. return;
  196. }
  197. if (ctx == CMD_CTX_COMPLETED) {
  198. dev_warn(nvmeq->q_dmadev,
  199. "completed id %d twice on queue %d\n",
  200. cqe->command_id, le16_to_cpup(&cqe->sq_id));
  201. return;
  202. }
  203. if (ctx == CMD_CTX_INVALID) {
  204. dev_warn(nvmeq->q_dmadev,
  205. "invalid id %d completed on queue %d\n",
  206. cqe->command_id, le16_to_cpup(&cqe->sq_id));
  207. return;
  208. }
  209. dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
  210. }
  211. static void async_completion(struct nvme_queue *nvmeq, void *ctx,
  212. struct nvme_completion *cqe)
  213. {
  214. struct async_cmd_info *cmdinfo = ctx;
  215. cmdinfo->result = le32_to_cpup(&cqe->result);
  216. cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
  217. queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
  218. }
  219. /*
  220. * Called with local interrupts disabled and the q_lock held. May not sleep.
  221. */
  222. static void *free_cmdid(struct nvme_queue *nvmeq, int cmdid,
  223. nvme_completion_fn *fn)
  224. {
  225. void *ctx;
  226. struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
  227. if (cmdid >= nvmeq->q_depth || !info[cmdid].fn) {
  228. if (fn)
  229. *fn = special_completion;
  230. return CMD_CTX_INVALID;
  231. }
  232. if (fn)
  233. *fn = info[cmdid].fn;
  234. ctx = info[cmdid].ctx;
  235. info[cmdid].fn = special_completion;
  236. info[cmdid].ctx = CMD_CTX_COMPLETED;
  237. clear_bit(cmdid, nvmeq->cmdid_data);
  238. wake_up(&nvmeq->sq_full);
  239. return ctx;
  240. }
  241. static void *cancel_cmdid(struct nvme_queue *nvmeq, int cmdid,
  242. nvme_completion_fn *fn)
  243. {
  244. void *ctx;
  245. struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
  246. if (fn)
  247. *fn = info[cmdid].fn;
  248. ctx = info[cmdid].ctx;
  249. info[cmdid].fn = special_completion;
  250. info[cmdid].ctx = CMD_CTX_CANCELLED;
  251. return ctx;
  252. }
  253. static struct nvme_queue *raw_nvmeq(struct nvme_dev *dev, int qid)
  254. {
  255. return rcu_dereference_raw(dev->queues[qid]);
  256. }
  257. static struct nvme_queue *get_nvmeq(struct nvme_dev *dev) __acquires(RCU)
  258. {
  259. struct nvme_queue *nvmeq;
  260. unsigned queue_id = get_cpu_var(*dev->io_queue);
  261. rcu_read_lock();
  262. nvmeq = rcu_dereference(dev->queues[queue_id]);
  263. if (nvmeq)
  264. return nvmeq;
  265. rcu_read_unlock();
  266. put_cpu_var(*dev->io_queue);
  267. return NULL;
  268. }
  269. static void put_nvmeq(struct nvme_queue *nvmeq) __releases(RCU)
  270. {
  271. rcu_read_unlock();
  272. put_cpu_var(nvmeq->dev->io_queue);
  273. }
  274. static struct nvme_queue *lock_nvmeq(struct nvme_dev *dev, int q_idx)
  275. __acquires(RCU)
  276. {
  277. struct nvme_queue *nvmeq;
  278. rcu_read_lock();
  279. nvmeq = rcu_dereference(dev->queues[q_idx]);
  280. if (nvmeq)
  281. return nvmeq;
  282. rcu_read_unlock();
  283. return NULL;
  284. }
  285. static void unlock_nvmeq(struct nvme_queue *nvmeq) __releases(RCU)
  286. {
  287. rcu_read_unlock();
  288. }
  289. /**
  290. * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
  291. * @nvmeq: The queue to use
  292. * @cmd: The command to send
  293. *
  294. * Safe to use from interrupt context
  295. */
  296. static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
  297. {
  298. unsigned long flags;
  299. u16 tail;
  300. spin_lock_irqsave(&nvmeq->q_lock, flags);
  301. if (nvmeq->q_suspended) {
  302. spin_unlock_irqrestore(&nvmeq->q_lock, flags);
  303. return -EBUSY;
  304. }
  305. tail = nvmeq->sq_tail;
  306. memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
  307. if (++tail == nvmeq->q_depth)
  308. tail = 0;
  309. writel(tail, nvmeq->q_db);
  310. nvmeq->sq_tail = tail;
  311. spin_unlock_irqrestore(&nvmeq->q_lock, flags);
  312. return 0;
  313. }
  314. static __le64 **iod_list(struct nvme_iod *iod)
  315. {
  316. return ((void *)iod) + iod->offset;
  317. }
  318. /*
  319. * Will slightly overestimate the number of pages needed. This is OK
  320. * as it only leads to a small amount of wasted memory for the lifetime of
  321. * the I/O.
  322. */
  323. static int nvme_npages(unsigned size)
  324. {
  325. unsigned nprps = DIV_ROUND_UP(size + PAGE_SIZE, PAGE_SIZE);
  326. return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
  327. }
  328. static struct nvme_iod *
  329. nvme_alloc_iod(unsigned nseg, unsigned nbytes, gfp_t gfp)
  330. {
  331. struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
  332. sizeof(__le64 *) * nvme_npages(nbytes) +
  333. sizeof(struct scatterlist) * nseg, gfp);
  334. if (iod) {
  335. iod->offset = offsetof(struct nvme_iod, sg[nseg]);
  336. iod->npages = -1;
  337. iod->length = nbytes;
  338. iod->nents = 0;
  339. iod->first_dma = 0ULL;
  340. iod->start_time = jiffies;
  341. }
  342. return iod;
  343. }
  344. void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
  345. {
  346. const int last_prp = PAGE_SIZE / 8 - 1;
  347. int i;
  348. __le64 **list = iod_list(iod);
  349. dma_addr_t prp_dma = iod->first_dma;
  350. if (iod->npages == 0)
  351. dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
  352. for (i = 0; i < iod->npages; i++) {
  353. __le64 *prp_list = list[i];
  354. dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
  355. dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
  356. prp_dma = next_prp_dma;
  357. }
  358. kfree(iod);
  359. }
  360. static void nvme_start_io_acct(struct bio *bio)
  361. {
  362. struct gendisk *disk = bio->bi_bdev->bd_disk;
  363. if (blk_queue_io_stat(disk->queue)) {
  364. const int rw = bio_data_dir(bio);
  365. int cpu = part_stat_lock();
  366. part_round_stats(cpu, &disk->part0);
  367. part_stat_inc(cpu, &disk->part0, ios[rw]);
  368. part_stat_add(cpu, &disk->part0, sectors[rw],
  369. bio_sectors(bio));
  370. part_inc_in_flight(&disk->part0, rw);
  371. part_stat_unlock();
  372. }
  373. }
  374. static void nvme_end_io_acct(struct bio *bio, unsigned long start_time)
  375. {
  376. struct gendisk *disk = bio->bi_bdev->bd_disk;
  377. if (blk_queue_io_stat(disk->queue)) {
  378. const int rw = bio_data_dir(bio);
  379. unsigned long duration = jiffies - start_time;
  380. int cpu = part_stat_lock();
  381. part_stat_add(cpu, &disk->part0, ticks[rw], duration);
  382. part_round_stats(cpu, &disk->part0);
  383. part_dec_in_flight(&disk->part0, rw);
  384. part_stat_unlock();
  385. }
  386. }
  387. static void bio_completion(struct nvme_queue *nvmeq, void *ctx,
  388. struct nvme_completion *cqe)
  389. {
  390. struct nvme_iod *iod = ctx;
  391. struct bio *bio = iod->private;
  392. u16 status = le16_to_cpup(&cqe->status) >> 1;
  393. int error = 0;
  394. if (unlikely(status)) {
  395. if (!(status & NVME_SC_DNR ||
  396. bio->bi_rw & REQ_FAILFAST_MASK) &&
  397. (jiffies - iod->start_time) < IOD_TIMEOUT) {
  398. if (!waitqueue_active(&nvmeq->sq_full))
  399. add_wait_queue(&nvmeq->sq_full,
  400. &nvmeq->sq_cong_wait);
  401. list_add_tail(&iod->node, &nvmeq->iod_bio);
  402. wake_up(&nvmeq->sq_full);
  403. return;
  404. }
  405. error = -EIO;
  406. }
  407. if (iod->nents) {
  408. dma_unmap_sg(nvmeq->q_dmadev, iod->sg, iod->nents,
  409. bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  410. nvme_end_io_acct(bio, iod->start_time);
  411. }
  412. nvme_free_iod(nvmeq->dev, iod);
  413. trace_block_bio_complete(bdev_get_queue(bio->bi_bdev), bio, error);
  414. bio_endio(bio, error);
  415. }
  416. /* length is in bytes. gfp flags indicates whether we may sleep. */
  417. int nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod, int total_len,
  418. gfp_t gfp)
  419. {
  420. struct dma_pool *pool;
  421. int length = total_len;
  422. struct scatterlist *sg = iod->sg;
  423. int dma_len = sg_dma_len(sg);
  424. u64 dma_addr = sg_dma_address(sg);
  425. int offset = offset_in_page(dma_addr);
  426. __le64 *prp_list;
  427. __le64 **list = iod_list(iod);
  428. dma_addr_t prp_dma;
  429. int nprps, i;
  430. length -= (PAGE_SIZE - offset);
  431. if (length <= 0)
  432. return total_len;
  433. dma_len -= (PAGE_SIZE - offset);
  434. if (dma_len) {
  435. dma_addr += (PAGE_SIZE - offset);
  436. } else {
  437. sg = sg_next(sg);
  438. dma_addr = sg_dma_address(sg);
  439. dma_len = sg_dma_len(sg);
  440. }
  441. if (length <= PAGE_SIZE) {
  442. iod->first_dma = dma_addr;
  443. return total_len;
  444. }
  445. nprps = DIV_ROUND_UP(length, PAGE_SIZE);
  446. if (nprps <= (256 / 8)) {
  447. pool = dev->prp_small_pool;
  448. iod->npages = 0;
  449. } else {
  450. pool = dev->prp_page_pool;
  451. iod->npages = 1;
  452. }
  453. prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
  454. if (!prp_list) {
  455. iod->first_dma = dma_addr;
  456. iod->npages = -1;
  457. return (total_len - length) + PAGE_SIZE;
  458. }
  459. list[0] = prp_list;
  460. iod->first_dma = prp_dma;
  461. i = 0;
  462. for (;;) {
  463. if (i == PAGE_SIZE / 8) {
  464. __le64 *old_prp_list = prp_list;
  465. prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
  466. if (!prp_list)
  467. return total_len - length;
  468. list[iod->npages++] = prp_list;
  469. prp_list[0] = old_prp_list[i - 1];
  470. old_prp_list[i - 1] = cpu_to_le64(prp_dma);
  471. i = 1;
  472. }
  473. prp_list[i++] = cpu_to_le64(dma_addr);
  474. dma_len -= PAGE_SIZE;
  475. dma_addr += PAGE_SIZE;
  476. length -= PAGE_SIZE;
  477. if (length <= 0)
  478. break;
  479. if (dma_len > 0)
  480. continue;
  481. BUG_ON(dma_len < 0);
  482. sg = sg_next(sg);
  483. dma_addr = sg_dma_address(sg);
  484. dma_len = sg_dma_len(sg);
  485. }
  486. return total_len;
  487. }
  488. static int nvme_split_and_submit(struct bio *bio, struct nvme_queue *nvmeq,
  489. int len)
  490. {
  491. struct bio *split = bio_split(bio, len >> 9, GFP_ATOMIC, NULL);
  492. if (!split)
  493. return -ENOMEM;
  494. trace_block_split(bdev_get_queue(bio->bi_bdev), bio,
  495. split->bi_iter.bi_sector);
  496. bio_chain(split, bio);
  497. if (!waitqueue_active(&nvmeq->sq_full))
  498. add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
  499. bio_list_add(&nvmeq->sq_cong, split);
  500. bio_list_add(&nvmeq->sq_cong, bio);
  501. wake_up(&nvmeq->sq_full);
  502. return 0;
  503. }
  504. /* NVMe scatterlists require no holes in the virtual address */
  505. #define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \
  506. (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE))
  507. static int nvme_map_bio(struct nvme_queue *nvmeq, struct nvme_iod *iod,
  508. struct bio *bio, enum dma_data_direction dma_dir, int psegs)
  509. {
  510. struct bio_vec bvec, bvprv;
  511. struct bvec_iter iter;
  512. struct scatterlist *sg = NULL;
  513. int length = 0, nsegs = 0, split_len = bio->bi_iter.bi_size;
  514. int first = 1;
  515. if (nvmeq->dev->stripe_size)
  516. split_len = nvmeq->dev->stripe_size -
  517. ((bio->bi_iter.bi_sector << 9) &
  518. (nvmeq->dev->stripe_size - 1));
  519. sg_init_table(iod->sg, psegs);
  520. bio_for_each_segment(bvec, bio, iter) {
  521. if (!first && BIOVEC_PHYS_MERGEABLE(&bvprv, &bvec)) {
  522. sg->length += bvec.bv_len;
  523. } else {
  524. if (!first && BIOVEC_NOT_VIRT_MERGEABLE(&bvprv, &bvec))
  525. return nvme_split_and_submit(bio, nvmeq,
  526. length);
  527. sg = sg ? sg + 1 : iod->sg;
  528. sg_set_page(sg, bvec.bv_page,
  529. bvec.bv_len, bvec.bv_offset);
  530. nsegs++;
  531. }
  532. if (split_len - length < bvec.bv_len)
  533. return nvme_split_and_submit(bio, nvmeq, split_len);
  534. length += bvec.bv_len;
  535. bvprv = bvec;
  536. first = 0;
  537. }
  538. iod->nents = nsegs;
  539. sg_mark_end(sg);
  540. if (dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir) == 0)
  541. return -ENOMEM;
  542. BUG_ON(length != bio->bi_iter.bi_size);
  543. return length;
  544. }
  545. static int nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
  546. struct bio *bio, struct nvme_iod *iod, int cmdid)
  547. {
  548. struct nvme_dsm_range *range =
  549. (struct nvme_dsm_range *)iod_list(iod)[0];
  550. struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
  551. range->cattr = cpu_to_le32(0);
  552. range->nlb = cpu_to_le32(bio->bi_iter.bi_size >> ns->lba_shift);
  553. range->slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector));
  554. memset(cmnd, 0, sizeof(*cmnd));
  555. cmnd->dsm.opcode = nvme_cmd_dsm;
  556. cmnd->dsm.command_id = cmdid;
  557. cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
  558. cmnd->dsm.prp1 = cpu_to_le64(iod->first_dma);
  559. cmnd->dsm.nr = 0;
  560. cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
  561. if (++nvmeq->sq_tail == nvmeq->q_depth)
  562. nvmeq->sq_tail = 0;
  563. writel(nvmeq->sq_tail, nvmeq->q_db);
  564. return 0;
  565. }
  566. static int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
  567. int cmdid)
  568. {
  569. struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
  570. memset(cmnd, 0, sizeof(*cmnd));
  571. cmnd->common.opcode = nvme_cmd_flush;
  572. cmnd->common.command_id = cmdid;
  573. cmnd->common.nsid = cpu_to_le32(ns->ns_id);
  574. if (++nvmeq->sq_tail == nvmeq->q_depth)
  575. nvmeq->sq_tail = 0;
  576. writel(nvmeq->sq_tail, nvmeq->q_db);
  577. return 0;
  578. }
  579. static int nvme_submit_iod(struct nvme_queue *nvmeq, struct nvme_iod *iod)
  580. {
  581. struct bio *bio = iod->private;
  582. struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
  583. struct nvme_command *cmnd;
  584. int cmdid;
  585. u16 control;
  586. u32 dsmgmt;
  587. cmdid = alloc_cmdid(nvmeq, iod, bio_completion, NVME_IO_TIMEOUT);
  588. if (unlikely(cmdid < 0))
  589. return cmdid;
  590. if (bio->bi_rw & REQ_DISCARD)
  591. return nvme_submit_discard(nvmeq, ns, bio, iod, cmdid);
  592. if (bio->bi_rw & REQ_FLUSH)
  593. return nvme_submit_flush(nvmeq, ns, cmdid);
  594. control = 0;
  595. if (bio->bi_rw & REQ_FUA)
  596. control |= NVME_RW_FUA;
  597. if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
  598. control |= NVME_RW_LR;
  599. dsmgmt = 0;
  600. if (bio->bi_rw & REQ_RAHEAD)
  601. dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
  602. cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
  603. memset(cmnd, 0, sizeof(*cmnd));
  604. cmnd->rw.opcode = bio_data_dir(bio) ? nvme_cmd_write : nvme_cmd_read;
  605. cmnd->rw.command_id = cmdid;
  606. cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
  607. cmnd->rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
  608. cmnd->rw.prp2 = cpu_to_le64(iod->first_dma);
  609. cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, bio->bi_iter.bi_sector));
  610. cmnd->rw.length =
  611. cpu_to_le16((bio->bi_iter.bi_size >> ns->lba_shift) - 1);
  612. cmnd->rw.control = cpu_to_le16(control);
  613. cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
  614. if (++nvmeq->sq_tail == nvmeq->q_depth)
  615. nvmeq->sq_tail = 0;
  616. writel(nvmeq->sq_tail, nvmeq->q_db);
  617. return 0;
  618. }
  619. static int nvme_split_flush_data(struct nvme_queue *nvmeq, struct bio *bio)
  620. {
  621. struct bio *split = bio_clone(bio, GFP_ATOMIC);
  622. if (!split)
  623. return -ENOMEM;
  624. split->bi_iter.bi_size = 0;
  625. split->bi_phys_segments = 0;
  626. bio->bi_rw &= ~REQ_FLUSH;
  627. bio_chain(split, bio);
  628. if (!waitqueue_active(&nvmeq->sq_full))
  629. add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
  630. bio_list_add(&nvmeq->sq_cong, split);
  631. bio_list_add(&nvmeq->sq_cong, bio);
  632. wake_up_process(nvme_thread);
  633. return 0;
  634. }
  635. /*
  636. * Called with local interrupts disabled and the q_lock held. May not sleep.
  637. */
  638. static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
  639. struct bio *bio)
  640. {
  641. struct nvme_iod *iod;
  642. int psegs = bio_phys_segments(ns->queue, bio);
  643. int result;
  644. if ((bio->bi_rw & REQ_FLUSH) && psegs)
  645. return nvme_split_flush_data(nvmeq, bio);
  646. iod = nvme_alloc_iod(psegs, bio->bi_iter.bi_size, GFP_ATOMIC);
  647. if (!iod)
  648. return -ENOMEM;
  649. iod->private = bio;
  650. if (bio->bi_rw & REQ_DISCARD) {
  651. void *range;
  652. /*
  653. * We reuse the small pool to allocate the 16-byte range here
  654. * as it is not worth having a special pool for these or
  655. * additional cases to handle freeing the iod.
  656. */
  657. range = dma_pool_alloc(nvmeq->dev->prp_small_pool,
  658. GFP_ATOMIC,
  659. &iod->first_dma);
  660. if (!range) {
  661. result = -ENOMEM;
  662. goto free_iod;
  663. }
  664. iod_list(iod)[0] = (__le64 *)range;
  665. iod->npages = 0;
  666. } else if (psegs) {
  667. result = nvme_map_bio(nvmeq, iod, bio,
  668. bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE,
  669. psegs);
  670. if (result <= 0)
  671. goto free_iod;
  672. if (nvme_setup_prps(nvmeq->dev, iod, result, GFP_ATOMIC) !=
  673. result) {
  674. result = -ENOMEM;
  675. goto free_iod;
  676. }
  677. nvme_start_io_acct(bio);
  678. }
  679. if (unlikely(nvme_submit_iod(nvmeq, iod))) {
  680. if (!waitqueue_active(&nvmeq->sq_full))
  681. add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
  682. list_add_tail(&iod->node, &nvmeq->iod_bio);
  683. }
  684. return 0;
  685. free_iod:
  686. nvme_free_iod(nvmeq->dev, iod);
  687. return result;
  688. }
  689. static int nvme_process_cq(struct nvme_queue *nvmeq)
  690. {
  691. u16 head, phase;
  692. head = nvmeq->cq_head;
  693. phase = nvmeq->cq_phase;
  694. for (;;) {
  695. void *ctx;
  696. nvme_completion_fn fn;
  697. struct nvme_completion cqe = nvmeq->cqes[head];
  698. if ((le16_to_cpu(cqe.status) & 1) != phase)
  699. break;
  700. nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
  701. if (++head == nvmeq->q_depth) {
  702. head = 0;
  703. phase = !phase;
  704. }
  705. ctx = free_cmdid(nvmeq, cqe.command_id, &fn);
  706. fn(nvmeq, ctx, &cqe);
  707. }
  708. /* If the controller ignores the cq head doorbell and continuously
  709. * writes to the queue, it is theoretically possible to wrap around
  710. * the queue twice and mistakenly return IRQ_NONE. Linux only
  711. * requires that 0.1% of your interrupts are handled, so this isn't
  712. * a big problem.
  713. */
  714. if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
  715. return 0;
  716. writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
  717. nvmeq->cq_head = head;
  718. nvmeq->cq_phase = phase;
  719. nvmeq->cqe_seen = 1;
  720. return 1;
  721. }
  722. static void nvme_make_request(struct request_queue *q, struct bio *bio)
  723. {
  724. struct nvme_ns *ns = q->queuedata;
  725. struct nvme_queue *nvmeq = get_nvmeq(ns->dev);
  726. int result = -EBUSY;
  727. if (!nvmeq) {
  728. bio_endio(bio, -EIO);
  729. return;
  730. }
  731. spin_lock_irq(&nvmeq->q_lock);
  732. if (!nvmeq->q_suspended && bio_list_empty(&nvmeq->sq_cong))
  733. result = nvme_submit_bio_queue(nvmeq, ns, bio);
  734. if (unlikely(result)) {
  735. if (!waitqueue_active(&nvmeq->sq_full))
  736. add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
  737. bio_list_add(&nvmeq->sq_cong, bio);
  738. }
  739. nvme_process_cq(nvmeq);
  740. spin_unlock_irq(&nvmeq->q_lock);
  741. put_nvmeq(nvmeq);
  742. }
  743. static irqreturn_t nvme_irq(int irq, void *data)
  744. {
  745. irqreturn_t result;
  746. struct nvme_queue *nvmeq = data;
  747. spin_lock(&nvmeq->q_lock);
  748. nvme_process_cq(nvmeq);
  749. result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
  750. nvmeq->cqe_seen = 0;
  751. spin_unlock(&nvmeq->q_lock);
  752. return result;
  753. }
  754. static irqreturn_t nvme_irq_check(int irq, void *data)
  755. {
  756. struct nvme_queue *nvmeq = data;
  757. struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
  758. if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
  759. return IRQ_NONE;
  760. return IRQ_WAKE_THREAD;
  761. }
  762. static void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
  763. {
  764. spin_lock_irq(&nvmeq->q_lock);
  765. cancel_cmdid(nvmeq, cmdid, NULL);
  766. spin_unlock_irq(&nvmeq->q_lock);
  767. }
  768. struct sync_cmd_info {
  769. struct task_struct *task;
  770. u32 result;
  771. int status;
  772. };
  773. static void sync_completion(struct nvme_queue *nvmeq, void *ctx,
  774. struct nvme_completion *cqe)
  775. {
  776. struct sync_cmd_info *cmdinfo = ctx;
  777. cmdinfo->result = le32_to_cpup(&cqe->result);
  778. cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
  779. wake_up_process(cmdinfo->task);
  780. }
  781. /*
  782. * Returns 0 on success. If the result is negative, it's a Linux error code;
  783. * if the result is positive, it's an NVM Express status code
  784. */
  785. static int nvme_submit_sync_cmd(struct nvme_dev *dev, int q_idx,
  786. struct nvme_command *cmd,
  787. u32 *result, unsigned timeout)
  788. {
  789. int cmdid, ret;
  790. struct sync_cmd_info cmdinfo;
  791. struct nvme_queue *nvmeq;
  792. nvmeq = lock_nvmeq(dev, q_idx);
  793. if (!nvmeq)
  794. return -ENODEV;
  795. cmdinfo.task = current;
  796. cmdinfo.status = -EINTR;
  797. cmdid = alloc_cmdid(nvmeq, &cmdinfo, sync_completion, timeout);
  798. if (cmdid < 0) {
  799. unlock_nvmeq(nvmeq);
  800. return cmdid;
  801. }
  802. cmd->common.command_id = cmdid;
  803. set_current_state(TASK_KILLABLE);
  804. ret = nvme_submit_cmd(nvmeq, cmd);
  805. if (ret) {
  806. free_cmdid(nvmeq, cmdid, NULL);
  807. unlock_nvmeq(nvmeq);
  808. set_current_state(TASK_RUNNING);
  809. return ret;
  810. }
  811. unlock_nvmeq(nvmeq);
  812. schedule_timeout(timeout);
  813. if (cmdinfo.status == -EINTR) {
  814. nvmeq = lock_nvmeq(dev, q_idx);
  815. if (nvmeq) {
  816. nvme_abort_command(nvmeq, cmdid);
  817. unlock_nvmeq(nvmeq);
  818. }
  819. return -EINTR;
  820. }
  821. if (result)
  822. *result = cmdinfo.result;
  823. return cmdinfo.status;
  824. }
  825. static int nvme_submit_async_cmd(struct nvme_queue *nvmeq,
  826. struct nvme_command *cmd,
  827. struct async_cmd_info *cmdinfo, unsigned timeout)
  828. {
  829. int cmdid;
  830. cmdid = alloc_cmdid_killable(nvmeq, cmdinfo, async_completion, timeout);
  831. if (cmdid < 0)
  832. return cmdid;
  833. cmdinfo->status = -EINTR;
  834. cmd->common.command_id = cmdid;
  835. return nvme_submit_cmd(nvmeq, cmd);
  836. }
  837. int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
  838. u32 *result)
  839. {
  840. return nvme_submit_sync_cmd(dev, 0, cmd, result, ADMIN_TIMEOUT);
  841. }
  842. int nvme_submit_io_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
  843. u32 *result)
  844. {
  845. return nvme_submit_sync_cmd(dev, smp_processor_id() + 1, cmd, result,
  846. NVME_IO_TIMEOUT);
  847. }
  848. static int nvme_submit_admin_cmd_async(struct nvme_dev *dev,
  849. struct nvme_command *cmd, struct async_cmd_info *cmdinfo)
  850. {
  851. return nvme_submit_async_cmd(raw_nvmeq(dev, 0), cmd, cmdinfo,
  852. ADMIN_TIMEOUT);
  853. }
  854. static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
  855. {
  856. int status;
  857. struct nvme_command c;
  858. memset(&c, 0, sizeof(c));
  859. c.delete_queue.opcode = opcode;
  860. c.delete_queue.qid = cpu_to_le16(id);
  861. status = nvme_submit_admin_cmd(dev, &c, NULL);
  862. if (status)
  863. return -EIO;
  864. return 0;
  865. }
  866. static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
  867. struct nvme_queue *nvmeq)
  868. {
  869. int status;
  870. struct nvme_command c;
  871. int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
  872. memset(&c, 0, sizeof(c));
  873. c.create_cq.opcode = nvme_admin_create_cq;
  874. c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
  875. c.create_cq.cqid = cpu_to_le16(qid);
  876. c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
  877. c.create_cq.cq_flags = cpu_to_le16(flags);
  878. c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
  879. status = nvme_submit_admin_cmd(dev, &c, NULL);
  880. if (status)
  881. return -EIO;
  882. return 0;
  883. }
  884. static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
  885. struct nvme_queue *nvmeq)
  886. {
  887. int status;
  888. struct nvme_command c;
  889. int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
  890. memset(&c, 0, sizeof(c));
  891. c.create_sq.opcode = nvme_admin_create_sq;
  892. c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
  893. c.create_sq.sqid = cpu_to_le16(qid);
  894. c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
  895. c.create_sq.sq_flags = cpu_to_le16(flags);
  896. c.create_sq.cqid = cpu_to_le16(qid);
  897. status = nvme_submit_admin_cmd(dev, &c, NULL);
  898. if (status)
  899. return -EIO;
  900. return 0;
  901. }
  902. static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
  903. {
  904. return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
  905. }
  906. static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
  907. {
  908. return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
  909. }
  910. int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
  911. dma_addr_t dma_addr)
  912. {
  913. struct nvme_command c;
  914. memset(&c, 0, sizeof(c));
  915. c.identify.opcode = nvme_admin_identify;
  916. c.identify.nsid = cpu_to_le32(nsid);
  917. c.identify.prp1 = cpu_to_le64(dma_addr);
  918. c.identify.cns = cpu_to_le32(cns);
  919. return nvme_submit_admin_cmd(dev, &c, NULL);
  920. }
  921. int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
  922. dma_addr_t dma_addr, u32 *result)
  923. {
  924. struct nvme_command c;
  925. memset(&c, 0, sizeof(c));
  926. c.features.opcode = nvme_admin_get_features;
  927. c.features.nsid = cpu_to_le32(nsid);
  928. c.features.prp1 = cpu_to_le64(dma_addr);
  929. c.features.fid = cpu_to_le32(fid);
  930. return nvme_submit_admin_cmd(dev, &c, result);
  931. }
  932. int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
  933. dma_addr_t dma_addr, u32 *result)
  934. {
  935. struct nvme_command c;
  936. memset(&c, 0, sizeof(c));
  937. c.features.opcode = nvme_admin_set_features;
  938. c.features.prp1 = cpu_to_le64(dma_addr);
  939. c.features.fid = cpu_to_le32(fid);
  940. c.features.dword11 = cpu_to_le32(dword11);
  941. return nvme_submit_admin_cmd(dev, &c, result);
  942. }
  943. /**
  944. * nvme_abort_cmd - Attempt aborting a command
  945. * @cmdid: Command id of a timed out IO
  946. * @queue: The queue with timed out IO
  947. *
  948. * Schedule controller reset if the command was already aborted once before and
  949. * still hasn't been returned to the driver, or if this is the admin queue.
  950. */
  951. static void nvme_abort_cmd(int cmdid, struct nvme_queue *nvmeq)
  952. {
  953. int a_cmdid;
  954. struct nvme_command cmd;
  955. struct nvme_dev *dev = nvmeq->dev;
  956. struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
  957. struct nvme_queue *adminq;
  958. if (!nvmeq->qid || info[cmdid].aborted) {
  959. if (work_busy(&dev->reset_work))
  960. return;
  961. list_del_init(&dev->node);
  962. dev_warn(&dev->pci_dev->dev,
  963. "I/O %d QID %d timeout, reset controller\n", cmdid,
  964. nvmeq->qid);
  965. dev->reset_workfn = nvme_reset_failed_dev;
  966. queue_work(nvme_workq, &dev->reset_work);
  967. return;
  968. }
  969. if (!dev->abort_limit)
  970. return;
  971. adminq = rcu_dereference(dev->queues[0]);
  972. a_cmdid = alloc_cmdid(adminq, CMD_CTX_ABORT, special_completion,
  973. ADMIN_TIMEOUT);
  974. if (a_cmdid < 0)
  975. return;
  976. memset(&cmd, 0, sizeof(cmd));
  977. cmd.abort.opcode = nvme_admin_abort_cmd;
  978. cmd.abort.cid = cmdid;
  979. cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
  980. cmd.abort.command_id = a_cmdid;
  981. --dev->abort_limit;
  982. info[cmdid].aborted = 1;
  983. info[cmdid].timeout = jiffies + ADMIN_TIMEOUT;
  984. dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", cmdid,
  985. nvmeq->qid);
  986. nvme_submit_cmd(adminq, &cmd);
  987. }
  988. /**
  989. * nvme_cancel_ios - Cancel outstanding I/Os
  990. * @queue: The queue to cancel I/Os on
  991. * @timeout: True to only cancel I/Os which have timed out
  992. */
  993. static void nvme_cancel_ios(struct nvme_queue *nvmeq, bool timeout)
  994. {
  995. int depth = nvmeq->q_depth - 1;
  996. struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
  997. unsigned long now = jiffies;
  998. int cmdid;
  999. for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) {
  1000. void *ctx;
  1001. nvme_completion_fn fn;
  1002. static struct nvme_completion cqe = {
  1003. .status = cpu_to_le16(NVME_SC_ABORT_REQ << 1),
  1004. };
  1005. if (timeout && !time_after(now, info[cmdid].timeout))
  1006. continue;
  1007. if (info[cmdid].ctx == CMD_CTX_CANCELLED)
  1008. continue;
  1009. if (timeout && nvmeq->dev->initialized) {
  1010. nvme_abort_cmd(cmdid, nvmeq);
  1011. continue;
  1012. }
  1013. dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n", cmdid,
  1014. nvmeq->qid);
  1015. ctx = cancel_cmdid(nvmeq, cmdid, &fn);
  1016. fn(nvmeq, ctx, &cqe);
  1017. }
  1018. }
  1019. static void nvme_free_queue(struct rcu_head *r)
  1020. {
  1021. struct nvme_queue *nvmeq = container_of(r, struct nvme_queue, r_head);
  1022. spin_lock_irq(&nvmeq->q_lock);
  1023. while (bio_list_peek(&nvmeq->sq_cong)) {
  1024. struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
  1025. bio_endio(bio, -EIO);
  1026. }
  1027. while (!list_empty(&nvmeq->iod_bio)) {
  1028. static struct nvme_completion cqe = {
  1029. .status = cpu_to_le16(
  1030. (NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1),
  1031. };
  1032. struct nvme_iod *iod = list_first_entry(&nvmeq->iod_bio,
  1033. struct nvme_iod,
  1034. node);
  1035. list_del(&iod->node);
  1036. bio_completion(nvmeq, iod, &cqe);
  1037. }
  1038. spin_unlock_irq(&nvmeq->q_lock);
  1039. dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
  1040. (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
  1041. dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
  1042. nvmeq->sq_cmds, nvmeq->sq_dma_addr);
  1043. if (nvmeq->qid)
  1044. free_cpumask_var(nvmeq->cpu_mask);
  1045. kfree(nvmeq);
  1046. }
  1047. static void nvme_free_queues(struct nvme_dev *dev, int lowest)
  1048. {
  1049. int i;
  1050. for (i = dev->queue_count - 1; i >= lowest; i--) {
  1051. struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
  1052. rcu_assign_pointer(dev->queues[i], NULL);
  1053. call_rcu(&nvmeq->r_head, nvme_free_queue);
  1054. dev->queue_count--;
  1055. }
  1056. }
  1057. /**
  1058. * nvme_suspend_queue - put queue into suspended state
  1059. * @nvmeq - queue to suspend
  1060. *
  1061. * Returns 1 if already suspended, 0 otherwise.
  1062. */
  1063. static int nvme_suspend_queue(struct nvme_queue *nvmeq)
  1064. {
  1065. int vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
  1066. spin_lock_irq(&nvmeq->q_lock);
  1067. if (nvmeq->q_suspended) {
  1068. spin_unlock_irq(&nvmeq->q_lock);
  1069. return 1;
  1070. }
  1071. nvmeq->q_suspended = 1;
  1072. nvmeq->dev->online_queues--;
  1073. spin_unlock_irq(&nvmeq->q_lock);
  1074. irq_set_affinity_hint(vector, NULL);
  1075. free_irq(vector, nvmeq);
  1076. return 0;
  1077. }
  1078. static void nvme_clear_queue(struct nvme_queue *nvmeq)
  1079. {
  1080. spin_lock_irq(&nvmeq->q_lock);
  1081. nvme_process_cq(nvmeq);
  1082. nvme_cancel_ios(nvmeq, false);
  1083. spin_unlock_irq(&nvmeq->q_lock);
  1084. }
  1085. static void nvme_disable_queue(struct nvme_dev *dev, int qid)
  1086. {
  1087. struct nvme_queue *nvmeq = raw_nvmeq(dev, qid);
  1088. if (!nvmeq)
  1089. return;
  1090. if (nvme_suspend_queue(nvmeq))
  1091. return;
  1092. /* Don't tell the adapter to delete the admin queue.
  1093. * Don't tell a removed adapter to delete IO queues. */
  1094. if (qid && readl(&dev->bar->csts) != -1) {
  1095. adapter_delete_sq(dev, qid);
  1096. adapter_delete_cq(dev, qid);
  1097. }
  1098. nvme_clear_queue(nvmeq);
  1099. }
  1100. static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
  1101. int depth, int vector)
  1102. {
  1103. struct device *dmadev = &dev->pci_dev->dev;
  1104. unsigned extra = nvme_queue_extra(depth);
  1105. struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
  1106. if (!nvmeq)
  1107. return NULL;
  1108. nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
  1109. &nvmeq->cq_dma_addr, GFP_KERNEL);
  1110. if (!nvmeq->cqes)
  1111. goto free_nvmeq;
  1112. memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
  1113. nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
  1114. &nvmeq->sq_dma_addr, GFP_KERNEL);
  1115. if (!nvmeq->sq_cmds)
  1116. goto free_cqdma;
  1117. if (qid && !zalloc_cpumask_var(&nvmeq->cpu_mask, GFP_KERNEL))
  1118. goto free_sqdma;
  1119. nvmeq->q_dmadev = dmadev;
  1120. nvmeq->dev = dev;
  1121. snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
  1122. dev->instance, qid);
  1123. spin_lock_init(&nvmeq->q_lock);
  1124. nvmeq->cq_head = 0;
  1125. nvmeq->cq_phase = 1;
  1126. init_waitqueue_head(&nvmeq->sq_full);
  1127. init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
  1128. bio_list_init(&nvmeq->sq_cong);
  1129. INIT_LIST_HEAD(&nvmeq->iod_bio);
  1130. nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
  1131. nvmeq->q_depth = depth;
  1132. nvmeq->cq_vector = vector;
  1133. nvmeq->qid = qid;
  1134. nvmeq->q_suspended = 1;
  1135. dev->queue_count++;
  1136. rcu_assign_pointer(dev->queues[qid], nvmeq);
  1137. return nvmeq;
  1138. free_sqdma:
  1139. dma_free_coherent(dmadev, SQ_SIZE(depth), (void *)nvmeq->sq_cmds,
  1140. nvmeq->sq_dma_addr);
  1141. free_cqdma:
  1142. dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes,
  1143. nvmeq->cq_dma_addr);
  1144. free_nvmeq:
  1145. kfree(nvmeq);
  1146. return NULL;
  1147. }
  1148. static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
  1149. const char *name)
  1150. {
  1151. if (use_threaded_interrupts)
  1152. return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
  1153. nvme_irq_check, nvme_irq, IRQF_SHARED,
  1154. name, nvmeq);
  1155. return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
  1156. IRQF_SHARED, name, nvmeq);
  1157. }
  1158. static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
  1159. {
  1160. struct nvme_dev *dev = nvmeq->dev;
  1161. unsigned extra = nvme_queue_extra(nvmeq->q_depth);
  1162. nvmeq->sq_tail = 0;
  1163. nvmeq->cq_head = 0;
  1164. nvmeq->cq_phase = 1;
  1165. nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
  1166. memset(nvmeq->cmdid_data, 0, extra);
  1167. memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
  1168. nvme_cancel_ios(nvmeq, false);
  1169. nvmeq->q_suspended = 0;
  1170. dev->online_queues++;
  1171. }
  1172. static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
  1173. {
  1174. struct nvme_dev *dev = nvmeq->dev;
  1175. int result;
  1176. result = adapter_alloc_cq(dev, qid, nvmeq);
  1177. if (result < 0)
  1178. return result;
  1179. result = adapter_alloc_sq(dev, qid, nvmeq);
  1180. if (result < 0)
  1181. goto release_cq;
  1182. result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
  1183. if (result < 0)
  1184. goto release_sq;
  1185. spin_lock_irq(&nvmeq->q_lock);
  1186. nvme_init_queue(nvmeq, qid);
  1187. spin_unlock_irq(&nvmeq->q_lock);
  1188. return result;
  1189. release_sq:
  1190. adapter_delete_sq(dev, qid);
  1191. release_cq:
  1192. adapter_delete_cq(dev, qid);
  1193. return result;
  1194. }
  1195. static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
  1196. {
  1197. unsigned long timeout;
  1198. u32 bit = enabled ? NVME_CSTS_RDY : 0;
  1199. timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
  1200. while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
  1201. msleep(100);
  1202. if (fatal_signal_pending(current))
  1203. return -EINTR;
  1204. if (time_after(jiffies, timeout)) {
  1205. dev_err(&dev->pci_dev->dev,
  1206. "Device not ready; aborting %s\n", enabled ?
  1207. "initialisation" : "reset");
  1208. return -ENODEV;
  1209. }
  1210. }
  1211. return 0;
  1212. }
  1213. /*
  1214. * If the device has been passed off to us in an enabled state, just clear
  1215. * the enabled bit. The spec says we should set the 'shutdown notification
  1216. * bits', but doing so may cause the device to complete commands to the
  1217. * admin queue ... and we don't know what memory that might be pointing at!
  1218. */
  1219. static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
  1220. {
  1221. u32 cc = readl(&dev->bar->cc);
  1222. if (cc & NVME_CC_ENABLE)
  1223. writel(cc & ~NVME_CC_ENABLE, &dev->bar->cc);
  1224. return nvme_wait_ready(dev, cap, false);
  1225. }
  1226. static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
  1227. {
  1228. return nvme_wait_ready(dev, cap, true);
  1229. }
  1230. static int nvme_shutdown_ctrl(struct nvme_dev *dev)
  1231. {
  1232. unsigned long timeout;
  1233. u32 cc;
  1234. cc = (readl(&dev->bar->cc) & ~NVME_CC_SHN_MASK) | NVME_CC_SHN_NORMAL;
  1235. writel(cc, &dev->bar->cc);
  1236. timeout = 2 * HZ + jiffies;
  1237. while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
  1238. NVME_CSTS_SHST_CMPLT) {
  1239. msleep(100);
  1240. if (fatal_signal_pending(current))
  1241. return -EINTR;
  1242. if (time_after(jiffies, timeout)) {
  1243. dev_err(&dev->pci_dev->dev,
  1244. "Device shutdown incomplete; abort shutdown\n");
  1245. return -ENODEV;
  1246. }
  1247. }
  1248. return 0;
  1249. }
  1250. static int nvme_configure_admin_queue(struct nvme_dev *dev)
  1251. {
  1252. int result;
  1253. u32 aqa;
  1254. u64 cap = readq(&dev->bar->cap);
  1255. struct nvme_queue *nvmeq;
  1256. result = nvme_disable_ctrl(dev, cap);
  1257. if (result < 0)
  1258. return result;
  1259. nvmeq = raw_nvmeq(dev, 0);
  1260. if (!nvmeq) {
  1261. nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
  1262. if (!nvmeq)
  1263. return -ENOMEM;
  1264. }
  1265. aqa = nvmeq->q_depth - 1;
  1266. aqa |= aqa << 16;
  1267. dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
  1268. dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
  1269. dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
  1270. dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
  1271. writel(aqa, &dev->bar->aqa);
  1272. writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
  1273. writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
  1274. writel(dev->ctrl_config, &dev->bar->cc);
  1275. result = nvme_enable_ctrl(dev, cap);
  1276. if (result)
  1277. return result;
  1278. result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
  1279. if (result)
  1280. return result;
  1281. spin_lock_irq(&nvmeq->q_lock);
  1282. nvme_init_queue(nvmeq, 0);
  1283. spin_unlock_irq(&nvmeq->q_lock);
  1284. return result;
  1285. }
  1286. struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
  1287. unsigned long addr, unsigned length)
  1288. {
  1289. int i, err, count, nents, offset;
  1290. struct scatterlist *sg;
  1291. struct page **pages;
  1292. struct nvme_iod *iod;
  1293. if (addr & 3)
  1294. return ERR_PTR(-EINVAL);
  1295. if (!length || length > INT_MAX - PAGE_SIZE)
  1296. return ERR_PTR(-EINVAL);
  1297. offset = offset_in_page(addr);
  1298. count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
  1299. pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
  1300. if (!pages)
  1301. return ERR_PTR(-ENOMEM);
  1302. err = get_user_pages_fast(addr, count, 1, pages);
  1303. if (err < count) {
  1304. count = err;
  1305. err = -EFAULT;
  1306. goto put_pages;
  1307. }
  1308. err = -ENOMEM;
  1309. iod = nvme_alloc_iod(count, length, GFP_KERNEL);
  1310. if (!iod)
  1311. goto put_pages;
  1312. sg = iod->sg;
  1313. sg_init_table(sg, count);
  1314. for (i = 0; i < count; i++) {
  1315. sg_set_page(&sg[i], pages[i],
  1316. min_t(unsigned, length, PAGE_SIZE - offset),
  1317. offset);
  1318. length -= (PAGE_SIZE - offset);
  1319. offset = 0;
  1320. }
  1321. sg_mark_end(&sg[i - 1]);
  1322. iod->nents = count;
  1323. nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
  1324. write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1325. if (!nents)
  1326. goto free_iod;
  1327. kfree(pages);
  1328. return iod;
  1329. free_iod:
  1330. kfree(iod);
  1331. put_pages:
  1332. for (i = 0; i < count; i++)
  1333. put_page(pages[i]);
  1334. kfree(pages);
  1335. return ERR_PTR(err);
  1336. }
  1337. void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
  1338. struct nvme_iod *iod)
  1339. {
  1340. int i;
  1341. dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
  1342. write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1343. for (i = 0; i < iod->nents; i++)
  1344. put_page(sg_page(&iod->sg[i]));
  1345. }
  1346. static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
  1347. {
  1348. struct nvme_dev *dev = ns->dev;
  1349. struct nvme_user_io io;
  1350. struct nvme_command c;
  1351. unsigned length, meta_len;
  1352. int status, i;
  1353. struct nvme_iod *iod, *meta_iod = NULL;
  1354. dma_addr_t meta_dma_addr;
  1355. void *meta, *uninitialized_var(meta_mem);
  1356. if (copy_from_user(&io, uio, sizeof(io)))
  1357. return -EFAULT;
  1358. length = (io.nblocks + 1) << ns->lba_shift;
  1359. meta_len = (io.nblocks + 1) * ns->ms;
  1360. if (meta_len && ((io.metadata & 3) || !io.metadata))
  1361. return -EINVAL;
  1362. switch (io.opcode) {
  1363. case nvme_cmd_write:
  1364. case nvme_cmd_read:
  1365. case nvme_cmd_compare:
  1366. iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
  1367. break;
  1368. default:
  1369. return -EINVAL;
  1370. }
  1371. if (IS_ERR(iod))
  1372. return PTR_ERR(iod);
  1373. memset(&c, 0, sizeof(c));
  1374. c.rw.opcode = io.opcode;
  1375. c.rw.flags = io.flags;
  1376. c.rw.nsid = cpu_to_le32(ns->ns_id);
  1377. c.rw.slba = cpu_to_le64(io.slba);
  1378. c.rw.length = cpu_to_le16(io.nblocks);
  1379. c.rw.control = cpu_to_le16(io.control);
  1380. c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
  1381. c.rw.reftag = cpu_to_le32(io.reftag);
  1382. c.rw.apptag = cpu_to_le16(io.apptag);
  1383. c.rw.appmask = cpu_to_le16(io.appmask);
  1384. if (meta_len) {
  1385. meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata,
  1386. meta_len);
  1387. if (IS_ERR(meta_iod)) {
  1388. status = PTR_ERR(meta_iod);
  1389. meta_iod = NULL;
  1390. goto unmap;
  1391. }
  1392. meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len,
  1393. &meta_dma_addr, GFP_KERNEL);
  1394. if (!meta_mem) {
  1395. status = -ENOMEM;
  1396. goto unmap;
  1397. }
  1398. if (io.opcode & 1) {
  1399. int meta_offset = 0;
  1400. for (i = 0; i < meta_iod->nents; i++) {
  1401. meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
  1402. meta_iod->sg[i].offset;
  1403. memcpy(meta_mem + meta_offset, meta,
  1404. meta_iod->sg[i].length);
  1405. kunmap_atomic(meta);
  1406. meta_offset += meta_iod->sg[i].length;
  1407. }
  1408. }
  1409. c.rw.metadata = cpu_to_le64(meta_dma_addr);
  1410. }
  1411. length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
  1412. c.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
  1413. c.rw.prp2 = cpu_to_le64(iod->first_dma);
  1414. if (length != (io.nblocks + 1) << ns->lba_shift)
  1415. status = -ENOMEM;
  1416. else
  1417. status = nvme_submit_io_cmd(dev, &c, NULL);
  1418. if (meta_len) {
  1419. if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) {
  1420. int meta_offset = 0;
  1421. for (i = 0; i < meta_iod->nents; i++) {
  1422. meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
  1423. meta_iod->sg[i].offset;
  1424. memcpy(meta, meta_mem + meta_offset,
  1425. meta_iod->sg[i].length);
  1426. kunmap_atomic(meta);
  1427. meta_offset += meta_iod->sg[i].length;
  1428. }
  1429. }
  1430. dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem,
  1431. meta_dma_addr);
  1432. }
  1433. unmap:
  1434. nvme_unmap_user_pages(dev, io.opcode & 1, iod);
  1435. nvme_free_iod(dev, iod);
  1436. if (meta_iod) {
  1437. nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod);
  1438. nvme_free_iod(dev, meta_iod);
  1439. }
  1440. return status;
  1441. }
  1442. static int nvme_user_admin_cmd(struct nvme_dev *dev,
  1443. struct nvme_admin_cmd __user *ucmd)
  1444. {
  1445. struct nvme_admin_cmd cmd;
  1446. struct nvme_command c;
  1447. int status, length;
  1448. struct nvme_iod *uninitialized_var(iod);
  1449. unsigned timeout;
  1450. if (!capable(CAP_SYS_ADMIN))
  1451. return -EACCES;
  1452. if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
  1453. return -EFAULT;
  1454. memset(&c, 0, sizeof(c));
  1455. c.common.opcode = cmd.opcode;
  1456. c.common.flags = cmd.flags;
  1457. c.common.nsid = cpu_to_le32(cmd.nsid);
  1458. c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
  1459. c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
  1460. c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
  1461. c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
  1462. c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
  1463. c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
  1464. c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
  1465. c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
  1466. length = cmd.data_len;
  1467. if (cmd.data_len) {
  1468. iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
  1469. length);
  1470. if (IS_ERR(iod))
  1471. return PTR_ERR(iod);
  1472. length = nvme_setup_prps(dev, iod, length, GFP_KERNEL);
  1473. c.common.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
  1474. c.common.prp2 = cpu_to_le64(iod->first_dma);
  1475. }
  1476. timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) :
  1477. ADMIN_TIMEOUT;
  1478. if (length != cmd.data_len)
  1479. status = -ENOMEM;
  1480. else
  1481. status = nvme_submit_sync_cmd(dev, 0, &c, &cmd.result, timeout);
  1482. if (cmd.data_len) {
  1483. nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
  1484. nvme_free_iod(dev, iod);
  1485. }
  1486. if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result,
  1487. sizeof(cmd.result)))
  1488. status = -EFAULT;
  1489. return status;
  1490. }
  1491. static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
  1492. unsigned long arg)
  1493. {
  1494. struct nvme_ns *ns = bdev->bd_disk->private_data;
  1495. switch (cmd) {
  1496. case NVME_IOCTL_ID:
  1497. force_successful_syscall_return();
  1498. return ns->ns_id;
  1499. case NVME_IOCTL_ADMIN_CMD:
  1500. return nvme_user_admin_cmd(ns->dev, (void __user *)arg);
  1501. case NVME_IOCTL_SUBMIT_IO:
  1502. return nvme_submit_io(ns, (void __user *)arg);
  1503. case SG_GET_VERSION_NUM:
  1504. return nvme_sg_get_version_num((void __user *)arg);
  1505. case SG_IO:
  1506. return nvme_sg_io(ns, (void __user *)arg);
  1507. default:
  1508. return -ENOTTY;
  1509. }
  1510. }
  1511. #ifdef CONFIG_COMPAT
  1512. static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
  1513. unsigned int cmd, unsigned long arg)
  1514. {
  1515. struct nvme_ns *ns = bdev->bd_disk->private_data;
  1516. switch (cmd) {
  1517. case SG_IO:
  1518. return nvme_sg_io32(ns, arg);
  1519. }
  1520. return nvme_ioctl(bdev, mode, cmd, arg);
  1521. }
  1522. #else
  1523. #define nvme_compat_ioctl NULL
  1524. #endif
  1525. static int nvme_open(struct block_device *bdev, fmode_t mode)
  1526. {
  1527. struct nvme_ns *ns = bdev->bd_disk->private_data;
  1528. struct nvme_dev *dev = ns->dev;
  1529. kref_get(&dev->kref);
  1530. return 0;
  1531. }
  1532. static void nvme_free_dev(struct kref *kref);
  1533. static void nvme_release(struct gendisk *disk, fmode_t mode)
  1534. {
  1535. struct nvme_ns *ns = disk->private_data;
  1536. struct nvme_dev *dev = ns->dev;
  1537. kref_put(&dev->kref, nvme_free_dev);
  1538. }
  1539. static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo)
  1540. {
  1541. /* some standard values */
  1542. geo->heads = 1 << 6;
  1543. geo->sectors = 1 << 5;
  1544. geo->cylinders = get_capacity(bd->bd_disk) >> 11;
  1545. return 0;
  1546. }
  1547. static const struct block_device_operations nvme_fops = {
  1548. .owner = THIS_MODULE,
  1549. .ioctl = nvme_ioctl,
  1550. .compat_ioctl = nvme_compat_ioctl,
  1551. .open = nvme_open,
  1552. .release = nvme_release,
  1553. .getgeo = nvme_getgeo,
  1554. };
  1555. static void nvme_resubmit_iods(struct nvme_queue *nvmeq)
  1556. {
  1557. struct nvme_iod *iod, *next;
  1558. list_for_each_entry_safe(iod, next, &nvmeq->iod_bio, node) {
  1559. if (unlikely(nvme_submit_iod(nvmeq, iod)))
  1560. break;
  1561. list_del(&iod->node);
  1562. if (bio_list_empty(&nvmeq->sq_cong) &&
  1563. list_empty(&nvmeq->iod_bio))
  1564. remove_wait_queue(&nvmeq->sq_full,
  1565. &nvmeq->sq_cong_wait);
  1566. }
  1567. }
  1568. static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
  1569. {
  1570. while (bio_list_peek(&nvmeq->sq_cong)) {
  1571. struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
  1572. struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
  1573. if (bio_list_empty(&nvmeq->sq_cong) &&
  1574. list_empty(&nvmeq->iod_bio))
  1575. remove_wait_queue(&nvmeq->sq_full,
  1576. &nvmeq->sq_cong_wait);
  1577. if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
  1578. if (!waitqueue_active(&nvmeq->sq_full))
  1579. add_wait_queue(&nvmeq->sq_full,
  1580. &nvmeq->sq_cong_wait);
  1581. bio_list_add_head(&nvmeq->sq_cong, bio);
  1582. break;
  1583. }
  1584. }
  1585. }
  1586. static int nvme_kthread(void *data)
  1587. {
  1588. struct nvme_dev *dev, *next;
  1589. while (!kthread_should_stop()) {
  1590. set_current_state(TASK_INTERRUPTIBLE);
  1591. spin_lock(&dev_list_lock);
  1592. list_for_each_entry_safe(dev, next, &dev_list, node) {
  1593. int i;
  1594. if (readl(&dev->bar->csts) & NVME_CSTS_CFS &&
  1595. dev->initialized) {
  1596. if (work_busy(&dev->reset_work))
  1597. continue;
  1598. list_del_init(&dev->node);
  1599. dev_warn(&dev->pci_dev->dev,
  1600. "Failed status, reset controller\n");
  1601. dev->reset_workfn = nvme_reset_failed_dev;
  1602. queue_work(nvme_workq, &dev->reset_work);
  1603. continue;
  1604. }
  1605. rcu_read_lock();
  1606. for (i = 0; i < dev->queue_count; i++) {
  1607. struct nvme_queue *nvmeq =
  1608. rcu_dereference(dev->queues[i]);
  1609. if (!nvmeq)
  1610. continue;
  1611. spin_lock_irq(&nvmeq->q_lock);
  1612. if (nvmeq->q_suspended)
  1613. goto unlock;
  1614. nvme_process_cq(nvmeq);
  1615. nvme_cancel_ios(nvmeq, true);
  1616. nvme_resubmit_bios(nvmeq);
  1617. nvme_resubmit_iods(nvmeq);
  1618. unlock:
  1619. spin_unlock_irq(&nvmeq->q_lock);
  1620. }
  1621. rcu_read_unlock();
  1622. }
  1623. spin_unlock(&dev_list_lock);
  1624. schedule_timeout(round_jiffies_relative(HZ));
  1625. }
  1626. return 0;
  1627. }
  1628. static void nvme_config_discard(struct nvme_ns *ns)
  1629. {
  1630. u32 logical_block_size = queue_logical_block_size(ns->queue);
  1631. ns->queue->limits.discard_zeroes_data = 0;
  1632. ns->queue->limits.discard_alignment = logical_block_size;
  1633. ns->queue->limits.discard_granularity = logical_block_size;
  1634. ns->queue->limits.max_discard_sectors = 0xffffffff;
  1635. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
  1636. }
  1637. static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid,
  1638. struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
  1639. {
  1640. struct nvme_ns *ns;
  1641. struct gendisk *disk;
  1642. int lbaf;
  1643. if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
  1644. return NULL;
  1645. ns = kzalloc(sizeof(*ns), GFP_KERNEL);
  1646. if (!ns)
  1647. return NULL;
  1648. ns->queue = blk_alloc_queue(GFP_KERNEL);
  1649. if (!ns->queue)
  1650. goto out_free_ns;
  1651. ns->queue->queue_flags = QUEUE_FLAG_DEFAULT;
  1652. queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
  1653. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
  1654. queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, ns->queue);
  1655. blk_queue_make_request(ns->queue, nvme_make_request);
  1656. ns->dev = dev;
  1657. ns->queue->queuedata = ns;
  1658. disk = alloc_disk(0);
  1659. if (!disk)
  1660. goto out_free_queue;
  1661. ns->ns_id = nsid;
  1662. ns->disk = disk;
  1663. lbaf = id->flbas & 0xf;
  1664. ns->lba_shift = id->lbaf[lbaf].ds;
  1665. ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
  1666. blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
  1667. if (dev->max_hw_sectors)
  1668. blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
  1669. if (dev->vwc & NVME_CTRL_VWC_PRESENT)
  1670. blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
  1671. disk->major = nvme_major;
  1672. disk->first_minor = 0;
  1673. disk->fops = &nvme_fops;
  1674. disk->private_data = ns;
  1675. disk->queue = ns->queue;
  1676. disk->driverfs_dev = &dev->pci_dev->dev;
  1677. disk->flags = GENHD_FL_EXT_DEVT;
  1678. sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
  1679. set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
  1680. if (dev->oncs & NVME_CTRL_ONCS_DSM)
  1681. nvme_config_discard(ns);
  1682. return ns;
  1683. out_free_queue:
  1684. blk_cleanup_queue(ns->queue);
  1685. out_free_ns:
  1686. kfree(ns);
  1687. return NULL;
  1688. }
  1689. static int nvme_find_closest_node(int node)
  1690. {
  1691. int n, val, min_val = INT_MAX, best_node = node;
  1692. for_each_online_node(n) {
  1693. if (n == node)
  1694. continue;
  1695. val = node_distance(node, n);
  1696. if (val < min_val) {
  1697. min_val = val;
  1698. best_node = n;
  1699. }
  1700. }
  1701. return best_node;
  1702. }
  1703. static void nvme_set_queue_cpus(cpumask_t *qmask, struct nvme_queue *nvmeq,
  1704. int count)
  1705. {
  1706. int cpu;
  1707. for_each_cpu(cpu, qmask) {
  1708. if (cpumask_weight(nvmeq->cpu_mask) >= count)
  1709. break;
  1710. if (!cpumask_test_and_set_cpu(cpu, nvmeq->cpu_mask))
  1711. *per_cpu_ptr(nvmeq->dev->io_queue, cpu) = nvmeq->qid;
  1712. }
  1713. }
  1714. static void nvme_add_cpus(cpumask_t *mask, const cpumask_t *unassigned_cpus,
  1715. const cpumask_t *new_mask, struct nvme_queue *nvmeq, int cpus_per_queue)
  1716. {
  1717. int next_cpu;
  1718. for_each_cpu(next_cpu, new_mask) {
  1719. cpumask_or(mask, mask, get_cpu_mask(next_cpu));
  1720. cpumask_or(mask, mask, topology_thread_cpumask(next_cpu));
  1721. cpumask_and(mask, mask, unassigned_cpus);
  1722. nvme_set_queue_cpus(mask, nvmeq, cpus_per_queue);
  1723. }
  1724. }
  1725. static void nvme_create_io_queues(struct nvme_dev *dev)
  1726. {
  1727. unsigned i, max;
  1728. max = min(dev->max_qid, num_online_cpus());
  1729. for (i = dev->queue_count; i <= max; i++)
  1730. if (!nvme_alloc_queue(dev, i, dev->q_depth, i - 1))
  1731. break;
  1732. max = min(dev->queue_count - 1, num_online_cpus());
  1733. for (i = dev->online_queues; i <= max; i++)
  1734. if (nvme_create_queue(raw_nvmeq(dev, i), i))
  1735. break;
  1736. }
  1737. /*
  1738. * If there are fewer queues than online cpus, this will try to optimally
  1739. * assign a queue to multiple cpus by grouping cpus that are "close" together:
  1740. * thread siblings, core, socket, closest node, then whatever else is
  1741. * available.
  1742. */
  1743. static void nvme_assign_io_queues(struct nvme_dev *dev)
  1744. {
  1745. unsigned cpu, cpus_per_queue, queues, remainder, i;
  1746. cpumask_var_t unassigned_cpus;
  1747. nvme_create_io_queues(dev);
  1748. queues = min(dev->online_queues - 1, num_online_cpus());
  1749. if (!queues)
  1750. return;
  1751. cpus_per_queue = num_online_cpus() / queues;
  1752. remainder = queues - (num_online_cpus() - queues * cpus_per_queue);
  1753. if (!alloc_cpumask_var(&unassigned_cpus, GFP_KERNEL))
  1754. return;
  1755. cpumask_copy(unassigned_cpus, cpu_online_mask);
  1756. cpu = cpumask_first(unassigned_cpus);
  1757. for (i = 1; i <= queues; i++) {
  1758. struct nvme_queue *nvmeq = lock_nvmeq(dev, i);
  1759. cpumask_t mask;
  1760. cpumask_clear(nvmeq->cpu_mask);
  1761. if (!cpumask_weight(unassigned_cpus)) {
  1762. unlock_nvmeq(nvmeq);
  1763. break;
  1764. }
  1765. mask = *get_cpu_mask(cpu);
  1766. nvme_set_queue_cpus(&mask, nvmeq, cpus_per_queue);
  1767. if (cpus_weight(mask) < cpus_per_queue)
  1768. nvme_add_cpus(&mask, unassigned_cpus,
  1769. topology_thread_cpumask(cpu),
  1770. nvmeq, cpus_per_queue);
  1771. if (cpus_weight(mask) < cpus_per_queue)
  1772. nvme_add_cpus(&mask, unassigned_cpus,
  1773. topology_core_cpumask(cpu),
  1774. nvmeq, cpus_per_queue);
  1775. if (cpus_weight(mask) < cpus_per_queue)
  1776. nvme_add_cpus(&mask, unassigned_cpus,
  1777. cpumask_of_node(cpu_to_node(cpu)),
  1778. nvmeq, cpus_per_queue);
  1779. if (cpus_weight(mask) < cpus_per_queue)
  1780. nvme_add_cpus(&mask, unassigned_cpus,
  1781. cpumask_of_node(
  1782. nvme_find_closest_node(
  1783. cpu_to_node(cpu))),
  1784. nvmeq, cpus_per_queue);
  1785. if (cpus_weight(mask) < cpus_per_queue)
  1786. nvme_add_cpus(&mask, unassigned_cpus,
  1787. unassigned_cpus,
  1788. nvmeq, cpus_per_queue);
  1789. WARN(cpumask_weight(nvmeq->cpu_mask) != cpus_per_queue,
  1790. "nvme%d qid:%d mis-matched queue-to-cpu assignment\n",
  1791. dev->instance, i);
  1792. irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
  1793. nvmeq->cpu_mask);
  1794. cpumask_andnot(unassigned_cpus, unassigned_cpus,
  1795. nvmeq->cpu_mask);
  1796. cpu = cpumask_next(cpu, unassigned_cpus);
  1797. if (remainder && !--remainder)
  1798. cpus_per_queue++;
  1799. unlock_nvmeq(nvmeq);
  1800. }
  1801. WARN(cpumask_weight(unassigned_cpus), "nvme%d unassigned online cpus\n",
  1802. dev->instance);
  1803. i = 0;
  1804. cpumask_andnot(unassigned_cpus, cpu_possible_mask, cpu_online_mask);
  1805. for_each_cpu(cpu, unassigned_cpus)
  1806. *per_cpu_ptr(dev->io_queue, cpu) = (i++ % queues) + 1;
  1807. free_cpumask_var(unassigned_cpus);
  1808. }
  1809. static int set_queue_count(struct nvme_dev *dev, int count)
  1810. {
  1811. int status;
  1812. u32 result;
  1813. u32 q_count = (count - 1) | ((count - 1) << 16);
  1814. status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
  1815. &result);
  1816. if (status < 0)
  1817. return status;
  1818. if (status > 0) {
  1819. dev_err(&dev->pci_dev->dev, "Could not set queue count (%d)\n",
  1820. status);
  1821. return -EBUSY;
  1822. }
  1823. return min(result & 0xffff, result >> 16) + 1;
  1824. }
  1825. static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
  1826. {
  1827. return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
  1828. }
  1829. static void nvme_cpu_workfn(struct work_struct *work)
  1830. {
  1831. struct nvme_dev *dev = container_of(work, struct nvme_dev, cpu_work);
  1832. if (dev->initialized)
  1833. nvme_assign_io_queues(dev);
  1834. }
  1835. static int nvme_cpu_notify(struct notifier_block *self,
  1836. unsigned long action, void *hcpu)
  1837. {
  1838. struct nvme_dev *dev;
  1839. switch (action) {
  1840. case CPU_ONLINE:
  1841. case CPU_DEAD:
  1842. spin_lock(&dev_list_lock);
  1843. list_for_each_entry(dev, &dev_list, node)
  1844. schedule_work(&dev->cpu_work);
  1845. spin_unlock(&dev_list_lock);
  1846. break;
  1847. }
  1848. return NOTIFY_OK;
  1849. }
  1850. static int nvme_setup_io_queues(struct nvme_dev *dev)
  1851. {
  1852. struct nvme_queue *adminq = raw_nvmeq(dev, 0);
  1853. struct pci_dev *pdev = dev->pci_dev;
  1854. int result, i, vecs, nr_io_queues, size;
  1855. nr_io_queues = num_possible_cpus();
  1856. result = set_queue_count(dev, nr_io_queues);
  1857. if (result < 0)
  1858. return result;
  1859. if (result < nr_io_queues)
  1860. nr_io_queues = result;
  1861. size = db_bar_size(dev, nr_io_queues);
  1862. if (size > 8192) {
  1863. iounmap(dev->bar);
  1864. do {
  1865. dev->bar = ioremap(pci_resource_start(pdev, 0), size);
  1866. if (dev->bar)
  1867. break;
  1868. if (!--nr_io_queues)
  1869. return -ENOMEM;
  1870. size = db_bar_size(dev, nr_io_queues);
  1871. } while (1);
  1872. dev->dbs = ((void __iomem *)dev->bar) + 4096;
  1873. adminq->q_db = dev->dbs;
  1874. }
  1875. /* Deregister the admin queue's interrupt */
  1876. free_irq(dev->entry[0].vector, adminq);
  1877. for (i = 0; i < nr_io_queues; i++)
  1878. dev->entry[i].entry = i;
  1879. vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
  1880. if (vecs < 0) {
  1881. vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
  1882. if (vecs < 0) {
  1883. vecs = 1;
  1884. } else {
  1885. for (i = 0; i < vecs; i++)
  1886. dev->entry[i].vector = i + pdev->irq;
  1887. }
  1888. }
  1889. /*
  1890. * Should investigate if there's a performance win from allocating
  1891. * more queues than interrupt vectors; it might allow the submission
  1892. * path to scale better, even if the receive path is limited by the
  1893. * number of interrupts.
  1894. */
  1895. nr_io_queues = vecs;
  1896. dev->max_qid = nr_io_queues;
  1897. result = queue_request_irq(dev, adminq, adminq->irqname);
  1898. if (result) {
  1899. adminq->q_suspended = 1;
  1900. goto free_queues;
  1901. }
  1902. /* Free previously allocated queues that are no longer usable */
  1903. nvme_free_queues(dev, nr_io_queues + 1);
  1904. nvme_assign_io_queues(dev);
  1905. return 0;
  1906. free_queues:
  1907. nvme_free_queues(dev, 1);
  1908. return result;
  1909. }
  1910. /*
  1911. * Return: error value if an error occurred setting up the queues or calling
  1912. * Identify Device. 0 if these succeeded, even if adding some of the
  1913. * namespaces failed. At the moment, these failures are silent. TBD which
  1914. * failures should be reported.
  1915. */
  1916. static int nvme_dev_add(struct nvme_dev *dev)
  1917. {
  1918. struct pci_dev *pdev = dev->pci_dev;
  1919. int res;
  1920. unsigned nn, i;
  1921. struct nvme_ns *ns;
  1922. struct nvme_id_ctrl *ctrl;
  1923. struct nvme_id_ns *id_ns;
  1924. void *mem;
  1925. dma_addr_t dma_addr;
  1926. int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
  1927. mem = dma_alloc_coherent(&pdev->dev, 8192, &dma_addr, GFP_KERNEL);
  1928. if (!mem)
  1929. return -ENOMEM;
  1930. res = nvme_identify(dev, 0, 1, dma_addr);
  1931. if (res) {
  1932. dev_err(&pdev->dev, "Identify Controller failed (%d)\n", res);
  1933. res = -EIO;
  1934. goto out;
  1935. }
  1936. ctrl = mem;
  1937. nn = le32_to_cpup(&ctrl->nn);
  1938. dev->oncs = le16_to_cpup(&ctrl->oncs);
  1939. dev->abort_limit = ctrl->acl + 1;
  1940. dev->vwc = ctrl->vwc;
  1941. memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
  1942. memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
  1943. memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
  1944. if (ctrl->mdts)
  1945. dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
  1946. if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
  1947. (pdev->device == 0x0953) && ctrl->vs[3])
  1948. dev->stripe_size = 1 << (ctrl->vs[3] + shift);
  1949. id_ns = mem;
  1950. for (i = 1; i <= nn; i++) {
  1951. res = nvme_identify(dev, i, 0, dma_addr);
  1952. if (res)
  1953. continue;
  1954. if (id_ns->ncap == 0)
  1955. continue;
  1956. res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
  1957. dma_addr + 4096, NULL);
  1958. if (res)
  1959. memset(mem + 4096, 0, 4096);
  1960. ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
  1961. if (ns)
  1962. list_add_tail(&ns->list, &dev->namespaces);
  1963. }
  1964. list_for_each_entry(ns, &dev->namespaces, list)
  1965. add_disk(ns->disk);
  1966. res = 0;
  1967. out:
  1968. dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
  1969. return res;
  1970. }
  1971. static int nvme_dev_map(struct nvme_dev *dev)
  1972. {
  1973. u64 cap;
  1974. int bars, result = -ENOMEM;
  1975. struct pci_dev *pdev = dev->pci_dev;
  1976. if (pci_enable_device_mem(pdev))
  1977. return result;
  1978. dev->entry[0].vector = pdev->irq;
  1979. pci_set_master(pdev);
  1980. bars = pci_select_bars(pdev, IORESOURCE_MEM);
  1981. if (pci_request_selected_regions(pdev, bars, "nvme"))
  1982. goto disable_pci;
  1983. if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
  1984. dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
  1985. goto disable;
  1986. dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
  1987. if (!dev->bar)
  1988. goto disable;
  1989. if (readl(&dev->bar->csts) == -1) {
  1990. result = -ENODEV;
  1991. goto unmap;
  1992. }
  1993. cap = readq(&dev->bar->cap);
  1994. dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
  1995. dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
  1996. dev->dbs = ((void __iomem *)dev->bar) + 4096;
  1997. return 0;
  1998. unmap:
  1999. iounmap(dev->bar);
  2000. dev->bar = NULL;
  2001. disable:
  2002. pci_release_regions(pdev);
  2003. disable_pci:
  2004. pci_disable_device(pdev);
  2005. return result;
  2006. }
  2007. static void nvme_dev_unmap(struct nvme_dev *dev)
  2008. {
  2009. if (dev->pci_dev->msi_enabled)
  2010. pci_disable_msi(dev->pci_dev);
  2011. else if (dev->pci_dev->msix_enabled)
  2012. pci_disable_msix(dev->pci_dev);
  2013. if (dev->bar) {
  2014. iounmap(dev->bar);
  2015. dev->bar = NULL;
  2016. pci_release_regions(dev->pci_dev);
  2017. }
  2018. if (pci_is_enabled(dev->pci_dev))
  2019. pci_disable_device(dev->pci_dev);
  2020. }
  2021. struct nvme_delq_ctx {
  2022. struct task_struct *waiter;
  2023. struct kthread_worker *worker;
  2024. atomic_t refcount;
  2025. };
  2026. static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
  2027. {
  2028. dq->waiter = current;
  2029. mb();
  2030. for (;;) {
  2031. set_current_state(TASK_KILLABLE);
  2032. if (!atomic_read(&dq->refcount))
  2033. break;
  2034. if (!schedule_timeout(ADMIN_TIMEOUT) ||
  2035. fatal_signal_pending(current)) {
  2036. set_current_state(TASK_RUNNING);
  2037. nvme_disable_ctrl(dev, readq(&dev->bar->cap));
  2038. nvme_disable_queue(dev, 0);
  2039. send_sig(SIGKILL, dq->worker->task, 1);
  2040. flush_kthread_worker(dq->worker);
  2041. return;
  2042. }
  2043. }
  2044. set_current_state(TASK_RUNNING);
  2045. }
  2046. static void nvme_put_dq(struct nvme_delq_ctx *dq)
  2047. {
  2048. atomic_dec(&dq->refcount);
  2049. if (dq->waiter)
  2050. wake_up_process(dq->waiter);
  2051. }
  2052. static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
  2053. {
  2054. atomic_inc(&dq->refcount);
  2055. return dq;
  2056. }
  2057. static void nvme_del_queue_end(struct nvme_queue *nvmeq)
  2058. {
  2059. struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
  2060. nvme_clear_queue(nvmeq);
  2061. nvme_put_dq(dq);
  2062. }
  2063. static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
  2064. kthread_work_func_t fn)
  2065. {
  2066. struct nvme_command c;
  2067. memset(&c, 0, sizeof(c));
  2068. c.delete_queue.opcode = opcode;
  2069. c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
  2070. init_kthread_work(&nvmeq->cmdinfo.work, fn);
  2071. return nvme_submit_admin_cmd_async(nvmeq->dev, &c, &nvmeq->cmdinfo);
  2072. }
  2073. static void nvme_del_cq_work_handler(struct kthread_work *work)
  2074. {
  2075. struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
  2076. cmdinfo.work);
  2077. nvme_del_queue_end(nvmeq);
  2078. }
  2079. static int nvme_delete_cq(struct nvme_queue *nvmeq)
  2080. {
  2081. return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
  2082. nvme_del_cq_work_handler);
  2083. }
  2084. static void nvme_del_sq_work_handler(struct kthread_work *work)
  2085. {
  2086. struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
  2087. cmdinfo.work);
  2088. int status = nvmeq->cmdinfo.status;
  2089. if (!status)
  2090. status = nvme_delete_cq(nvmeq);
  2091. if (status)
  2092. nvme_del_queue_end(nvmeq);
  2093. }
  2094. static int nvme_delete_sq(struct nvme_queue *nvmeq)
  2095. {
  2096. return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
  2097. nvme_del_sq_work_handler);
  2098. }
  2099. static void nvme_del_queue_start(struct kthread_work *work)
  2100. {
  2101. struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
  2102. cmdinfo.work);
  2103. allow_signal(SIGKILL);
  2104. if (nvme_delete_sq(nvmeq))
  2105. nvme_del_queue_end(nvmeq);
  2106. }
  2107. static void nvme_disable_io_queues(struct nvme_dev *dev)
  2108. {
  2109. int i;
  2110. DEFINE_KTHREAD_WORKER_ONSTACK(worker);
  2111. struct nvme_delq_ctx dq;
  2112. struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
  2113. &worker, "nvme%d", dev->instance);
  2114. if (IS_ERR(kworker_task)) {
  2115. dev_err(&dev->pci_dev->dev,
  2116. "Failed to create queue del task\n");
  2117. for (i = dev->queue_count - 1; i > 0; i--)
  2118. nvme_disable_queue(dev, i);
  2119. return;
  2120. }
  2121. dq.waiter = NULL;
  2122. atomic_set(&dq.refcount, 0);
  2123. dq.worker = &worker;
  2124. for (i = dev->queue_count - 1; i > 0; i--) {
  2125. struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
  2126. if (nvme_suspend_queue(nvmeq))
  2127. continue;
  2128. nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
  2129. nvmeq->cmdinfo.worker = dq.worker;
  2130. init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
  2131. queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
  2132. }
  2133. nvme_wait_dq(&dq, dev);
  2134. kthread_stop(kworker_task);
  2135. }
  2136. /*
  2137. * Remove the node from the device list and check
  2138. * for whether or not we need to stop the nvme_thread.
  2139. */
  2140. static void nvme_dev_list_remove(struct nvme_dev *dev)
  2141. {
  2142. struct task_struct *tmp = NULL;
  2143. spin_lock(&dev_list_lock);
  2144. list_del_init(&dev->node);
  2145. if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
  2146. tmp = nvme_thread;
  2147. nvme_thread = NULL;
  2148. }
  2149. spin_unlock(&dev_list_lock);
  2150. if (tmp)
  2151. kthread_stop(tmp);
  2152. }
  2153. static void nvme_dev_shutdown(struct nvme_dev *dev)
  2154. {
  2155. int i;
  2156. dev->initialized = 0;
  2157. nvme_dev_list_remove(dev);
  2158. if (!dev->bar || (dev->bar && readl(&dev->bar->csts) == -1)) {
  2159. for (i = dev->queue_count - 1; i >= 0; i--) {
  2160. struct nvme_queue *nvmeq = raw_nvmeq(dev, i);
  2161. nvme_suspend_queue(nvmeq);
  2162. nvme_clear_queue(nvmeq);
  2163. }
  2164. } else {
  2165. nvme_disable_io_queues(dev);
  2166. nvme_shutdown_ctrl(dev);
  2167. nvme_disable_queue(dev, 0);
  2168. }
  2169. nvme_dev_unmap(dev);
  2170. }
  2171. static void nvme_dev_remove(struct nvme_dev *dev)
  2172. {
  2173. struct nvme_ns *ns;
  2174. list_for_each_entry(ns, &dev->namespaces, list) {
  2175. if (ns->disk->flags & GENHD_FL_UP)
  2176. del_gendisk(ns->disk);
  2177. if (!blk_queue_dying(ns->queue))
  2178. blk_cleanup_queue(ns->queue);
  2179. }
  2180. }
  2181. static int nvme_setup_prp_pools(struct nvme_dev *dev)
  2182. {
  2183. struct device *dmadev = &dev->pci_dev->dev;
  2184. dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
  2185. PAGE_SIZE, PAGE_SIZE, 0);
  2186. if (!dev->prp_page_pool)
  2187. return -ENOMEM;
  2188. /* Optimisation for I/Os between 4k and 128k */
  2189. dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
  2190. 256, 256, 0);
  2191. if (!dev->prp_small_pool) {
  2192. dma_pool_destroy(dev->prp_page_pool);
  2193. return -ENOMEM;
  2194. }
  2195. return 0;
  2196. }
  2197. static void nvme_release_prp_pools(struct nvme_dev *dev)
  2198. {
  2199. dma_pool_destroy(dev->prp_page_pool);
  2200. dma_pool_destroy(dev->prp_small_pool);
  2201. }
  2202. static DEFINE_IDA(nvme_instance_ida);
  2203. static int nvme_set_instance(struct nvme_dev *dev)
  2204. {
  2205. int instance, error;
  2206. do {
  2207. if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
  2208. return -ENODEV;
  2209. spin_lock(&dev_list_lock);
  2210. error = ida_get_new(&nvme_instance_ida, &instance);
  2211. spin_unlock(&dev_list_lock);
  2212. } while (error == -EAGAIN);
  2213. if (error)
  2214. return -ENODEV;
  2215. dev->instance = instance;
  2216. return 0;
  2217. }
  2218. static void nvme_release_instance(struct nvme_dev *dev)
  2219. {
  2220. spin_lock(&dev_list_lock);
  2221. ida_remove(&nvme_instance_ida, dev->instance);
  2222. spin_unlock(&dev_list_lock);
  2223. }
  2224. static void nvme_free_namespaces(struct nvme_dev *dev)
  2225. {
  2226. struct nvme_ns *ns, *next;
  2227. list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
  2228. list_del(&ns->list);
  2229. put_disk(ns->disk);
  2230. kfree(ns);
  2231. }
  2232. }
  2233. static void nvme_free_dev(struct kref *kref)
  2234. {
  2235. struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
  2236. nvme_free_namespaces(dev);
  2237. free_percpu(dev->io_queue);
  2238. kfree(dev->queues);
  2239. kfree(dev->entry);
  2240. kfree(dev);
  2241. }
  2242. static int nvme_dev_open(struct inode *inode, struct file *f)
  2243. {
  2244. struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev,
  2245. miscdev);
  2246. kref_get(&dev->kref);
  2247. f->private_data = dev;
  2248. return 0;
  2249. }
  2250. static int nvme_dev_release(struct inode *inode, struct file *f)
  2251. {
  2252. struct nvme_dev *dev = f->private_data;
  2253. kref_put(&dev->kref, nvme_free_dev);
  2254. return 0;
  2255. }
  2256. static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  2257. {
  2258. struct nvme_dev *dev = f->private_data;
  2259. switch (cmd) {
  2260. case NVME_IOCTL_ADMIN_CMD:
  2261. return nvme_user_admin_cmd(dev, (void __user *)arg);
  2262. default:
  2263. return -ENOTTY;
  2264. }
  2265. }
  2266. static const struct file_operations nvme_dev_fops = {
  2267. .owner = THIS_MODULE,
  2268. .open = nvme_dev_open,
  2269. .release = nvme_dev_release,
  2270. .unlocked_ioctl = nvme_dev_ioctl,
  2271. .compat_ioctl = nvme_dev_ioctl,
  2272. };
  2273. static int nvme_dev_start(struct nvme_dev *dev)
  2274. {
  2275. int result;
  2276. bool start_thread = false;
  2277. result = nvme_dev_map(dev);
  2278. if (result)
  2279. return result;
  2280. result = nvme_configure_admin_queue(dev);
  2281. if (result)
  2282. goto unmap;
  2283. spin_lock(&dev_list_lock);
  2284. if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
  2285. start_thread = true;
  2286. nvme_thread = NULL;
  2287. }
  2288. list_add(&dev->node, &dev_list);
  2289. spin_unlock(&dev_list_lock);
  2290. if (start_thread) {
  2291. nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
  2292. wake_up(&nvme_kthread_wait);
  2293. } else
  2294. wait_event_killable(nvme_kthread_wait, nvme_thread);
  2295. if (IS_ERR_OR_NULL(nvme_thread)) {
  2296. result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
  2297. goto disable;
  2298. }
  2299. result = nvme_setup_io_queues(dev);
  2300. if (result && result != -EBUSY)
  2301. goto disable;
  2302. return result;
  2303. disable:
  2304. nvme_disable_queue(dev, 0);
  2305. nvme_dev_list_remove(dev);
  2306. unmap:
  2307. nvme_dev_unmap(dev);
  2308. return result;
  2309. }
  2310. static int nvme_remove_dead_ctrl(void *arg)
  2311. {
  2312. struct nvme_dev *dev = (struct nvme_dev *)arg;
  2313. struct pci_dev *pdev = dev->pci_dev;
  2314. if (pci_get_drvdata(pdev))
  2315. pci_stop_and_remove_bus_device(pdev);
  2316. kref_put(&dev->kref, nvme_free_dev);
  2317. return 0;
  2318. }
  2319. static void nvme_remove_disks(struct work_struct *ws)
  2320. {
  2321. struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
  2322. nvme_dev_remove(dev);
  2323. nvme_free_queues(dev, 1);
  2324. }
  2325. static int nvme_dev_resume(struct nvme_dev *dev)
  2326. {
  2327. int ret;
  2328. ret = nvme_dev_start(dev);
  2329. if (ret && ret != -EBUSY)
  2330. return ret;
  2331. if (ret == -EBUSY) {
  2332. spin_lock(&dev_list_lock);
  2333. dev->reset_workfn = nvme_remove_disks;
  2334. queue_work(nvme_workq, &dev->reset_work);
  2335. spin_unlock(&dev_list_lock);
  2336. }
  2337. dev->initialized = 1;
  2338. return 0;
  2339. }
  2340. static void nvme_dev_reset(struct nvme_dev *dev)
  2341. {
  2342. nvme_dev_shutdown(dev);
  2343. if (nvme_dev_resume(dev)) {
  2344. dev_err(&dev->pci_dev->dev, "Device failed to resume\n");
  2345. kref_get(&dev->kref);
  2346. if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
  2347. dev->instance))) {
  2348. dev_err(&dev->pci_dev->dev,
  2349. "Failed to start controller remove task\n");
  2350. kref_put(&dev->kref, nvme_free_dev);
  2351. }
  2352. }
  2353. }
  2354. static void nvme_reset_failed_dev(struct work_struct *ws)
  2355. {
  2356. struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
  2357. nvme_dev_reset(dev);
  2358. }
  2359. static void nvme_reset_workfn(struct work_struct *work)
  2360. {
  2361. struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
  2362. dev->reset_workfn(work);
  2363. }
  2364. static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  2365. {
  2366. int result = -ENOMEM;
  2367. struct nvme_dev *dev;
  2368. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  2369. if (!dev)
  2370. return -ENOMEM;
  2371. dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
  2372. GFP_KERNEL);
  2373. if (!dev->entry)
  2374. goto free;
  2375. dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
  2376. GFP_KERNEL);
  2377. if (!dev->queues)
  2378. goto free;
  2379. dev->io_queue = alloc_percpu(unsigned short);
  2380. if (!dev->io_queue)
  2381. goto free;
  2382. INIT_LIST_HEAD(&dev->namespaces);
  2383. dev->reset_workfn = nvme_reset_failed_dev;
  2384. INIT_WORK(&dev->reset_work, nvme_reset_workfn);
  2385. INIT_WORK(&dev->cpu_work, nvme_cpu_workfn);
  2386. dev->pci_dev = pdev;
  2387. pci_set_drvdata(pdev, dev);
  2388. result = nvme_set_instance(dev);
  2389. if (result)
  2390. goto free;
  2391. result = nvme_setup_prp_pools(dev);
  2392. if (result)
  2393. goto release;
  2394. kref_init(&dev->kref);
  2395. result = nvme_dev_start(dev);
  2396. if (result) {
  2397. if (result == -EBUSY)
  2398. goto create_cdev;
  2399. goto release_pools;
  2400. }
  2401. result = nvme_dev_add(dev);
  2402. if (result)
  2403. goto shutdown;
  2404. create_cdev:
  2405. scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance);
  2406. dev->miscdev.minor = MISC_DYNAMIC_MINOR;
  2407. dev->miscdev.parent = &pdev->dev;
  2408. dev->miscdev.name = dev->name;
  2409. dev->miscdev.fops = &nvme_dev_fops;
  2410. result = misc_register(&dev->miscdev);
  2411. if (result)
  2412. goto remove;
  2413. dev->initialized = 1;
  2414. return 0;
  2415. remove:
  2416. nvme_dev_remove(dev);
  2417. nvme_free_namespaces(dev);
  2418. shutdown:
  2419. nvme_dev_shutdown(dev);
  2420. release_pools:
  2421. nvme_free_queues(dev, 0);
  2422. nvme_release_prp_pools(dev);
  2423. release:
  2424. nvme_release_instance(dev);
  2425. free:
  2426. free_percpu(dev->io_queue);
  2427. kfree(dev->queues);
  2428. kfree(dev->entry);
  2429. kfree(dev);
  2430. return result;
  2431. }
  2432. static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
  2433. {
  2434. struct nvme_dev *dev = pci_get_drvdata(pdev);
  2435. if (prepare)
  2436. nvme_dev_shutdown(dev);
  2437. else
  2438. nvme_dev_resume(dev);
  2439. }
  2440. static void nvme_shutdown(struct pci_dev *pdev)
  2441. {
  2442. struct nvme_dev *dev = pci_get_drvdata(pdev);
  2443. nvme_dev_shutdown(dev);
  2444. }
  2445. static void nvme_remove(struct pci_dev *pdev)
  2446. {
  2447. struct nvme_dev *dev = pci_get_drvdata(pdev);
  2448. spin_lock(&dev_list_lock);
  2449. list_del_init(&dev->node);
  2450. spin_unlock(&dev_list_lock);
  2451. pci_set_drvdata(pdev, NULL);
  2452. flush_work(&dev->reset_work);
  2453. flush_work(&dev->cpu_work);
  2454. misc_deregister(&dev->miscdev);
  2455. nvme_dev_remove(dev);
  2456. nvme_dev_shutdown(dev);
  2457. nvme_free_queues(dev, 0);
  2458. rcu_barrier();
  2459. nvme_release_instance(dev);
  2460. nvme_release_prp_pools(dev);
  2461. kref_put(&dev->kref, nvme_free_dev);
  2462. }
  2463. /* These functions are yet to be implemented */
  2464. #define nvme_error_detected NULL
  2465. #define nvme_dump_registers NULL
  2466. #define nvme_link_reset NULL
  2467. #define nvme_slot_reset NULL
  2468. #define nvme_error_resume NULL
  2469. #ifdef CONFIG_PM_SLEEP
  2470. static int nvme_suspend(struct device *dev)
  2471. {
  2472. struct pci_dev *pdev = to_pci_dev(dev);
  2473. struct nvme_dev *ndev = pci_get_drvdata(pdev);
  2474. nvme_dev_shutdown(ndev);
  2475. return 0;
  2476. }
  2477. static int nvme_resume(struct device *dev)
  2478. {
  2479. struct pci_dev *pdev = to_pci_dev(dev);
  2480. struct nvme_dev *ndev = pci_get_drvdata(pdev);
  2481. if (nvme_dev_resume(ndev) && !work_busy(&ndev->reset_work)) {
  2482. ndev->reset_workfn = nvme_reset_failed_dev;
  2483. queue_work(nvme_workq, &ndev->reset_work);
  2484. }
  2485. return 0;
  2486. }
  2487. #endif
  2488. static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
  2489. static const struct pci_error_handlers nvme_err_handler = {
  2490. .error_detected = nvme_error_detected,
  2491. .mmio_enabled = nvme_dump_registers,
  2492. .link_reset = nvme_link_reset,
  2493. .slot_reset = nvme_slot_reset,
  2494. .resume = nvme_error_resume,
  2495. .reset_notify = nvme_reset_notify,
  2496. };
  2497. /* Move to pci_ids.h later */
  2498. #define PCI_CLASS_STORAGE_EXPRESS 0x010802
  2499. static const struct pci_device_id nvme_id_table[] = {
  2500. { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
  2501. { 0, }
  2502. };
  2503. MODULE_DEVICE_TABLE(pci, nvme_id_table);
  2504. static struct pci_driver nvme_driver = {
  2505. .name = "nvme",
  2506. .id_table = nvme_id_table,
  2507. .probe = nvme_probe,
  2508. .remove = nvme_remove,
  2509. .shutdown = nvme_shutdown,
  2510. .driver = {
  2511. .pm = &nvme_dev_pm_ops,
  2512. },
  2513. .err_handler = &nvme_err_handler,
  2514. };
  2515. static int __init nvme_init(void)
  2516. {
  2517. int result;
  2518. init_waitqueue_head(&nvme_kthread_wait);
  2519. nvme_workq = create_singlethread_workqueue("nvme");
  2520. if (!nvme_workq)
  2521. return -ENOMEM;
  2522. result = register_blkdev(nvme_major, "nvme");
  2523. if (result < 0)
  2524. goto kill_workq;
  2525. else if (result > 0)
  2526. nvme_major = result;
  2527. nvme_nb.notifier_call = &nvme_cpu_notify;
  2528. result = register_hotcpu_notifier(&nvme_nb);
  2529. if (result)
  2530. goto unregister_blkdev;
  2531. result = pci_register_driver(&nvme_driver);
  2532. if (result)
  2533. goto unregister_hotcpu;
  2534. return 0;
  2535. unregister_hotcpu:
  2536. unregister_hotcpu_notifier(&nvme_nb);
  2537. unregister_blkdev:
  2538. unregister_blkdev(nvme_major, "nvme");
  2539. kill_workq:
  2540. destroy_workqueue(nvme_workq);
  2541. return result;
  2542. }
  2543. static void __exit nvme_exit(void)
  2544. {
  2545. pci_unregister_driver(&nvme_driver);
  2546. unregister_hotcpu_notifier(&nvme_nb);
  2547. unregister_blkdev(nvme_major, "nvme");
  2548. destroy_workqueue(nvme_workq);
  2549. BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
  2550. _nvme_check_size();
  2551. }
  2552. MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
  2553. MODULE_LICENSE("GPL");
  2554. MODULE_VERSION("0.9");
  2555. module_init(nvme_init);
  2556. module_exit(nvme_exit);