nvme.c 43 KB

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