1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright IBM Corp. 2018
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>
18 #include <asm/debug.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 },
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
));
48 debug_text_event(ism_debug_info
, 0, "cmd failure");
49 debug_event(ism_debug_info
, 0, resp
, sizeof(*resp
));
52 __ism_read_cmd(ism
, resp
+ 1, sizeof(*resp
), resp
->len
- sizeof(*resp
));
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
)
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
))
79 debug_text_event(ism_debug_info
, 3, "query info");
80 debug_event(ism_debug_info
, 3, &cmd
.response
, sizeof(cmd
.response
));
85 static int register_sba(struct ism_dev
*ism
)
87 union ism_reg_sba cmd
;
88 dma_addr_t dma_handle
;
91 sba
= dma_alloc_coherent(&ism
->pdev
->dev
, PAGE_SIZE
, &dma_handle
,
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
);
107 ism
->sba_dma_addr
= dma_handle
;
112 static int register_ieq(struct ism_dev
*ism
)
114 union ism_reg_ieq cmd
;
115 dma_addr_t dma_handle
;
118 ieq
= dma_alloc_coherent(&ism
->pdev
->dev
, PAGE_SIZE
, &dma_handle
,
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
);
136 ism
->ieq_dma_addr
= dma_handle
;
141 static int unregister_sba(struct ism_dev
*ism
)
148 ret
= ism_cmd_simple(ism
, ISM_UNREG_SBA
);
149 if (ret
&& ret
!= ISM_ERROR
)
152 dma_free_coherent(&ism
->pdev
->dev
, PAGE_SIZE
,
153 ism
->sba
, ism
->sba_dma_addr
);
156 ism
->sba_dma_addr
= 0;
161 static int unregister_ieq(struct ism_dev
*ism
)
168 ret
= ism_cmd_simple(ism
, ISM_UNREG_IEQ
);
169 if (ret
&& ret
!= ISM_ERROR
)
172 dma_free_coherent(&ism
->pdev
->dev
, PAGE_SIZE
,
173 ism
->ieq
, ism
->ieq_dma_addr
);
176 ism
->ieq_dma_addr
= 0;
181 static int ism_read_local_gid(struct ism_dev
*ism
)
183 union ism_read_gid cmd
;
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
);
194 ism
->smcd
->local_gid
= cmd
.response
.gid
;
199 static int ism_query_rgid(struct smcd_dev
*smcd
, u64 rgid
, u32 vid_valid
,
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
)
227 if (PAGE_ALIGN(dmb
->dmb_len
) > dma_get_max_seg_size(&ism
->pdev
->dev
))
231 bit
= find_next_zero_bit(ism
->sba_bitmap
, ISM_NR_DMBS
,
233 if (bit
== ISM_NR_DMBS
)
238 if (dmb
->sba_idx
< ISM_DMB_BIT_OFFSET
||
239 test_and_set_bit(dmb
->sba_idx
, ism
->sba_bitmap
))
242 dmb
->cpu_addr
= dma_alloc_coherent(&ism
->pdev
->dev
, dmb
->dmb_len
,
244 GFP_KERNEL
| __GFP_NOWARN
| __GFP_NOMEMALLOC
| __GFP_COMP
| __GFP_NORETRY
);
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
;
257 ret
= ism_alloc_dmb(ism
, dmb
);
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
);
274 ism_free_dmb(ism
, dmb
);
277 dmb
->dmb_tok
= cmd
.response
.dmb_tok
;
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
;
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
)
298 ism_free_dmb(ism
, dmb
);
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
;
374 bytes
= max_bytes(offset
, size
, PAGE_SIZE
);
375 dmb_req
= ISM_CREATE_REQ(dmb_tok
, idx
, size
== bytes
? sf
: 0,
378 ret
= __ism_move(ism
, dmb_req
, data
, bytes
);
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
))
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
;
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
);
417 bit
= find_next_bit_inv(bv
, end
, bit
);
421 clear_bit_inv(bit
, bv
);
422 ism
->sba
->dmbe_mask
[bit
+ ISM_DMB_BIT_OFFSET
] = 0;
424 smcd_handle_irq(ism
->smcd
, bit
+ ISM_DMB_BIT_OFFSET
);
430 ism_handle_event(ism
);
432 spin_unlock(&ism
->lock
);
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
;
453 ret
= pci_alloc_irq_vectors(pdev
, 1, 1, PCI_IRQ_MSI
);
457 ret
= request_irq(pci_irq_vector(pdev
, 0), ism_handle_irq
, 0,
458 pci_name(pdev
), ism
);
462 ret
= register_sba(ism
);
466 ret
= register_ieq(ism
);
470 ret
= ism_read_local_gid(ism
);
474 ret
= smcd_register_dev(ism
->smcd
);
486 free_irq(pci_irq_vector(pdev
, 0), ism
);
488 pci_free_irq_vectors(pdev
);
493 static int ism_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
498 ism
= kzalloc(sizeof(*ism
), GFP_KERNEL
);
502 spin_lock_init(&ism
->lock
);
503 dev_set_drvdata(&pdev
->dev
, ism
);
506 ret
= pci_enable_device_mem(pdev
);
510 ret
= pci_request_mem_regions(pdev
, DRV_NAME
);
514 ret
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(64));
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
,
527 ism
->smcd
->priv
= ism
;
528 ret
= ism_dev_init(ism
);
535 smcd_free_dev(ism
->smcd
);
537 pci_release_mem_regions(pdev
);
539 pci_disable_device(pdev
);
542 dev_set_drvdata(&pdev
->dev
, NULL
);
546 static void ism_dev_exit(struct ism_dev
*ism
)
548 struct pci_dev
*pdev
= ism
->pdev
;
550 smcd_unregister_dev(ism
->smcd
);
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
);
563 smcd_free_dev(ism
->smcd
);
564 pci_release_mem_regions(pdev
);
565 pci_disable_device(pdev
);
566 dev_set_drvdata(&pdev
->dev
, NULL
);
570 static int ism_suspend(struct device
*dev
)
572 struct ism_dev
*ism
= dev_get_drvdata(dev
);
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
= {
589 .id_table
= ism_device_table
,
591 .remove
= ism_remove
,
597 static int __init
ism_init(void)
601 ism_debug_info
= debug_register("ism", 2, 1, 16);
605 debug_register_view(ism_debug_info
, &debug_hex_ascii_view
);
606 ret
= pci_register_driver(&ism_driver
);
608 debug_unregister(ism_debug_info
);
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
);