treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / s390 / net / ism_drv.c
blob4fc2056bd2272b2607d4d6a89c73b0e91c7c9015
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * ISM driver for s390.
5 * Copyright IBM Corp. 2018
6 */
7 #define KMSG_COMPONENT "ism"
8 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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 <net/smc.h>
18 #include <asm/debug.h>
20 #include "ism.h"
22 MODULE_DESCRIPTION("ISM driver for s390");
23 MODULE_LICENSE("GPL");
25 #define PCI_DEVICE_ID_IBM_ISM 0x04ED
26 #define DRV_NAME "ism"
28 static const struct pci_device_id ism_device_table[] = {
29 { PCI_VDEVICE(IBM, PCI_DEVICE_ID_IBM_ISM), 0 },
30 { 0, }
32 MODULE_DEVICE_TABLE(pci, ism_device_table);
34 static debug_info_t *ism_debug_info;
36 static int ism_cmd(struct ism_dev *ism, void *cmd)
38 struct ism_req_hdr *req = cmd;
39 struct ism_resp_hdr *resp = cmd;
41 __ism_write_cmd(ism, req + 1, sizeof(*req), req->len - sizeof(*req));
42 __ism_write_cmd(ism, req, 0, sizeof(*req));
44 WRITE_ONCE(resp->ret, ISM_ERROR);
46 __ism_read_cmd(ism, resp, 0, sizeof(*resp));
47 if (resp->ret) {
48 debug_text_event(ism_debug_info, 0, "cmd failure");
49 debug_event(ism_debug_info, 0, resp, sizeof(*resp));
50 goto out;
52 __ism_read_cmd(ism, resp + 1, sizeof(*resp), resp->len - sizeof(*resp));
53 out:
54 return resp->ret;
57 static int ism_cmd_simple(struct ism_dev *ism, u32 cmd_code)
59 union ism_cmd_simple cmd;
61 memset(&cmd, 0, sizeof(cmd));
62 cmd.request.hdr.cmd = cmd_code;
63 cmd.request.hdr.len = sizeof(cmd.request);
65 return ism_cmd(ism, &cmd);
68 static int query_info(struct ism_dev *ism)
70 union ism_qi cmd;
72 memset(&cmd, 0, sizeof(cmd));
73 cmd.request.hdr.cmd = ISM_QUERY_INFO;
74 cmd.request.hdr.len = sizeof(cmd.request);
76 if (ism_cmd(ism, &cmd))
77 goto out;
79 debug_text_event(ism_debug_info, 3, "query info");
80 debug_event(ism_debug_info, 3, &cmd.response, sizeof(cmd.response));
81 out:
82 return 0;
85 static int register_sba(struct ism_dev *ism)
87 union ism_reg_sba cmd;
88 dma_addr_t dma_handle;
89 struct ism_sba *sba;
91 sba = dma_alloc_coherent(&ism->pdev->dev, PAGE_SIZE, &dma_handle,
92 GFP_KERNEL);
93 if (!sba)
94 return -ENOMEM;
96 memset(&cmd, 0, sizeof(cmd));
97 cmd.request.hdr.cmd = ISM_REG_SBA;
98 cmd.request.hdr.len = sizeof(cmd.request);
99 cmd.request.sba = dma_handle;
101 if (ism_cmd(ism, &cmd)) {
102 dma_free_coherent(&ism->pdev->dev, PAGE_SIZE, sba, dma_handle);
103 return -EIO;
106 ism->sba = sba;
107 ism->sba_dma_addr = dma_handle;
109 return 0;
112 static int register_ieq(struct ism_dev *ism)
114 union ism_reg_ieq cmd;
115 dma_addr_t dma_handle;
116 struct ism_eq *ieq;
118 ieq = dma_alloc_coherent(&ism->pdev->dev, PAGE_SIZE, &dma_handle,
119 GFP_KERNEL);
120 if (!ieq)
121 return -ENOMEM;
123 memset(&cmd, 0, sizeof(cmd));
124 cmd.request.hdr.cmd = ISM_REG_IEQ;
125 cmd.request.hdr.len = sizeof(cmd.request);
126 cmd.request.ieq = dma_handle;
127 cmd.request.len = sizeof(*ieq);
129 if (ism_cmd(ism, &cmd)) {
130 dma_free_coherent(&ism->pdev->dev, PAGE_SIZE, ieq, dma_handle);
131 return -EIO;
134 ism->ieq = ieq;
135 ism->ieq_idx = -1;
136 ism->ieq_dma_addr = dma_handle;
138 return 0;
141 static int unregister_sba(struct ism_dev *ism)
143 int ret;
145 if (!ism->sba)
146 return 0;
148 ret = ism_cmd_simple(ism, ISM_UNREG_SBA);
149 if (ret && ret != ISM_ERROR)
150 return -EIO;
152 dma_free_coherent(&ism->pdev->dev, PAGE_SIZE,
153 ism->sba, ism->sba_dma_addr);
155 ism->sba = NULL;
156 ism->sba_dma_addr = 0;
158 return 0;
161 static int unregister_ieq(struct ism_dev *ism)
163 int ret;
165 if (!ism->ieq)
166 return 0;
168 ret = ism_cmd_simple(ism, ISM_UNREG_IEQ);
169 if (ret && ret != ISM_ERROR)
170 return -EIO;
172 dma_free_coherent(&ism->pdev->dev, PAGE_SIZE,
173 ism->ieq, ism->ieq_dma_addr);
175 ism->ieq = NULL;
176 ism->ieq_dma_addr = 0;
178 return 0;
181 static int ism_read_local_gid(struct ism_dev *ism)
183 union ism_read_gid cmd;
184 int ret;
186 memset(&cmd, 0, sizeof(cmd));
187 cmd.request.hdr.cmd = ISM_READ_GID;
188 cmd.request.hdr.len = sizeof(cmd.request);
190 ret = ism_cmd(ism, &cmd);
191 if (ret)
192 goto out;
194 ism->smcd->local_gid = cmd.response.gid;
195 out:
196 return ret;
199 static int ism_query_rgid(struct smcd_dev *smcd, u64 rgid, u32 vid_valid,
200 u32 vid)
202 struct ism_dev *ism = smcd->priv;
203 union ism_query_rgid cmd;
205 memset(&cmd, 0, sizeof(cmd));
206 cmd.request.hdr.cmd = ISM_QUERY_RGID;
207 cmd.request.hdr.len = sizeof(cmd.request);
209 cmd.request.rgid = rgid;
210 cmd.request.vlan_valid = vid_valid;
211 cmd.request.vlan_id = vid;
213 return ism_cmd(ism, &cmd);
216 static void ism_free_dmb(struct ism_dev *ism, struct smcd_dmb *dmb)
218 clear_bit(dmb->sba_idx, ism->sba_bitmap);
219 dma_free_coherent(&ism->pdev->dev, dmb->dmb_len,
220 dmb->cpu_addr, dmb->dma_addr);
223 static int ism_alloc_dmb(struct ism_dev *ism, struct smcd_dmb *dmb)
225 unsigned long bit;
227 if (PAGE_ALIGN(dmb->dmb_len) > dma_get_max_seg_size(&ism->pdev->dev))
228 return -EINVAL;
230 if (!dmb->sba_idx) {
231 bit = find_next_zero_bit(ism->sba_bitmap, ISM_NR_DMBS,
232 ISM_DMB_BIT_OFFSET);
233 if (bit == ISM_NR_DMBS)
234 return -ENOMEM;
236 dmb->sba_idx = bit;
238 if (dmb->sba_idx < ISM_DMB_BIT_OFFSET ||
239 test_and_set_bit(dmb->sba_idx, ism->sba_bitmap))
240 return -EINVAL;
242 dmb->cpu_addr = dma_alloc_coherent(&ism->pdev->dev, dmb->dmb_len,
243 &dmb->dma_addr,
244 GFP_KERNEL | __GFP_NOWARN | __GFP_NOMEMALLOC | __GFP_COMP | __GFP_NORETRY);
245 if (!dmb->cpu_addr)
246 clear_bit(dmb->sba_idx, ism->sba_bitmap);
248 return dmb->cpu_addr ? 0 : -ENOMEM;
251 static int ism_register_dmb(struct smcd_dev *smcd, struct smcd_dmb *dmb)
253 struct ism_dev *ism = smcd->priv;
254 union ism_reg_dmb cmd;
255 int ret;
257 ret = ism_alloc_dmb(ism, dmb);
258 if (ret)
259 goto out;
261 memset(&cmd, 0, sizeof(cmd));
262 cmd.request.hdr.cmd = ISM_REG_DMB;
263 cmd.request.hdr.len = sizeof(cmd.request);
265 cmd.request.dmb = dmb->dma_addr;
266 cmd.request.dmb_len = dmb->dmb_len;
267 cmd.request.sba_idx = dmb->sba_idx;
268 cmd.request.vlan_valid = dmb->vlan_valid;
269 cmd.request.vlan_id = dmb->vlan_id;
270 cmd.request.rgid = dmb->rgid;
272 ret = ism_cmd(ism, &cmd);
273 if (ret) {
274 ism_free_dmb(ism, dmb);
275 goto out;
277 dmb->dmb_tok = cmd.response.dmb_tok;
278 out:
279 return ret;
282 static int ism_unregister_dmb(struct smcd_dev *smcd, struct smcd_dmb *dmb)
284 struct ism_dev *ism = smcd->priv;
285 union ism_unreg_dmb cmd;
286 int ret;
288 memset(&cmd, 0, sizeof(cmd));
289 cmd.request.hdr.cmd = ISM_UNREG_DMB;
290 cmd.request.hdr.len = sizeof(cmd.request);
292 cmd.request.dmb_tok = dmb->dmb_tok;
294 ret = ism_cmd(ism, &cmd);
295 if (ret && ret != ISM_ERROR)
296 goto out;
298 ism_free_dmb(ism, dmb);
299 out:
300 return ret;
303 static int ism_add_vlan_id(struct smcd_dev *smcd, u64 vlan_id)
305 struct ism_dev *ism = smcd->priv;
306 union ism_set_vlan_id cmd;
308 memset(&cmd, 0, sizeof(cmd));
309 cmd.request.hdr.cmd = ISM_ADD_VLAN_ID;
310 cmd.request.hdr.len = sizeof(cmd.request);
312 cmd.request.vlan_id = vlan_id;
314 return ism_cmd(ism, &cmd);
317 static int ism_del_vlan_id(struct smcd_dev *smcd, u64 vlan_id)
319 struct ism_dev *ism = smcd->priv;
320 union ism_set_vlan_id cmd;
322 memset(&cmd, 0, sizeof(cmd));
323 cmd.request.hdr.cmd = ISM_DEL_VLAN_ID;
324 cmd.request.hdr.len = sizeof(cmd.request);
326 cmd.request.vlan_id = vlan_id;
328 return ism_cmd(ism, &cmd);
331 static int ism_set_vlan_required(struct smcd_dev *smcd)
333 return ism_cmd_simple(smcd->priv, ISM_SET_VLAN);
336 static int ism_reset_vlan_required(struct smcd_dev *smcd)
338 return ism_cmd_simple(smcd->priv, ISM_RESET_VLAN);
341 static int ism_signal_ieq(struct smcd_dev *smcd, u64 rgid, u32 trigger_irq,
342 u32 event_code, u64 info)
344 struct ism_dev *ism = smcd->priv;
345 union ism_sig_ieq cmd;
347 memset(&cmd, 0, sizeof(cmd));
348 cmd.request.hdr.cmd = ISM_SIGNAL_IEQ;
349 cmd.request.hdr.len = sizeof(cmd.request);
351 cmd.request.rgid = rgid;
352 cmd.request.trigger_irq = trigger_irq;
353 cmd.request.event_code = event_code;
354 cmd.request.info = info;
356 return ism_cmd(ism, &cmd);
359 static unsigned int max_bytes(unsigned int start, unsigned int len,
360 unsigned int boundary)
362 return min(boundary - (start & (boundary - 1)), len);
365 static int ism_move(struct smcd_dev *smcd, u64 dmb_tok, unsigned int idx,
366 bool sf, unsigned int offset, void *data, unsigned int size)
368 struct ism_dev *ism = smcd->priv;
369 unsigned int bytes;
370 u64 dmb_req;
371 int ret;
373 while (size) {
374 bytes = max_bytes(offset, size, PAGE_SIZE);
375 dmb_req = ISM_CREATE_REQ(dmb_tok, idx, size == bytes ? sf : 0,
376 offset);
378 ret = __ism_move(ism, dmb_req, data, bytes);
379 if (ret)
380 return ret;
382 size -= bytes;
383 data += bytes;
384 offset += bytes;
387 return 0;
390 static void ism_handle_event(struct ism_dev *ism)
392 struct smcd_event *entry;
394 while ((ism->ieq_idx + 1) != READ_ONCE(ism->ieq->header.idx)) {
395 if (++(ism->ieq_idx) == ARRAY_SIZE(ism->ieq->entry))
396 ism->ieq_idx = 0;
398 entry = &ism->ieq->entry[ism->ieq_idx];
399 debug_event(ism_debug_info, 2, entry, sizeof(*entry));
400 smcd_handle_event(ism->smcd, entry);
404 static irqreturn_t ism_handle_irq(int irq, void *data)
406 struct ism_dev *ism = data;
407 unsigned long bit, end;
408 unsigned long *bv;
410 bv = (void *) &ism->sba->dmb_bits[ISM_DMB_WORD_OFFSET];
411 end = sizeof(ism->sba->dmb_bits) * BITS_PER_BYTE - ISM_DMB_BIT_OFFSET;
413 spin_lock(&ism->lock);
414 ism->sba->s = 0;
415 barrier();
416 for (bit = 0;;) {
417 bit = find_next_bit_inv(bv, end, bit);
418 if (bit >= end)
419 break;
421 clear_bit_inv(bit, bv);
422 ism->sba->dmbe_mask[bit + ISM_DMB_BIT_OFFSET] = 0;
423 barrier();
424 smcd_handle_irq(ism->smcd, bit + ISM_DMB_BIT_OFFSET);
427 if (ism->sba->e) {
428 ism->sba->e = 0;
429 barrier();
430 ism_handle_event(ism);
432 spin_unlock(&ism->lock);
433 return IRQ_HANDLED;
436 static const struct smcd_ops ism_ops = {
437 .query_remote_gid = ism_query_rgid,
438 .register_dmb = ism_register_dmb,
439 .unregister_dmb = ism_unregister_dmb,
440 .add_vlan_id = ism_add_vlan_id,
441 .del_vlan_id = ism_del_vlan_id,
442 .set_vlan_required = ism_set_vlan_required,
443 .reset_vlan_required = ism_reset_vlan_required,
444 .signal_event = ism_signal_ieq,
445 .move_data = ism_move,
448 static int ism_dev_init(struct ism_dev *ism)
450 struct pci_dev *pdev = ism->pdev;
451 int ret;
453 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
454 if (ret <= 0)
455 goto out;
457 ret = request_irq(pci_irq_vector(pdev, 0), ism_handle_irq, 0,
458 pci_name(pdev), ism);
459 if (ret)
460 goto free_vectors;
462 ret = register_sba(ism);
463 if (ret)
464 goto free_irq;
466 ret = register_ieq(ism);
467 if (ret)
468 goto unreg_sba;
470 ret = ism_read_local_gid(ism);
471 if (ret)
472 goto unreg_ieq;
474 ret = smcd_register_dev(ism->smcd);
475 if (ret)
476 goto unreg_ieq;
478 query_info(ism);
479 return 0;
481 unreg_ieq:
482 unregister_ieq(ism);
483 unreg_sba:
484 unregister_sba(ism);
485 free_irq:
486 free_irq(pci_irq_vector(pdev, 0), ism);
487 free_vectors:
488 pci_free_irq_vectors(pdev);
489 out:
490 return ret;
493 static int ism_probe(struct pci_dev *pdev, const struct pci_device_id *id)
495 struct ism_dev *ism;
496 int ret;
498 ism = kzalloc(sizeof(*ism), GFP_KERNEL);
499 if (!ism)
500 return -ENOMEM;
502 spin_lock_init(&ism->lock);
503 dev_set_drvdata(&pdev->dev, ism);
504 ism->pdev = pdev;
506 ret = pci_enable_device_mem(pdev);
507 if (ret)
508 goto err;
510 ret = pci_request_mem_regions(pdev, DRV_NAME);
511 if (ret)
512 goto err_disable;
514 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
515 if (ret)
516 goto err_resource;
518 dma_set_seg_boundary(&pdev->dev, SZ_1M - 1);
519 dma_set_max_seg_size(&pdev->dev, SZ_1M);
520 pci_set_master(pdev);
522 ism->smcd = smcd_alloc_dev(&pdev->dev, dev_name(&pdev->dev), &ism_ops,
523 ISM_NR_DMBS);
524 if (!ism->smcd)
525 goto err_resource;
527 ism->smcd->priv = ism;
528 ret = ism_dev_init(ism);
529 if (ret)
530 goto err_free;
532 return 0;
534 err_free:
535 smcd_free_dev(ism->smcd);
536 err_resource:
537 pci_release_mem_regions(pdev);
538 err_disable:
539 pci_disable_device(pdev);
540 err:
541 kfree(ism);
542 dev_set_drvdata(&pdev->dev, NULL);
543 return ret;
546 static void ism_dev_exit(struct ism_dev *ism)
548 struct pci_dev *pdev = ism->pdev;
550 smcd_unregister_dev(ism->smcd);
551 unregister_ieq(ism);
552 unregister_sba(ism);
553 free_irq(pci_irq_vector(pdev, 0), ism);
554 pci_free_irq_vectors(pdev);
557 static void ism_remove(struct pci_dev *pdev)
559 struct ism_dev *ism = dev_get_drvdata(&pdev->dev);
561 ism_dev_exit(ism);
563 smcd_free_dev(ism->smcd);
564 pci_release_mem_regions(pdev);
565 pci_disable_device(pdev);
566 dev_set_drvdata(&pdev->dev, NULL);
567 kfree(ism);
570 static int ism_suspend(struct device *dev)
572 struct ism_dev *ism = dev_get_drvdata(dev);
574 ism_dev_exit(ism);
575 return 0;
578 static int ism_resume(struct device *dev)
580 struct ism_dev *ism = dev_get_drvdata(dev);
582 return ism_dev_init(ism);
585 static SIMPLE_DEV_PM_OPS(ism_pm_ops, ism_suspend, ism_resume);
587 static struct pci_driver ism_driver = {
588 .name = DRV_NAME,
589 .id_table = ism_device_table,
590 .probe = ism_probe,
591 .remove = ism_remove,
592 .driver = {
593 .pm = &ism_pm_ops,
597 static int __init ism_init(void)
599 int ret;
601 ism_debug_info = debug_register("ism", 2, 1, 16);
602 if (!ism_debug_info)
603 return -ENODEV;
605 debug_register_view(ism_debug_info, &debug_hex_ascii_view);
606 ret = pci_register_driver(&ism_driver);
607 if (ret)
608 debug_unregister(ism_debug_info);
610 return ret;
613 static void __exit ism_exit(void)
615 pci_unregister_driver(&ism_driver);
616 debug_unregister(ism_debug_info);
619 module_init(ism_init);
620 module_exit(ism_exit);