nvme.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  1. /*
  2. * NVM Express device driver
  3. * Copyright (c) 2011, 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. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include <linux/nvme.h>
  19. #include <linux/bio.h>
  20. #include <linux/bitops.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/delay.h>
  23. #include <linux/errno.h>
  24. #include <linux/fs.h>
  25. #include <linux/genhd.h>
  26. #include <linux/idr.h>
  27. #include <linux/init.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/io.h>
  30. #include <linux/kdev_t.h>
  31. #include <linux/kthread.h>
  32. #include <linux/kernel.h>
  33. #include <linux/mm.h>
  34. #include <linux/module.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/pci.h>
  37. #include <linux/poison.h>
  38. #include <linux/sched.h>
  39. #include <linux/slab.h>
  40. #include <linux/types.h>
  41. #include <asm-generic/io-64-nonatomic-lo-hi.h>
  42. #define NVME_Q_DEPTH 1024
  43. #define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
  44. #define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
  45. #define NVME_MINORS 64
  46. #define NVME_IO_TIMEOUT (5 * HZ)
  47. #define ADMIN_TIMEOUT (60 * HZ)
  48. static int nvme_major;
  49. module_param(nvme_major, int, 0);
  50. static int use_threaded_interrupts;
  51. module_param(use_threaded_interrupts, int, 0);
  52. static DEFINE_SPINLOCK(dev_list_lock);
  53. static LIST_HEAD(dev_list);
  54. static struct task_struct *nvme_thread;
  55. /*
  56. * Represents an NVM Express device. Each nvme_dev is a PCI function.
  57. */
  58. struct nvme_dev {
  59. struct list_head node;
  60. struct nvme_queue **queues;
  61. u32 __iomem *dbs;
  62. struct pci_dev *pci_dev;
  63. struct dma_pool *prp_page_pool;
  64. struct dma_pool *prp_small_pool;
  65. int instance;
  66. int queue_count;
  67. int db_stride;
  68. u32 ctrl_config;
  69. struct msix_entry *entry;
  70. struct nvme_bar __iomem *bar;
  71. struct list_head namespaces;
  72. char serial[20];
  73. char model[40];
  74. char firmware_rev[8];
  75. u32 max_hw_sectors;
  76. };
  77. /*
  78. * An NVM Express namespace is equivalent to a SCSI LUN
  79. */
  80. struct nvme_ns {
  81. struct list_head list;
  82. struct nvme_dev *dev;
  83. struct request_queue *queue;
  84. struct gendisk *disk;
  85. int ns_id;
  86. int lba_shift;
  87. };
  88. /*
  89. * An NVM Express queue. Each device has at least two (one for admin
  90. * commands and one for I/O commands).
  91. */
  92. struct nvme_queue {
  93. struct device *q_dmadev;
  94. struct nvme_dev *dev;
  95. spinlock_t q_lock;
  96. struct nvme_command *sq_cmds;
  97. volatile struct nvme_completion *cqes;
  98. dma_addr_t sq_dma_addr;
  99. dma_addr_t cq_dma_addr;
  100. wait_queue_head_t sq_full;
  101. wait_queue_t sq_cong_wait;
  102. struct bio_list sq_cong;
  103. u32 __iomem *q_db;
  104. u16 q_depth;
  105. u16 cq_vector;
  106. u16 sq_head;
  107. u16 sq_tail;
  108. u16 cq_head;
  109. u16 cq_phase;
  110. unsigned long cmdid_data[];
  111. };
  112. /*
  113. * Check we didin't inadvertently grow the command struct
  114. */
  115. static inline void _nvme_check_size(void)
  116. {
  117. BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
  118. BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
  119. BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
  120. BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
  121. BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
  122. BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
  123. BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
  124. BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
  125. BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
  126. }
  127. typedef void (*nvme_completion_fn)(struct nvme_dev *, void *,
  128. struct nvme_completion *);
  129. struct nvme_cmd_info {
  130. nvme_completion_fn fn;
  131. void *ctx;
  132. unsigned long timeout;
  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. /**
  139. * alloc_cmdid() - Allocate a Command ID
  140. * @nvmeq: The queue that will be used for this command
  141. * @ctx: A pointer that will be passed to the handler
  142. * @handler: The function to call on completion
  143. *
  144. * Allocate a Command ID for a queue. The data passed in will
  145. * be passed to the completion handler. This is implemented by using
  146. * the bottom two bits of the ctx pointer to store the handler ID.
  147. * Passing in a pointer that's not 4-byte aligned will cause a BUG.
  148. * We can change this if it becomes a problem.
  149. *
  150. * May be called with local interrupts disabled and the q_lock held,
  151. * or with interrupts enabled and no locks held.
  152. */
  153. static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx,
  154. nvme_completion_fn handler, unsigned timeout)
  155. {
  156. int depth = nvmeq->q_depth - 1;
  157. struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
  158. int cmdid;
  159. do {
  160. cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
  161. if (cmdid >= depth)
  162. return -EBUSY;
  163. } while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
  164. info[cmdid].fn = handler;
  165. info[cmdid].ctx = ctx;
  166. info[cmdid].timeout = jiffies + timeout;
  167. return cmdid;
  168. }
  169. static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
  170. nvme_completion_fn handler, unsigned timeout)
  171. {
  172. int cmdid;
  173. wait_event_killable(nvmeq->sq_full,
  174. (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0);
  175. return (cmdid < 0) ? -EINTR : cmdid;
  176. }
  177. /* Special values must be less than 0x1000 */
  178. #define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
  179. #define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
  180. #define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
  181. #define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
  182. #define CMD_CTX_FLUSH (0x318 + CMD_CTX_BASE)
  183. static void special_completion(struct nvme_dev *dev, void *ctx,
  184. struct nvme_completion *cqe)
  185. {
  186. if (ctx == CMD_CTX_CANCELLED)
  187. return;
  188. if (ctx == CMD_CTX_FLUSH)
  189. return;
  190. if (ctx == CMD_CTX_COMPLETED) {
  191. dev_warn(&dev->pci_dev->dev,
  192. "completed id %d twice on queue %d\n",
  193. cqe->command_id, le16_to_cpup(&cqe->sq_id));
  194. return;
  195. }
  196. if (ctx == CMD_CTX_INVALID) {
  197. dev_warn(&dev->pci_dev->dev,
  198. "invalid id %d completed on queue %d\n",
  199. cqe->command_id, le16_to_cpup(&cqe->sq_id));
  200. return;
  201. }
  202. dev_warn(&dev->pci_dev->dev, "Unknown special completion %p\n", ctx);
  203. }
  204. /*
  205. * Called with local interrupts disabled and the q_lock held. May not sleep.
  206. */
  207. static void *free_cmdid(struct nvme_queue *nvmeq, int cmdid,
  208. nvme_completion_fn *fn)
  209. {
  210. void *ctx;
  211. struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
  212. if (cmdid >= nvmeq->q_depth) {
  213. *fn = special_completion;
  214. return CMD_CTX_INVALID;
  215. }
  216. if (fn)
  217. *fn = info[cmdid].fn;
  218. ctx = info[cmdid].ctx;
  219. info[cmdid].fn = special_completion;
  220. info[cmdid].ctx = CMD_CTX_COMPLETED;
  221. clear_bit(cmdid, nvmeq->cmdid_data);
  222. wake_up(&nvmeq->sq_full);
  223. return ctx;
  224. }
  225. static void *cancel_cmdid(struct nvme_queue *nvmeq, int cmdid,
  226. nvme_completion_fn *fn)
  227. {
  228. void *ctx;
  229. struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
  230. if (fn)
  231. *fn = info[cmdid].fn;
  232. ctx = info[cmdid].ctx;
  233. info[cmdid].fn = special_completion;
  234. info[cmdid].ctx = CMD_CTX_CANCELLED;
  235. return ctx;
  236. }
  237. static struct nvme_queue *get_nvmeq(struct nvme_dev *dev)
  238. {
  239. return dev->queues[get_cpu() + 1];
  240. }
  241. static void put_nvmeq(struct nvme_queue *nvmeq)
  242. {
  243. put_cpu();
  244. }
  245. /**
  246. * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
  247. * @nvmeq: The queue to use
  248. * @cmd: The command to send
  249. *
  250. * Safe to use from interrupt context
  251. */
  252. static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
  253. {
  254. unsigned long flags;
  255. u16 tail;
  256. spin_lock_irqsave(&nvmeq->q_lock, flags);
  257. tail = nvmeq->sq_tail;
  258. memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
  259. if (++tail == nvmeq->q_depth)
  260. tail = 0;
  261. writel(tail, nvmeq->q_db);
  262. nvmeq->sq_tail = tail;
  263. spin_unlock_irqrestore(&nvmeq->q_lock, flags);
  264. return 0;
  265. }
  266. /*
  267. * The nvme_iod describes the data in an I/O, including the list of PRP
  268. * entries. You can't see it in this data structure because C doesn't let
  269. * me express that. Use nvme_alloc_iod to ensure there's enough space
  270. * allocated to store the PRP list.
  271. */
  272. struct nvme_iod {
  273. void *private; /* For the use of the submitter of the I/O */
  274. int npages; /* In the PRP list. 0 means small pool in use */
  275. int offset; /* Of PRP list */
  276. int nents; /* Used in scatterlist */
  277. int length; /* Of data, in bytes */
  278. dma_addr_t first_dma;
  279. struct scatterlist sg[0];
  280. };
  281. static __le64 **iod_list(struct nvme_iod *iod)
  282. {
  283. return ((void *)iod) + iod->offset;
  284. }
  285. /*
  286. * Will slightly overestimate the number of pages needed. This is OK
  287. * as it only leads to a small amount of wasted memory for the lifetime of
  288. * the I/O.
  289. */
  290. static int nvme_npages(unsigned size)
  291. {
  292. unsigned nprps = DIV_ROUND_UP(size + PAGE_SIZE, PAGE_SIZE);
  293. return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
  294. }
  295. static struct nvme_iod *
  296. nvme_alloc_iod(unsigned nseg, unsigned nbytes, gfp_t gfp)
  297. {
  298. struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
  299. sizeof(__le64 *) * nvme_npages(nbytes) +
  300. sizeof(struct scatterlist) * nseg, gfp);
  301. if (iod) {
  302. iod->offset = offsetof(struct nvme_iod, sg[nseg]);
  303. iod->npages = -1;
  304. iod->length = nbytes;
  305. }
  306. return iod;
  307. }
  308. static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
  309. {
  310. const int last_prp = PAGE_SIZE / 8 - 1;
  311. int i;
  312. __le64 **list = iod_list(iod);
  313. dma_addr_t prp_dma = iod->first_dma;
  314. if (iod->npages == 0)
  315. dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
  316. for (i = 0; i < iod->npages; i++) {
  317. __le64 *prp_list = list[i];
  318. dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
  319. dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
  320. prp_dma = next_prp_dma;
  321. }
  322. kfree(iod);
  323. }
  324. static void requeue_bio(struct nvme_dev *dev, struct bio *bio)
  325. {
  326. struct nvme_queue *nvmeq = get_nvmeq(dev);
  327. if (bio_list_empty(&nvmeq->sq_cong))
  328. add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
  329. bio_list_add(&nvmeq->sq_cong, bio);
  330. put_nvmeq(nvmeq);
  331. wake_up_process(nvme_thread);
  332. }
  333. static void bio_completion(struct nvme_dev *dev, void *ctx,
  334. struct nvme_completion *cqe)
  335. {
  336. struct nvme_iod *iod = ctx;
  337. struct bio *bio = iod->private;
  338. u16 status = le16_to_cpup(&cqe->status) >> 1;
  339. dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
  340. bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  341. nvme_free_iod(dev, iod);
  342. if (status) {
  343. bio_endio(bio, -EIO);
  344. } else if (bio->bi_vcnt > bio->bi_idx) {
  345. requeue_bio(dev, bio);
  346. } else {
  347. bio_endio(bio, 0);
  348. }
  349. }
  350. /* length is in bytes. gfp flags indicates whether we may sleep. */
  351. static int nvme_setup_prps(struct nvme_dev *dev,
  352. struct nvme_common_command *cmd, struct nvme_iod *iod,
  353. int total_len, gfp_t gfp)
  354. {
  355. struct dma_pool *pool;
  356. int length = total_len;
  357. struct scatterlist *sg = iod->sg;
  358. int dma_len = sg_dma_len(sg);
  359. u64 dma_addr = sg_dma_address(sg);
  360. int offset = offset_in_page(dma_addr);
  361. __le64 *prp_list;
  362. __le64 **list = iod_list(iod);
  363. dma_addr_t prp_dma;
  364. int nprps, i;
  365. cmd->prp1 = cpu_to_le64(dma_addr);
  366. length -= (PAGE_SIZE - offset);
  367. if (length <= 0)
  368. return total_len;
  369. dma_len -= (PAGE_SIZE - offset);
  370. if (dma_len) {
  371. dma_addr += (PAGE_SIZE - offset);
  372. } else {
  373. sg = sg_next(sg);
  374. dma_addr = sg_dma_address(sg);
  375. dma_len = sg_dma_len(sg);
  376. }
  377. if (length <= PAGE_SIZE) {
  378. cmd->prp2 = cpu_to_le64(dma_addr);
  379. return total_len;
  380. }
  381. nprps = DIV_ROUND_UP(length, PAGE_SIZE);
  382. if (nprps <= (256 / 8)) {
  383. pool = dev->prp_small_pool;
  384. iod->npages = 0;
  385. } else {
  386. pool = dev->prp_page_pool;
  387. iod->npages = 1;
  388. }
  389. prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
  390. if (!prp_list) {
  391. cmd->prp2 = cpu_to_le64(dma_addr);
  392. iod->npages = -1;
  393. return (total_len - length) + PAGE_SIZE;
  394. }
  395. list[0] = prp_list;
  396. iod->first_dma = prp_dma;
  397. cmd->prp2 = cpu_to_le64(prp_dma);
  398. i = 0;
  399. for (;;) {
  400. if (i == PAGE_SIZE / 8) {
  401. __le64 *old_prp_list = prp_list;
  402. prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
  403. if (!prp_list)
  404. return total_len - length;
  405. list[iod->npages++] = prp_list;
  406. prp_list[0] = old_prp_list[i - 1];
  407. old_prp_list[i - 1] = cpu_to_le64(prp_dma);
  408. i = 1;
  409. }
  410. prp_list[i++] = cpu_to_le64(dma_addr);
  411. dma_len -= PAGE_SIZE;
  412. dma_addr += PAGE_SIZE;
  413. length -= PAGE_SIZE;
  414. if (length <= 0)
  415. break;
  416. if (dma_len > 0)
  417. continue;
  418. BUG_ON(dma_len < 0);
  419. sg = sg_next(sg);
  420. dma_addr = sg_dma_address(sg);
  421. dma_len = sg_dma_len(sg);
  422. }
  423. return total_len;
  424. }
  425. /* NVMe scatterlists require no holes in the virtual address */
  426. #define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \
  427. (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE))
  428. static int nvme_map_bio(struct device *dev, struct nvme_iod *iod,
  429. struct bio *bio, enum dma_data_direction dma_dir, int psegs)
  430. {
  431. struct bio_vec *bvec, *bvprv = NULL;
  432. struct scatterlist *sg = NULL;
  433. int i, old_idx, length = 0, nsegs = 0;
  434. sg_init_table(iod->sg, psegs);
  435. old_idx = bio->bi_idx;
  436. bio_for_each_segment(bvec, bio, i) {
  437. if (bvprv && BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) {
  438. sg->length += bvec->bv_len;
  439. } else {
  440. if (bvprv && BIOVEC_NOT_VIRT_MERGEABLE(bvprv, bvec))
  441. break;
  442. sg = sg ? sg + 1 : iod->sg;
  443. sg_set_page(sg, bvec->bv_page, bvec->bv_len,
  444. bvec->bv_offset);
  445. nsegs++;
  446. }
  447. length += bvec->bv_len;
  448. bvprv = bvec;
  449. }
  450. bio->bi_idx = i;
  451. iod->nents = nsegs;
  452. sg_mark_end(sg);
  453. if (dma_map_sg(dev, iod->sg, iod->nents, dma_dir) == 0) {
  454. bio->bi_idx = old_idx;
  455. return -ENOMEM;
  456. }
  457. return length;
  458. }
  459. static int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
  460. int cmdid)
  461. {
  462. struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
  463. memset(cmnd, 0, sizeof(*cmnd));
  464. cmnd->common.opcode = nvme_cmd_flush;
  465. cmnd->common.command_id = cmdid;
  466. cmnd->common.nsid = cpu_to_le32(ns->ns_id);
  467. if (++nvmeq->sq_tail == nvmeq->q_depth)
  468. nvmeq->sq_tail = 0;
  469. writel(nvmeq->sq_tail, nvmeq->q_db);
  470. return 0;
  471. }
  472. static int nvme_submit_flush_data(struct nvme_queue *nvmeq, struct nvme_ns *ns)
  473. {
  474. int cmdid = alloc_cmdid(nvmeq, (void *)CMD_CTX_FLUSH,
  475. special_completion, NVME_IO_TIMEOUT);
  476. if (unlikely(cmdid < 0))
  477. return cmdid;
  478. return nvme_submit_flush(nvmeq, ns, cmdid);
  479. }
  480. /*
  481. * Called with local interrupts disabled and the q_lock held. May not sleep.
  482. */
  483. static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
  484. struct bio *bio)
  485. {
  486. struct nvme_command *cmnd;
  487. struct nvme_iod *iod;
  488. enum dma_data_direction dma_dir;
  489. int cmdid, length, result = -ENOMEM;
  490. u16 control;
  491. u32 dsmgmt;
  492. int psegs = bio_phys_segments(ns->queue, bio);
  493. if ((bio->bi_rw & REQ_FLUSH) && psegs) {
  494. result = nvme_submit_flush_data(nvmeq, ns);
  495. if (result)
  496. return result;
  497. }
  498. iod = nvme_alloc_iod(psegs, bio->bi_size, GFP_ATOMIC);
  499. if (!iod)
  500. goto nomem;
  501. iod->private = bio;
  502. result = -EBUSY;
  503. cmdid = alloc_cmdid(nvmeq, iod, bio_completion, NVME_IO_TIMEOUT);
  504. if (unlikely(cmdid < 0))
  505. goto free_iod;
  506. if ((bio->bi_rw & REQ_FLUSH) && !psegs)
  507. return nvme_submit_flush(nvmeq, ns, cmdid);
  508. control = 0;
  509. if (bio->bi_rw & REQ_FUA)
  510. control |= NVME_RW_FUA;
  511. if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
  512. control |= NVME_RW_LR;
  513. dsmgmt = 0;
  514. if (bio->bi_rw & REQ_RAHEAD)
  515. dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
  516. cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
  517. memset(cmnd, 0, sizeof(*cmnd));
  518. if (bio_data_dir(bio)) {
  519. cmnd->rw.opcode = nvme_cmd_write;
  520. dma_dir = DMA_TO_DEVICE;
  521. } else {
  522. cmnd->rw.opcode = nvme_cmd_read;
  523. dma_dir = DMA_FROM_DEVICE;
  524. }
  525. result = nvme_map_bio(nvmeq->q_dmadev, iod, bio, dma_dir, psegs);
  526. if (result < 0)
  527. goto free_cmdid;
  528. length = result;
  529. cmnd->rw.command_id = cmdid;
  530. cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
  531. length = nvme_setup_prps(nvmeq->dev, &cmnd->common, iod, length,
  532. GFP_ATOMIC);
  533. cmnd->rw.slba = cpu_to_le64(bio->bi_sector >> (ns->lba_shift - 9));
  534. cmnd->rw.length = cpu_to_le16((length >> ns->lba_shift) - 1);
  535. cmnd->rw.control = cpu_to_le16(control);
  536. cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
  537. bio->bi_sector += length >> 9;
  538. if (++nvmeq->sq_tail == nvmeq->q_depth)
  539. nvmeq->sq_tail = 0;
  540. writel(nvmeq->sq_tail, nvmeq->q_db);
  541. return 0;
  542. free_cmdid:
  543. free_cmdid(nvmeq, cmdid, NULL);
  544. free_iod:
  545. nvme_free_iod(nvmeq->dev, iod);
  546. nomem:
  547. return result;
  548. }
  549. static void nvme_make_request(struct request_queue *q, struct bio *bio)
  550. {
  551. struct nvme_ns *ns = q->queuedata;
  552. struct nvme_queue *nvmeq = get_nvmeq(ns->dev);
  553. int result = -EBUSY;
  554. spin_lock_irq(&nvmeq->q_lock);
  555. if (bio_list_empty(&nvmeq->sq_cong))
  556. result = nvme_submit_bio_queue(nvmeq, ns, bio);
  557. if (unlikely(result)) {
  558. if (bio_list_empty(&nvmeq->sq_cong))
  559. add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
  560. bio_list_add(&nvmeq->sq_cong, bio);
  561. }
  562. spin_unlock_irq(&nvmeq->q_lock);
  563. put_nvmeq(nvmeq);
  564. }
  565. static irqreturn_t nvme_process_cq(struct nvme_queue *nvmeq)
  566. {
  567. u16 head, phase;
  568. head = nvmeq->cq_head;
  569. phase = nvmeq->cq_phase;
  570. for (;;) {
  571. void *ctx;
  572. nvme_completion_fn fn;
  573. struct nvme_completion cqe = nvmeq->cqes[head];
  574. if ((le16_to_cpu(cqe.status) & 1) != phase)
  575. break;
  576. nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
  577. if (++head == nvmeq->q_depth) {
  578. head = 0;
  579. phase = !phase;
  580. }
  581. ctx = free_cmdid(nvmeq, cqe.command_id, &fn);
  582. fn(nvmeq->dev, ctx, &cqe);
  583. }
  584. /* If the controller ignores the cq head doorbell and continuously
  585. * writes to the queue, it is theoretically possible to wrap around
  586. * the queue twice and mistakenly return IRQ_NONE. Linux only
  587. * requires that 0.1% of your interrupts are handled, so this isn't
  588. * a big problem.
  589. */
  590. if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
  591. return IRQ_NONE;
  592. writel(head, nvmeq->q_db + (1 << nvmeq->dev->db_stride));
  593. nvmeq->cq_head = head;
  594. nvmeq->cq_phase = phase;
  595. return IRQ_HANDLED;
  596. }
  597. static irqreturn_t nvme_irq(int irq, void *data)
  598. {
  599. irqreturn_t result;
  600. struct nvme_queue *nvmeq = data;
  601. spin_lock(&nvmeq->q_lock);
  602. result = nvme_process_cq(nvmeq);
  603. spin_unlock(&nvmeq->q_lock);
  604. return result;
  605. }
  606. static irqreturn_t nvme_irq_check(int irq, void *data)
  607. {
  608. struct nvme_queue *nvmeq = data;
  609. struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
  610. if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
  611. return IRQ_NONE;
  612. return IRQ_WAKE_THREAD;
  613. }
  614. static void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
  615. {
  616. spin_lock_irq(&nvmeq->q_lock);
  617. cancel_cmdid(nvmeq, cmdid, NULL);
  618. spin_unlock_irq(&nvmeq->q_lock);
  619. }
  620. struct sync_cmd_info {
  621. struct task_struct *task;
  622. u32 result;
  623. int status;
  624. };
  625. static void sync_completion(struct nvme_dev *dev, void *ctx,
  626. struct nvme_completion *cqe)
  627. {
  628. struct sync_cmd_info *cmdinfo = ctx;
  629. cmdinfo->result = le32_to_cpup(&cqe->result);
  630. cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
  631. wake_up_process(cmdinfo->task);
  632. }
  633. /*
  634. * Returns 0 on success. If the result is negative, it's a Linux error code;
  635. * if the result is positive, it's an NVM Express status code
  636. */
  637. static int nvme_submit_sync_cmd(struct nvme_queue *nvmeq,
  638. struct nvme_command *cmd, u32 *result, unsigned timeout)
  639. {
  640. int cmdid;
  641. struct sync_cmd_info cmdinfo;
  642. cmdinfo.task = current;
  643. cmdinfo.status = -EINTR;
  644. cmdid = alloc_cmdid_killable(nvmeq, &cmdinfo, sync_completion,
  645. timeout);
  646. if (cmdid < 0)
  647. return cmdid;
  648. cmd->common.command_id = cmdid;
  649. set_current_state(TASK_KILLABLE);
  650. nvme_submit_cmd(nvmeq, cmd);
  651. schedule();
  652. if (cmdinfo.status == -EINTR) {
  653. nvme_abort_command(nvmeq, cmdid);
  654. return -EINTR;
  655. }
  656. if (result)
  657. *result = cmdinfo.result;
  658. return cmdinfo.status;
  659. }
  660. static int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
  661. u32 *result)
  662. {
  663. return nvme_submit_sync_cmd(dev->queues[0], cmd, result, ADMIN_TIMEOUT);
  664. }
  665. static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
  666. {
  667. int status;
  668. struct nvme_command c;
  669. memset(&c, 0, sizeof(c));
  670. c.delete_queue.opcode = opcode;
  671. c.delete_queue.qid = cpu_to_le16(id);
  672. status = nvme_submit_admin_cmd(dev, &c, NULL);
  673. if (status)
  674. return -EIO;
  675. return 0;
  676. }
  677. static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
  678. struct nvme_queue *nvmeq)
  679. {
  680. int status;
  681. struct nvme_command c;
  682. int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
  683. memset(&c, 0, sizeof(c));
  684. c.create_cq.opcode = nvme_admin_create_cq;
  685. c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
  686. c.create_cq.cqid = cpu_to_le16(qid);
  687. c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
  688. c.create_cq.cq_flags = cpu_to_le16(flags);
  689. c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
  690. status = nvme_submit_admin_cmd(dev, &c, NULL);
  691. if (status)
  692. return -EIO;
  693. return 0;
  694. }
  695. static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
  696. struct nvme_queue *nvmeq)
  697. {
  698. int status;
  699. struct nvme_command c;
  700. int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
  701. memset(&c, 0, sizeof(c));
  702. c.create_sq.opcode = nvme_admin_create_sq;
  703. c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
  704. c.create_sq.sqid = cpu_to_le16(qid);
  705. c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
  706. c.create_sq.sq_flags = cpu_to_le16(flags);
  707. c.create_sq.cqid = cpu_to_le16(qid);
  708. status = nvme_submit_admin_cmd(dev, &c, NULL);
  709. if (status)
  710. return -EIO;
  711. return 0;
  712. }
  713. static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
  714. {
  715. return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
  716. }
  717. static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
  718. {
  719. return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
  720. }
  721. static int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
  722. dma_addr_t dma_addr)
  723. {
  724. struct nvme_command c;
  725. memset(&c, 0, sizeof(c));
  726. c.identify.opcode = nvme_admin_identify;
  727. c.identify.nsid = cpu_to_le32(nsid);
  728. c.identify.prp1 = cpu_to_le64(dma_addr);
  729. c.identify.cns = cpu_to_le32(cns);
  730. return nvme_submit_admin_cmd(dev, &c, NULL);
  731. }
  732. static int nvme_get_features(struct nvme_dev *dev, unsigned fid,
  733. unsigned nsid, dma_addr_t dma_addr)
  734. {
  735. struct nvme_command c;
  736. memset(&c, 0, sizeof(c));
  737. c.features.opcode = nvme_admin_get_features;
  738. c.features.nsid = cpu_to_le32(nsid);
  739. c.features.prp1 = cpu_to_le64(dma_addr);
  740. c.features.fid = cpu_to_le32(fid);
  741. return nvme_submit_admin_cmd(dev, &c, NULL);
  742. }
  743. static int nvme_set_features(struct nvme_dev *dev, unsigned fid,
  744. unsigned dword11, dma_addr_t dma_addr, u32 *result)
  745. {
  746. struct nvme_command c;
  747. memset(&c, 0, sizeof(c));
  748. c.features.opcode = nvme_admin_set_features;
  749. c.features.prp1 = cpu_to_le64(dma_addr);
  750. c.features.fid = cpu_to_le32(fid);
  751. c.features.dword11 = cpu_to_le32(dword11);
  752. return nvme_submit_admin_cmd(dev, &c, result);
  753. }
  754. /**
  755. * nvme_cancel_ios - Cancel outstanding I/Os
  756. * @queue: The queue to cancel I/Os on
  757. * @timeout: True to only cancel I/Os which have timed out
  758. */
  759. static void nvme_cancel_ios(struct nvme_queue *nvmeq, bool timeout)
  760. {
  761. int depth = nvmeq->q_depth - 1;
  762. struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
  763. unsigned long now = jiffies;
  764. int cmdid;
  765. for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) {
  766. void *ctx;
  767. nvme_completion_fn fn;
  768. static struct nvme_completion cqe = {
  769. .status = cpu_to_le16(NVME_SC_ABORT_REQ) << 1,
  770. };
  771. if (timeout && !time_after(now, info[cmdid].timeout))
  772. continue;
  773. dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d\n", cmdid);
  774. ctx = cancel_cmdid(nvmeq, cmdid, &fn);
  775. fn(nvmeq->dev, ctx, &cqe);
  776. }
  777. }
  778. static void nvme_free_queue_mem(struct nvme_queue *nvmeq)
  779. {
  780. dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
  781. (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
  782. dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
  783. nvmeq->sq_cmds, nvmeq->sq_dma_addr);
  784. kfree(nvmeq);
  785. }
  786. static void nvme_free_queue(struct nvme_dev *dev, int qid)
  787. {
  788. struct nvme_queue *nvmeq = dev->queues[qid];
  789. int vector = dev->entry[nvmeq->cq_vector].vector;
  790. spin_lock_irq(&nvmeq->q_lock);
  791. nvme_cancel_ios(nvmeq, false);
  792. spin_unlock_irq(&nvmeq->q_lock);
  793. irq_set_affinity_hint(vector, NULL);
  794. free_irq(vector, nvmeq);
  795. /* Don't tell the adapter to delete the admin queue */
  796. if (qid) {
  797. adapter_delete_sq(dev, qid);
  798. adapter_delete_cq(dev, qid);
  799. }
  800. nvme_free_queue_mem(nvmeq);
  801. }
  802. static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
  803. int depth, int vector)
  804. {
  805. struct device *dmadev = &dev->pci_dev->dev;
  806. unsigned extra = DIV_ROUND_UP(depth, 8) + (depth *
  807. sizeof(struct nvme_cmd_info));
  808. struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
  809. if (!nvmeq)
  810. return NULL;
  811. nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
  812. &nvmeq->cq_dma_addr, GFP_KERNEL);
  813. if (!nvmeq->cqes)
  814. goto free_nvmeq;
  815. memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
  816. nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
  817. &nvmeq->sq_dma_addr, GFP_KERNEL);
  818. if (!nvmeq->sq_cmds)
  819. goto free_cqdma;
  820. nvmeq->q_dmadev = dmadev;
  821. nvmeq->dev = dev;
  822. spin_lock_init(&nvmeq->q_lock);
  823. nvmeq->cq_head = 0;
  824. nvmeq->cq_phase = 1;
  825. init_waitqueue_head(&nvmeq->sq_full);
  826. init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
  827. bio_list_init(&nvmeq->sq_cong);
  828. nvmeq->q_db = &dev->dbs[qid << (dev->db_stride + 1)];
  829. nvmeq->q_depth = depth;
  830. nvmeq->cq_vector = vector;
  831. return nvmeq;
  832. free_cqdma:
  833. dma_free_coherent(dmadev, CQ_SIZE(nvmeq->q_depth), (void *)nvmeq->cqes,
  834. nvmeq->cq_dma_addr);
  835. free_nvmeq:
  836. kfree(nvmeq);
  837. return NULL;
  838. }
  839. static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
  840. const char *name)
  841. {
  842. if (use_threaded_interrupts)
  843. return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
  844. nvme_irq_check, nvme_irq,
  845. IRQF_DISABLED | IRQF_SHARED,
  846. name, nvmeq);
  847. return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
  848. IRQF_DISABLED | IRQF_SHARED, name, nvmeq);
  849. }
  850. static __devinit struct nvme_queue *nvme_create_queue(struct nvme_dev *dev,
  851. int qid, int cq_size, int vector)
  852. {
  853. int result;
  854. struct nvme_queue *nvmeq = nvme_alloc_queue(dev, qid, cq_size, vector);
  855. if (!nvmeq)
  856. return ERR_PTR(-ENOMEM);
  857. result = adapter_alloc_cq(dev, qid, nvmeq);
  858. if (result < 0)
  859. goto free_nvmeq;
  860. result = adapter_alloc_sq(dev, qid, nvmeq);
  861. if (result < 0)
  862. goto release_cq;
  863. result = queue_request_irq(dev, nvmeq, "nvme");
  864. if (result < 0)
  865. goto release_sq;
  866. return nvmeq;
  867. release_sq:
  868. adapter_delete_sq(dev, qid);
  869. release_cq:
  870. adapter_delete_cq(dev, qid);
  871. free_nvmeq:
  872. dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
  873. (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
  874. dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
  875. nvmeq->sq_cmds, nvmeq->sq_dma_addr);
  876. kfree(nvmeq);
  877. return ERR_PTR(result);
  878. }
  879. static int __devinit nvme_configure_admin_queue(struct nvme_dev *dev)
  880. {
  881. int result = 0;
  882. u32 aqa;
  883. u64 cap;
  884. unsigned long timeout;
  885. struct nvme_queue *nvmeq;
  886. dev->dbs = ((void __iomem *)dev->bar) + 4096;
  887. nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
  888. if (!nvmeq)
  889. return -ENOMEM;
  890. aqa = nvmeq->q_depth - 1;
  891. aqa |= aqa << 16;
  892. dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
  893. dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
  894. dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
  895. dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
  896. writel(0, &dev->bar->cc);
  897. writel(aqa, &dev->bar->aqa);
  898. writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
  899. writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
  900. writel(dev->ctrl_config, &dev->bar->cc);
  901. cap = readq(&dev->bar->cap);
  902. timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
  903. dev->db_stride = NVME_CAP_STRIDE(cap);
  904. while (!result && !(readl(&dev->bar->csts) & NVME_CSTS_RDY)) {
  905. msleep(100);
  906. if (fatal_signal_pending(current))
  907. result = -EINTR;
  908. if (time_after(jiffies, timeout)) {
  909. dev_err(&dev->pci_dev->dev,
  910. "Device not ready; aborting initialisation\n");
  911. result = -ENODEV;
  912. }
  913. }
  914. if (result) {
  915. nvme_free_queue_mem(nvmeq);
  916. return result;
  917. }
  918. result = queue_request_irq(dev, nvmeq, "nvme admin");
  919. dev->queues[0] = nvmeq;
  920. return result;
  921. }
  922. static struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
  923. unsigned long addr, unsigned length)
  924. {
  925. int i, err, count, nents, offset;
  926. struct scatterlist *sg;
  927. struct page **pages;
  928. struct nvme_iod *iod;
  929. if (addr & 3)
  930. return ERR_PTR(-EINVAL);
  931. if (!length)
  932. return ERR_PTR(-EINVAL);
  933. offset = offset_in_page(addr);
  934. count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
  935. pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
  936. if (!pages)
  937. return ERR_PTR(-ENOMEM);
  938. err = get_user_pages_fast(addr, count, 1, pages);
  939. if (err < count) {
  940. count = err;
  941. err = -EFAULT;
  942. goto put_pages;
  943. }
  944. iod = nvme_alloc_iod(count, length, GFP_KERNEL);
  945. sg = iod->sg;
  946. sg_init_table(sg, count);
  947. for (i = 0; i < count; i++) {
  948. sg_set_page(&sg[i], pages[i],
  949. min_t(int, length, PAGE_SIZE - offset), offset);
  950. length -= (PAGE_SIZE - offset);
  951. offset = 0;
  952. }
  953. sg_mark_end(&sg[i - 1]);
  954. iod->nents = count;
  955. err = -ENOMEM;
  956. nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
  957. write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  958. if (!nents)
  959. goto free_iod;
  960. kfree(pages);
  961. return iod;
  962. free_iod:
  963. kfree(iod);
  964. put_pages:
  965. for (i = 0; i < count; i++)
  966. put_page(pages[i]);
  967. kfree(pages);
  968. return ERR_PTR(err);
  969. }
  970. static void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
  971. struct nvme_iod *iod)
  972. {
  973. int i;
  974. dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
  975. write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  976. for (i = 0; i < iod->nents; i++)
  977. put_page(sg_page(&iod->sg[i]));
  978. }
  979. static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
  980. {
  981. struct nvme_dev *dev = ns->dev;
  982. struct nvme_queue *nvmeq;
  983. struct nvme_user_io io;
  984. struct nvme_command c;
  985. unsigned length;
  986. int status;
  987. struct nvme_iod *iod;
  988. if (copy_from_user(&io, uio, sizeof(io)))
  989. return -EFAULT;
  990. length = (io.nblocks + 1) << ns->lba_shift;
  991. switch (io.opcode) {
  992. case nvme_cmd_write:
  993. case nvme_cmd_read:
  994. case nvme_cmd_compare:
  995. iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
  996. break;
  997. default:
  998. return -EINVAL;
  999. }
  1000. if (IS_ERR(iod))
  1001. return PTR_ERR(iod);
  1002. memset(&c, 0, sizeof(c));
  1003. c.rw.opcode = io.opcode;
  1004. c.rw.flags = io.flags;
  1005. c.rw.nsid = cpu_to_le32(ns->ns_id);
  1006. c.rw.slba = cpu_to_le64(io.slba);
  1007. c.rw.length = cpu_to_le16(io.nblocks);
  1008. c.rw.control = cpu_to_le16(io.control);
  1009. c.rw.dsmgmt = cpu_to_le16(io.dsmgmt);
  1010. c.rw.reftag = io.reftag;
  1011. c.rw.apptag = io.apptag;
  1012. c.rw.appmask = io.appmask;
  1013. /* XXX: metadata */
  1014. length = nvme_setup_prps(dev, &c.common, iod, length, GFP_KERNEL);
  1015. nvmeq = get_nvmeq(dev);
  1016. /*
  1017. * Since nvme_submit_sync_cmd sleeps, we can't keep preemption
  1018. * disabled. We may be preempted at any point, and be rescheduled
  1019. * to a different CPU. That will cause cacheline bouncing, but no
  1020. * additional races since q_lock already protects against other CPUs.
  1021. */
  1022. put_nvmeq(nvmeq);
  1023. if (length != (io.nblocks + 1) << ns->lba_shift)
  1024. status = -ENOMEM;
  1025. else
  1026. status = nvme_submit_sync_cmd(nvmeq, &c, NULL, NVME_IO_TIMEOUT);
  1027. nvme_unmap_user_pages(dev, io.opcode & 1, iod);
  1028. nvme_free_iod(dev, iod);
  1029. return status;
  1030. }
  1031. static int nvme_user_admin_cmd(struct nvme_dev *dev,
  1032. struct nvme_admin_cmd __user *ucmd)
  1033. {
  1034. struct nvme_admin_cmd cmd;
  1035. struct nvme_command c;
  1036. int status, length;
  1037. struct nvme_iod *uninitialized_var(iod);
  1038. if (!capable(CAP_SYS_ADMIN))
  1039. return -EACCES;
  1040. if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
  1041. return -EFAULT;
  1042. memset(&c, 0, sizeof(c));
  1043. c.common.opcode = cmd.opcode;
  1044. c.common.flags = cmd.flags;
  1045. c.common.nsid = cpu_to_le32(cmd.nsid);
  1046. c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
  1047. c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
  1048. c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
  1049. c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
  1050. c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
  1051. c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
  1052. c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
  1053. c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
  1054. length = cmd.data_len;
  1055. if (cmd.data_len) {
  1056. iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
  1057. length);
  1058. if (IS_ERR(iod))
  1059. return PTR_ERR(iod);
  1060. length = nvme_setup_prps(dev, &c.common, iod, length,
  1061. GFP_KERNEL);
  1062. }
  1063. if (length != cmd.data_len)
  1064. status = -ENOMEM;
  1065. else
  1066. status = nvme_submit_admin_cmd(dev, &c, NULL);
  1067. if (cmd.data_len) {
  1068. nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
  1069. nvme_free_iod(dev, iod);
  1070. }
  1071. return status;
  1072. }
  1073. static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
  1074. unsigned long arg)
  1075. {
  1076. struct nvme_ns *ns = bdev->bd_disk->private_data;
  1077. switch (cmd) {
  1078. case NVME_IOCTL_ID:
  1079. return ns->ns_id;
  1080. case NVME_IOCTL_ADMIN_CMD:
  1081. return nvme_user_admin_cmd(ns->dev, (void __user *)arg);
  1082. case NVME_IOCTL_SUBMIT_IO:
  1083. return nvme_submit_io(ns, (void __user *)arg);
  1084. default:
  1085. return -ENOTTY;
  1086. }
  1087. }
  1088. static const struct block_device_operations nvme_fops = {
  1089. .owner = THIS_MODULE,
  1090. .ioctl = nvme_ioctl,
  1091. .compat_ioctl = nvme_ioctl,
  1092. };
  1093. static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
  1094. {
  1095. while (bio_list_peek(&nvmeq->sq_cong)) {
  1096. struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
  1097. struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
  1098. if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
  1099. bio_list_add_head(&nvmeq->sq_cong, bio);
  1100. break;
  1101. }
  1102. if (bio_list_empty(&nvmeq->sq_cong))
  1103. remove_wait_queue(&nvmeq->sq_full,
  1104. &nvmeq->sq_cong_wait);
  1105. }
  1106. }
  1107. static int nvme_kthread(void *data)
  1108. {
  1109. struct nvme_dev *dev;
  1110. while (!kthread_should_stop()) {
  1111. __set_current_state(TASK_RUNNING);
  1112. spin_lock(&dev_list_lock);
  1113. list_for_each_entry(dev, &dev_list, node) {
  1114. int i;
  1115. for (i = 0; i < dev->queue_count; i++) {
  1116. struct nvme_queue *nvmeq = dev->queues[i];
  1117. if (!nvmeq)
  1118. continue;
  1119. spin_lock_irq(&nvmeq->q_lock);
  1120. if (nvme_process_cq(nvmeq))
  1121. printk("process_cq did something\n");
  1122. nvme_cancel_ios(nvmeq, true);
  1123. nvme_resubmit_bios(nvmeq);
  1124. spin_unlock_irq(&nvmeq->q_lock);
  1125. }
  1126. }
  1127. spin_unlock(&dev_list_lock);
  1128. set_current_state(TASK_INTERRUPTIBLE);
  1129. schedule_timeout(HZ);
  1130. }
  1131. return 0;
  1132. }
  1133. static DEFINE_IDA(nvme_index_ida);
  1134. static int nvme_get_ns_idx(void)
  1135. {
  1136. int index, error;
  1137. do {
  1138. if (!ida_pre_get(&nvme_index_ida, GFP_KERNEL))
  1139. return -1;
  1140. spin_lock(&dev_list_lock);
  1141. error = ida_get_new(&nvme_index_ida, &index);
  1142. spin_unlock(&dev_list_lock);
  1143. } while (error == -EAGAIN);
  1144. if (error)
  1145. index = -1;
  1146. return index;
  1147. }
  1148. static void nvme_put_ns_idx(int index)
  1149. {
  1150. spin_lock(&dev_list_lock);
  1151. ida_remove(&nvme_index_ida, index);
  1152. spin_unlock(&dev_list_lock);
  1153. }
  1154. static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, int nsid,
  1155. struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
  1156. {
  1157. struct nvme_ns *ns;
  1158. struct gendisk *disk;
  1159. int lbaf;
  1160. if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
  1161. return NULL;
  1162. ns = kzalloc(sizeof(*ns), GFP_KERNEL);
  1163. if (!ns)
  1164. return NULL;
  1165. ns->queue = blk_alloc_queue(GFP_KERNEL);
  1166. if (!ns->queue)
  1167. goto out_free_ns;
  1168. ns->queue->queue_flags = QUEUE_FLAG_DEFAULT;
  1169. queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
  1170. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
  1171. /* queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue); */
  1172. blk_queue_make_request(ns->queue, nvme_make_request);
  1173. ns->dev = dev;
  1174. ns->queue->queuedata = ns;
  1175. disk = alloc_disk(NVME_MINORS);
  1176. if (!disk)
  1177. goto out_free_queue;
  1178. ns->ns_id = nsid;
  1179. ns->disk = disk;
  1180. lbaf = id->flbas & 0xf;
  1181. ns->lba_shift = id->lbaf[lbaf].ds;
  1182. blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
  1183. if (dev->max_hw_sectors)
  1184. blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
  1185. disk->major = nvme_major;
  1186. disk->minors = NVME_MINORS;
  1187. disk->first_minor = NVME_MINORS * nvme_get_ns_idx();
  1188. disk->fops = &nvme_fops;
  1189. disk->private_data = ns;
  1190. disk->queue = ns->queue;
  1191. disk->driverfs_dev = &dev->pci_dev->dev;
  1192. sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
  1193. set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
  1194. return ns;
  1195. out_free_queue:
  1196. blk_cleanup_queue(ns->queue);
  1197. out_free_ns:
  1198. kfree(ns);
  1199. return NULL;
  1200. }
  1201. static void nvme_ns_free(struct nvme_ns *ns)
  1202. {
  1203. int index = ns->disk->first_minor / NVME_MINORS;
  1204. put_disk(ns->disk);
  1205. nvme_put_ns_idx(index);
  1206. blk_cleanup_queue(ns->queue);
  1207. kfree(ns);
  1208. }
  1209. static int set_queue_count(struct nvme_dev *dev, int count)
  1210. {
  1211. int status;
  1212. u32 result;
  1213. u32 q_count = (count - 1) | ((count - 1) << 16);
  1214. status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
  1215. &result);
  1216. if (status)
  1217. return -EIO;
  1218. return min(result & 0xffff, result >> 16) + 1;
  1219. }
  1220. static int __devinit nvme_setup_io_queues(struct nvme_dev *dev)
  1221. {
  1222. int result, cpu, i, nr_io_queues, db_bar_size, q_depth;
  1223. nr_io_queues = num_online_cpus();
  1224. result = set_queue_count(dev, nr_io_queues);
  1225. if (result < 0)
  1226. return result;
  1227. if (result < nr_io_queues)
  1228. nr_io_queues = result;
  1229. /* Deregister the admin queue's interrupt */
  1230. free_irq(dev->entry[0].vector, dev->queues[0]);
  1231. db_bar_size = 4096 + ((nr_io_queues + 1) << (dev->db_stride + 3));
  1232. if (db_bar_size > 8192) {
  1233. iounmap(dev->bar);
  1234. dev->bar = ioremap(pci_resource_start(dev->pci_dev, 0),
  1235. db_bar_size);
  1236. dev->dbs = ((void __iomem *)dev->bar) + 4096;
  1237. dev->queues[0]->q_db = dev->dbs;
  1238. }
  1239. for (i = 0; i < nr_io_queues; i++)
  1240. dev->entry[i].entry = i;
  1241. for (;;) {
  1242. result = pci_enable_msix(dev->pci_dev, dev->entry,
  1243. nr_io_queues);
  1244. if (result == 0) {
  1245. break;
  1246. } else if (result > 0) {
  1247. nr_io_queues = result;
  1248. continue;
  1249. } else {
  1250. nr_io_queues = 1;
  1251. break;
  1252. }
  1253. }
  1254. result = queue_request_irq(dev, dev->queues[0], "nvme admin");
  1255. /* XXX: handle failure here */
  1256. cpu = cpumask_first(cpu_online_mask);
  1257. for (i = 0; i < nr_io_queues; i++) {
  1258. irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu));
  1259. cpu = cpumask_next(cpu, cpu_online_mask);
  1260. }
  1261. q_depth = min_t(int, NVME_CAP_MQES(readq(&dev->bar->cap)) + 1,
  1262. NVME_Q_DEPTH);
  1263. for (i = 0; i < nr_io_queues; i++) {
  1264. dev->queues[i + 1] = nvme_create_queue(dev, i + 1, q_depth, i);
  1265. if (IS_ERR(dev->queues[i + 1]))
  1266. return PTR_ERR(dev->queues[i + 1]);
  1267. dev->queue_count++;
  1268. }
  1269. for (; i < num_possible_cpus(); i++) {
  1270. int target = i % rounddown_pow_of_two(dev->queue_count - 1);
  1271. dev->queues[i + 1] = dev->queues[target + 1];
  1272. }
  1273. return 0;
  1274. }
  1275. static void nvme_free_queues(struct nvme_dev *dev)
  1276. {
  1277. int i;
  1278. for (i = dev->queue_count - 1; i >= 0; i--)
  1279. nvme_free_queue(dev, i);
  1280. }
  1281. static int __devinit nvme_dev_add(struct nvme_dev *dev)
  1282. {
  1283. int res, nn, i;
  1284. struct nvme_ns *ns, *next;
  1285. struct nvme_id_ctrl *ctrl;
  1286. struct nvme_id_ns *id_ns;
  1287. void *mem;
  1288. dma_addr_t dma_addr;
  1289. res = nvme_setup_io_queues(dev);
  1290. if (res)
  1291. return res;
  1292. mem = dma_alloc_coherent(&dev->pci_dev->dev, 8192, &dma_addr,
  1293. GFP_KERNEL);
  1294. res = nvme_identify(dev, 0, 1, dma_addr);
  1295. if (res) {
  1296. res = -EIO;
  1297. goto out_free;
  1298. }
  1299. ctrl = mem;
  1300. nn = le32_to_cpup(&ctrl->nn);
  1301. memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
  1302. memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
  1303. memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
  1304. if (ctrl->mdts) {
  1305. int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
  1306. dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
  1307. }
  1308. id_ns = mem;
  1309. for (i = 1; i <= nn; i++) {
  1310. res = nvme_identify(dev, i, 0, dma_addr);
  1311. if (res)
  1312. continue;
  1313. if (id_ns->ncap == 0)
  1314. continue;
  1315. res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
  1316. dma_addr + 4096);
  1317. if (res)
  1318. continue;
  1319. ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
  1320. if (ns)
  1321. list_add_tail(&ns->list, &dev->namespaces);
  1322. }
  1323. list_for_each_entry(ns, &dev->namespaces, list)
  1324. add_disk(ns->disk);
  1325. goto out;
  1326. out_free:
  1327. list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
  1328. list_del(&ns->list);
  1329. nvme_ns_free(ns);
  1330. }
  1331. out:
  1332. dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
  1333. return res;
  1334. }
  1335. static int nvme_dev_remove(struct nvme_dev *dev)
  1336. {
  1337. struct nvme_ns *ns, *next;
  1338. spin_lock(&dev_list_lock);
  1339. list_del(&dev->node);
  1340. spin_unlock(&dev_list_lock);
  1341. list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
  1342. list_del(&ns->list);
  1343. del_gendisk(ns->disk);
  1344. nvme_ns_free(ns);
  1345. }
  1346. nvme_free_queues(dev);
  1347. return 0;
  1348. }
  1349. static int nvme_setup_prp_pools(struct nvme_dev *dev)
  1350. {
  1351. struct device *dmadev = &dev->pci_dev->dev;
  1352. dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
  1353. PAGE_SIZE, PAGE_SIZE, 0);
  1354. if (!dev->prp_page_pool)
  1355. return -ENOMEM;
  1356. /* Optimisation for I/Os between 4k and 128k */
  1357. dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
  1358. 256, 256, 0);
  1359. if (!dev->prp_small_pool) {
  1360. dma_pool_destroy(dev->prp_page_pool);
  1361. return -ENOMEM;
  1362. }
  1363. return 0;
  1364. }
  1365. static void nvme_release_prp_pools(struct nvme_dev *dev)
  1366. {
  1367. dma_pool_destroy(dev->prp_page_pool);
  1368. dma_pool_destroy(dev->prp_small_pool);
  1369. }
  1370. static DEFINE_IDA(nvme_instance_ida);
  1371. static int nvme_set_instance(struct nvme_dev *dev)
  1372. {
  1373. int instance, error;
  1374. do {
  1375. if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
  1376. return -ENODEV;
  1377. spin_lock(&dev_list_lock);
  1378. error = ida_get_new(&nvme_instance_ida, &instance);
  1379. spin_unlock(&dev_list_lock);
  1380. } while (error == -EAGAIN);
  1381. if (error)
  1382. return -ENODEV;
  1383. dev->instance = instance;
  1384. return 0;
  1385. }
  1386. static void nvme_release_instance(struct nvme_dev *dev)
  1387. {
  1388. spin_lock(&dev_list_lock);
  1389. ida_remove(&nvme_instance_ida, dev->instance);
  1390. spin_unlock(&dev_list_lock);
  1391. }
  1392. static int __devinit nvme_probe(struct pci_dev *pdev,
  1393. const struct pci_device_id *id)
  1394. {
  1395. int bars, result = -ENOMEM;
  1396. struct nvme_dev *dev;
  1397. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1398. if (!dev)
  1399. return -ENOMEM;
  1400. dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
  1401. GFP_KERNEL);
  1402. if (!dev->entry)
  1403. goto free;
  1404. dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
  1405. GFP_KERNEL);
  1406. if (!dev->queues)
  1407. goto free;
  1408. if (pci_enable_device_mem(pdev))
  1409. goto free;
  1410. pci_set_master(pdev);
  1411. bars = pci_select_bars(pdev, IORESOURCE_MEM);
  1412. if (pci_request_selected_regions(pdev, bars, "nvme"))
  1413. goto disable;
  1414. INIT_LIST_HEAD(&dev->namespaces);
  1415. dev->pci_dev = pdev;
  1416. pci_set_drvdata(pdev, dev);
  1417. dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
  1418. dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
  1419. result = nvme_set_instance(dev);
  1420. if (result)
  1421. goto disable;
  1422. dev->entry[0].vector = pdev->irq;
  1423. result = nvme_setup_prp_pools(dev);
  1424. if (result)
  1425. goto disable_msix;
  1426. dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
  1427. if (!dev->bar) {
  1428. result = -ENOMEM;
  1429. goto disable_msix;
  1430. }
  1431. result = nvme_configure_admin_queue(dev);
  1432. if (result)
  1433. goto unmap;
  1434. dev->queue_count++;
  1435. spin_lock(&dev_list_lock);
  1436. list_add(&dev->node, &dev_list);
  1437. spin_unlock(&dev_list_lock);
  1438. result = nvme_dev_add(dev);
  1439. if (result)
  1440. goto delete;
  1441. return 0;
  1442. delete:
  1443. spin_lock(&dev_list_lock);
  1444. list_del(&dev->node);
  1445. spin_unlock(&dev_list_lock);
  1446. nvme_free_queues(dev);
  1447. unmap:
  1448. iounmap(dev->bar);
  1449. disable_msix:
  1450. pci_disable_msix(pdev);
  1451. nvme_release_instance(dev);
  1452. nvme_release_prp_pools(dev);
  1453. disable:
  1454. pci_disable_device(pdev);
  1455. pci_release_regions(pdev);
  1456. free:
  1457. kfree(dev->queues);
  1458. kfree(dev->entry);
  1459. kfree(dev);
  1460. return result;
  1461. }
  1462. static void __devexit nvme_remove(struct pci_dev *pdev)
  1463. {
  1464. struct nvme_dev *dev = pci_get_drvdata(pdev);
  1465. nvme_dev_remove(dev);
  1466. pci_disable_msix(pdev);
  1467. iounmap(dev->bar);
  1468. nvme_release_instance(dev);
  1469. nvme_release_prp_pools(dev);
  1470. pci_disable_device(pdev);
  1471. pci_release_regions(pdev);
  1472. kfree(dev->queues);
  1473. kfree(dev->entry);
  1474. kfree(dev);
  1475. }
  1476. /* These functions are yet to be implemented */
  1477. #define nvme_error_detected NULL
  1478. #define nvme_dump_registers NULL
  1479. #define nvme_link_reset NULL
  1480. #define nvme_slot_reset NULL
  1481. #define nvme_error_resume NULL
  1482. #define nvme_suspend NULL
  1483. #define nvme_resume NULL
  1484. static const struct pci_error_handlers nvme_err_handler = {
  1485. .error_detected = nvme_error_detected,
  1486. .mmio_enabled = nvme_dump_registers,
  1487. .link_reset = nvme_link_reset,
  1488. .slot_reset = nvme_slot_reset,
  1489. .resume = nvme_error_resume,
  1490. };
  1491. /* Move to pci_ids.h later */
  1492. #define PCI_CLASS_STORAGE_EXPRESS 0x010802
  1493. static DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = {
  1494. { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
  1495. { 0, }
  1496. };
  1497. MODULE_DEVICE_TABLE(pci, nvme_id_table);
  1498. static struct pci_driver nvme_driver = {
  1499. .name = "nvme",
  1500. .id_table = nvme_id_table,
  1501. .probe = nvme_probe,
  1502. .remove = __devexit_p(nvme_remove),
  1503. .suspend = nvme_suspend,
  1504. .resume = nvme_resume,
  1505. .err_handler = &nvme_err_handler,
  1506. };
  1507. static int __init nvme_init(void)
  1508. {
  1509. int result;
  1510. nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
  1511. if (IS_ERR(nvme_thread))
  1512. return PTR_ERR(nvme_thread);
  1513. result = register_blkdev(nvme_major, "nvme");
  1514. if (result < 0)
  1515. goto kill_kthread;
  1516. else if (result > 0)
  1517. nvme_major = result;
  1518. result = pci_register_driver(&nvme_driver);
  1519. if (result)
  1520. goto unregister_blkdev;
  1521. return 0;
  1522. unregister_blkdev:
  1523. unregister_blkdev(nvme_major, "nvme");
  1524. kill_kthread:
  1525. kthread_stop(nvme_thread);
  1526. return result;
  1527. }
  1528. static void __exit nvme_exit(void)
  1529. {
  1530. pci_unregister_driver(&nvme_driver);
  1531. unregister_blkdev(nvme_major, "nvme");
  1532. kthread_stop(nvme_thread);
  1533. }
  1534. MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
  1535. MODULE_LICENSE("GPL");
  1536. MODULE_VERSION("0.8");
  1537. module_init(nvme_init);
  1538. module_exit(nvme_exit);