xref: /linux-6.15/drivers/nvme/target/pci-epf.c (revision 8113d610)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * NVMe PCI Endpoint Function target driver.
4  *
5  * Copyright (c) 2024, Western Digital Corporation or its affiliates.
6  * Copyright (c) 2024, Rick Wertenbroek <[email protected]>
7  *                     REDS Institute, HEIG-VD, HES-SO, Switzerland
8  */
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 
11 #include <linux/delay.h>
12 #include <linux/dmaengine.h>
13 #include <linux/io.h>
14 #include <linux/mempool.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/nvme.h>
18 #include <linux/pci_ids.h>
19 #include <linux/pci-epc.h>
20 #include <linux/pci-epf.h>
21 #include <linux/pci_regs.h>
22 #include <linux/slab.h>
23 
24 #include "nvmet.h"
25 
26 static LIST_HEAD(nvmet_pci_epf_ports);
27 static DEFINE_MUTEX(nvmet_pci_epf_ports_mutex);
28 
29 /*
30  * Default and maximum allowed data transfer size. For the default,
31  * allow up to 128 page-sized segments. For the maximum allowed,
32  * use 4 times the default (which is completely arbitrary).
33  */
34 #define NVMET_PCI_EPF_MAX_SEGS		128
35 #define NVMET_PCI_EPF_MDTS_KB		\
36 	(NVMET_PCI_EPF_MAX_SEGS << (PAGE_SHIFT - 10))
37 #define NVMET_PCI_EPF_MAX_MDTS_KB	(NVMET_PCI_EPF_MDTS_KB * 4)
38 
39 /*
40  * IRQ vector coalescing threshold: by default, post 8 CQEs before raising an
41  * interrupt vector to the host. This default 8 is completely arbitrary and can
42  * be changed by the host with a nvme_set_features command.
43  */
44 #define NVMET_PCI_EPF_IV_THRESHOLD	8
45 
46 /*
47  * BAR CC register and SQ polling intervals.
48  */
49 #define NVMET_PCI_EPF_CC_POLL_INTERVAL	msecs_to_jiffies(10)
50 #define NVMET_PCI_EPF_SQ_POLL_INTERVAL	msecs_to_jiffies(5)
51 #define NVMET_PCI_EPF_SQ_POLL_IDLE	msecs_to_jiffies(5000)
52 
53 /*
54  * SQ arbitration burst default: fetch at most 8 commands at a time from an SQ.
55  */
56 #define NVMET_PCI_EPF_SQ_AB		8
57 
58 /*
59  * Handling of CQs is normally immediate, unless we fail to map a CQ or the CQ
60  * is full, in which case we retry the CQ processing after this interval.
61  */
62 #define NVMET_PCI_EPF_CQ_RETRY_INTERVAL	msecs_to_jiffies(1)
63 
64 enum nvmet_pci_epf_queue_flags {
65 	NVMET_PCI_EPF_Q_LIVE = 0,	/* The queue is live */
66 	NVMET_PCI_EPF_Q_IRQ_ENABLED,	/* IRQ is enabled for this queue */
67 };
68 
69 /*
70  * IRQ vector descriptor.
71  */
72 struct nvmet_pci_epf_irq_vector {
73 	unsigned int	vector;
74 	unsigned int	ref;
75 	bool		cd;
76 	int		nr_irqs;
77 };
78 
79 struct nvmet_pci_epf_queue {
80 	union {
81 		struct nvmet_sq		nvme_sq;
82 		struct nvmet_cq		nvme_cq;
83 	};
84 	struct nvmet_pci_epf_ctrl	*ctrl;
85 	unsigned long			flags;
86 
87 	u64				pci_addr;
88 	size_t				pci_size;
89 	struct pci_epc_map		pci_map;
90 
91 	u16				qid;
92 	u16				depth;
93 	u16				vector;
94 	u16				head;
95 	u16				tail;
96 	u16				phase;
97 	u32				db;
98 
99 	size_t				qes;
100 
101 	struct nvmet_pci_epf_irq_vector	*iv;
102 	struct workqueue_struct		*iod_wq;
103 	struct delayed_work		work;
104 	spinlock_t			lock;
105 	struct list_head		list;
106 };
107 
108 /*
109  * PCI Root Complex (RC) address data segment for mapping an admin or
110  * I/O command buffer @buf of @length bytes to the PCI address @pci_addr.
111  */
112 struct nvmet_pci_epf_segment {
113 	void				*buf;
114 	u64				pci_addr;
115 	u32				length;
116 };
117 
118 /*
119  * Command descriptors.
120  */
121 struct nvmet_pci_epf_iod {
122 	struct list_head		link;
123 
124 	struct nvmet_req		req;
125 	struct nvme_command		cmd;
126 	struct nvme_completion		cqe;
127 	unsigned int			status;
128 
129 	struct nvmet_pci_epf_ctrl	*ctrl;
130 
131 	struct nvmet_pci_epf_queue	*sq;
132 	struct nvmet_pci_epf_queue	*cq;
133 
134 	/* Data transfer size and direction for the command. */
135 	size_t				data_len;
136 	enum dma_data_direction		dma_dir;
137 
138 	/*
139 	 * PCI Root Complex (RC) address data segments: if nr_data_segs is 1, we
140 	 * use only @data_seg. Otherwise, the array of segments @data_segs is
141 	 * allocated to manage multiple PCI address data segments. @data_sgl and
142 	 * @data_sgt are used to setup the command request for execution by the
143 	 * target core.
144 	 */
145 	unsigned int			nr_data_segs;
146 	struct nvmet_pci_epf_segment	data_seg;
147 	struct nvmet_pci_epf_segment	*data_segs;
148 	struct scatterlist		data_sgl;
149 	struct sg_table			data_sgt;
150 
151 	struct work_struct		work;
152 	struct completion		done;
153 };
154 
155 /*
156  * PCI target controller private data.
157  */
158 struct nvmet_pci_epf_ctrl {
159 	struct nvmet_pci_epf		*nvme_epf;
160 	struct nvmet_port		*port;
161 	struct nvmet_ctrl		*tctrl;
162 	struct device			*dev;
163 
164 	unsigned int			nr_queues;
165 	struct nvmet_pci_epf_queue	*sq;
166 	struct nvmet_pci_epf_queue	*cq;
167 	unsigned int			sq_ab;
168 
169 	mempool_t			iod_pool;
170 	void				*bar;
171 	u64				cap;
172 	u32				cc;
173 	u32				csts;
174 
175 	size_t				io_sqes;
176 	size_t				io_cqes;
177 
178 	size_t				mps_shift;
179 	size_t				mps;
180 	size_t				mps_mask;
181 
182 	unsigned int			mdts;
183 
184 	struct delayed_work		poll_cc;
185 	struct delayed_work		poll_sqs;
186 
187 	struct mutex			irq_lock;
188 	struct nvmet_pci_epf_irq_vector	*irq_vectors;
189 	unsigned int			irq_vector_threshold;
190 
191 	bool				link_up;
192 	bool				enabled;
193 };
194 
195 /*
196  * PCI EPF driver private data.
197  */
198 struct nvmet_pci_epf {
199 	struct pci_epf			*epf;
200 
201 	const struct pci_epc_features	*epc_features;
202 
203 	void				*reg_bar;
204 	size_t				msix_table_offset;
205 
206 	unsigned int			irq_type;
207 	unsigned int			nr_vectors;
208 
209 	struct nvmet_pci_epf_ctrl	ctrl;
210 
211 	bool				dma_enabled;
212 	struct dma_chan			*dma_tx_chan;
213 	struct mutex			dma_tx_lock;
214 	struct dma_chan			*dma_rx_chan;
215 	struct mutex			dma_rx_lock;
216 
217 	struct mutex			mmio_lock;
218 
219 	/* PCI endpoint function configfs attributes. */
220 	struct config_group		group;
221 	__le16				portid;
222 	char				subsysnqn[NVMF_NQN_SIZE];
223 	unsigned int			mdts_kb;
224 };
225 
nvmet_pci_epf_bar_read32(struct nvmet_pci_epf_ctrl * ctrl,u32 off)226 static inline u32 nvmet_pci_epf_bar_read32(struct nvmet_pci_epf_ctrl *ctrl,
227 					   u32 off)
228 {
229 	__le32 *bar_reg = ctrl->bar + off;
230 
231 	return le32_to_cpu(READ_ONCE(*bar_reg));
232 }
233 
nvmet_pci_epf_bar_write32(struct nvmet_pci_epf_ctrl * ctrl,u32 off,u32 val)234 static inline void nvmet_pci_epf_bar_write32(struct nvmet_pci_epf_ctrl *ctrl,
235 					     u32 off, u32 val)
236 {
237 	__le32 *bar_reg = ctrl->bar + off;
238 
239 	WRITE_ONCE(*bar_reg, cpu_to_le32(val));
240 }
241 
nvmet_pci_epf_bar_read64(struct nvmet_pci_epf_ctrl * ctrl,u32 off)242 static inline u64 nvmet_pci_epf_bar_read64(struct nvmet_pci_epf_ctrl *ctrl,
243 					   u32 off)
244 {
245 	return (u64)nvmet_pci_epf_bar_read32(ctrl, off) |
246 		((u64)nvmet_pci_epf_bar_read32(ctrl, off + 4) << 32);
247 }
248 
nvmet_pci_epf_bar_write64(struct nvmet_pci_epf_ctrl * ctrl,u32 off,u64 val)249 static inline void nvmet_pci_epf_bar_write64(struct nvmet_pci_epf_ctrl *ctrl,
250 					     u32 off, u64 val)
251 {
252 	nvmet_pci_epf_bar_write32(ctrl, off, val & 0xFFFFFFFF);
253 	nvmet_pci_epf_bar_write32(ctrl, off + 4, (val >> 32) & 0xFFFFFFFF);
254 }
255 
nvmet_pci_epf_mem_map(struct nvmet_pci_epf * nvme_epf,u64 pci_addr,size_t size,struct pci_epc_map * map)256 static inline int nvmet_pci_epf_mem_map(struct nvmet_pci_epf *nvme_epf,
257 		u64 pci_addr, size_t size, struct pci_epc_map *map)
258 {
259 	struct pci_epf *epf = nvme_epf->epf;
260 
261 	return pci_epc_mem_map(epf->epc, epf->func_no, epf->vfunc_no,
262 			       pci_addr, size, map);
263 }
264 
nvmet_pci_epf_mem_unmap(struct nvmet_pci_epf * nvme_epf,struct pci_epc_map * map)265 static inline void nvmet_pci_epf_mem_unmap(struct nvmet_pci_epf *nvme_epf,
266 					   struct pci_epc_map *map)
267 {
268 	struct pci_epf *epf = nvme_epf->epf;
269 
270 	pci_epc_mem_unmap(epf->epc, epf->func_no, epf->vfunc_no, map);
271 }
272 
273 struct nvmet_pci_epf_dma_filter {
274 	struct device *dev;
275 	u32 dma_mask;
276 };
277 
nvmet_pci_epf_dma_filter(struct dma_chan * chan,void * arg)278 static bool nvmet_pci_epf_dma_filter(struct dma_chan *chan, void *arg)
279 {
280 	struct nvmet_pci_epf_dma_filter *filter = arg;
281 	struct dma_slave_caps caps;
282 
283 	memset(&caps, 0, sizeof(caps));
284 	dma_get_slave_caps(chan, &caps);
285 
286 	return chan->device->dev == filter->dev &&
287 		(filter->dma_mask & caps.directions);
288 }
289 
nvmet_pci_epf_init_dma(struct nvmet_pci_epf * nvme_epf)290 static void nvmet_pci_epf_init_dma(struct nvmet_pci_epf *nvme_epf)
291 {
292 	struct pci_epf *epf = nvme_epf->epf;
293 	struct device *dev = &epf->dev;
294 	struct nvmet_pci_epf_dma_filter filter;
295 	struct dma_chan *chan;
296 	dma_cap_mask_t mask;
297 
298 	mutex_init(&nvme_epf->dma_rx_lock);
299 	mutex_init(&nvme_epf->dma_tx_lock);
300 
301 	dma_cap_zero(mask);
302 	dma_cap_set(DMA_SLAVE, mask);
303 
304 	filter.dev = epf->epc->dev.parent;
305 	filter.dma_mask = BIT(DMA_DEV_TO_MEM);
306 
307 	chan = dma_request_channel(mask, nvmet_pci_epf_dma_filter, &filter);
308 	if (!chan)
309 		goto out_dma_no_rx;
310 
311 	nvme_epf->dma_rx_chan = chan;
312 
313 	filter.dma_mask = BIT(DMA_MEM_TO_DEV);
314 	chan = dma_request_channel(mask, nvmet_pci_epf_dma_filter, &filter);
315 	if (!chan)
316 		goto out_dma_no_tx;
317 
318 	nvme_epf->dma_tx_chan = chan;
319 
320 	nvme_epf->dma_enabled = true;
321 
322 	dev_dbg(dev, "Using DMA RX channel %s, maximum segment size %u B\n",
323 		dma_chan_name(chan),
324 		dma_get_max_seg_size(dmaengine_get_dma_device(chan)));
325 
326 	dev_dbg(dev, "Using DMA TX channel %s, maximum segment size %u B\n",
327 		dma_chan_name(chan),
328 		dma_get_max_seg_size(dmaengine_get_dma_device(chan)));
329 
330 	return;
331 
332 out_dma_no_tx:
333 	dma_release_channel(nvme_epf->dma_rx_chan);
334 	nvme_epf->dma_rx_chan = NULL;
335 
336 out_dma_no_rx:
337 	mutex_destroy(&nvme_epf->dma_rx_lock);
338 	mutex_destroy(&nvme_epf->dma_tx_lock);
339 	nvme_epf->dma_enabled = false;
340 
341 	dev_info(&epf->dev, "DMA not supported, falling back to MMIO\n");
342 }
343 
nvmet_pci_epf_deinit_dma(struct nvmet_pci_epf * nvme_epf)344 static void nvmet_pci_epf_deinit_dma(struct nvmet_pci_epf *nvme_epf)
345 {
346 	if (!nvme_epf->dma_enabled)
347 		return;
348 
349 	dma_release_channel(nvme_epf->dma_tx_chan);
350 	nvme_epf->dma_tx_chan = NULL;
351 	dma_release_channel(nvme_epf->dma_rx_chan);
352 	nvme_epf->dma_rx_chan = NULL;
353 	mutex_destroy(&nvme_epf->dma_rx_lock);
354 	mutex_destroy(&nvme_epf->dma_tx_lock);
355 	nvme_epf->dma_enabled = false;
356 }
357 
nvmet_pci_epf_dma_transfer(struct nvmet_pci_epf * nvme_epf,struct nvmet_pci_epf_segment * seg,enum dma_data_direction dir)358 static int nvmet_pci_epf_dma_transfer(struct nvmet_pci_epf *nvme_epf,
359 		struct nvmet_pci_epf_segment *seg, enum dma_data_direction dir)
360 {
361 	struct pci_epf *epf = nvme_epf->epf;
362 	struct dma_async_tx_descriptor *desc;
363 	struct dma_slave_config sconf = {};
364 	struct device *dev = &epf->dev;
365 	struct device *dma_dev;
366 	struct dma_chan *chan;
367 	dma_cookie_t cookie;
368 	dma_addr_t dma_addr;
369 	struct mutex *lock;
370 	int ret;
371 
372 	switch (dir) {
373 	case DMA_FROM_DEVICE:
374 		lock = &nvme_epf->dma_rx_lock;
375 		chan = nvme_epf->dma_rx_chan;
376 		sconf.direction = DMA_DEV_TO_MEM;
377 		sconf.src_addr = seg->pci_addr;
378 		break;
379 	case DMA_TO_DEVICE:
380 		lock = &nvme_epf->dma_tx_lock;
381 		chan = nvme_epf->dma_tx_chan;
382 		sconf.direction = DMA_MEM_TO_DEV;
383 		sconf.dst_addr = seg->pci_addr;
384 		break;
385 	default:
386 		return -EINVAL;
387 	}
388 
389 	mutex_lock(lock);
390 
391 	dma_dev = dmaengine_get_dma_device(chan);
392 	dma_addr = dma_map_single(dma_dev, seg->buf, seg->length, dir);
393 	ret = dma_mapping_error(dma_dev, dma_addr);
394 	if (ret)
395 		goto unlock;
396 
397 	ret = dmaengine_slave_config(chan, &sconf);
398 	if (ret) {
399 		dev_err(dev, "Failed to configure DMA channel\n");
400 		goto unmap;
401 	}
402 
403 	desc = dmaengine_prep_slave_single(chan, dma_addr, seg->length,
404 					   sconf.direction, DMA_CTRL_ACK);
405 	if (!desc) {
406 		dev_err(dev, "Failed to prepare DMA\n");
407 		ret = -EIO;
408 		goto unmap;
409 	}
410 
411 	cookie = dmaengine_submit(desc);
412 	ret = dma_submit_error(cookie);
413 	if (ret) {
414 		dev_err(dev, "Failed to do DMA submit (err=%d)\n", ret);
415 		goto unmap;
416 	}
417 
418 	if (dma_sync_wait(chan, cookie) != DMA_COMPLETE) {
419 		dev_err(dev, "DMA transfer failed\n");
420 		ret = -EIO;
421 	}
422 
423 	dmaengine_terminate_sync(chan);
424 
425 unmap:
426 	dma_unmap_single(dma_dev, dma_addr, seg->length, dir);
427 
428 unlock:
429 	mutex_unlock(lock);
430 
431 	return ret;
432 }
433 
nvmet_pci_epf_mmio_transfer(struct nvmet_pci_epf * nvme_epf,struct nvmet_pci_epf_segment * seg,enum dma_data_direction dir)434 static int nvmet_pci_epf_mmio_transfer(struct nvmet_pci_epf *nvme_epf,
435 		struct nvmet_pci_epf_segment *seg, enum dma_data_direction dir)
436 {
437 	u64 pci_addr = seg->pci_addr;
438 	u32 length = seg->length;
439 	void *buf = seg->buf;
440 	struct pci_epc_map map;
441 	int ret = -EINVAL;
442 
443 	/*
444 	 * Note: MMIO transfers do not need serialization but this is a
445 	 * simple way to avoid using too many mapping windows.
446 	 */
447 	mutex_lock(&nvme_epf->mmio_lock);
448 
449 	while (length) {
450 		ret = nvmet_pci_epf_mem_map(nvme_epf, pci_addr, length, &map);
451 		if (ret)
452 			break;
453 
454 		switch (dir) {
455 		case DMA_FROM_DEVICE:
456 			memcpy_fromio(buf, map.virt_addr, map.pci_size);
457 			break;
458 		case DMA_TO_DEVICE:
459 			memcpy_toio(map.virt_addr, buf, map.pci_size);
460 			break;
461 		default:
462 			ret = -EINVAL;
463 			goto unlock;
464 		}
465 
466 		pci_addr += map.pci_size;
467 		buf += map.pci_size;
468 		length -= map.pci_size;
469 
470 		nvmet_pci_epf_mem_unmap(nvme_epf, &map);
471 	}
472 
473 unlock:
474 	mutex_unlock(&nvme_epf->mmio_lock);
475 
476 	return ret;
477 }
478 
nvmet_pci_epf_transfer_seg(struct nvmet_pci_epf * nvme_epf,struct nvmet_pci_epf_segment * seg,enum dma_data_direction dir)479 static inline int nvmet_pci_epf_transfer_seg(struct nvmet_pci_epf *nvme_epf,
480 		struct nvmet_pci_epf_segment *seg, enum dma_data_direction dir)
481 {
482 	if (nvme_epf->dma_enabled)
483 		return nvmet_pci_epf_dma_transfer(nvme_epf, seg, dir);
484 
485 	return nvmet_pci_epf_mmio_transfer(nvme_epf, seg, dir);
486 }
487 
nvmet_pci_epf_transfer(struct nvmet_pci_epf_ctrl * ctrl,void * buf,u64 pci_addr,u32 length,enum dma_data_direction dir)488 static inline int nvmet_pci_epf_transfer(struct nvmet_pci_epf_ctrl *ctrl,
489 					 void *buf, u64 pci_addr, u32 length,
490 					 enum dma_data_direction dir)
491 {
492 	struct nvmet_pci_epf_segment seg = {
493 		.buf = buf,
494 		.pci_addr = pci_addr,
495 		.length = length,
496 	};
497 
498 	return nvmet_pci_epf_transfer_seg(ctrl->nvme_epf, &seg, dir);
499 }
500 
nvmet_pci_epf_alloc_irq_vectors(struct nvmet_pci_epf_ctrl * ctrl)501 static int nvmet_pci_epf_alloc_irq_vectors(struct nvmet_pci_epf_ctrl *ctrl)
502 {
503 	ctrl->irq_vectors = kcalloc(ctrl->nr_queues,
504 				    sizeof(struct nvmet_pci_epf_irq_vector),
505 				    GFP_KERNEL);
506 	if (!ctrl->irq_vectors)
507 		return -ENOMEM;
508 
509 	mutex_init(&ctrl->irq_lock);
510 
511 	return 0;
512 }
513 
nvmet_pci_epf_free_irq_vectors(struct nvmet_pci_epf_ctrl * ctrl)514 static void nvmet_pci_epf_free_irq_vectors(struct nvmet_pci_epf_ctrl *ctrl)
515 {
516 	if (ctrl->irq_vectors) {
517 		mutex_destroy(&ctrl->irq_lock);
518 		kfree(ctrl->irq_vectors);
519 		ctrl->irq_vectors = NULL;
520 	}
521 }
522 
523 static struct nvmet_pci_epf_irq_vector *
nvmet_pci_epf_find_irq_vector(struct nvmet_pci_epf_ctrl * ctrl,u16 vector)524 nvmet_pci_epf_find_irq_vector(struct nvmet_pci_epf_ctrl *ctrl, u16 vector)
525 {
526 	struct nvmet_pci_epf_irq_vector *iv;
527 	int i;
528 
529 	lockdep_assert_held(&ctrl->irq_lock);
530 
531 	for (i = 0; i < ctrl->nr_queues; i++) {
532 		iv = &ctrl->irq_vectors[i];
533 		if (iv->ref && iv->vector == vector)
534 			return iv;
535 	}
536 
537 	return NULL;
538 }
539 
540 static struct nvmet_pci_epf_irq_vector *
nvmet_pci_epf_add_irq_vector(struct nvmet_pci_epf_ctrl * ctrl,u16 vector)541 nvmet_pci_epf_add_irq_vector(struct nvmet_pci_epf_ctrl *ctrl, u16 vector)
542 {
543 	struct nvmet_pci_epf_irq_vector *iv;
544 	int i;
545 
546 	mutex_lock(&ctrl->irq_lock);
547 
548 	iv = nvmet_pci_epf_find_irq_vector(ctrl, vector);
549 	if (iv) {
550 		iv->ref++;
551 		goto unlock;
552 	}
553 
554 	for (i = 0; i < ctrl->nr_queues; i++) {
555 		iv = &ctrl->irq_vectors[i];
556 		if (!iv->ref)
557 			break;
558 	}
559 
560 	if (WARN_ON_ONCE(!iv))
561 		goto unlock;
562 
563 	iv->ref = 1;
564 	iv->vector = vector;
565 	iv->nr_irqs = 0;
566 
567 unlock:
568 	mutex_unlock(&ctrl->irq_lock);
569 
570 	return iv;
571 }
572 
nvmet_pci_epf_remove_irq_vector(struct nvmet_pci_epf_ctrl * ctrl,u16 vector)573 static void nvmet_pci_epf_remove_irq_vector(struct nvmet_pci_epf_ctrl *ctrl,
574 					    u16 vector)
575 {
576 	struct nvmet_pci_epf_irq_vector *iv;
577 
578 	mutex_lock(&ctrl->irq_lock);
579 
580 	iv = nvmet_pci_epf_find_irq_vector(ctrl, vector);
581 	if (iv) {
582 		iv->ref--;
583 		if (!iv->ref) {
584 			iv->vector = 0;
585 			iv->nr_irqs = 0;
586 		}
587 	}
588 
589 	mutex_unlock(&ctrl->irq_lock);
590 }
591 
nvmet_pci_epf_should_raise_irq(struct nvmet_pci_epf_ctrl * ctrl,struct nvmet_pci_epf_queue * cq,bool force)592 static bool nvmet_pci_epf_should_raise_irq(struct nvmet_pci_epf_ctrl *ctrl,
593 		struct nvmet_pci_epf_queue *cq, bool force)
594 {
595 	struct nvmet_pci_epf_irq_vector *iv = cq->iv;
596 	bool ret;
597 
598 	/* IRQ coalescing for the admin queue is not allowed. */
599 	if (!cq->qid)
600 		return true;
601 
602 	if (iv->cd)
603 		return true;
604 
605 	if (force) {
606 		ret = iv->nr_irqs > 0;
607 	} else {
608 		iv->nr_irqs++;
609 		ret = iv->nr_irqs >= ctrl->irq_vector_threshold;
610 	}
611 	if (ret)
612 		iv->nr_irqs = 0;
613 
614 	return ret;
615 }
616 
nvmet_pci_epf_raise_irq(struct nvmet_pci_epf_ctrl * ctrl,struct nvmet_pci_epf_queue * cq,bool force)617 static void nvmet_pci_epf_raise_irq(struct nvmet_pci_epf_ctrl *ctrl,
618 		struct nvmet_pci_epf_queue *cq, bool force)
619 {
620 	struct nvmet_pci_epf *nvme_epf = ctrl->nvme_epf;
621 	struct pci_epf *epf = nvme_epf->epf;
622 	int ret = 0;
623 
624 	if (!test_bit(NVMET_PCI_EPF_Q_LIVE, &cq->flags) ||
625 	    !test_bit(NVMET_PCI_EPF_Q_IRQ_ENABLED, &cq->flags))
626 		return;
627 
628 	mutex_lock(&ctrl->irq_lock);
629 
630 	if (!nvmet_pci_epf_should_raise_irq(ctrl, cq, force))
631 		goto unlock;
632 
633 	switch (nvme_epf->irq_type) {
634 	case PCI_IRQ_MSIX:
635 	case PCI_IRQ_MSI:
636 		/*
637 		 * If we fail to raise an MSI or MSI-X interrupt, it is likely
638 		 * because the host is using legacy INTX IRQs (e.g. BIOS,
639 		 * grub), but we can fallback to the INTX type only if the
640 		 * endpoint controller supports this type.
641 		 */
642 		ret = pci_epc_raise_irq(epf->epc, epf->func_no, epf->vfunc_no,
643 					nvme_epf->irq_type, cq->vector + 1);
644 		if (!ret || !nvme_epf->epc_features->intx_capable)
645 			break;
646 		fallthrough;
647 	case PCI_IRQ_INTX:
648 		ret = pci_epc_raise_irq(epf->epc, epf->func_no, epf->vfunc_no,
649 					PCI_IRQ_INTX, 0);
650 		break;
651 	default:
652 		WARN_ON_ONCE(1);
653 		ret = -EINVAL;
654 		break;
655 	}
656 
657 	if (ret)
658 		dev_err_ratelimited(ctrl->dev,
659 				    "CQ[%u]: Failed to raise IRQ (err=%d)\n",
660 				    cq->qid, ret);
661 
662 unlock:
663 	mutex_unlock(&ctrl->irq_lock);
664 }
665 
nvmet_pci_epf_iod_name(struct nvmet_pci_epf_iod * iod)666 static inline const char *nvmet_pci_epf_iod_name(struct nvmet_pci_epf_iod *iod)
667 {
668 	return nvme_opcode_str(iod->sq->qid, iod->cmd.common.opcode);
669 }
670 
671 static void nvmet_pci_epf_exec_iod_work(struct work_struct *work);
672 
673 static struct nvmet_pci_epf_iod *
nvmet_pci_epf_alloc_iod(struct nvmet_pci_epf_queue * sq)674 nvmet_pci_epf_alloc_iod(struct nvmet_pci_epf_queue *sq)
675 {
676 	struct nvmet_pci_epf_ctrl *ctrl = sq->ctrl;
677 	struct nvmet_pci_epf_iod *iod;
678 
679 	iod = mempool_alloc(&ctrl->iod_pool, GFP_KERNEL);
680 	if (unlikely(!iod))
681 		return NULL;
682 
683 	memset(iod, 0, sizeof(*iod));
684 	iod->req.cmd = &iod->cmd;
685 	iod->req.cqe = &iod->cqe;
686 	iod->req.port = ctrl->port;
687 	iod->ctrl = ctrl;
688 	iod->sq = sq;
689 	iod->cq = &ctrl->cq[sq->qid];
690 	INIT_LIST_HEAD(&iod->link);
691 	iod->dma_dir = DMA_NONE;
692 	INIT_WORK(&iod->work, nvmet_pci_epf_exec_iod_work);
693 	init_completion(&iod->done);
694 
695 	return iod;
696 }
697 
698 /*
699  * Allocate or grow a command table of PCI segments.
700  */
nvmet_pci_epf_alloc_iod_data_segs(struct nvmet_pci_epf_iod * iod,int nsegs)701 static int nvmet_pci_epf_alloc_iod_data_segs(struct nvmet_pci_epf_iod *iod,
702 					     int nsegs)
703 {
704 	struct nvmet_pci_epf_segment *segs;
705 	int nr_segs = iod->nr_data_segs + nsegs;
706 
707 	segs = krealloc(iod->data_segs,
708 			nr_segs * sizeof(struct nvmet_pci_epf_segment),
709 			GFP_KERNEL | __GFP_ZERO);
710 	if (!segs)
711 		return -ENOMEM;
712 
713 	iod->nr_data_segs = nr_segs;
714 	iod->data_segs = segs;
715 
716 	return 0;
717 }
718 
nvmet_pci_epf_free_iod(struct nvmet_pci_epf_iod * iod)719 static void nvmet_pci_epf_free_iod(struct nvmet_pci_epf_iod *iod)
720 {
721 	int i;
722 
723 	if (iod->data_segs) {
724 		for (i = 0; i < iod->nr_data_segs; i++)
725 			kfree(iod->data_segs[i].buf);
726 		if (iod->data_segs != &iod->data_seg)
727 			kfree(iod->data_segs);
728 	}
729 	if (iod->data_sgt.nents > 1)
730 		sg_free_table(&iod->data_sgt);
731 	mempool_free(iod, &iod->ctrl->iod_pool);
732 }
733 
nvmet_pci_epf_transfer_iod_data(struct nvmet_pci_epf_iod * iod)734 static int nvmet_pci_epf_transfer_iod_data(struct nvmet_pci_epf_iod *iod)
735 {
736 	struct nvmet_pci_epf *nvme_epf = iod->ctrl->nvme_epf;
737 	struct nvmet_pci_epf_segment *seg = &iod->data_segs[0];
738 	int i, ret;
739 
740 	/* Split the data transfer according to the PCI segments. */
741 	for (i = 0; i < iod->nr_data_segs; i++, seg++) {
742 		ret = nvmet_pci_epf_transfer_seg(nvme_epf, seg, iod->dma_dir);
743 		if (ret) {
744 			iod->status = NVME_SC_DATA_XFER_ERROR | NVME_STATUS_DNR;
745 			return ret;
746 		}
747 	}
748 
749 	return 0;
750 }
751 
nvmet_pci_epf_prp_ofst(struct nvmet_pci_epf_ctrl * ctrl,u64 prp)752 static inline u32 nvmet_pci_epf_prp_ofst(struct nvmet_pci_epf_ctrl *ctrl,
753 					 u64 prp)
754 {
755 	return prp & ctrl->mps_mask;
756 }
757 
nvmet_pci_epf_prp_size(struct nvmet_pci_epf_ctrl * ctrl,u64 prp)758 static inline size_t nvmet_pci_epf_prp_size(struct nvmet_pci_epf_ctrl *ctrl,
759 					    u64 prp)
760 {
761 	return ctrl->mps - nvmet_pci_epf_prp_ofst(ctrl, prp);
762 }
763 
764 /*
765  * Transfer a PRP list from the host and return the number of prps.
766  */
nvmet_pci_epf_get_prp_list(struct nvmet_pci_epf_ctrl * ctrl,u64 prp,size_t xfer_len,__le64 * prps)767 static int nvmet_pci_epf_get_prp_list(struct nvmet_pci_epf_ctrl *ctrl, u64 prp,
768 				      size_t xfer_len, __le64 *prps)
769 {
770 	size_t nr_prps = (xfer_len + ctrl->mps_mask) >> ctrl->mps_shift;
771 	u32 length;
772 	int ret;
773 
774 	/*
775 	 * Compute the number of PRPs required for the number of bytes to
776 	 * transfer (xfer_len). If this number overflows the memory page size
777 	 * with the PRP list pointer specified, only return the space available
778 	 * in the memory page, the last PRP in there will be a PRP list pointer
779 	 * to the remaining PRPs.
780 	 */
781 	length = min(nvmet_pci_epf_prp_size(ctrl, prp), nr_prps << 3);
782 	ret = nvmet_pci_epf_transfer(ctrl, prps, prp, length, DMA_FROM_DEVICE);
783 	if (ret)
784 		return ret;
785 
786 	return length >> 3;
787 }
788 
nvmet_pci_epf_iod_parse_prp_list(struct nvmet_pci_epf_ctrl * ctrl,struct nvmet_pci_epf_iod * iod)789 static int nvmet_pci_epf_iod_parse_prp_list(struct nvmet_pci_epf_ctrl *ctrl,
790 					    struct nvmet_pci_epf_iod *iod)
791 {
792 	struct nvme_command *cmd = &iod->cmd;
793 	struct nvmet_pci_epf_segment *seg;
794 	size_t size = 0, ofst, prp_size, xfer_len;
795 	size_t transfer_len = iod->data_len;
796 	int nr_segs, nr_prps = 0;
797 	u64 pci_addr, prp;
798 	int i = 0, ret;
799 	__le64 *prps;
800 
801 	prps = kzalloc(ctrl->mps, GFP_KERNEL);
802 	if (!prps)
803 		goto err_internal;
804 
805 	/*
806 	 * Allocate PCI segments for the command: this considers the worst case
807 	 * scenario where all prps are discontiguous, so get as many segments
808 	 * as we can have prps. In practice, most of the time, we will have
809 	 * far less PCI segments than prps.
810 	 */
811 	prp = le64_to_cpu(cmd->common.dptr.prp1);
812 	if (!prp)
813 		goto err_invalid_field;
814 
815 	ofst = nvmet_pci_epf_prp_ofst(ctrl, prp);
816 	nr_segs = (transfer_len + ofst + ctrl->mps - 1) >> ctrl->mps_shift;
817 
818 	ret = nvmet_pci_epf_alloc_iod_data_segs(iod, nr_segs);
819 	if (ret)
820 		goto err_internal;
821 
822 	/* Set the first segment using prp1. */
823 	seg = &iod->data_segs[0];
824 	seg->pci_addr = prp;
825 	seg->length = nvmet_pci_epf_prp_size(ctrl, prp);
826 
827 	size = seg->length;
828 	pci_addr = prp + size;
829 	nr_segs = 1;
830 
831 	/*
832 	 * Now build the PCI address segments using the PRP lists, starting
833 	 * from prp2.
834 	 */
835 	prp = le64_to_cpu(cmd->common.dptr.prp2);
836 	if (!prp)
837 		goto err_invalid_field;
838 
839 	while (size < transfer_len) {
840 		xfer_len = transfer_len - size;
841 
842 		if (!nr_prps) {
843 			nr_prps = nvmet_pci_epf_get_prp_list(ctrl, prp,
844 							     xfer_len, prps);
845 			if (nr_prps < 0)
846 				goto err_internal;
847 
848 			i = 0;
849 			ofst = 0;
850 		}
851 
852 		/* Current entry */
853 		prp = le64_to_cpu(prps[i]);
854 		if (!prp)
855 			goto err_invalid_field;
856 
857 		/* Did we reach the last PRP entry of the list? */
858 		if (xfer_len > ctrl->mps && i == nr_prps - 1) {
859 			/* We need more PRPs: PRP is a list pointer. */
860 			nr_prps = 0;
861 			continue;
862 		}
863 
864 		/* Only the first PRP is allowed to have an offset. */
865 		if (nvmet_pci_epf_prp_ofst(ctrl, prp))
866 			goto err_invalid_offset;
867 
868 		if (prp != pci_addr) {
869 			/* Discontiguous prp: new segment. */
870 			nr_segs++;
871 			if (WARN_ON_ONCE(nr_segs > iod->nr_data_segs))
872 				goto err_internal;
873 
874 			seg++;
875 			seg->pci_addr = prp;
876 			seg->length = 0;
877 			pci_addr = prp;
878 		}
879 
880 		prp_size = min_t(size_t, ctrl->mps, xfer_len);
881 		seg->length += prp_size;
882 		pci_addr += prp_size;
883 		size += prp_size;
884 
885 		i++;
886 	}
887 
888 	iod->nr_data_segs = nr_segs;
889 	ret = 0;
890 
891 	if (size != transfer_len) {
892 		dev_err(ctrl->dev,
893 			"PRPs transfer length mismatch: got %zu B, need %zu B\n",
894 			size, transfer_len);
895 		goto err_internal;
896 	}
897 
898 	kfree(prps);
899 
900 	return 0;
901 
902 err_invalid_offset:
903 	dev_err(ctrl->dev, "PRPs list invalid offset\n");
904 	iod->status = NVME_SC_PRP_INVALID_OFFSET | NVME_STATUS_DNR;
905 	goto err;
906 
907 err_invalid_field:
908 	dev_err(ctrl->dev, "PRPs list invalid field\n");
909 	iod->status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
910 	goto err;
911 
912 err_internal:
913 	dev_err(ctrl->dev, "PRPs list internal error\n");
914 	iod->status = NVME_SC_INTERNAL | NVME_STATUS_DNR;
915 
916 err:
917 	kfree(prps);
918 	return -EINVAL;
919 }
920 
nvmet_pci_epf_iod_parse_prp_simple(struct nvmet_pci_epf_ctrl * ctrl,struct nvmet_pci_epf_iod * iod)921 static int nvmet_pci_epf_iod_parse_prp_simple(struct nvmet_pci_epf_ctrl *ctrl,
922 					      struct nvmet_pci_epf_iod *iod)
923 {
924 	struct nvme_command *cmd = &iod->cmd;
925 	size_t transfer_len = iod->data_len;
926 	int ret, nr_segs = 1;
927 	u64 prp1, prp2 = 0;
928 	size_t prp1_size;
929 
930 	prp1 = le64_to_cpu(cmd->common.dptr.prp1);
931 	prp1_size = nvmet_pci_epf_prp_size(ctrl, prp1);
932 
933 	/* For commands crossing a page boundary, we should have prp2. */
934 	if (transfer_len > prp1_size) {
935 		prp2 = le64_to_cpu(cmd->common.dptr.prp2);
936 		if (!prp2) {
937 			iod->status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
938 			return -EINVAL;
939 		}
940 		if (nvmet_pci_epf_prp_ofst(ctrl, prp2)) {
941 			iod->status =
942 				NVME_SC_PRP_INVALID_OFFSET | NVME_STATUS_DNR;
943 			return -EINVAL;
944 		}
945 		if (prp2 != prp1 + prp1_size)
946 			nr_segs = 2;
947 	}
948 
949 	if (nr_segs == 1) {
950 		iod->nr_data_segs = 1;
951 		iod->data_segs = &iod->data_seg;
952 		iod->data_segs[0].pci_addr = prp1;
953 		iod->data_segs[0].length = transfer_len;
954 		return 0;
955 	}
956 
957 	ret = nvmet_pci_epf_alloc_iod_data_segs(iod, nr_segs);
958 	if (ret) {
959 		iod->status = NVME_SC_INTERNAL | NVME_STATUS_DNR;
960 		return ret;
961 	}
962 
963 	iod->data_segs[0].pci_addr = prp1;
964 	iod->data_segs[0].length = prp1_size;
965 	iod->data_segs[1].pci_addr = prp2;
966 	iod->data_segs[1].length = transfer_len - prp1_size;
967 
968 	return 0;
969 }
970 
nvmet_pci_epf_iod_parse_prps(struct nvmet_pci_epf_iod * iod)971 static int nvmet_pci_epf_iod_parse_prps(struct nvmet_pci_epf_iod *iod)
972 {
973 	struct nvmet_pci_epf_ctrl *ctrl = iod->ctrl;
974 	u64 prp1 = le64_to_cpu(iod->cmd.common.dptr.prp1);
975 	size_t ofst;
976 
977 	/* Get the PCI address segments for the command using its PRPs. */
978 	ofst = nvmet_pci_epf_prp_ofst(ctrl, prp1);
979 	if (ofst & 0x3) {
980 		iod->status = NVME_SC_PRP_INVALID_OFFSET | NVME_STATUS_DNR;
981 		return -EINVAL;
982 	}
983 
984 	if (iod->data_len + ofst <= ctrl->mps * 2)
985 		return nvmet_pci_epf_iod_parse_prp_simple(ctrl, iod);
986 
987 	return nvmet_pci_epf_iod_parse_prp_list(ctrl, iod);
988 }
989 
990 /*
991  * Transfer an SGL segment from the host and return the number of data
992  * descriptors and the next segment descriptor, if any.
993  */
994 static struct nvme_sgl_desc *
nvmet_pci_epf_get_sgl_segment(struct nvmet_pci_epf_ctrl * ctrl,struct nvme_sgl_desc * desc,unsigned int * nr_sgls)995 nvmet_pci_epf_get_sgl_segment(struct nvmet_pci_epf_ctrl *ctrl,
996 			      struct nvme_sgl_desc *desc, unsigned int *nr_sgls)
997 {
998 	struct nvme_sgl_desc *sgls;
999 	u32 length = le32_to_cpu(desc->length);
1000 	int nr_descs, ret;
1001 	void *buf;
1002 
1003 	buf = kmalloc(length, GFP_KERNEL);
1004 	if (!buf)
1005 		return NULL;
1006 
1007 	ret = nvmet_pci_epf_transfer(ctrl, buf, le64_to_cpu(desc->addr), length,
1008 				     DMA_FROM_DEVICE);
1009 	if (ret) {
1010 		kfree(buf);
1011 		return NULL;
1012 	}
1013 
1014 	sgls = buf;
1015 	nr_descs = length / sizeof(struct nvme_sgl_desc);
1016 	if (sgls[nr_descs - 1].type == (NVME_SGL_FMT_SEG_DESC << 4) ||
1017 	    sgls[nr_descs - 1].type == (NVME_SGL_FMT_LAST_SEG_DESC << 4)) {
1018 		/*
1019 		 * We have another SGL segment following this one: do not count
1020 		 * it as a regular data SGL descriptor and return it to the
1021 		 * caller.
1022 		 */
1023 		*desc = sgls[nr_descs - 1];
1024 		nr_descs--;
1025 	} else {
1026 		/* We do not have another SGL segment after this one. */
1027 		desc->length = 0;
1028 	}
1029 
1030 	*nr_sgls = nr_descs;
1031 
1032 	return sgls;
1033 }
1034 
nvmet_pci_epf_iod_parse_sgl_segments(struct nvmet_pci_epf_ctrl * ctrl,struct nvmet_pci_epf_iod * iod)1035 static int nvmet_pci_epf_iod_parse_sgl_segments(struct nvmet_pci_epf_ctrl *ctrl,
1036 						struct nvmet_pci_epf_iod *iod)
1037 {
1038 	struct nvme_command *cmd = &iod->cmd;
1039 	struct nvme_sgl_desc seg = cmd->common.dptr.sgl;
1040 	struct nvme_sgl_desc *sgls = NULL;
1041 	int n = 0, i, nr_sgls;
1042 	int ret;
1043 
1044 	/*
1045 	 * We do not support inline data nor keyed SGLs, so we should be seeing
1046 	 * only segment descriptors.
1047 	 */
1048 	if (seg.type != (NVME_SGL_FMT_SEG_DESC << 4) &&
1049 	    seg.type != (NVME_SGL_FMT_LAST_SEG_DESC << 4)) {
1050 		iod->status = NVME_SC_SGL_INVALID_TYPE | NVME_STATUS_DNR;
1051 		return -EIO;
1052 	}
1053 
1054 	while (seg.length) {
1055 		sgls = nvmet_pci_epf_get_sgl_segment(ctrl, &seg, &nr_sgls);
1056 		if (!sgls) {
1057 			iod->status = NVME_SC_INTERNAL | NVME_STATUS_DNR;
1058 			return -EIO;
1059 		}
1060 
1061 		/* Grow the PCI segment table as needed. */
1062 		ret = nvmet_pci_epf_alloc_iod_data_segs(iod, nr_sgls);
1063 		if (ret) {
1064 			iod->status = NVME_SC_INTERNAL | NVME_STATUS_DNR;
1065 			goto out;
1066 		}
1067 
1068 		/*
1069 		 * Parse the SGL descriptors to build the PCI segment table,
1070 		 * checking the descriptor type as we go.
1071 		 */
1072 		for (i = 0; i < nr_sgls; i++) {
1073 			if (sgls[i].type != (NVME_SGL_FMT_DATA_DESC << 4)) {
1074 				iod->status = NVME_SC_SGL_INVALID_TYPE |
1075 					NVME_STATUS_DNR;
1076 				goto out;
1077 			}
1078 			iod->data_segs[n].pci_addr = le64_to_cpu(sgls[i].addr);
1079 			iod->data_segs[n].length = le32_to_cpu(sgls[i].length);
1080 			n++;
1081 		}
1082 
1083 		kfree(sgls);
1084 	}
1085 
1086  out:
1087 	if (iod->status != NVME_SC_SUCCESS) {
1088 		kfree(sgls);
1089 		return -EIO;
1090 	}
1091 
1092 	return 0;
1093 }
1094 
nvmet_pci_epf_iod_parse_sgls(struct nvmet_pci_epf_iod * iod)1095 static int nvmet_pci_epf_iod_parse_sgls(struct nvmet_pci_epf_iod *iod)
1096 {
1097 	struct nvmet_pci_epf_ctrl *ctrl = iod->ctrl;
1098 	struct nvme_sgl_desc *sgl = &iod->cmd.common.dptr.sgl;
1099 
1100 	if (sgl->type == (NVME_SGL_FMT_DATA_DESC << 4)) {
1101 		/* Single data descriptor case. */
1102 		iod->nr_data_segs = 1;
1103 		iod->data_segs = &iod->data_seg;
1104 		iod->data_seg.pci_addr = le64_to_cpu(sgl->addr);
1105 		iod->data_seg.length = le32_to_cpu(sgl->length);
1106 		return 0;
1107 	}
1108 
1109 	return nvmet_pci_epf_iod_parse_sgl_segments(ctrl, iod);
1110 }
1111 
nvmet_pci_epf_alloc_iod_data_buf(struct nvmet_pci_epf_iod * iod)1112 static int nvmet_pci_epf_alloc_iod_data_buf(struct nvmet_pci_epf_iod *iod)
1113 {
1114 	struct nvmet_pci_epf_ctrl *ctrl = iod->ctrl;
1115 	struct nvmet_req *req = &iod->req;
1116 	struct nvmet_pci_epf_segment *seg;
1117 	struct scatterlist *sg;
1118 	int ret, i;
1119 
1120 	if (iod->data_len > ctrl->mdts) {
1121 		iod->status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1122 		return -EINVAL;
1123 	}
1124 
1125 	/*
1126 	 * Get the PCI address segments for the command data buffer using either
1127 	 * its SGLs or PRPs.
1128 	 */
1129 	if (iod->cmd.common.flags & NVME_CMD_SGL_ALL)
1130 		ret = nvmet_pci_epf_iod_parse_sgls(iod);
1131 	else
1132 		ret = nvmet_pci_epf_iod_parse_prps(iod);
1133 	if (ret)
1134 		return ret;
1135 
1136 	/* Get a command buffer using SGLs matching the PCI segments. */
1137 	if (iod->nr_data_segs == 1) {
1138 		sg_init_table(&iod->data_sgl, 1);
1139 		iod->data_sgt.sgl = &iod->data_sgl;
1140 		iod->data_sgt.nents = 1;
1141 		iod->data_sgt.orig_nents = 1;
1142 	} else {
1143 		ret = sg_alloc_table(&iod->data_sgt, iod->nr_data_segs,
1144 				     GFP_KERNEL);
1145 		if (ret)
1146 			goto err_nomem;
1147 	}
1148 
1149 	for_each_sgtable_sg(&iod->data_sgt, sg, i) {
1150 		seg = &iod->data_segs[i];
1151 		seg->buf = kmalloc(seg->length, GFP_KERNEL);
1152 		if (!seg->buf)
1153 			goto err_nomem;
1154 		sg_set_buf(sg, seg->buf, seg->length);
1155 	}
1156 
1157 	req->transfer_len = iod->data_len;
1158 	req->sg = iod->data_sgt.sgl;
1159 	req->sg_cnt = iod->data_sgt.nents;
1160 
1161 	return 0;
1162 
1163 err_nomem:
1164 	iod->status = NVME_SC_INTERNAL | NVME_STATUS_DNR;
1165 	return -ENOMEM;
1166 }
1167 
nvmet_pci_epf_complete_iod(struct nvmet_pci_epf_iod * iod)1168 static void nvmet_pci_epf_complete_iod(struct nvmet_pci_epf_iod *iod)
1169 {
1170 	struct nvmet_pci_epf_queue *cq = iod->cq;
1171 	unsigned long flags;
1172 
1173 	/* Print an error message for failed commands, except AENs. */
1174 	iod->status = le16_to_cpu(iod->cqe.status) >> 1;
1175 	if (iod->status && iod->cmd.common.opcode != nvme_admin_async_event)
1176 		dev_err(iod->ctrl->dev,
1177 			"CQ[%d]: Command %s (0x%x) status 0x%0x\n",
1178 			iod->sq->qid, nvmet_pci_epf_iod_name(iod),
1179 			iod->cmd.common.opcode, iod->status);
1180 
1181 	/*
1182 	 * Add the command to the list of completed commands and schedule the
1183 	 * CQ work.
1184 	 */
1185 	spin_lock_irqsave(&cq->lock, flags);
1186 	list_add_tail(&iod->link, &cq->list);
1187 	queue_delayed_work(system_highpri_wq, &cq->work, 0);
1188 	spin_unlock_irqrestore(&cq->lock, flags);
1189 }
1190 
nvmet_pci_epf_drain_queue(struct nvmet_pci_epf_queue * queue)1191 static void nvmet_pci_epf_drain_queue(struct nvmet_pci_epf_queue *queue)
1192 {
1193 	struct nvmet_pci_epf_iod *iod;
1194 	unsigned long flags;
1195 
1196 	spin_lock_irqsave(&queue->lock, flags);
1197 	while (!list_empty(&queue->list)) {
1198 		iod = list_first_entry(&queue->list, struct nvmet_pci_epf_iod,
1199 				       link);
1200 		list_del_init(&iod->link);
1201 		nvmet_pci_epf_free_iod(iod);
1202 	}
1203 	spin_unlock_irqrestore(&queue->lock, flags);
1204 }
1205 
nvmet_pci_epf_add_port(struct nvmet_port * port)1206 static int nvmet_pci_epf_add_port(struct nvmet_port *port)
1207 {
1208 	mutex_lock(&nvmet_pci_epf_ports_mutex);
1209 	list_add_tail(&port->entry, &nvmet_pci_epf_ports);
1210 	mutex_unlock(&nvmet_pci_epf_ports_mutex);
1211 	return 0;
1212 }
1213 
nvmet_pci_epf_remove_port(struct nvmet_port * port)1214 static void nvmet_pci_epf_remove_port(struct nvmet_port *port)
1215 {
1216 	mutex_lock(&nvmet_pci_epf_ports_mutex);
1217 	list_del_init(&port->entry);
1218 	mutex_unlock(&nvmet_pci_epf_ports_mutex);
1219 }
1220 
1221 static struct nvmet_port *
nvmet_pci_epf_find_port(struct nvmet_pci_epf_ctrl * ctrl,__le16 portid)1222 nvmet_pci_epf_find_port(struct nvmet_pci_epf_ctrl *ctrl, __le16 portid)
1223 {
1224 	struct nvmet_port *p, *port = NULL;
1225 
1226 	mutex_lock(&nvmet_pci_epf_ports_mutex);
1227 	list_for_each_entry(p, &nvmet_pci_epf_ports, entry) {
1228 		if (p->disc_addr.portid == portid) {
1229 			port = p;
1230 			break;
1231 		}
1232 	}
1233 	mutex_unlock(&nvmet_pci_epf_ports_mutex);
1234 
1235 	return port;
1236 }
1237 
nvmet_pci_epf_queue_response(struct nvmet_req * req)1238 static void nvmet_pci_epf_queue_response(struct nvmet_req *req)
1239 {
1240 	struct nvmet_pci_epf_iod *iod =
1241 		container_of(req, struct nvmet_pci_epf_iod, req);
1242 
1243 	iod->status = le16_to_cpu(req->cqe->status) >> 1;
1244 
1245 	/* If we have no data to transfer, directly complete the command. */
1246 	if (!iod->data_len || iod->dma_dir != DMA_TO_DEVICE) {
1247 		nvmet_pci_epf_complete_iod(iod);
1248 		return;
1249 	}
1250 
1251 	complete(&iod->done);
1252 }
1253 
nvmet_pci_epf_get_mdts(const struct nvmet_ctrl * tctrl)1254 static u8 nvmet_pci_epf_get_mdts(const struct nvmet_ctrl *tctrl)
1255 {
1256 	struct nvmet_pci_epf_ctrl *ctrl = tctrl->drvdata;
1257 	int page_shift = NVME_CAP_MPSMIN(tctrl->cap) + 12;
1258 
1259 	return ilog2(ctrl->mdts) - page_shift;
1260 }
1261 
nvmet_pci_epf_create_cq(struct nvmet_ctrl * tctrl,u16 cqid,u16 flags,u16 qsize,u64 pci_addr,u16 vector)1262 static u16 nvmet_pci_epf_create_cq(struct nvmet_ctrl *tctrl,
1263 		u16 cqid, u16 flags, u16 qsize, u64 pci_addr, u16 vector)
1264 {
1265 	struct nvmet_pci_epf_ctrl *ctrl = tctrl->drvdata;
1266 	struct nvmet_pci_epf_queue *cq = &ctrl->cq[cqid];
1267 	u16 status;
1268 	int ret;
1269 
1270 	if (test_bit(NVMET_PCI_EPF_Q_LIVE, &cq->flags))
1271 		return NVME_SC_QID_INVALID | NVME_STATUS_DNR;
1272 
1273 	if (!(flags & NVME_QUEUE_PHYS_CONTIG))
1274 		return NVME_SC_INVALID_QUEUE | NVME_STATUS_DNR;
1275 
1276 	cq->pci_addr = pci_addr;
1277 	cq->qid = cqid;
1278 	cq->depth = qsize + 1;
1279 	cq->vector = vector;
1280 	cq->head = 0;
1281 	cq->tail = 0;
1282 	cq->phase = 1;
1283 	cq->db = NVME_REG_DBS + (((cqid * 2) + 1) * sizeof(u32));
1284 	nvmet_pci_epf_bar_write32(ctrl, cq->db, 0);
1285 
1286 	if (!cqid)
1287 		cq->qes = sizeof(struct nvme_completion);
1288 	else
1289 		cq->qes = ctrl->io_cqes;
1290 	cq->pci_size = cq->qes * cq->depth;
1291 
1292 	if (flags & NVME_CQ_IRQ_ENABLED) {
1293 		cq->iv = nvmet_pci_epf_add_irq_vector(ctrl, vector);
1294 		if (!cq->iv)
1295 			return NVME_SC_INTERNAL | NVME_STATUS_DNR;
1296 		set_bit(NVMET_PCI_EPF_Q_IRQ_ENABLED, &cq->flags);
1297 	}
1298 
1299 	status = nvmet_cq_create(tctrl, &cq->nvme_cq, cqid, cq->depth);
1300 	if (status != NVME_SC_SUCCESS)
1301 		goto err;
1302 
1303 	/*
1304 	 * Map the CQ PCI address space and since PCI endpoint controllers may
1305 	 * return a partial mapping, check that the mapping is large enough.
1306 	 */
1307 	ret = nvmet_pci_epf_mem_map(ctrl->nvme_epf, cq->pci_addr, cq->pci_size,
1308 				    &cq->pci_map);
1309 	if (ret) {
1310 		dev_err(ctrl->dev, "Failed to map CQ %u (err=%d)\n",
1311 			cq->qid, ret);
1312 		goto err_internal;
1313 	}
1314 
1315 	if (cq->pci_map.pci_size < cq->pci_size) {
1316 		dev_err(ctrl->dev, "Invalid partial mapping of queue %u\n",
1317 			cq->qid);
1318 		goto err_unmap_queue;
1319 	}
1320 
1321 	set_bit(NVMET_PCI_EPF_Q_LIVE, &cq->flags);
1322 
1323 	if (test_bit(NVMET_PCI_EPF_Q_IRQ_ENABLED, &cq->flags))
1324 		dev_dbg(ctrl->dev,
1325 			"CQ[%u]: %u entries of %zu B, IRQ vector %u\n",
1326 			cqid, qsize, cq->qes, cq->vector);
1327 	else
1328 		dev_dbg(ctrl->dev,
1329 			"CQ[%u]: %u entries of %zu B, IRQ disabled\n",
1330 			cqid, qsize, cq->qes);
1331 
1332 	return NVME_SC_SUCCESS;
1333 
1334 err_unmap_queue:
1335 	nvmet_pci_epf_mem_unmap(ctrl->nvme_epf, &cq->pci_map);
1336 err_internal:
1337 	status = NVME_SC_INTERNAL | NVME_STATUS_DNR;
1338 err:
1339 	if (test_and_clear_bit(NVMET_PCI_EPF_Q_IRQ_ENABLED, &cq->flags))
1340 		nvmet_pci_epf_remove_irq_vector(ctrl, cq->vector);
1341 	return status;
1342 }
1343 
nvmet_pci_epf_delete_cq(struct nvmet_ctrl * tctrl,u16 cqid)1344 static u16 nvmet_pci_epf_delete_cq(struct nvmet_ctrl *tctrl, u16 cqid)
1345 {
1346 	struct nvmet_pci_epf_ctrl *ctrl = tctrl->drvdata;
1347 	struct nvmet_pci_epf_queue *cq = &ctrl->cq[cqid];
1348 
1349 	if (!test_and_clear_bit(NVMET_PCI_EPF_Q_LIVE, &cq->flags))
1350 		return NVME_SC_QID_INVALID | NVME_STATUS_DNR;
1351 
1352 	cancel_delayed_work_sync(&cq->work);
1353 	nvmet_pci_epf_drain_queue(cq);
1354 	if (test_and_clear_bit(NVMET_PCI_EPF_Q_IRQ_ENABLED, &cq->flags))
1355 		nvmet_pci_epf_remove_irq_vector(ctrl, cq->vector);
1356 	nvmet_pci_epf_mem_unmap(ctrl->nvme_epf, &cq->pci_map);
1357 
1358 	return NVME_SC_SUCCESS;
1359 }
1360 
nvmet_pci_epf_create_sq(struct nvmet_ctrl * tctrl,u16 sqid,u16 flags,u16 qsize,u64 pci_addr)1361 static u16 nvmet_pci_epf_create_sq(struct nvmet_ctrl *tctrl,
1362 		u16 sqid, u16 flags, u16 qsize, u64 pci_addr)
1363 {
1364 	struct nvmet_pci_epf_ctrl *ctrl = tctrl->drvdata;
1365 	struct nvmet_pci_epf_queue *sq = &ctrl->sq[sqid];
1366 	u16 status;
1367 
1368 	if (test_bit(NVMET_PCI_EPF_Q_LIVE, &sq->flags))
1369 		return NVME_SC_QID_INVALID | NVME_STATUS_DNR;
1370 
1371 	if (!(flags & NVME_QUEUE_PHYS_CONTIG))
1372 		return NVME_SC_INVALID_QUEUE | NVME_STATUS_DNR;
1373 
1374 	sq->pci_addr = pci_addr;
1375 	sq->qid = sqid;
1376 	sq->depth = qsize + 1;
1377 	sq->head = 0;
1378 	sq->tail = 0;
1379 	sq->phase = 0;
1380 	sq->db = NVME_REG_DBS + (sqid * 2 * sizeof(u32));
1381 	nvmet_pci_epf_bar_write32(ctrl, sq->db, 0);
1382 	if (!sqid)
1383 		sq->qes = 1UL << NVME_ADM_SQES;
1384 	else
1385 		sq->qes = ctrl->io_sqes;
1386 	sq->pci_size = sq->qes * sq->depth;
1387 
1388 	status = nvmet_sq_create(tctrl, &sq->nvme_sq, sqid, sq->depth);
1389 	if (status != NVME_SC_SUCCESS)
1390 		return status;
1391 
1392 	sq->iod_wq = alloc_workqueue("sq%d_wq", WQ_UNBOUND,
1393 				min_t(int, sq->depth, WQ_MAX_ACTIVE), sqid);
1394 	if (!sq->iod_wq) {
1395 		dev_err(ctrl->dev, "Failed to create SQ %d work queue\n", sqid);
1396 		status = NVME_SC_INTERNAL | NVME_STATUS_DNR;
1397 		goto out_destroy_sq;
1398 	}
1399 
1400 	set_bit(NVMET_PCI_EPF_Q_LIVE, &sq->flags);
1401 
1402 	dev_dbg(ctrl->dev, "SQ[%u]: %u entries of %zu B\n",
1403 		sqid, qsize, sq->qes);
1404 
1405 	return NVME_SC_SUCCESS;
1406 
1407 out_destroy_sq:
1408 	nvmet_sq_destroy(&sq->nvme_sq);
1409 	return status;
1410 }
1411 
nvmet_pci_epf_delete_sq(struct nvmet_ctrl * tctrl,u16 sqid)1412 static u16 nvmet_pci_epf_delete_sq(struct nvmet_ctrl *tctrl, u16 sqid)
1413 {
1414 	struct nvmet_pci_epf_ctrl *ctrl = tctrl->drvdata;
1415 	struct nvmet_pci_epf_queue *sq = &ctrl->sq[sqid];
1416 
1417 	if (!test_and_clear_bit(NVMET_PCI_EPF_Q_LIVE, &sq->flags))
1418 		return NVME_SC_QID_INVALID | NVME_STATUS_DNR;
1419 
1420 	destroy_workqueue(sq->iod_wq);
1421 	sq->iod_wq = NULL;
1422 
1423 	nvmet_pci_epf_drain_queue(sq);
1424 
1425 	if (sq->nvme_sq.ctrl)
1426 		nvmet_sq_destroy(&sq->nvme_sq);
1427 
1428 	return NVME_SC_SUCCESS;
1429 }
1430 
nvmet_pci_epf_get_feat(const struct nvmet_ctrl * tctrl,u8 feat,void * data)1431 static u16 nvmet_pci_epf_get_feat(const struct nvmet_ctrl *tctrl,
1432 				  u8 feat, void *data)
1433 {
1434 	struct nvmet_pci_epf_ctrl *ctrl = tctrl->drvdata;
1435 	struct nvmet_feat_arbitration *arb;
1436 	struct nvmet_feat_irq_coalesce *irqc;
1437 	struct nvmet_feat_irq_config *irqcfg;
1438 	struct nvmet_pci_epf_irq_vector *iv;
1439 	u16 status;
1440 
1441 	switch (feat) {
1442 	case NVME_FEAT_ARBITRATION:
1443 		arb = data;
1444 		if (!ctrl->sq_ab)
1445 			arb->ab = 0x7;
1446 		else
1447 			arb->ab = ilog2(ctrl->sq_ab);
1448 		return NVME_SC_SUCCESS;
1449 
1450 	case NVME_FEAT_IRQ_COALESCE:
1451 		irqc = data;
1452 		irqc->thr = ctrl->irq_vector_threshold;
1453 		irqc->time = 0;
1454 		return NVME_SC_SUCCESS;
1455 
1456 	case NVME_FEAT_IRQ_CONFIG:
1457 		irqcfg = data;
1458 		mutex_lock(&ctrl->irq_lock);
1459 		iv = nvmet_pci_epf_find_irq_vector(ctrl, irqcfg->iv);
1460 		if (iv) {
1461 			irqcfg->cd = iv->cd;
1462 			status = NVME_SC_SUCCESS;
1463 		} else {
1464 			status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1465 		}
1466 		mutex_unlock(&ctrl->irq_lock);
1467 		return status;
1468 
1469 	default:
1470 		return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1471 	}
1472 }
1473 
nvmet_pci_epf_set_feat(const struct nvmet_ctrl * tctrl,u8 feat,void * data)1474 static u16 nvmet_pci_epf_set_feat(const struct nvmet_ctrl *tctrl,
1475 				  u8 feat, void *data)
1476 {
1477 	struct nvmet_pci_epf_ctrl *ctrl = tctrl->drvdata;
1478 	struct nvmet_feat_arbitration *arb;
1479 	struct nvmet_feat_irq_coalesce *irqc;
1480 	struct nvmet_feat_irq_config *irqcfg;
1481 	struct nvmet_pci_epf_irq_vector *iv;
1482 	u16 status;
1483 
1484 	switch (feat) {
1485 	case NVME_FEAT_ARBITRATION:
1486 		arb = data;
1487 		if (arb->ab == 0x7)
1488 			ctrl->sq_ab = 0;
1489 		else
1490 			ctrl->sq_ab = 1 << arb->ab;
1491 		return NVME_SC_SUCCESS;
1492 
1493 	case NVME_FEAT_IRQ_COALESCE:
1494 		/*
1495 		 * Since we do not implement precise IRQ coalescing timing,
1496 		 * ignore the time field.
1497 		 */
1498 		irqc = data;
1499 		ctrl->irq_vector_threshold = irqc->thr + 1;
1500 		return NVME_SC_SUCCESS;
1501 
1502 	case NVME_FEAT_IRQ_CONFIG:
1503 		irqcfg = data;
1504 		mutex_lock(&ctrl->irq_lock);
1505 		iv = nvmet_pci_epf_find_irq_vector(ctrl, irqcfg->iv);
1506 		if (iv) {
1507 			iv->cd = irqcfg->cd;
1508 			status = NVME_SC_SUCCESS;
1509 		} else {
1510 			status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1511 		}
1512 		mutex_unlock(&ctrl->irq_lock);
1513 		return status;
1514 
1515 	default:
1516 		return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1517 	}
1518 }
1519 
1520 static const struct nvmet_fabrics_ops nvmet_pci_epf_fabrics_ops = {
1521 	.owner		= THIS_MODULE,
1522 	.type		= NVMF_TRTYPE_PCI,
1523 	.add_port	= nvmet_pci_epf_add_port,
1524 	.remove_port	= nvmet_pci_epf_remove_port,
1525 	.queue_response = nvmet_pci_epf_queue_response,
1526 	.get_mdts	= nvmet_pci_epf_get_mdts,
1527 	.create_cq	= nvmet_pci_epf_create_cq,
1528 	.delete_cq	= nvmet_pci_epf_delete_cq,
1529 	.create_sq	= nvmet_pci_epf_create_sq,
1530 	.delete_sq	= nvmet_pci_epf_delete_sq,
1531 	.get_feature	= nvmet_pci_epf_get_feat,
1532 	.set_feature	= nvmet_pci_epf_set_feat,
1533 };
1534 
1535 static void nvmet_pci_epf_cq_work(struct work_struct *work);
1536 
nvmet_pci_epf_init_queue(struct nvmet_pci_epf_ctrl * ctrl,unsigned int qid,bool sq)1537 static void nvmet_pci_epf_init_queue(struct nvmet_pci_epf_ctrl *ctrl,
1538 				     unsigned int qid, bool sq)
1539 {
1540 	struct nvmet_pci_epf_queue *queue;
1541 
1542 	if (sq) {
1543 		queue = &ctrl->sq[qid];
1544 	} else {
1545 		queue = &ctrl->cq[qid];
1546 		INIT_DELAYED_WORK(&queue->work, nvmet_pci_epf_cq_work);
1547 	}
1548 	queue->ctrl = ctrl;
1549 	queue->qid = qid;
1550 	spin_lock_init(&queue->lock);
1551 	INIT_LIST_HEAD(&queue->list);
1552 }
1553 
nvmet_pci_epf_alloc_queues(struct nvmet_pci_epf_ctrl * ctrl)1554 static int nvmet_pci_epf_alloc_queues(struct nvmet_pci_epf_ctrl *ctrl)
1555 {
1556 	unsigned int qid;
1557 
1558 	ctrl->sq = kcalloc(ctrl->nr_queues,
1559 			   sizeof(struct nvmet_pci_epf_queue), GFP_KERNEL);
1560 	if (!ctrl->sq)
1561 		return -ENOMEM;
1562 
1563 	ctrl->cq = kcalloc(ctrl->nr_queues,
1564 			   sizeof(struct nvmet_pci_epf_queue), GFP_KERNEL);
1565 	if (!ctrl->cq) {
1566 		kfree(ctrl->sq);
1567 		ctrl->sq = NULL;
1568 		return -ENOMEM;
1569 	}
1570 
1571 	for (qid = 0; qid < ctrl->nr_queues; qid++) {
1572 		nvmet_pci_epf_init_queue(ctrl, qid, true);
1573 		nvmet_pci_epf_init_queue(ctrl, qid, false);
1574 	}
1575 
1576 	return 0;
1577 }
1578 
nvmet_pci_epf_free_queues(struct nvmet_pci_epf_ctrl * ctrl)1579 static void nvmet_pci_epf_free_queues(struct nvmet_pci_epf_ctrl *ctrl)
1580 {
1581 	kfree(ctrl->sq);
1582 	ctrl->sq = NULL;
1583 	kfree(ctrl->cq);
1584 	ctrl->cq = NULL;
1585 }
1586 
nvmet_pci_epf_exec_iod_work(struct work_struct * work)1587 static void nvmet_pci_epf_exec_iod_work(struct work_struct *work)
1588 {
1589 	struct nvmet_pci_epf_iod *iod =
1590 		container_of(work, struct nvmet_pci_epf_iod, work);
1591 	struct nvmet_req *req = &iod->req;
1592 	int ret;
1593 
1594 	if (!iod->ctrl->link_up) {
1595 		nvmet_pci_epf_free_iod(iod);
1596 		return;
1597 	}
1598 
1599 	if (!test_bit(NVMET_PCI_EPF_Q_LIVE, &iod->sq->flags)) {
1600 		iod->status = NVME_SC_QID_INVALID | NVME_STATUS_DNR;
1601 		goto complete;
1602 	}
1603 
1604 	if (!nvmet_req_init(req, &iod->cq->nvme_cq, &iod->sq->nvme_sq,
1605 			    &nvmet_pci_epf_fabrics_ops))
1606 		goto complete;
1607 
1608 	iod->data_len = nvmet_req_transfer_len(req);
1609 	if (iod->data_len) {
1610 		/*
1611 		 * Get the data DMA transfer direction. Here "device" means the
1612 		 * PCI root-complex host.
1613 		 */
1614 		if (nvme_is_write(&iod->cmd))
1615 			iod->dma_dir = DMA_FROM_DEVICE;
1616 		else
1617 			iod->dma_dir = DMA_TO_DEVICE;
1618 
1619 		/*
1620 		 * Setup the command data buffer and get the command data from
1621 		 * the host if needed.
1622 		 */
1623 		ret = nvmet_pci_epf_alloc_iod_data_buf(iod);
1624 		if (!ret && iod->dma_dir == DMA_FROM_DEVICE)
1625 			ret = nvmet_pci_epf_transfer_iod_data(iod);
1626 		if (ret) {
1627 			nvmet_req_uninit(req);
1628 			goto complete;
1629 		}
1630 	}
1631 
1632 	req->execute(req);
1633 
1634 	/*
1635 	 * If we do not have data to transfer after the command execution
1636 	 * finishes, nvmet_pci_epf_queue_response() will complete the command
1637 	 * directly. No need to wait for the completion in this case.
1638 	 */
1639 	if (!iod->data_len || iod->dma_dir != DMA_TO_DEVICE)
1640 		return;
1641 
1642 	wait_for_completion(&iod->done);
1643 
1644 	if (iod->status == NVME_SC_SUCCESS) {
1645 		WARN_ON_ONCE(!iod->data_len || iod->dma_dir != DMA_TO_DEVICE);
1646 		nvmet_pci_epf_transfer_iod_data(iod);
1647 	}
1648 
1649 complete:
1650 	nvmet_pci_epf_complete_iod(iod);
1651 }
1652 
nvmet_pci_epf_process_sq(struct nvmet_pci_epf_ctrl * ctrl,struct nvmet_pci_epf_queue * sq)1653 static int nvmet_pci_epf_process_sq(struct nvmet_pci_epf_ctrl *ctrl,
1654 				    struct nvmet_pci_epf_queue *sq)
1655 {
1656 	struct nvmet_pci_epf_iod *iod;
1657 	int ret, n = 0;
1658 	u16 head = sq->head;
1659 
1660 	sq->tail = nvmet_pci_epf_bar_read32(ctrl, sq->db);
1661 	while (head != sq->tail && (!ctrl->sq_ab || n < ctrl->sq_ab)) {
1662 		iod = nvmet_pci_epf_alloc_iod(sq);
1663 		if (!iod)
1664 			break;
1665 
1666 		/* Get the NVMe command submitted by the host. */
1667 		ret = nvmet_pci_epf_transfer(ctrl, &iod->cmd,
1668 					     sq->pci_addr + head * sq->qes,
1669 					     sq->qes, DMA_FROM_DEVICE);
1670 		if (ret) {
1671 			/* Not much we can do... */
1672 			nvmet_pci_epf_free_iod(iod);
1673 			break;
1674 		}
1675 
1676 		dev_dbg(ctrl->dev, "SQ[%u]: head %u, tail %u, command %s\n",
1677 			sq->qid, head, sq->tail,
1678 			nvmet_pci_epf_iod_name(iod));
1679 
1680 		head++;
1681 		if (head == sq->depth)
1682 			head = 0;
1683 		WRITE_ONCE(sq->head, head);
1684 		n++;
1685 
1686 		queue_work_on(WORK_CPU_UNBOUND, sq->iod_wq, &iod->work);
1687 
1688 		sq->tail = nvmet_pci_epf_bar_read32(ctrl, sq->db);
1689 	}
1690 
1691 	return n;
1692 }
1693 
nvmet_pci_epf_poll_sqs_work(struct work_struct * work)1694 static void nvmet_pci_epf_poll_sqs_work(struct work_struct *work)
1695 {
1696 	struct nvmet_pci_epf_ctrl *ctrl =
1697 		container_of(work, struct nvmet_pci_epf_ctrl, poll_sqs.work);
1698 	struct nvmet_pci_epf_queue *sq;
1699 	unsigned long limit = jiffies;
1700 	unsigned long last = 0;
1701 	int i, nr_sqs;
1702 
1703 	while (ctrl->link_up && ctrl->enabled) {
1704 		nr_sqs = 0;
1705 		/* Do round-robin arbitration. */
1706 		for (i = 0; i < ctrl->nr_queues; i++) {
1707 			sq = &ctrl->sq[i];
1708 			if (!test_bit(NVMET_PCI_EPF_Q_LIVE, &sq->flags))
1709 				continue;
1710 			if (nvmet_pci_epf_process_sq(ctrl, sq))
1711 				nr_sqs++;
1712 		}
1713 
1714 		/*
1715 		 * If we have been running for a while, reschedule to let other
1716 		 * tasks run and to avoid RCU stalls.
1717 		 */
1718 		if (time_is_before_jiffies(limit + secs_to_jiffies(1))) {
1719 			cond_resched();
1720 			limit = jiffies;
1721 			continue;
1722 		}
1723 
1724 		if (nr_sqs) {
1725 			last = jiffies;
1726 			continue;
1727 		}
1728 
1729 		/*
1730 		 * If we have not received any command on any queue for more
1731 		 * than NVMET_PCI_EPF_SQ_POLL_IDLE, assume we are idle and
1732 		 * reschedule. This avoids "burning" a CPU when the controller
1733 		 * is idle for a long time.
1734 		 */
1735 		if (time_is_before_jiffies(last + NVMET_PCI_EPF_SQ_POLL_IDLE))
1736 			break;
1737 
1738 		cpu_relax();
1739 	}
1740 
1741 	schedule_delayed_work(&ctrl->poll_sqs, NVMET_PCI_EPF_SQ_POLL_INTERVAL);
1742 }
1743 
nvmet_pci_epf_cq_work(struct work_struct * work)1744 static void nvmet_pci_epf_cq_work(struct work_struct *work)
1745 {
1746 	struct nvmet_pci_epf_queue *cq =
1747 		container_of(work, struct nvmet_pci_epf_queue, work.work);
1748 	struct nvmet_pci_epf_ctrl *ctrl = cq->ctrl;
1749 	struct nvme_completion *cqe;
1750 	struct nvmet_pci_epf_iod *iod;
1751 	unsigned long flags;
1752 	int ret = 0, n = 0;
1753 
1754 	while (test_bit(NVMET_PCI_EPF_Q_LIVE, &cq->flags) && ctrl->link_up) {
1755 
1756 		/* Check that the CQ is not full. */
1757 		cq->head = nvmet_pci_epf_bar_read32(ctrl, cq->db);
1758 		if (cq->head == cq->tail + 1) {
1759 			ret = -EAGAIN;
1760 			break;
1761 		}
1762 
1763 		spin_lock_irqsave(&cq->lock, flags);
1764 		iod = list_first_entry_or_null(&cq->list,
1765 					       struct nvmet_pci_epf_iod, link);
1766 		if (iod)
1767 			list_del_init(&iod->link);
1768 		spin_unlock_irqrestore(&cq->lock, flags);
1769 
1770 		if (!iod)
1771 			break;
1772 
1773 		/*
1774 		 * Post the IOD completion entry. If the IOD request was
1775 		 * executed (req->execute() called), the CQE is already
1776 		 * initialized. However, the IOD may have been failed before
1777 		 * that, leaving the CQE not properly initialized. So always
1778 		 * initialize it here.
1779 		 */
1780 		cqe = &iod->cqe;
1781 		cqe->sq_head = cpu_to_le16(READ_ONCE(iod->sq->head));
1782 		cqe->sq_id = cpu_to_le16(iod->sq->qid);
1783 		cqe->command_id = iod->cmd.common.command_id;
1784 		cqe->status = cpu_to_le16((iod->status << 1) | cq->phase);
1785 
1786 		dev_dbg(ctrl->dev,
1787 			"CQ[%u]: %s status 0x%x, result 0x%llx, head %u, tail %u, phase %u\n",
1788 			cq->qid, nvmet_pci_epf_iod_name(iod), iod->status,
1789 			le64_to_cpu(cqe->result.u64), cq->head, cq->tail,
1790 			cq->phase);
1791 
1792 		memcpy_toio(cq->pci_map.virt_addr + cq->tail * cq->qes,
1793 			    cqe, cq->qes);
1794 
1795 		cq->tail++;
1796 		if (cq->tail >= cq->depth) {
1797 			cq->tail = 0;
1798 			cq->phase ^= 1;
1799 		}
1800 
1801 		nvmet_pci_epf_free_iod(iod);
1802 
1803 		/* Signal the host. */
1804 		nvmet_pci_epf_raise_irq(ctrl, cq, false);
1805 		n++;
1806 	}
1807 
1808 	/*
1809 	 * We do not support precise IRQ coalescing time (100ns units as per
1810 	 * NVMe specifications). So if we have posted completion entries without
1811 	 * reaching the interrupt coalescing threshold, raise an interrupt.
1812 	 */
1813 	if (n)
1814 		nvmet_pci_epf_raise_irq(ctrl, cq, true);
1815 
1816 	if (ret < 0)
1817 		queue_delayed_work(system_highpri_wq, &cq->work,
1818 				   NVMET_PCI_EPF_CQ_RETRY_INTERVAL);
1819 }
1820 
nvmet_pci_epf_clear_ctrl_config(struct nvmet_pci_epf_ctrl * ctrl)1821 static void nvmet_pci_epf_clear_ctrl_config(struct nvmet_pci_epf_ctrl *ctrl)
1822 {
1823 	struct nvmet_ctrl *tctrl = ctrl->tctrl;
1824 
1825 	/* Initialize controller status. */
1826 	tctrl->csts = 0;
1827 	ctrl->csts = 0;
1828 	nvmet_pci_epf_bar_write32(ctrl, NVME_REG_CSTS, ctrl->csts);
1829 
1830 	/* Initialize controller configuration and start polling. */
1831 	tctrl->cc = 0;
1832 	ctrl->cc = 0;
1833 	nvmet_pci_epf_bar_write32(ctrl, NVME_REG_CC, ctrl->cc);
1834 }
1835 
nvmet_pci_epf_enable_ctrl(struct nvmet_pci_epf_ctrl * ctrl)1836 static int nvmet_pci_epf_enable_ctrl(struct nvmet_pci_epf_ctrl *ctrl)
1837 {
1838 	u64 pci_addr, asq, acq;
1839 	u32 aqa;
1840 	u16 status, qsize;
1841 
1842 	if (ctrl->enabled)
1843 		return 0;
1844 
1845 	dev_info(ctrl->dev, "Enabling controller\n");
1846 
1847 	ctrl->mps_shift = nvmet_cc_mps(ctrl->cc) + 12;
1848 	ctrl->mps = 1UL << ctrl->mps_shift;
1849 	ctrl->mps_mask = ctrl->mps - 1;
1850 
1851 	ctrl->io_sqes = 1UL << nvmet_cc_iosqes(ctrl->cc);
1852 	if (ctrl->io_sqes < sizeof(struct nvme_command)) {
1853 		dev_err(ctrl->dev, "Unsupported I/O SQES %zu (need %zu)\n",
1854 			ctrl->io_sqes, sizeof(struct nvme_command));
1855 		goto err;
1856 	}
1857 
1858 	ctrl->io_cqes = 1UL << nvmet_cc_iocqes(ctrl->cc);
1859 	if (ctrl->io_cqes < sizeof(struct nvme_completion)) {
1860 		dev_err(ctrl->dev, "Unsupported I/O CQES %zu (need %zu)\n",
1861 			ctrl->io_sqes, sizeof(struct nvme_completion));
1862 		goto err;
1863 	}
1864 
1865 	/* Create the admin queue. */
1866 	aqa = nvmet_pci_epf_bar_read32(ctrl, NVME_REG_AQA);
1867 	asq = nvmet_pci_epf_bar_read64(ctrl, NVME_REG_ASQ);
1868 	acq = nvmet_pci_epf_bar_read64(ctrl, NVME_REG_ACQ);
1869 
1870 	qsize = (aqa & 0x0fff0000) >> 16;
1871 	pci_addr = acq & GENMASK_ULL(63, 12);
1872 	status = nvmet_pci_epf_create_cq(ctrl->tctrl, 0,
1873 				NVME_CQ_IRQ_ENABLED | NVME_QUEUE_PHYS_CONTIG,
1874 				qsize, pci_addr, 0);
1875 	if (status != NVME_SC_SUCCESS) {
1876 		dev_err(ctrl->dev, "Failed to create admin completion queue\n");
1877 		goto err;
1878 	}
1879 
1880 	qsize = aqa & 0x00000fff;
1881 	pci_addr = asq & GENMASK_ULL(63, 12);
1882 	status = nvmet_pci_epf_create_sq(ctrl->tctrl, 0, NVME_QUEUE_PHYS_CONTIG,
1883 					 qsize, pci_addr);
1884 	if (status != NVME_SC_SUCCESS) {
1885 		dev_err(ctrl->dev, "Failed to create admin submission queue\n");
1886 		nvmet_pci_epf_delete_cq(ctrl->tctrl, 0);
1887 		goto err;
1888 	}
1889 
1890 	ctrl->sq_ab = NVMET_PCI_EPF_SQ_AB;
1891 	ctrl->irq_vector_threshold = NVMET_PCI_EPF_IV_THRESHOLD;
1892 	ctrl->enabled = true;
1893 	ctrl->csts = NVME_CSTS_RDY;
1894 
1895 	/* Start polling the controller SQs. */
1896 	schedule_delayed_work(&ctrl->poll_sqs, 0);
1897 
1898 	return 0;
1899 
1900 err:
1901 	nvmet_pci_epf_clear_ctrl_config(ctrl);
1902 	return -EINVAL;
1903 }
1904 
nvmet_pci_epf_disable_ctrl(struct nvmet_pci_epf_ctrl * ctrl,bool shutdown)1905 static void nvmet_pci_epf_disable_ctrl(struct nvmet_pci_epf_ctrl *ctrl,
1906 				       bool shutdown)
1907 {
1908 	int qid;
1909 
1910 	if (!ctrl->enabled)
1911 		return;
1912 
1913 	dev_info(ctrl->dev, "%s controller\n",
1914 		 shutdown ? "Shutting down" : "Disabling");
1915 
1916 	ctrl->enabled = false;
1917 	cancel_delayed_work_sync(&ctrl->poll_sqs);
1918 
1919 	/* Delete all I/O queues first. */
1920 	for (qid = 1; qid < ctrl->nr_queues; qid++)
1921 		nvmet_pci_epf_delete_sq(ctrl->tctrl, qid);
1922 
1923 	for (qid = 1; qid < ctrl->nr_queues; qid++)
1924 		nvmet_pci_epf_delete_cq(ctrl->tctrl, qid);
1925 
1926 	/* Delete the admin queue last. */
1927 	nvmet_pci_epf_delete_sq(ctrl->tctrl, 0);
1928 	nvmet_pci_epf_delete_cq(ctrl->tctrl, 0);
1929 
1930 	ctrl->csts &= ~NVME_CSTS_RDY;
1931 	if (shutdown) {
1932 		ctrl->csts |= NVME_CSTS_SHST_CMPLT;
1933 		ctrl->cc &= ~NVME_CC_ENABLE;
1934 		nvmet_pci_epf_bar_write32(ctrl, NVME_REG_CC, ctrl->cc);
1935 	}
1936 }
1937 
nvmet_pci_epf_poll_cc_work(struct work_struct * work)1938 static void nvmet_pci_epf_poll_cc_work(struct work_struct *work)
1939 {
1940 	struct nvmet_pci_epf_ctrl *ctrl =
1941 		container_of(work, struct nvmet_pci_epf_ctrl, poll_cc.work);
1942 	u32 old_cc, new_cc;
1943 	int ret;
1944 
1945 	if (!ctrl->tctrl)
1946 		return;
1947 
1948 	old_cc = ctrl->cc;
1949 	new_cc = nvmet_pci_epf_bar_read32(ctrl, NVME_REG_CC);
1950 	if (new_cc == old_cc)
1951 		goto reschedule_work;
1952 
1953 	ctrl->cc = new_cc;
1954 
1955 	if (nvmet_cc_en(new_cc) && !nvmet_cc_en(old_cc)) {
1956 		ret = nvmet_pci_epf_enable_ctrl(ctrl);
1957 		if (ret)
1958 			goto reschedule_work;
1959 	}
1960 
1961 	if (!nvmet_cc_en(new_cc) && nvmet_cc_en(old_cc))
1962 		nvmet_pci_epf_disable_ctrl(ctrl, false);
1963 
1964 	if (nvmet_cc_shn(new_cc) && !nvmet_cc_shn(old_cc))
1965 		nvmet_pci_epf_disable_ctrl(ctrl, true);
1966 
1967 	if (!nvmet_cc_shn(new_cc) && nvmet_cc_shn(old_cc))
1968 		ctrl->csts &= ~NVME_CSTS_SHST_CMPLT;
1969 
1970 	nvmet_update_cc(ctrl->tctrl, ctrl->cc);
1971 	nvmet_pci_epf_bar_write32(ctrl, NVME_REG_CSTS, ctrl->csts);
1972 
1973 reschedule_work:
1974 	schedule_delayed_work(&ctrl->poll_cc, NVMET_PCI_EPF_CC_POLL_INTERVAL);
1975 }
1976 
nvmet_pci_epf_init_bar(struct nvmet_pci_epf_ctrl * ctrl)1977 static void nvmet_pci_epf_init_bar(struct nvmet_pci_epf_ctrl *ctrl)
1978 {
1979 	struct nvmet_ctrl *tctrl = ctrl->tctrl;
1980 
1981 	ctrl->bar = ctrl->nvme_epf->reg_bar;
1982 
1983 	/* Copy the target controller capabilities as a base. */
1984 	ctrl->cap = tctrl->cap;
1985 
1986 	/* Contiguous Queues Required (CQR). */
1987 	ctrl->cap |= 0x1ULL << 16;
1988 
1989 	/* Set Doorbell stride to 4B (DSTRB). */
1990 	ctrl->cap &= ~GENMASK_ULL(35, 32);
1991 
1992 	/* Clear NVM Subsystem Reset Supported (NSSRS). */
1993 	ctrl->cap &= ~(0x1ULL << 36);
1994 
1995 	/* Clear Boot Partition Support (BPS). */
1996 	ctrl->cap &= ~(0x1ULL << 45);
1997 
1998 	/* Clear Persistent Memory Region Supported (PMRS). */
1999 	ctrl->cap &= ~(0x1ULL << 56);
2000 
2001 	/* Clear Controller Memory Buffer Supported (CMBS). */
2002 	ctrl->cap &= ~(0x1ULL << 57);
2003 
2004 	nvmet_pci_epf_bar_write64(ctrl, NVME_REG_CAP, ctrl->cap);
2005 	nvmet_pci_epf_bar_write32(ctrl, NVME_REG_VS, tctrl->subsys->ver);
2006 
2007 	nvmet_pci_epf_clear_ctrl_config(ctrl);
2008 }
2009 
nvmet_pci_epf_create_ctrl(struct nvmet_pci_epf * nvme_epf,unsigned int max_nr_queues)2010 static int nvmet_pci_epf_create_ctrl(struct nvmet_pci_epf *nvme_epf,
2011 				     unsigned int max_nr_queues)
2012 {
2013 	struct nvmet_pci_epf_ctrl *ctrl = &nvme_epf->ctrl;
2014 	struct nvmet_alloc_ctrl_args args = {};
2015 	char hostnqn[NVMF_NQN_SIZE];
2016 	uuid_t id;
2017 	int ret;
2018 
2019 	memset(ctrl, 0, sizeof(*ctrl));
2020 	ctrl->dev = &nvme_epf->epf->dev;
2021 	mutex_init(&ctrl->irq_lock);
2022 	ctrl->nvme_epf = nvme_epf;
2023 	ctrl->mdts = nvme_epf->mdts_kb * SZ_1K;
2024 	INIT_DELAYED_WORK(&ctrl->poll_cc, nvmet_pci_epf_poll_cc_work);
2025 	INIT_DELAYED_WORK(&ctrl->poll_sqs, nvmet_pci_epf_poll_sqs_work);
2026 
2027 	ret = mempool_init_kmalloc_pool(&ctrl->iod_pool,
2028 					max_nr_queues * NVMET_MAX_QUEUE_SIZE,
2029 					sizeof(struct nvmet_pci_epf_iod));
2030 	if (ret) {
2031 		dev_err(ctrl->dev, "Failed to initialize IOD mempool\n");
2032 		return ret;
2033 	}
2034 
2035 	ctrl->port = nvmet_pci_epf_find_port(ctrl, nvme_epf->portid);
2036 	if (!ctrl->port) {
2037 		dev_err(ctrl->dev, "Port not found\n");
2038 		ret = -EINVAL;
2039 		goto out_mempool_exit;
2040 	}
2041 
2042 	/* Create the target controller. */
2043 	uuid_gen(&id);
2044 	snprintf(hostnqn, NVMF_NQN_SIZE,
2045 		 "nqn.2014-08.org.nvmexpress:uuid:%pUb", &id);
2046 	args.port = ctrl->port;
2047 	args.subsysnqn = nvme_epf->subsysnqn;
2048 	memset(&id, 0, sizeof(uuid_t));
2049 	args.hostid = &id;
2050 	args.hostnqn = hostnqn;
2051 	args.ops = &nvmet_pci_epf_fabrics_ops;
2052 
2053 	ctrl->tctrl = nvmet_alloc_ctrl(&args);
2054 	if (!ctrl->tctrl) {
2055 		dev_err(ctrl->dev, "Failed to create target controller\n");
2056 		ret = -ENOMEM;
2057 		goto out_mempool_exit;
2058 	}
2059 	ctrl->tctrl->drvdata = ctrl;
2060 
2061 	/* We do not support protection information for now. */
2062 	if (ctrl->tctrl->pi_support) {
2063 		dev_err(ctrl->dev,
2064 			"Protection information (PI) is not supported\n");
2065 		ret = -ENOTSUPP;
2066 		goto out_put_ctrl;
2067 	}
2068 
2069 	/* Allocate our queues, up to the maximum number. */
2070 	ctrl->nr_queues = min(ctrl->tctrl->subsys->max_qid + 1, max_nr_queues);
2071 	ret = nvmet_pci_epf_alloc_queues(ctrl);
2072 	if (ret)
2073 		goto out_put_ctrl;
2074 
2075 	/*
2076 	 * Allocate the IRQ vectors descriptors. We cannot have more than the
2077 	 * maximum number of queues.
2078 	 */
2079 	ret = nvmet_pci_epf_alloc_irq_vectors(ctrl);
2080 	if (ret)
2081 		goto out_free_queues;
2082 
2083 	dev_info(ctrl->dev,
2084 		 "New PCI ctrl \"%s\", %u I/O queues, mdts %u B\n",
2085 		 ctrl->tctrl->subsys->subsysnqn, ctrl->nr_queues - 1,
2086 		 ctrl->mdts);
2087 
2088 	/* Initialize BAR 0 using the target controller CAP. */
2089 	nvmet_pci_epf_init_bar(ctrl);
2090 
2091 	return 0;
2092 
2093 out_free_queues:
2094 	nvmet_pci_epf_free_queues(ctrl);
2095 out_put_ctrl:
2096 	nvmet_ctrl_put(ctrl->tctrl);
2097 	ctrl->tctrl = NULL;
2098 out_mempool_exit:
2099 	mempool_exit(&ctrl->iod_pool);
2100 	return ret;
2101 }
2102 
nvmet_pci_epf_start_ctrl(struct nvmet_pci_epf_ctrl * ctrl)2103 static void nvmet_pci_epf_start_ctrl(struct nvmet_pci_epf_ctrl *ctrl)
2104 {
2105 
2106 	dev_info(ctrl->dev, "PCI link up\n");
2107 	ctrl->link_up = true;
2108 
2109 	schedule_delayed_work(&ctrl->poll_cc, NVMET_PCI_EPF_CC_POLL_INTERVAL);
2110 }
2111 
nvmet_pci_epf_stop_ctrl(struct nvmet_pci_epf_ctrl * ctrl)2112 static void nvmet_pci_epf_stop_ctrl(struct nvmet_pci_epf_ctrl *ctrl)
2113 {
2114 	dev_info(ctrl->dev, "PCI link down\n");
2115 	ctrl->link_up = false;
2116 
2117 	cancel_delayed_work_sync(&ctrl->poll_cc);
2118 
2119 	nvmet_pci_epf_disable_ctrl(ctrl, false);
2120 	nvmet_pci_epf_clear_ctrl_config(ctrl);
2121 }
2122 
nvmet_pci_epf_destroy_ctrl(struct nvmet_pci_epf_ctrl * ctrl)2123 static void nvmet_pci_epf_destroy_ctrl(struct nvmet_pci_epf_ctrl *ctrl)
2124 {
2125 	if (!ctrl->tctrl)
2126 		return;
2127 
2128 	dev_info(ctrl->dev, "Destroying PCI ctrl \"%s\"\n",
2129 		 ctrl->tctrl->subsys->subsysnqn);
2130 
2131 	nvmet_pci_epf_stop_ctrl(ctrl);
2132 
2133 	nvmet_pci_epf_free_queues(ctrl);
2134 	nvmet_pci_epf_free_irq_vectors(ctrl);
2135 
2136 	nvmet_ctrl_put(ctrl->tctrl);
2137 	ctrl->tctrl = NULL;
2138 
2139 	mempool_exit(&ctrl->iod_pool);
2140 }
2141 
nvmet_pci_epf_configure_bar(struct nvmet_pci_epf * nvme_epf)2142 static int nvmet_pci_epf_configure_bar(struct nvmet_pci_epf *nvme_epf)
2143 {
2144 	struct pci_epf *epf = nvme_epf->epf;
2145 	const struct pci_epc_features *epc_features = nvme_epf->epc_features;
2146 	size_t reg_size, reg_bar_size;
2147 	size_t msix_table_size = 0;
2148 
2149 	/*
2150 	 * The first free BAR will be our register BAR and per NVMe
2151 	 * specifications, it must be BAR 0.
2152 	 */
2153 	if (pci_epc_get_first_free_bar(epc_features) != BAR_0) {
2154 		dev_err(&epf->dev, "BAR 0 is not free\n");
2155 		return -ENODEV;
2156 	}
2157 
2158 	/*
2159 	 * While NVMe PCIe Transport Specification 1.1, section 2.1.10, claims
2160 	 * that the BAR0 type is Implementation Specific, in NVMe 1.1, the type
2161 	 * is required to be 64-bit. Thus, for interoperability, always set the
2162 	 * type to 64-bit. In the rare case that the PCI EPC does not support
2163 	 * configuring BAR0 as 64-bit, the call to pci_epc_set_bar() will fail,
2164 	 * and we will return failure back to the user.
2165 	 */
2166 	epf->bar[BAR_0].flags |= PCI_BASE_ADDRESS_MEM_TYPE_64;
2167 
2168 	/*
2169 	 * Calculate the size of the register bar: NVMe registers first with
2170 	 * enough space for the doorbells, followed by the MSI-X table
2171 	 * if supported.
2172 	 */
2173 	reg_size = NVME_REG_DBS + (NVMET_NR_QUEUES * 2 * sizeof(u32));
2174 	reg_size = ALIGN(reg_size, 8);
2175 
2176 	if (epc_features->msix_capable) {
2177 		size_t pba_size;
2178 
2179 		msix_table_size = PCI_MSIX_ENTRY_SIZE * epf->msix_interrupts;
2180 		nvme_epf->msix_table_offset = reg_size;
2181 		pba_size = ALIGN(DIV_ROUND_UP(epf->msix_interrupts, 8), 8);
2182 
2183 		reg_size += msix_table_size + pba_size;
2184 	}
2185 
2186 	if (epc_features->bar[BAR_0].type == BAR_FIXED) {
2187 		if (reg_size > epc_features->bar[BAR_0].fixed_size) {
2188 			dev_err(&epf->dev,
2189 				"BAR 0 size %llu B too small, need %zu B\n",
2190 				epc_features->bar[BAR_0].fixed_size,
2191 				reg_size);
2192 			return -ENOMEM;
2193 		}
2194 		reg_bar_size = epc_features->bar[BAR_0].fixed_size;
2195 	} else {
2196 		reg_bar_size = ALIGN(reg_size, max(epc_features->align, 4096));
2197 	}
2198 
2199 	nvme_epf->reg_bar = pci_epf_alloc_space(epf, reg_bar_size, BAR_0,
2200 						epc_features, PRIMARY_INTERFACE);
2201 	if (!nvme_epf->reg_bar) {
2202 		dev_err(&epf->dev, "Failed to allocate BAR 0\n");
2203 		return -ENOMEM;
2204 	}
2205 	memset(nvme_epf->reg_bar, 0, reg_bar_size);
2206 
2207 	return 0;
2208 }
2209 
nvmet_pci_epf_free_bar(struct nvmet_pci_epf * nvme_epf)2210 static void nvmet_pci_epf_free_bar(struct nvmet_pci_epf *nvme_epf)
2211 {
2212 	struct pci_epf *epf = nvme_epf->epf;
2213 
2214 	if (!nvme_epf->reg_bar)
2215 		return;
2216 
2217 	pci_epf_free_space(epf, nvme_epf->reg_bar, BAR_0, PRIMARY_INTERFACE);
2218 	nvme_epf->reg_bar = NULL;
2219 }
2220 
nvmet_pci_epf_clear_bar(struct nvmet_pci_epf * nvme_epf)2221 static void nvmet_pci_epf_clear_bar(struct nvmet_pci_epf *nvme_epf)
2222 {
2223 	struct pci_epf *epf = nvme_epf->epf;
2224 
2225 	pci_epc_clear_bar(epf->epc, epf->func_no, epf->vfunc_no,
2226 			  &epf->bar[BAR_0]);
2227 }
2228 
nvmet_pci_epf_init_irq(struct nvmet_pci_epf * nvme_epf)2229 static int nvmet_pci_epf_init_irq(struct nvmet_pci_epf *nvme_epf)
2230 {
2231 	const struct pci_epc_features *epc_features = nvme_epf->epc_features;
2232 	struct pci_epf *epf = nvme_epf->epf;
2233 	int ret;
2234 
2235 	/* Enable MSI-X if supported, otherwise, use MSI. */
2236 	if (epc_features->msix_capable && epf->msix_interrupts) {
2237 		ret = pci_epc_set_msix(epf->epc, epf->func_no, epf->vfunc_no,
2238 				       epf->msix_interrupts, BAR_0,
2239 				       nvme_epf->msix_table_offset);
2240 		if (ret) {
2241 			dev_err(&epf->dev, "Failed to configure MSI-X\n");
2242 			return ret;
2243 		}
2244 
2245 		nvme_epf->nr_vectors = epf->msix_interrupts;
2246 		nvme_epf->irq_type = PCI_IRQ_MSIX;
2247 
2248 		return 0;
2249 	}
2250 
2251 	if (epc_features->msi_capable && epf->msi_interrupts) {
2252 		ret = pci_epc_set_msi(epf->epc, epf->func_no, epf->vfunc_no,
2253 				      epf->msi_interrupts);
2254 		if (ret) {
2255 			dev_err(&epf->dev, "Failed to configure MSI\n");
2256 			return ret;
2257 		}
2258 
2259 		nvme_epf->nr_vectors = epf->msi_interrupts;
2260 		nvme_epf->irq_type = PCI_IRQ_MSI;
2261 
2262 		return 0;
2263 	}
2264 
2265 	/* MSI and MSI-X are not supported: fall back to INTx. */
2266 	nvme_epf->nr_vectors = 1;
2267 	nvme_epf->irq_type = PCI_IRQ_INTX;
2268 
2269 	return 0;
2270 }
2271 
nvmet_pci_epf_epc_init(struct pci_epf * epf)2272 static int nvmet_pci_epf_epc_init(struct pci_epf *epf)
2273 {
2274 	struct nvmet_pci_epf *nvme_epf = epf_get_drvdata(epf);
2275 	const struct pci_epc_features *epc_features = nvme_epf->epc_features;
2276 	struct nvmet_pci_epf_ctrl *ctrl = &nvme_epf->ctrl;
2277 	unsigned int max_nr_queues = NVMET_NR_QUEUES;
2278 	int ret;
2279 
2280 	/* For now, do not support virtual functions. */
2281 	if (epf->vfunc_no > 0) {
2282 		dev_err(&epf->dev, "Virtual functions are not supported\n");
2283 		return -EINVAL;
2284 	}
2285 
2286 	/*
2287 	 * Cap the maximum number of queues we can support on the controller
2288 	 * with the number of IRQs we can use.
2289 	 */
2290 	if (epc_features->msix_capable && epf->msix_interrupts) {
2291 		dev_info(&epf->dev,
2292 			 "PCI endpoint controller supports MSI-X, %u vectors\n",
2293 			 epf->msix_interrupts);
2294 		max_nr_queues = min(max_nr_queues, epf->msix_interrupts);
2295 	} else if (epc_features->msi_capable && epf->msi_interrupts) {
2296 		dev_info(&epf->dev,
2297 			 "PCI endpoint controller supports MSI, %u vectors\n",
2298 			 epf->msi_interrupts);
2299 		max_nr_queues = min(max_nr_queues, epf->msi_interrupts);
2300 	}
2301 
2302 	if (max_nr_queues < 2) {
2303 		dev_err(&epf->dev, "Invalid maximum number of queues %u\n",
2304 			max_nr_queues);
2305 		return -EINVAL;
2306 	}
2307 
2308 	/* Create the target controller. */
2309 	ret = nvmet_pci_epf_create_ctrl(nvme_epf, max_nr_queues);
2310 	if (ret) {
2311 		dev_err(&epf->dev,
2312 			"Failed to create NVMe PCI target controller (err=%d)\n",
2313 			ret);
2314 		return ret;
2315 	}
2316 
2317 	/* Set device ID, class, etc. */
2318 	epf->header->vendorid = ctrl->tctrl->subsys->vendor_id;
2319 	epf->header->subsys_vendor_id = ctrl->tctrl->subsys->subsys_vendor_id;
2320 	ret = pci_epc_write_header(epf->epc, epf->func_no, epf->vfunc_no,
2321 				   epf->header);
2322 	if (ret) {
2323 		dev_err(&epf->dev,
2324 			"Failed to write configuration header (err=%d)\n", ret);
2325 		goto out_destroy_ctrl;
2326 	}
2327 
2328 	ret = pci_epc_set_bar(epf->epc, epf->func_no, epf->vfunc_no,
2329 			      &epf->bar[BAR_0]);
2330 	if (ret) {
2331 		dev_err(&epf->dev, "Failed to set BAR 0 (err=%d)\n", ret);
2332 		goto out_destroy_ctrl;
2333 	}
2334 
2335 	/*
2336 	 * Enable interrupts and start polling the controller BAR if we do not
2337 	 * have a link up notifier.
2338 	 */
2339 	ret = nvmet_pci_epf_init_irq(nvme_epf);
2340 	if (ret)
2341 		goto out_clear_bar;
2342 
2343 	if (!epc_features->linkup_notifier)
2344 		nvmet_pci_epf_start_ctrl(&nvme_epf->ctrl);
2345 
2346 	return 0;
2347 
2348 out_clear_bar:
2349 	nvmet_pci_epf_clear_bar(nvme_epf);
2350 out_destroy_ctrl:
2351 	nvmet_pci_epf_destroy_ctrl(&nvme_epf->ctrl);
2352 	return ret;
2353 }
2354 
nvmet_pci_epf_epc_deinit(struct pci_epf * epf)2355 static void nvmet_pci_epf_epc_deinit(struct pci_epf *epf)
2356 {
2357 	struct nvmet_pci_epf *nvme_epf = epf_get_drvdata(epf);
2358 	struct nvmet_pci_epf_ctrl *ctrl = &nvme_epf->ctrl;
2359 
2360 	nvmet_pci_epf_destroy_ctrl(ctrl);
2361 
2362 	nvmet_pci_epf_deinit_dma(nvme_epf);
2363 	nvmet_pci_epf_clear_bar(nvme_epf);
2364 }
2365 
nvmet_pci_epf_link_up(struct pci_epf * epf)2366 static int nvmet_pci_epf_link_up(struct pci_epf *epf)
2367 {
2368 	struct nvmet_pci_epf *nvme_epf = epf_get_drvdata(epf);
2369 	struct nvmet_pci_epf_ctrl *ctrl = &nvme_epf->ctrl;
2370 
2371 	nvmet_pci_epf_start_ctrl(ctrl);
2372 
2373 	return 0;
2374 }
2375 
nvmet_pci_epf_link_down(struct pci_epf * epf)2376 static int nvmet_pci_epf_link_down(struct pci_epf *epf)
2377 {
2378 	struct nvmet_pci_epf *nvme_epf = epf_get_drvdata(epf);
2379 	struct nvmet_pci_epf_ctrl *ctrl = &nvme_epf->ctrl;
2380 
2381 	nvmet_pci_epf_stop_ctrl(ctrl);
2382 
2383 	return 0;
2384 }
2385 
2386 static const struct pci_epc_event_ops nvmet_pci_epf_event_ops = {
2387 	.epc_init = nvmet_pci_epf_epc_init,
2388 	.epc_deinit = nvmet_pci_epf_epc_deinit,
2389 	.link_up = nvmet_pci_epf_link_up,
2390 	.link_down = nvmet_pci_epf_link_down,
2391 };
2392 
nvmet_pci_epf_bind(struct pci_epf * epf)2393 static int nvmet_pci_epf_bind(struct pci_epf *epf)
2394 {
2395 	struct nvmet_pci_epf *nvme_epf = epf_get_drvdata(epf);
2396 	const struct pci_epc_features *epc_features;
2397 	struct pci_epc *epc = epf->epc;
2398 	int ret;
2399 
2400 	if (WARN_ON_ONCE(!epc))
2401 		return -EINVAL;
2402 
2403 	epc_features = pci_epc_get_features(epc, epf->func_no, epf->vfunc_no);
2404 	if (!epc_features) {
2405 		dev_err(&epf->dev, "epc_features not implemented\n");
2406 		return -EOPNOTSUPP;
2407 	}
2408 	nvme_epf->epc_features = epc_features;
2409 
2410 	ret = nvmet_pci_epf_configure_bar(nvme_epf);
2411 	if (ret)
2412 		return ret;
2413 
2414 	nvmet_pci_epf_init_dma(nvme_epf);
2415 
2416 	return 0;
2417 }
2418 
nvmet_pci_epf_unbind(struct pci_epf * epf)2419 static void nvmet_pci_epf_unbind(struct pci_epf *epf)
2420 {
2421 	struct nvmet_pci_epf *nvme_epf = epf_get_drvdata(epf);
2422 	struct pci_epc *epc = epf->epc;
2423 
2424 	nvmet_pci_epf_destroy_ctrl(&nvme_epf->ctrl);
2425 
2426 	if (epc->init_complete) {
2427 		nvmet_pci_epf_deinit_dma(nvme_epf);
2428 		nvmet_pci_epf_clear_bar(nvme_epf);
2429 	}
2430 
2431 	nvmet_pci_epf_free_bar(nvme_epf);
2432 }
2433 
2434 static struct pci_epf_header nvme_epf_pci_header = {
2435 	.vendorid	= PCI_ANY_ID,
2436 	.deviceid	= PCI_ANY_ID,
2437 	.progif_code	= 0x02, /* NVM Express */
2438 	.baseclass_code = PCI_BASE_CLASS_STORAGE,
2439 	.subclass_code	= 0x08, /* Non-Volatile Memory controller */
2440 	.interrupt_pin	= PCI_INTERRUPT_INTA,
2441 };
2442 
nvmet_pci_epf_probe(struct pci_epf * epf,const struct pci_epf_device_id * id)2443 static int nvmet_pci_epf_probe(struct pci_epf *epf,
2444 			       const struct pci_epf_device_id *id)
2445 {
2446 	struct nvmet_pci_epf *nvme_epf;
2447 	int ret;
2448 
2449 	nvme_epf = devm_kzalloc(&epf->dev, sizeof(*nvme_epf), GFP_KERNEL);
2450 	if (!nvme_epf)
2451 		return -ENOMEM;
2452 
2453 	ret = devm_mutex_init(&epf->dev, &nvme_epf->mmio_lock);
2454 	if (ret)
2455 		return ret;
2456 
2457 	nvme_epf->epf = epf;
2458 	nvme_epf->mdts_kb = NVMET_PCI_EPF_MDTS_KB;
2459 
2460 	epf->event_ops = &nvmet_pci_epf_event_ops;
2461 	epf->header = &nvme_epf_pci_header;
2462 	epf_set_drvdata(epf, nvme_epf);
2463 
2464 	return 0;
2465 }
2466 
2467 #define to_nvme_epf(epf_group)	\
2468 	container_of(epf_group, struct nvmet_pci_epf, group)
2469 
nvmet_pci_epf_portid_show(struct config_item * item,char * page)2470 static ssize_t nvmet_pci_epf_portid_show(struct config_item *item, char *page)
2471 {
2472 	struct config_group *group = to_config_group(item);
2473 	struct nvmet_pci_epf *nvme_epf = to_nvme_epf(group);
2474 
2475 	return sysfs_emit(page, "%u\n", le16_to_cpu(nvme_epf->portid));
2476 }
2477 
nvmet_pci_epf_portid_store(struct config_item * item,const char * page,size_t len)2478 static ssize_t nvmet_pci_epf_portid_store(struct config_item *item,
2479 					  const char *page, size_t len)
2480 {
2481 	struct config_group *group = to_config_group(item);
2482 	struct nvmet_pci_epf *nvme_epf = to_nvme_epf(group);
2483 	u16 portid;
2484 
2485 	/* Do not allow setting this when the function is already started. */
2486 	if (nvme_epf->ctrl.tctrl)
2487 		return -EBUSY;
2488 
2489 	if (!len)
2490 		return -EINVAL;
2491 
2492 	if (kstrtou16(page, 0, &portid))
2493 		return -EINVAL;
2494 
2495 	nvme_epf->portid = cpu_to_le16(portid);
2496 
2497 	return len;
2498 }
2499 
2500 CONFIGFS_ATTR(nvmet_pci_epf_, portid);
2501 
nvmet_pci_epf_subsysnqn_show(struct config_item * item,char * page)2502 static ssize_t nvmet_pci_epf_subsysnqn_show(struct config_item *item,
2503 					    char *page)
2504 {
2505 	struct config_group *group = to_config_group(item);
2506 	struct nvmet_pci_epf *nvme_epf = to_nvme_epf(group);
2507 
2508 	return sysfs_emit(page, "%s\n", nvme_epf->subsysnqn);
2509 }
2510 
nvmet_pci_epf_subsysnqn_store(struct config_item * item,const char * page,size_t len)2511 static ssize_t nvmet_pci_epf_subsysnqn_store(struct config_item *item,
2512 					     const char *page, size_t len)
2513 {
2514 	struct config_group *group = to_config_group(item);
2515 	struct nvmet_pci_epf *nvme_epf = to_nvme_epf(group);
2516 
2517 	/* Do not allow setting this when the function is already started. */
2518 	if (nvme_epf->ctrl.tctrl)
2519 		return -EBUSY;
2520 
2521 	if (!len)
2522 		return -EINVAL;
2523 
2524 	strscpy(nvme_epf->subsysnqn, page, len);
2525 
2526 	return len;
2527 }
2528 
2529 CONFIGFS_ATTR(nvmet_pci_epf_, subsysnqn);
2530 
nvmet_pci_epf_mdts_kb_show(struct config_item * item,char * page)2531 static ssize_t nvmet_pci_epf_mdts_kb_show(struct config_item *item, char *page)
2532 {
2533 	struct config_group *group = to_config_group(item);
2534 	struct nvmet_pci_epf *nvme_epf = to_nvme_epf(group);
2535 
2536 	return sysfs_emit(page, "%u\n", nvme_epf->mdts_kb);
2537 }
2538 
nvmet_pci_epf_mdts_kb_store(struct config_item * item,const char * page,size_t len)2539 static ssize_t nvmet_pci_epf_mdts_kb_store(struct config_item *item,
2540 					   const char *page, size_t len)
2541 {
2542 	struct config_group *group = to_config_group(item);
2543 	struct nvmet_pci_epf *nvme_epf = to_nvme_epf(group);
2544 	unsigned long mdts_kb;
2545 	int ret;
2546 
2547 	if (nvme_epf->ctrl.tctrl)
2548 		return -EBUSY;
2549 
2550 	ret = kstrtoul(page, 0, &mdts_kb);
2551 	if (ret)
2552 		return ret;
2553 	if (!mdts_kb)
2554 		mdts_kb = NVMET_PCI_EPF_MDTS_KB;
2555 	else if (mdts_kb > NVMET_PCI_EPF_MAX_MDTS_KB)
2556 		mdts_kb = NVMET_PCI_EPF_MAX_MDTS_KB;
2557 
2558 	if (!is_power_of_2(mdts_kb))
2559 		return -EINVAL;
2560 
2561 	nvme_epf->mdts_kb = mdts_kb;
2562 
2563 	return len;
2564 }
2565 
2566 CONFIGFS_ATTR(nvmet_pci_epf_, mdts_kb);
2567 
2568 static struct configfs_attribute *nvmet_pci_epf_attrs[] = {
2569 	&nvmet_pci_epf_attr_portid,
2570 	&nvmet_pci_epf_attr_subsysnqn,
2571 	&nvmet_pci_epf_attr_mdts_kb,
2572 	NULL,
2573 };
2574 
2575 static const struct config_item_type nvmet_pci_epf_group_type = {
2576 	.ct_attrs	= nvmet_pci_epf_attrs,
2577 	.ct_owner	= THIS_MODULE,
2578 };
2579 
nvmet_pci_epf_add_cfs(struct pci_epf * epf,struct config_group * group)2580 static struct config_group *nvmet_pci_epf_add_cfs(struct pci_epf *epf,
2581 						  struct config_group *group)
2582 {
2583 	struct nvmet_pci_epf *nvme_epf = epf_get_drvdata(epf);
2584 
2585 	config_group_init_type_name(&nvme_epf->group, "nvme",
2586 				    &nvmet_pci_epf_group_type);
2587 
2588 	return &nvme_epf->group;
2589 }
2590 
2591 static const struct pci_epf_device_id nvmet_pci_epf_ids[] = {
2592 	{ .name = "nvmet_pci_epf" },
2593 	{},
2594 };
2595 
2596 static struct pci_epf_ops nvmet_pci_epf_ops = {
2597 	.bind	= nvmet_pci_epf_bind,
2598 	.unbind	= nvmet_pci_epf_unbind,
2599 	.add_cfs = nvmet_pci_epf_add_cfs,
2600 };
2601 
2602 static struct pci_epf_driver nvmet_pci_epf_driver = {
2603 	.driver.name	= "nvmet_pci_epf",
2604 	.probe		= nvmet_pci_epf_probe,
2605 	.id_table	= nvmet_pci_epf_ids,
2606 	.ops		= &nvmet_pci_epf_ops,
2607 	.owner		= THIS_MODULE,
2608 };
2609 
nvmet_pci_epf_init_module(void)2610 static int __init nvmet_pci_epf_init_module(void)
2611 {
2612 	int ret;
2613 
2614 	ret = pci_epf_register_driver(&nvmet_pci_epf_driver);
2615 	if (ret)
2616 		return ret;
2617 
2618 	ret = nvmet_register_transport(&nvmet_pci_epf_fabrics_ops);
2619 	if (ret) {
2620 		pci_epf_unregister_driver(&nvmet_pci_epf_driver);
2621 		return ret;
2622 	}
2623 
2624 	return 0;
2625 }
2626 
nvmet_pci_epf_cleanup_module(void)2627 static void __exit nvmet_pci_epf_cleanup_module(void)
2628 {
2629 	nvmet_unregister_transport(&nvmet_pci_epf_fabrics_ops);
2630 	pci_epf_unregister_driver(&nvmet_pci_epf_driver);
2631 }
2632 
2633 module_init(nvmet_pci_epf_init_module);
2634 module_exit(nvmet_pci_epf_cleanup_module);
2635 
2636 MODULE_DESCRIPTION("NVMe PCI Endpoint Function target driver");
2637 MODULE_AUTHOR("Damien Le Moal <[email protected]>");
2638 MODULE_LICENSE("GPL");
2639