nvme.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805
  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, unsigned nsid,
  733. dma_addr_t dma_addr, u32 *result)
  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, result);
  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. while (bio_list_peek(&nvmeq->sq_cong)) {
  793. struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
  794. bio_endio(bio, -EIO);
  795. }
  796. spin_unlock_irq(&nvmeq->q_lock);
  797. irq_set_affinity_hint(vector, NULL);
  798. free_irq(vector, nvmeq);
  799. /* Don't tell the adapter to delete the admin queue */
  800. if (qid) {
  801. adapter_delete_sq(dev, qid);
  802. adapter_delete_cq(dev, qid);
  803. }
  804. nvme_free_queue_mem(nvmeq);
  805. }
  806. static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
  807. int depth, int vector)
  808. {
  809. struct device *dmadev = &dev->pci_dev->dev;
  810. unsigned extra = DIV_ROUND_UP(depth, 8) + (depth *
  811. sizeof(struct nvme_cmd_info));
  812. struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
  813. if (!nvmeq)
  814. return NULL;
  815. nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
  816. &nvmeq->cq_dma_addr, GFP_KERNEL);
  817. if (!nvmeq->cqes)
  818. goto free_nvmeq;
  819. memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
  820. nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
  821. &nvmeq->sq_dma_addr, GFP_KERNEL);
  822. if (!nvmeq->sq_cmds)
  823. goto free_cqdma;
  824. nvmeq->q_dmadev = dmadev;
  825. nvmeq->dev = dev;
  826. spin_lock_init(&nvmeq->q_lock);
  827. nvmeq->cq_head = 0;
  828. nvmeq->cq_phase = 1;
  829. init_waitqueue_head(&nvmeq->sq_full);
  830. init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
  831. bio_list_init(&nvmeq->sq_cong);
  832. nvmeq->q_db = &dev->dbs[qid << (dev->db_stride + 1)];
  833. nvmeq->q_depth = depth;
  834. nvmeq->cq_vector = vector;
  835. return nvmeq;
  836. free_cqdma:
  837. dma_free_coherent(dmadev, CQ_SIZE(nvmeq->q_depth), (void *)nvmeq->cqes,
  838. nvmeq->cq_dma_addr);
  839. free_nvmeq:
  840. kfree(nvmeq);
  841. return NULL;
  842. }
  843. static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
  844. const char *name)
  845. {
  846. if (use_threaded_interrupts)
  847. return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
  848. nvme_irq_check, nvme_irq,
  849. IRQF_DISABLED | IRQF_SHARED,
  850. name, nvmeq);
  851. return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
  852. IRQF_DISABLED | IRQF_SHARED, name, nvmeq);
  853. }
  854. static __devinit struct nvme_queue *nvme_create_queue(struct nvme_dev *dev,
  855. int qid, int cq_size, int vector)
  856. {
  857. int result;
  858. struct nvme_queue *nvmeq = nvme_alloc_queue(dev, qid, cq_size, vector);
  859. if (!nvmeq)
  860. return ERR_PTR(-ENOMEM);
  861. result = adapter_alloc_cq(dev, qid, nvmeq);
  862. if (result < 0)
  863. goto free_nvmeq;
  864. result = adapter_alloc_sq(dev, qid, nvmeq);
  865. if (result < 0)
  866. goto release_cq;
  867. result = queue_request_irq(dev, nvmeq, "nvme");
  868. if (result < 0)
  869. goto release_sq;
  870. return nvmeq;
  871. release_sq:
  872. adapter_delete_sq(dev, qid);
  873. release_cq:
  874. adapter_delete_cq(dev, qid);
  875. free_nvmeq:
  876. dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
  877. (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
  878. dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
  879. nvmeq->sq_cmds, nvmeq->sq_dma_addr);
  880. kfree(nvmeq);
  881. return ERR_PTR(result);
  882. }
  883. static int __devinit nvme_configure_admin_queue(struct nvme_dev *dev)
  884. {
  885. int result = 0;
  886. u32 aqa;
  887. u64 cap;
  888. unsigned long timeout;
  889. struct nvme_queue *nvmeq;
  890. dev->dbs = ((void __iomem *)dev->bar) + 4096;
  891. nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
  892. if (!nvmeq)
  893. return -ENOMEM;
  894. aqa = nvmeq->q_depth - 1;
  895. aqa |= aqa << 16;
  896. dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
  897. dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
  898. dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
  899. dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
  900. writel(0, &dev->bar->cc);
  901. writel(aqa, &dev->bar->aqa);
  902. writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
  903. writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
  904. writel(dev->ctrl_config, &dev->bar->cc);
  905. cap = readq(&dev->bar->cap);
  906. timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
  907. dev->db_stride = NVME_CAP_STRIDE(cap);
  908. while (!result && !(readl(&dev->bar->csts) & NVME_CSTS_RDY)) {
  909. msleep(100);
  910. if (fatal_signal_pending(current))
  911. result = -EINTR;
  912. if (time_after(jiffies, timeout)) {
  913. dev_err(&dev->pci_dev->dev,
  914. "Device not ready; aborting initialisation\n");
  915. result = -ENODEV;
  916. }
  917. }
  918. if (result) {
  919. nvme_free_queue_mem(nvmeq);
  920. return result;
  921. }
  922. result = queue_request_irq(dev, nvmeq, "nvme admin");
  923. dev->queues[0] = nvmeq;
  924. return result;
  925. }
  926. static struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
  927. unsigned long addr, unsigned length)
  928. {
  929. int i, err, count, nents, offset;
  930. struct scatterlist *sg;
  931. struct page **pages;
  932. struct nvme_iod *iod;
  933. if (addr & 3)
  934. return ERR_PTR(-EINVAL);
  935. if (!length)
  936. return ERR_PTR(-EINVAL);
  937. offset = offset_in_page(addr);
  938. count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
  939. pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
  940. if (!pages)
  941. return ERR_PTR(-ENOMEM);
  942. err = get_user_pages_fast(addr, count, 1, pages);
  943. if (err < count) {
  944. count = err;
  945. err = -EFAULT;
  946. goto put_pages;
  947. }
  948. iod = nvme_alloc_iod(count, length, GFP_KERNEL);
  949. sg = iod->sg;
  950. sg_init_table(sg, count);
  951. for (i = 0; i < count; i++) {
  952. sg_set_page(&sg[i], pages[i],
  953. min_t(int, length, PAGE_SIZE - offset), offset);
  954. length -= (PAGE_SIZE - offset);
  955. offset = 0;
  956. }
  957. sg_mark_end(&sg[i - 1]);
  958. iod->nents = count;
  959. err = -ENOMEM;
  960. nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
  961. write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  962. if (!nents)
  963. goto free_iod;
  964. kfree(pages);
  965. return iod;
  966. free_iod:
  967. kfree(iod);
  968. put_pages:
  969. for (i = 0; i < count; i++)
  970. put_page(pages[i]);
  971. kfree(pages);
  972. return ERR_PTR(err);
  973. }
  974. static void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
  975. struct nvme_iod *iod)
  976. {
  977. int i;
  978. dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
  979. write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  980. for (i = 0; i < iod->nents; i++)
  981. put_page(sg_page(&iod->sg[i]));
  982. }
  983. static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
  984. {
  985. struct nvme_dev *dev = ns->dev;
  986. struct nvme_queue *nvmeq;
  987. struct nvme_user_io io;
  988. struct nvme_command c;
  989. unsigned length;
  990. int status;
  991. struct nvme_iod *iod;
  992. if (copy_from_user(&io, uio, sizeof(io)))
  993. return -EFAULT;
  994. length = (io.nblocks + 1) << ns->lba_shift;
  995. switch (io.opcode) {
  996. case nvme_cmd_write:
  997. case nvme_cmd_read:
  998. case nvme_cmd_compare:
  999. iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
  1000. break;
  1001. default:
  1002. return -EINVAL;
  1003. }
  1004. if (IS_ERR(iod))
  1005. return PTR_ERR(iod);
  1006. memset(&c, 0, sizeof(c));
  1007. c.rw.opcode = io.opcode;
  1008. c.rw.flags = io.flags;
  1009. c.rw.nsid = cpu_to_le32(ns->ns_id);
  1010. c.rw.slba = cpu_to_le64(io.slba);
  1011. c.rw.length = cpu_to_le16(io.nblocks);
  1012. c.rw.control = cpu_to_le16(io.control);
  1013. c.rw.dsmgmt = cpu_to_le16(io.dsmgmt);
  1014. c.rw.reftag = io.reftag;
  1015. c.rw.apptag = io.apptag;
  1016. c.rw.appmask = io.appmask;
  1017. /* XXX: metadata */
  1018. length = nvme_setup_prps(dev, &c.common, iod, length, GFP_KERNEL);
  1019. nvmeq = get_nvmeq(dev);
  1020. /*
  1021. * Since nvme_submit_sync_cmd sleeps, we can't keep preemption
  1022. * disabled. We may be preempted at any point, and be rescheduled
  1023. * to a different CPU. That will cause cacheline bouncing, but no
  1024. * additional races since q_lock already protects against other CPUs.
  1025. */
  1026. put_nvmeq(nvmeq);
  1027. if (length != (io.nblocks + 1) << ns->lba_shift)
  1028. status = -ENOMEM;
  1029. else
  1030. status = nvme_submit_sync_cmd(nvmeq, &c, NULL, NVME_IO_TIMEOUT);
  1031. nvme_unmap_user_pages(dev, io.opcode & 1, iod);
  1032. nvme_free_iod(dev, iod);
  1033. return status;
  1034. }
  1035. static int nvme_user_admin_cmd(struct nvme_dev *dev,
  1036. struct nvme_admin_cmd __user *ucmd)
  1037. {
  1038. struct nvme_admin_cmd cmd;
  1039. struct nvme_command c;
  1040. int status, length;
  1041. struct nvme_iod *uninitialized_var(iod);
  1042. if (!capable(CAP_SYS_ADMIN))
  1043. return -EACCES;
  1044. if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
  1045. return -EFAULT;
  1046. memset(&c, 0, sizeof(c));
  1047. c.common.opcode = cmd.opcode;
  1048. c.common.flags = cmd.flags;
  1049. c.common.nsid = cpu_to_le32(cmd.nsid);
  1050. c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
  1051. c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
  1052. c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
  1053. c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
  1054. c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
  1055. c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
  1056. c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
  1057. c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
  1058. length = cmd.data_len;
  1059. if (cmd.data_len) {
  1060. iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
  1061. length);
  1062. if (IS_ERR(iod))
  1063. return PTR_ERR(iod);
  1064. length = nvme_setup_prps(dev, &c.common, iod, length,
  1065. GFP_KERNEL);
  1066. }
  1067. if (length != cmd.data_len)
  1068. status = -ENOMEM;
  1069. else
  1070. status = nvme_submit_admin_cmd(dev, &c, &cmd.result);
  1071. if (cmd.data_len) {
  1072. nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
  1073. nvme_free_iod(dev, iod);
  1074. }
  1075. if (!status && copy_to_user(&ucmd->result, &cmd.result,
  1076. sizeof(cmd.result)))
  1077. status = -EFAULT;
  1078. return status;
  1079. }
  1080. static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
  1081. unsigned long arg)
  1082. {
  1083. struct nvme_ns *ns = bdev->bd_disk->private_data;
  1084. switch (cmd) {
  1085. case NVME_IOCTL_ID:
  1086. return ns->ns_id;
  1087. case NVME_IOCTL_ADMIN_CMD:
  1088. return nvme_user_admin_cmd(ns->dev, (void __user *)arg);
  1089. case NVME_IOCTL_SUBMIT_IO:
  1090. return nvme_submit_io(ns, (void __user *)arg);
  1091. default:
  1092. return -ENOTTY;
  1093. }
  1094. }
  1095. static const struct block_device_operations nvme_fops = {
  1096. .owner = THIS_MODULE,
  1097. .ioctl = nvme_ioctl,
  1098. .compat_ioctl = nvme_ioctl,
  1099. };
  1100. static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
  1101. {
  1102. while (bio_list_peek(&nvmeq->sq_cong)) {
  1103. struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
  1104. struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
  1105. if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
  1106. bio_list_add_head(&nvmeq->sq_cong, bio);
  1107. break;
  1108. }
  1109. if (bio_list_empty(&nvmeq->sq_cong))
  1110. remove_wait_queue(&nvmeq->sq_full,
  1111. &nvmeq->sq_cong_wait);
  1112. }
  1113. }
  1114. static int nvme_kthread(void *data)
  1115. {
  1116. struct nvme_dev *dev;
  1117. while (!kthread_should_stop()) {
  1118. __set_current_state(TASK_RUNNING);
  1119. spin_lock(&dev_list_lock);
  1120. list_for_each_entry(dev, &dev_list, node) {
  1121. int i;
  1122. for (i = 0; i < dev->queue_count; i++) {
  1123. struct nvme_queue *nvmeq = dev->queues[i];
  1124. if (!nvmeq)
  1125. continue;
  1126. spin_lock_irq(&nvmeq->q_lock);
  1127. if (nvme_process_cq(nvmeq))
  1128. printk("process_cq did something\n");
  1129. nvme_cancel_ios(nvmeq, true);
  1130. nvme_resubmit_bios(nvmeq);
  1131. spin_unlock_irq(&nvmeq->q_lock);
  1132. }
  1133. }
  1134. spin_unlock(&dev_list_lock);
  1135. set_current_state(TASK_INTERRUPTIBLE);
  1136. schedule_timeout(HZ);
  1137. }
  1138. return 0;
  1139. }
  1140. static DEFINE_IDA(nvme_index_ida);
  1141. static int nvme_get_ns_idx(void)
  1142. {
  1143. int index, error;
  1144. do {
  1145. if (!ida_pre_get(&nvme_index_ida, GFP_KERNEL))
  1146. return -1;
  1147. spin_lock(&dev_list_lock);
  1148. error = ida_get_new(&nvme_index_ida, &index);
  1149. spin_unlock(&dev_list_lock);
  1150. } while (error == -EAGAIN);
  1151. if (error)
  1152. index = -1;
  1153. return index;
  1154. }
  1155. static void nvme_put_ns_idx(int index)
  1156. {
  1157. spin_lock(&dev_list_lock);
  1158. ida_remove(&nvme_index_ida, index);
  1159. spin_unlock(&dev_list_lock);
  1160. }
  1161. static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, int nsid,
  1162. struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
  1163. {
  1164. struct nvme_ns *ns;
  1165. struct gendisk *disk;
  1166. int lbaf;
  1167. if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
  1168. return NULL;
  1169. ns = kzalloc(sizeof(*ns), GFP_KERNEL);
  1170. if (!ns)
  1171. return NULL;
  1172. ns->queue = blk_alloc_queue(GFP_KERNEL);
  1173. if (!ns->queue)
  1174. goto out_free_ns;
  1175. ns->queue->queue_flags = QUEUE_FLAG_DEFAULT;
  1176. queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
  1177. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
  1178. /* queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue); */
  1179. blk_queue_make_request(ns->queue, nvme_make_request);
  1180. ns->dev = dev;
  1181. ns->queue->queuedata = ns;
  1182. disk = alloc_disk(NVME_MINORS);
  1183. if (!disk)
  1184. goto out_free_queue;
  1185. ns->ns_id = nsid;
  1186. ns->disk = disk;
  1187. lbaf = id->flbas & 0xf;
  1188. ns->lba_shift = id->lbaf[lbaf].ds;
  1189. blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
  1190. if (dev->max_hw_sectors)
  1191. blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
  1192. disk->major = nvme_major;
  1193. disk->minors = NVME_MINORS;
  1194. disk->first_minor = NVME_MINORS * nvme_get_ns_idx();
  1195. disk->fops = &nvme_fops;
  1196. disk->private_data = ns;
  1197. disk->queue = ns->queue;
  1198. disk->driverfs_dev = &dev->pci_dev->dev;
  1199. sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
  1200. set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
  1201. return ns;
  1202. out_free_queue:
  1203. blk_cleanup_queue(ns->queue);
  1204. out_free_ns:
  1205. kfree(ns);
  1206. return NULL;
  1207. }
  1208. static void nvme_ns_free(struct nvme_ns *ns)
  1209. {
  1210. int index = ns->disk->first_minor / NVME_MINORS;
  1211. put_disk(ns->disk);
  1212. nvme_put_ns_idx(index);
  1213. blk_cleanup_queue(ns->queue);
  1214. kfree(ns);
  1215. }
  1216. static int set_queue_count(struct nvme_dev *dev, int count)
  1217. {
  1218. int status;
  1219. u32 result;
  1220. u32 q_count = (count - 1) | ((count - 1) << 16);
  1221. status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
  1222. &result);
  1223. if (status)
  1224. return -EIO;
  1225. return min(result & 0xffff, result >> 16) + 1;
  1226. }
  1227. static int __devinit nvme_setup_io_queues(struct nvme_dev *dev)
  1228. {
  1229. int result, cpu, i, nr_io_queues, db_bar_size, q_depth;
  1230. nr_io_queues = num_online_cpus();
  1231. result = set_queue_count(dev, nr_io_queues);
  1232. if (result < 0)
  1233. return result;
  1234. if (result < nr_io_queues)
  1235. nr_io_queues = result;
  1236. /* Deregister the admin queue's interrupt */
  1237. free_irq(dev->entry[0].vector, dev->queues[0]);
  1238. db_bar_size = 4096 + ((nr_io_queues + 1) << (dev->db_stride + 3));
  1239. if (db_bar_size > 8192) {
  1240. iounmap(dev->bar);
  1241. dev->bar = ioremap(pci_resource_start(dev->pci_dev, 0),
  1242. db_bar_size);
  1243. dev->dbs = ((void __iomem *)dev->bar) + 4096;
  1244. dev->queues[0]->q_db = dev->dbs;
  1245. }
  1246. for (i = 0; i < nr_io_queues; i++)
  1247. dev->entry[i].entry = i;
  1248. for (;;) {
  1249. result = pci_enable_msix(dev->pci_dev, dev->entry,
  1250. nr_io_queues);
  1251. if (result == 0) {
  1252. break;
  1253. } else if (result > 0) {
  1254. nr_io_queues = result;
  1255. continue;
  1256. } else {
  1257. nr_io_queues = 1;
  1258. break;
  1259. }
  1260. }
  1261. result = queue_request_irq(dev, dev->queues[0], "nvme admin");
  1262. /* XXX: handle failure here */
  1263. cpu = cpumask_first(cpu_online_mask);
  1264. for (i = 0; i < nr_io_queues; i++) {
  1265. irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu));
  1266. cpu = cpumask_next(cpu, cpu_online_mask);
  1267. }
  1268. q_depth = min_t(int, NVME_CAP_MQES(readq(&dev->bar->cap)) + 1,
  1269. NVME_Q_DEPTH);
  1270. for (i = 0; i < nr_io_queues; i++) {
  1271. dev->queues[i + 1] = nvme_create_queue(dev, i + 1, q_depth, i);
  1272. if (IS_ERR(dev->queues[i + 1]))
  1273. return PTR_ERR(dev->queues[i + 1]);
  1274. dev->queue_count++;
  1275. }
  1276. for (; i < num_possible_cpus(); i++) {
  1277. int target = i % rounddown_pow_of_two(dev->queue_count - 1);
  1278. dev->queues[i + 1] = dev->queues[target + 1];
  1279. }
  1280. return 0;
  1281. }
  1282. static void nvme_free_queues(struct nvme_dev *dev)
  1283. {
  1284. int i;
  1285. for (i = dev->queue_count - 1; i >= 0; i--)
  1286. nvme_free_queue(dev, i);
  1287. }
  1288. static int __devinit nvme_dev_add(struct nvme_dev *dev)
  1289. {
  1290. int res, nn, i;
  1291. struct nvme_ns *ns, *next;
  1292. struct nvme_id_ctrl *ctrl;
  1293. struct nvme_id_ns *id_ns;
  1294. void *mem;
  1295. dma_addr_t dma_addr;
  1296. res = nvme_setup_io_queues(dev);
  1297. if (res)
  1298. return res;
  1299. mem = dma_alloc_coherent(&dev->pci_dev->dev, 8192, &dma_addr,
  1300. GFP_KERNEL);
  1301. res = nvme_identify(dev, 0, 1, dma_addr);
  1302. if (res) {
  1303. res = -EIO;
  1304. goto out_free;
  1305. }
  1306. ctrl = mem;
  1307. nn = le32_to_cpup(&ctrl->nn);
  1308. memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
  1309. memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
  1310. memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
  1311. if (ctrl->mdts) {
  1312. int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
  1313. dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
  1314. }
  1315. id_ns = mem;
  1316. for (i = 1; i <= nn; i++) {
  1317. res = nvme_identify(dev, i, 0, dma_addr);
  1318. if (res)
  1319. continue;
  1320. if (id_ns->ncap == 0)
  1321. continue;
  1322. res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
  1323. dma_addr + 4096, NULL);
  1324. if (res)
  1325. continue;
  1326. ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
  1327. if (ns)
  1328. list_add_tail(&ns->list, &dev->namespaces);
  1329. }
  1330. list_for_each_entry(ns, &dev->namespaces, list)
  1331. add_disk(ns->disk);
  1332. goto out;
  1333. out_free:
  1334. list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
  1335. list_del(&ns->list);
  1336. nvme_ns_free(ns);
  1337. }
  1338. out:
  1339. dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
  1340. return res;
  1341. }
  1342. static int nvme_dev_remove(struct nvme_dev *dev)
  1343. {
  1344. struct nvme_ns *ns, *next;
  1345. spin_lock(&dev_list_lock);
  1346. list_del(&dev->node);
  1347. spin_unlock(&dev_list_lock);
  1348. list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
  1349. list_del(&ns->list);
  1350. del_gendisk(ns->disk);
  1351. nvme_ns_free(ns);
  1352. }
  1353. nvme_free_queues(dev);
  1354. return 0;
  1355. }
  1356. static int nvme_setup_prp_pools(struct nvme_dev *dev)
  1357. {
  1358. struct device *dmadev = &dev->pci_dev->dev;
  1359. dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
  1360. PAGE_SIZE, PAGE_SIZE, 0);
  1361. if (!dev->prp_page_pool)
  1362. return -ENOMEM;
  1363. /* Optimisation for I/Os between 4k and 128k */
  1364. dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
  1365. 256, 256, 0);
  1366. if (!dev->prp_small_pool) {
  1367. dma_pool_destroy(dev->prp_page_pool);
  1368. return -ENOMEM;
  1369. }
  1370. return 0;
  1371. }
  1372. static void nvme_release_prp_pools(struct nvme_dev *dev)
  1373. {
  1374. dma_pool_destroy(dev->prp_page_pool);
  1375. dma_pool_destroy(dev->prp_small_pool);
  1376. }
  1377. static DEFINE_IDA(nvme_instance_ida);
  1378. static int nvme_set_instance(struct nvme_dev *dev)
  1379. {
  1380. int instance, error;
  1381. do {
  1382. if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
  1383. return -ENODEV;
  1384. spin_lock(&dev_list_lock);
  1385. error = ida_get_new(&nvme_instance_ida, &instance);
  1386. spin_unlock(&dev_list_lock);
  1387. } while (error == -EAGAIN);
  1388. if (error)
  1389. return -ENODEV;
  1390. dev->instance = instance;
  1391. return 0;
  1392. }
  1393. static void nvme_release_instance(struct nvme_dev *dev)
  1394. {
  1395. spin_lock(&dev_list_lock);
  1396. ida_remove(&nvme_instance_ida, dev->instance);
  1397. spin_unlock(&dev_list_lock);
  1398. }
  1399. static int __devinit nvme_probe(struct pci_dev *pdev,
  1400. const struct pci_device_id *id)
  1401. {
  1402. int bars, result = -ENOMEM;
  1403. struct nvme_dev *dev;
  1404. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1405. if (!dev)
  1406. return -ENOMEM;
  1407. dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
  1408. GFP_KERNEL);
  1409. if (!dev->entry)
  1410. goto free;
  1411. dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
  1412. GFP_KERNEL);
  1413. if (!dev->queues)
  1414. goto free;
  1415. if (pci_enable_device_mem(pdev))
  1416. goto free;
  1417. pci_set_master(pdev);
  1418. bars = pci_select_bars(pdev, IORESOURCE_MEM);
  1419. if (pci_request_selected_regions(pdev, bars, "nvme"))
  1420. goto disable;
  1421. INIT_LIST_HEAD(&dev->namespaces);
  1422. dev->pci_dev = pdev;
  1423. pci_set_drvdata(pdev, dev);
  1424. dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
  1425. dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
  1426. result = nvme_set_instance(dev);
  1427. if (result)
  1428. goto disable;
  1429. dev->entry[0].vector = pdev->irq;
  1430. result = nvme_setup_prp_pools(dev);
  1431. if (result)
  1432. goto disable_msix;
  1433. dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
  1434. if (!dev->bar) {
  1435. result = -ENOMEM;
  1436. goto disable_msix;
  1437. }
  1438. result = nvme_configure_admin_queue(dev);
  1439. if (result)
  1440. goto unmap;
  1441. dev->queue_count++;
  1442. spin_lock(&dev_list_lock);
  1443. list_add(&dev->node, &dev_list);
  1444. spin_unlock(&dev_list_lock);
  1445. result = nvme_dev_add(dev);
  1446. if (result)
  1447. goto delete;
  1448. return 0;
  1449. delete:
  1450. spin_lock(&dev_list_lock);
  1451. list_del(&dev->node);
  1452. spin_unlock(&dev_list_lock);
  1453. nvme_free_queues(dev);
  1454. unmap:
  1455. iounmap(dev->bar);
  1456. disable_msix:
  1457. pci_disable_msix(pdev);
  1458. nvme_release_instance(dev);
  1459. nvme_release_prp_pools(dev);
  1460. disable:
  1461. pci_disable_device(pdev);
  1462. pci_release_regions(pdev);
  1463. free:
  1464. kfree(dev->queues);
  1465. kfree(dev->entry);
  1466. kfree(dev);
  1467. return result;
  1468. }
  1469. static void __devexit nvme_remove(struct pci_dev *pdev)
  1470. {
  1471. struct nvme_dev *dev = pci_get_drvdata(pdev);
  1472. nvme_dev_remove(dev);
  1473. pci_disable_msix(pdev);
  1474. iounmap(dev->bar);
  1475. nvme_release_instance(dev);
  1476. nvme_release_prp_pools(dev);
  1477. pci_disable_device(pdev);
  1478. pci_release_regions(pdev);
  1479. kfree(dev->queues);
  1480. kfree(dev->entry);
  1481. kfree(dev);
  1482. }
  1483. /* These functions are yet to be implemented */
  1484. #define nvme_error_detected NULL
  1485. #define nvme_dump_registers NULL
  1486. #define nvme_link_reset NULL
  1487. #define nvme_slot_reset NULL
  1488. #define nvme_error_resume NULL
  1489. #define nvme_suspend NULL
  1490. #define nvme_resume NULL
  1491. static const struct pci_error_handlers nvme_err_handler = {
  1492. .error_detected = nvme_error_detected,
  1493. .mmio_enabled = nvme_dump_registers,
  1494. .link_reset = nvme_link_reset,
  1495. .slot_reset = nvme_slot_reset,
  1496. .resume = nvme_error_resume,
  1497. };
  1498. /* Move to pci_ids.h later */
  1499. #define PCI_CLASS_STORAGE_EXPRESS 0x010802
  1500. static DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = {
  1501. { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
  1502. { 0, }
  1503. };
  1504. MODULE_DEVICE_TABLE(pci, nvme_id_table);
  1505. static struct pci_driver nvme_driver = {
  1506. .name = "nvme",
  1507. .id_table = nvme_id_table,
  1508. .probe = nvme_probe,
  1509. .remove = __devexit_p(nvme_remove),
  1510. .suspend = nvme_suspend,
  1511. .resume = nvme_resume,
  1512. .err_handler = &nvme_err_handler,
  1513. };
  1514. static int __init nvme_init(void)
  1515. {
  1516. int result;
  1517. nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
  1518. if (IS_ERR(nvme_thread))
  1519. return PTR_ERR(nvme_thread);
  1520. result = register_blkdev(nvme_major, "nvme");
  1521. if (result < 0)
  1522. goto kill_kthread;
  1523. else if (result > 0)
  1524. nvme_major = result;
  1525. result = pci_register_driver(&nvme_driver);
  1526. if (result)
  1527. goto unregister_blkdev;
  1528. return 0;
  1529. unregister_blkdev:
  1530. unregister_blkdev(nvme_major, "nvme");
  1531. kill_kthread:
  1532. kthread_stop(nvme_thread);
  1533. return result;
  1534. }
  1535. static void __exit nvme_exit(void)
  1536. {
  1537. pci_unregister_driver(&nvme_driver);
  1538. unregister_blkdev(nvme_major, "nvme");
  1539. kthread_stop(nvme_thread);
  1540. }
  1541. MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
  1542. MODULE_LICENSE("GPL");
  1543. MODULE_VERSION("0.8");
  1544. module_init(nvme_init);
  1545. module_exit(nvme_exit);