xref: /openbmc/linux/drivers/s390/net/ism_drv.c (revision 1baedb13f1d50ae8c7852134fdf934b4463e9baa)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * ISM driver for s390.
4  *
5  * Copyright IBM Corp. 2018
6  */
7 #define KMSG_COMPONENT "ism"
8 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
9 
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/interrupt.h>
13 #include <linux/device.h>
14 #include <linux/pci.h>
15 #include <linux/err.h>
16 #include <linux/ctype.h>
17 #include <linux/processor.h>
18 #include <net/smc.h>
19 
20 #include <asm/debug.h>
21 
22 #include "ism.h"
23 
24 MODULE_DESCRIPTION("ISM driver for s390");
25 MODULE_LICENSE("GPL");
26 
27 #define PCI_DEVICE_ID_IBM_ISM 0x04ED
28 #define DRV_NAME "ism"
29 
30 static const struct pci_device_id ism_device_table[] = {
31 	{ PCI_VDEVICE(IBM, PCI_DEVICE_ID_IBM_ISM), 0 },
32 	{ 0, }
33 };
34 MODULE_DEVICE_TABLE(pci, ism_device_table);
35 
36 static debug_info_t *ism_debug_info;
37 
38 static int ism_cmd(struct ism_dev *ism, void *cmd)
39 {
40 	struct ism_req_hdr *req = cmd;
41 	struct ism_resp_hdr *resp = cmd;
42 
43 	__ism_write_cmd(ism, req + 1, sizeof(*req), req->len - sizeof(*req));
44 	__ism_write_cmd(ism, req, 0, sizeof(*req));
45 
46 	WRITE_ONCE(resp->ret, ISM_ERROR);
47 
48 	__ism_read_cmd(ism, resp, 0, sizeof(*resp));
49 	if (resp->ret) {
50 		debug_text_event(ism_debug_info, 0, "cmd failure");
51 		debug_event(ism_debug_info, 0, resp, sizeof(*resp));
52 		goto out;
53 	}
54 	__ism_read_cmd(ism, resp + 1, sizeof(*resp), resp->len - sizeof(*resp));
55 out:
56 	return resp->ret;
57 }
58 
59 static int ism_cmd_simple(struct ism_dev *ism, u32 cmd_code)
60 {
61 	union ism_cmd_simple cmd;
62 
63 	memset(&cmd, 0, sizeof(cmd));
64 	cmd.request.hdr.cmd = cmd_code;
65 	cmd.request.hdr.len = sizeof(cmd.request);
66 
67 	return ism_cmd(ism, &cmd);
68 }
69 
70 static int query_info(struct ism_dev *ism)
71 {
72 	union ism_qi cmd;
73 
74 	memset(&cmd, 0, sizeof(cmd));
75 	cmd.request.hdr.cmd = ISM_QUERY_INFO;
76 	cmd.request.hdr.len = sizeof(cmd.request);
77 
78 	if (ism_cmd(ism, &cmd))
79 		goto out;
80 
81 	debug_text_event(ism_debug_info, 3, "query info");
82 	debug_event(ism_debug_info, 3, &cmd.response, sizeof(cmd.response));
83 out:
84 	return 0;
85 }
86 
87 static int register_sba(struct ism_dev *ism)
88 {
89 	union ism_reg_sba cmd;
90 	dma_addr_t dma_handle;
91 	struct ism_sba *sba;
92 
93 	sba = dma_alloc_coherent(&ism->pdev->dev, PAGE_SIZE, &dma_handle,
94 				 GFP_KERNEL);
95 	if (!sba)
96 		return -ENOMEM;
97 
98 	memset(&cmd, 0, sizeof(cmd));
99 	cmd.request.hdr.cmd = ISM_REG_SBA;
100 	cmd.request.hdr.len = sizeof(cmd.request);
101 	cmd.request.sba = dma_handle;
102 
103 	if (ism_cmd(ism, &cmd)) {
104 		dma_free_coherent(&ism->pdev->dev, PAGE_SIZE, sba, dma_handle);
105 		return -EIO;
106 	}
107 
108 	ism->sba = sba;
109 	ism->sba_dma_addr = dma_handle;
110 
111 	return 0;
112 }
113 
114 static int register_ieq(struct ism_dev *ism)
115 {
116 	union ism_reg_ieq cmd;
117 	dma_addr_t dma_handle;
118 	struct ism_eq *ieq;
119 
120 	ieq = dma_alloc_coherent(&ism->pdev->dev, PAGE_SIZE, &dma_handle,
121 				 GFP_KERNEL);
122 	if (!ieq)
123 		return -ENOMEM;
124 
125 	memset(&cmd, 0, sizeof(cmd));
126 	cmd.request.hdr.cmd = ISM_REG_IEQ;
127 	cmd.request.hdr.len = sizeof(cmd.request);
128 	cmd.request.ieq = dma_handle;
129 	cmd.request.len = sizeof(*ieq);
130 
131 	if (ism_cmd(ism, &cmd)) {
132 		dma_free_coherent(&ism->pdev->dev, PAGE_SIZE, ieq, dma_handle);
133 		return -EIO;
134 	}
135 
136 	ism->ieq = ieq;
137 	ism->ieq_idx = -1;
138 	ism->ieq_dma_addr = dma_handle;
139 
140 	return 0;
141 }
142 
143 static int unregister_sba(struct ism_dev *ism)
144 {
145 	int ret;
146 
147 	if (!ism->sba)
148 		return 0;
149 
150 	ret = ism_cmd_simple(ism, ISM_UNREG_SBA);
151 	if (ret && ret != ISM_ERROR)
152 		return -EIO;
153 
154 	dma_free_coherent(&ism->pdev->dev, PAGE_SIZE,
155 			  ism->sba, ism->sba_dma_addr);
156 
157 	ism->sba = NULL;
158 	ism->sba_dma_addr = 0;
159 
160 	return 0;
161 }
162 
163 static int unregister_ieq(struct ism_dev *ism)
164 {
165 	int ret;
166 
167 	if (!ism->ieq)
168 		return 0;
169 
170 	ret = ism_cmd_simple(ism, ISM_UNREG_IEQ);
171 	if (ret && ret != ISM_ERROR)
172 		return -EIO;
173 
174 	dma_free_coherent(&ism->pdev->dev, PAGE_SIZE,
175 			  ism->ieq, ism->ieq_dma_addr);
176 
177 	ism->ieq = NULL;
178 	ism->ieq_dma_addr = 0;
179 
180 	return 0;
181 }
182 
183 static int ism_read_local_gid(struct ism_dev *ism)
184 {
185 	union ism_read_gid cmd;
186 	int ret;
187 
188 	memset(&cmd, 0, sizeof(cmd));
189 	cmd.request.hdr.cmd = ISM_READ_GID;
190 	cmd.request.hdr.len = sizeof(cmd.request);
191 
192 	ret = ism_cmd(ism, &cmd);
193 	if (ret)
194 		goto out;
195 
196 	ism->smcd->local_gid = cmd.response.gid;
197 out:
198 	return ret;
199 }
200 
201 static int ism_query_rgid(struct smcd_dev *smcd, u64 rgid, u32 vid_valid,
202 			  u32 vid)
203 {
204 	struct ism_dev *ism = smcd->priv;
205 	union ism_query_rgid cmd;
206 
207 	memset(&cmd, 0, sizeof(cmd));
208 	cmd.request.hdr.cmd = ISM_QUERY_RGID;
209 	cmd.request.hdr.len = sizeof(cmd.request);
210 
211 	cmd.request.rgid = rgid;
212 	cmd.request.vlan_valid = vid_valid;
213 	cmd.request.vlan_id = vid;
214 
215 	return ism_cmd(ism, &cmd);
216 }
217 
218 static void ism_free_dmb(struct ism_dev *ism, struct ism_dmb *dmb)
219 {
220 	clear_bit(dmb->sba_idx, ism->sba_bitmap);
221 	dma_free_coherent(&ism->pdev->dev, dmb->dmb_len,
222 			  dmb->cpu_addr, dmb->dma_addr);
223 }
224 
225 static int ism_alloc_dmb(struct ism_dev *ism, struct ism_dmb *dmb)
226 {
227 	unsigned long bit;
228 
229 	if (PAGE_ALIGN(dmb->dmb_len) > dma_get_max_seg_size(&ism->pdev->dev))
230 		return -EINVAL;
231 
232 	if (!dmb->sba_idx) {
233 		bit = find_next_zero_bit(ism->sba_bitmap, ISM_NR_DMBS,
234 					 ISM_DMB_BIT_OFFSET);
235 		if (bit == ISM_NR_DMBS)
236 			return -ENOSPC;
237 
238 		dmb->sba_idx = bit;
239 	}
240 	if (dmb->sba_idx < ISM_DMB_BIT_OFFSET ||
241 	    test_and_set_bit(dmb->sba_idx, ism->sba_bitmap))
242 		return -EINVAL;
243 
244 	dmb->cpu_addr = dma_alloc_coherent(&ism->pdev->dev, dmb->dmb_len,
245 					   &dmb->dma_addr,
246 					   GFP_KERNEL | __GFP_NOWARN |
247 					   __GFP_NOMEMALLOC | __GFP_NORETRY);
248 	if (!dmb->cpu_addr)
249 		clear_bit(dmb->sba_idx, ism->sba_bitmap);
250 
251 	return dmb->cpu_addr ? 0 : -ENOMEM;
252 }
253 
254 static int ism_register_dmb(struct smcd_dev *smcd, struct ism_dmb *dmb)
255 {
256 	struct ism_dev *ism = smcd->priv;
257 	union ism_reg_dmb cmd;
258 	int ret;
259 
260 	ret = ism_alloc_dmb(ism, dmb);
261 	if (ret)
262 		goto out;
263 
264 	memset(&cmd, 0, sizeof(cmd));
265 	cmd.request.hdr.cmd = ISM_REG_DMB;
266 	cmd.request.hdr.len = sizeof(cmd.request);
267 
268 	cmd.request.dmb = dmb->dma_addr;
269 	cmd.request.dmb_len = dmb->dmb_len;
270 	cmd.request.sba_idx = dmb->sba_idx;
271 	cmd.request.vlan_valid = dmb->vlan_valid;
272 	cmd.request.vlan_id = dmb->vlan_id;
273 	cmd.request.rgid = dmb->rgid;
274 
275 	ret = ism_cmd(ism, &cmd);
276 	if (ret) {
277 		ism_free_dmb(ism, dmb);
278 		goto out;
279 	}
280 	dmb->dmb_tok = cmd.response.dmb_tok;
281 out:
282 	return ret;
283 }
284 
285 static int smcd_register_dmb(struct smcd_dev *smcd, struct smcd_dmb *dmb)
286 {
287 	return ism_register_dmb(smcd, (struct ism_dmb *)dmb);
288 }
289 
290 static int ism_unregister_dmb(struct smcd_dev *smcd, struct ism_dmb *dmb)
291 {
292 	struct ism_dev *ism = smcd->priv;
293 	union ism_unreg_dmb cmd;
294 	int ret;
295 
296 	memset(&cmd, 0, sizeof(cmd));
297 	cmd.request.hdr.cmd = ISM_UNREG_DMB;
298 	cmd.request.hdr.len = sizeof(cmd.request);
299 
300 	cmd.request.dmb_tok = dmb->dmb_tok;
301 
302 	ret = ism_cmd(ism, &cmd);
303 	if (ret && ret != ISM_ERROR)
304 		goto out;
305 
306 	ism_free_dmb(ism, dmb);
307 out:
308 	return ret;
309 }
310 
311 static int smcd_unregister_dmb(struct smcd_dev *smcd, struct smcd_dmb *dmb)
312 {
313 	return ism_unregister_dmb(smcd, (struct ism_dmb *)dmb);
314 }
315 
316 static int ism_add_vlan_id(struct smcd_dev *smcd, u64 vlan_id)
317 {
318 	struct ism_dev *ism = smcd->priv;
319 	union ism_set_vlan_id cmd;
320 
321 	memset(&cmd, 0, sizeof(cmd));
322 	cmd.request.hdr.cmd = ISM_ADD_VLAN_ID;
323 	cmd.request.hdr.len = sizeof(cmd.request);
324 
325 	cmd.request.vlan_id = vlan_id;
326 
327 	return ism_cmd(ism, &cmd);
328 }
329 
330 static int ism_del_vlan_id(struct smcd_dev *smcd, u64 vlan_id)
331 {
332 	struct ism_dev *ism = smcd->priv;
333 	union ism_set_vlan_id cmd;
334 
335 	memset(&cmd, 0, sizeof(cmd));
336 	cmd.request.hdr.cmd = ISM_DEL_VLAN_ID;
337 	cmd.request.hdr.len = sizeof(cmd.request);
338 
339 	cmd.request.vlan_id = vlan_id;
340 
341 	return ism_cmd(ism, &cmd);
342 }
343 
344 static int ism_set_vlan_required(struct smcd_dev *smcd)
345 {
346 	return ism_cmd_simple(smcd->priv, ISM_SET_VLAN);
347 }
348 
349 static int ism_reset_vlan_required(struct smcd_dev *smcd)
350 {
351 	return ism_cmd_simple(smcd->priv, ISM_RESET_VLAN);
352 }
353 
354 static int ism_signal_ieq(struct smcd_dev *smcd, u64 rgid, u32 trigger_irq,
355 			  u32 event_code, u64 info)
356 {
357 	struct ism_dev *ism = smcd->priv;
358 	union ism_sig_ieq cmd;
359 
360 	memset(&cmd, 0, sizeof(cmd));
361 	cmd.request.hdr.cmd = ISM_SIGNAL_IEQ;
362 	cmd.request.hdr.len = sizeof(cmd.request);
363 
364 	cmd.request.rgid = rgid;
365 	cmd.request.trigger_irq = trigger_irq;
366 	cmd.request.event_code = event_code;
367 	cmd.request.info = info;
368 
369 	return ism_cmd(ism, &cmd);
370 }
371 
372 static unsigned int max_bytes(unsigned int start, unsigned int len,
373 			      unsigned int boundary)
374 {
375 	return min(boundary - (start & (boundary - 1)), len);
376 }
377 
378 static int ism_move(struct smcd_dev *smcd, u64 dmb_tok, unsigned int idx,
379 		    bool sf, unsigned int offset, void *data, unsigned int size)
380 {
381 	struct ism_dev *ism = smcd->priv;
382 	unsigned int bytes;
383 	u64 dmb_req;
384 	int ret;
385 
386 	while (size) {
387 		bytes = max_bytes(offset, size, PAGE_SIZE);
388 		dmb_req = ISM_CREATE_REQ(dmb_tok, idx, size == bytes ? sf : 0,
389 					 offset);
390 
391 		ret = __ism_move(ism, dmb_req, data, bytes);
392 		if (ret)
393 			return ret;
394 
395 		size -= bytes;
396 		data += bytes;
397 		offset += bytes;
398 	}
399 
400 	return 0;
401 }
402 
403 static struct ism_systemeid SYSTEM_EID = {
404 	.seid_string = "IBM-SYSZ-ISMSEID00000000",
405 	.serial_number = "0000",
406 	.type = "0000",
407 };
408 
409 static void ism_create_system_eid(void)
410 {
411 	struct cpuid id;
412 	u16 ident_tail;
413 	char tmp[5];
414 
415 	get_cpu_id(&id);
416 	ident_tail = (u16)(id.ident & ISM_IDENT_MASK);
417 	snprintf(tmp, 5, "%04X", ident_tail);
418 	memcpy(&SYSTEM_EID.serial_number, tmp, 4);
419 	snprintf(tmp, 5, "%04X", id.machine);
420 	memcpy(&SYSTEM_EID.type, tmp, 4);
421 }
422 
423 static u8 *ism_get_system_eid(void)
424 {
425 	return SYSTEM_EID.seid_string;
426 }
427 
428 static u16 ism_get_chid(struct smcd_dev *smcd)
429 {
430 	struct ism_dev *ism = (struct ism_dev *)smcd->priv;
431 
432 	if (!ism || !ism->pdev)
433 		return 0;
434 
435 	return to_zpci(ism->pdev)->pchid;
436 }
437 
438 static void ism_handle_event(struct ism_dev *ism)
439 {
440 	struct smcd_event *entry;
441 
442 	while ((ism->ieq_idx + 1) != READ_ONCE(ism->ieq->header.idx)) {
443 		if (++(ism->ieq_idx) == ARRAY_SIZE(ism->ieq->entry))
444 			ism->ieq_idx = 0;
445 
446 		entry = &ism->ieq->entry[ism->ieq_idx];
447 		debug_event(ism_debug_info, 2, entry, sizeof(*entry));
448 		smcd_handle_event(ism->smcd, entry);
449 	}
450 }
451 
452 static irqreturn_t ism_handle_irq(int irq, void *data)
453 {
454 	struct ism_dev *ism = data;
455 	unsigned long bit, end;
456 	unsigned long *bv;
457 	u16 dmbemask;
458 
459 	bv = (void *) &ism->sba->dmb_bits[ISM_DMB_WORD_OFFSET];
460 	end = sizeof(ism->sba->dmb_bits) * BITS_PER_BYTE - ISM_DMB_BIT_OFFSET;
461 
462 	spin_lock(&ism->lock);
463 	ism->sba->s = 0;
464 	barrier();
465 	for (bit = 0;;) {
466 		bit = find_next_bit_inv(bv, end, bit);
467 		if (bit >= end)
468 			break;
469 
470 		clear_bit_inv(bit, bv);
471 		dmbemask = ism->sba->dmbe_mask[bit + ISM_DMB_BIT_OFFSET];
472 		ism->sba->dmbe_mask[bit + ISM_DMB_BIT_OFFSET] = 0;
473 		barrier();
474 		smcd_handle_irq(ism->smcd, bit + ISM_DMB_BIT_OFFSET, dmbemask);
475 	}
476 
477 	if (ism->sba->e) {
478 		ism->sba->e = 0;
479 		barrier();
480 		ism_handle_event(ism);
481 	}
482 	spin_unlock(&ism->lock);
483 	return IRQ_HANDLED;
484 }
485 
486 static const struct smcd_ops ism_ops = {
487 	.query_remote_gid = ism_query_rgid,
488 	.register_dmb = smcd_register_dmb,
489 	.unregister_dmb = smcd_unregister_dmb,
490 	.add_vlan_id = ism_add_vlan_id,
491 	.del_vlan_id = ism_del_vlan_id,
492 	.set_vlan_required = ism_set_vlan_required,
493 	.reset_vlan_required = ism_reset_vlan_required,
494 	.signal_event = ism_signal_ieq,
495 	.move_data = ism_move,
496 	.get_system_eid = ism_get_system_eid,
497 	.get_chid = ism_get_chid,
498 };
499 
500 static int ism_dev_init(struct ism_dev *ism)
501 {
502 	struct pci_dev *pdev = ism->pdev;
503 	int ret;
504 
505 	ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
506 	if (ret <= 0)
507 		goto out;
508 
509 	ret = request_irq(pci_irq_vector(pdev, 0), ism_handle_irq, 0,
510 			  pci_name(pdev), ism);
511 	if (ret)
512 		goto free_vectors;
513 
514 	ret = register_sba(ism);
515 	if (ret)
516 		goto free_irq;
517 
518 	ret = register_ieq(ism);
519 	if (ret)
520 		goto unreg_sba;
521 
522 	ret = ism_read_local_gid(ism);
523 	if (ret)
524 		goto unreg_ieq;
525 
526 	if (!ism_add_vlan_id(ism->smcd, ISM_RESERVED_VLANID))
527 		/* hardware is V2 capable */
528 		ism_create_system_eid();
529 
530 	ret = smcd_register_dev(ism->smcd);
531 	if (ret)
532 		goto unreg_ieq;
533 
534 	query_info(ism);
535 	return 0;
536 
537 unreg_ieq:
538 	unregister_ieq(ism);
539 unreg_sba:
540 	unregister_sba(ism);
541 free_irq:
542 	free_irq(pci_irq_vector(pdev, 0), ism);
543 free_vectors:
544 	pci_free_irq_vectors(pdev);
545 out:
546 	return ret;
547 }
548 
549 static int ism_probe(struct pci_dev *pdev, const struct pci_device_id *id)
550 {
551 	struct ism_dev *ism;
552 	int ret;
553 
554 	ism = kzalloc(sizeof(*ism), GFP_KERNEL);
555 	if (!ism)
556 		return -ENOMEM;
557 
558 	spin_lock_init(&ism->lock);
559 	dev_set_drvdata(&pdev->dev, ism);
560 	ism->pdev = pdev;
561 
562 	ret = pci_enable_device_mem(pdev);
563 	if (ret)
564 		goto err;
565 
566 	ret = pci_request_mem_regions(pdev, DRV_NAME);
567 	if (ret)
568 		goto err_disable;
569 
570 	ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
571 	if (ret)
572 		goto err_resource;
573 
574 	dma_set_seg_boundary(&pdev->dev, SZ_1M - 1);
575 	dma_set_max_seg_size(&pdev->dev, SZ_1M);
576 	pci_set_master(pdev);
577 
578 	ism->smcd = smcd_alloc_dev(&pdev->dev, dev_name(&pdev->dev), &ism_ops,
579 				   ISM_NR_DMBS);
580 	if (!ism->smcd) {
581 		ret = -ENOMEM;
582 		goto err_resource;
583 	}
584 
585 	ism->smcd->priv = ism;
586 	ret = ism_dev_init(ism);
587 	if (ret)
588 		goto err_free;
589 
590 	return 0;
591 
592 err_free:
593 	smcd_free_dev(ism->smcd);
594 err_resource:
595 	pci_clear_master(pdev);
596 	pci_release_mem_regions(pdev);
597 err_disable:
598 	pci_disable_device(pdev);
599 err:
600 	kfree(ism);
601 	dev_set_drvdata(&pdev->dev, NULL);
602 	return ret;
603 }
604 
605 static void ism_dev_exit(struct ism_dev *ism)
606 {
607 	struct pci_dev *pdev = ism->pdev;
608 
609 	smcd_unregister_dev(ism->smcd);
610 	if (SYSTEM_EID.serial_number[0] != '0' ||
611 	    SYSTEM_EID.type[0] != '0')
612 		ism_del_vlan_id(ism->smcd, ISM_RESERVED_VLANID);
613 	unregister_ieq(ism);
614 	unregister_sba(ism);
615 	free_irq(pci_irq_vector(pdev, 0), ism);
616 	pci_free_irq_vectors(pdev);
617 }
618 
619 static void ism_remove(struct pci_dev *pdev)
620 {
621 	struct ism_dev *ism = dev_get_drvdata(&pdev->dev);
622 
623 	ism_dev_exit(ism);
624 
625 	smcd_free_dev(ism->smcd);
626 	pci_clear_master(pdev);
627 	pci_release_mem_regions(pdev);
628 	pci_disable_device(pdev);
629 	dev_set_drvdata(&pdev->dev, NULL);
630 	kfree(ism);
631 }
632 
633 static struct pci_driver ism_driver = {
634 	.name	  = DRV_NAME,
635 	.id_table = ism_device_table,
636 	.probe	  = ism_probe,
637 	.remove	  = ism_remove,
638 };
639 
640 static int __init ism_init(void)
641 {
642 	int ret;
643 
644 	ism_debug_info = debug_register("ism", 2, 1, 16);
645 	if (!ism_debug_info)
646 		return -ENODEV;
647 
648 	debug_register_view(ism_debug_info, &debug_hex_ascii_view);
649 	ret = pci_register_driver(&ism_driver);
650 	if (ret)
651 		debug_unregister(ism_debug_info);
652 
653 	return ret;
654 }
655 
656 static void __exit ism_exit(void)
657 {
658 	pci_unregister_driver(&ism_driver);
659 	debug_unregister(ism_debug_info);
660 }
661 
662 module_init(ism_init);
663 module_exit(ism_exit);
664