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/err.h>
15 #include <linux/ctype.h>
16 #include <linux/processor.h>
20 MODULE_DESCRIPTION("ISM driver for s390");
21 MODULE_LICENSE("GPL");
23 #define PCI_DEVICE_ID_IBM_ISM 0x04ED
24 #define DRV_NAME "ism"
26 static const struct pci_device_id ism_device_table[] = {
27 { PCI_VDEVICE(IBM, PCI_DEVICE_ID_IBM_ISM), 0 },
30 MODULE_DEVICE_TABLE(pci, ism_device_table);
32 static debug_info_t *ism_debug_info;
33 static const struct smcd_ops ism_ops;
35 #define NO_CLIENT 0xff /* must be >= MAX_CLIENTS */
36 static struct ism_client *clients[MAX_CLIENTS]; /* use an array rather than */
37 /* a list for fast mapping */
39 static DEFINE_MUTEX(clients_lock);
41 struct list_head list;
42 struct mutex mutex; /* protects ism device list */
45 static struct ism_dev_list ism_dev_list = {
46 .list = LIST_HEAD_INIT(ism_dev_list.list),
47 .mutex = __MUTEX_INITIALIZER(ism_dev_list.mutex),
50 static void ism_setup_forwarding(struct ism_client *client, struct ism_dev *ism)
54 spin_lock_irqsave(&ism->lock, flags);
55 ism->subs[client->id] = client;
56 spin_unlock_irqrestore(&ism->lock, flags);
59 int ism_register_client(struct ism_client *client)
64 mutex_lock(&ism_dev_list.mutex);
65 mutex_lock(&clients_lock);
66 for (i = 0; i < MAX_CLIENTS; ++i) {
76 mutex_unlock(&clients_lock);
78 if (i < MAX_CLIENTS) {
79 /* initialize with all devices that we got so far */
80 list_for_each_entry(ism, &ism_dev_list.list, list) {
83 ism_setup_forwarding(client, ism);
86 mutex_unlock(&ism_dev_list.mutex);
90 EXPORT_SYMBOL_GPL(ism_register_client);
92 int ism_unregister_client(struct ism_client *client)
98 mutex_lock(&ism_dev_list.mutex);
99 list_for_each_entry(ism, &ism_dev_list.list, list) {
100 spin_lock_irqsave(&ism->lock, flags);
101 /* Stop forwarding IRQs and events */
102 ism->subs[client->id] = NULL;
103 for (int i = 0; i < ISM_NR_DMBS; ++i) {
104 if (ism->sba_client_arr[i] == client->id) {
105 WARN(1, "%s: attempt to unregister '%s' with registered dmb(s)\n",
106 __func__, client->name);
111 spin_unlock_irqrestore(&ism->lock, flags);
113 mutex_unlock(&ism_dev_list.mutex);
115 mutex_lock(&clients_lock);
116 clients[client->id] = NULL;
117 if (client->id + 1 == max_client)
119 mutex_unlock(&clients_lock);
123 spin_unlock_irqrestore(&ism->lock, flags);
124 mutex_unlock(&ism_dev_list.mutex);
127 EXPORT_SYMBOL_GPL(ism_unregister_client);
129 static int ism_cmd(struct ism_dev *ism, void *cmd)
131 struct ism_req_hdr *req = cmd;
132 struct ism_resp_hdr *resp = cmd;
134 __ism_write_cmd(ism, req + 1, sizeof(*req), req->len - sizeof(*req));
135 __ism_write_cmd(ism, req, 0, sizeof(*req));
137 WRITE_ONCE(resp->ret, ISM_ERROR);
139 __ism_read_cmd(ism, resp, 0, sizeof(*resp));
141 debug_text_event(ism_debug_info, 0, "cmd failure");
142 debug_event(ism_debug_info, 0, resp, sizeof(*resp));
145 __ism_read_cmd(ism, resp + 1, sizeof(*resp), resp->len - sizeof(*resp));
150 static int ism_cmd_simple(struct ism_dev *ism, u32 cmd_code)
152 union ism_cmd_simple cmd;
154 memset(&cmd, 0, sizeof(cmd));
155 cmd.request.hdr.cmd = cmd_code;
156 cmd.request.hdr.len = sizeof(cmd.request);
158 return ism_cmd(ism, &cmd);
161 static int query_info(struct ism_dev *ism)
165 memset(&cmd, 0, sizeof(cmd));
166 cmd.request.hdr.cmd = ISM_QUERY_INFO;
167 cmd.request.hdr.len = sizeof(cmd.request);
169 if (ism_cmd(ism, &cmd))
172 debug_text_event(ism_debug_info, 3, "query info");
173 debug_event(ism_debug_info, 3, &cmd.response, sizeof(cmd.response));
178 static int register_sba(struct ism_dev *ism)
180 union ism_reg_sba cmd;
181 dma_addr_t dma_handle;
184 sba = dma_alloc_coherent(&ism->pdev->dev, PAGE_SIZE, &dma_handle,
189 memset(&cmd, 0, sizeof(cmd));
190 cmd.request.hdr.cmd = ISM_REG_SBA;
191 cmd.request.hdr.len = sizeof(cmd.request);
192 cmd.request.sba = dma_handle;
194 if (ism_cmd(ism, &cmd)) {
195 dma_free_coherent(&ism->pdev->dev, PAGE_SIZE, sba, dma_handle);
200 ism->sba_dma_addr = dma_handle;
205 static int register_ieq(struct ism_dev *ism)
207 union ism_reg_ieq cmd;
208 dma_addr_t dma_handle;
211 ieq = dma_alloc_coherent(&ism->pdev->dev, PAGE_SIZE, &dma_handle,
216 memset(&cmd, 0, sizeof(cmd));
217 cmd.request.hdr.cmd = ISM_REG_IEQ;
218 cmd.request.hdr.len = sizeof(cmd.request);
219 cmd.request.ieq = dma_handle;
220 cmd.request.len = sizeof(*ieq);
222 if (ism_cmd(ism, &cmd)) {
223 dma_free_coherent(&ism->pdev->dev, PAGE_SIZE, ieq, dma_handle);
229 ism->ieq_dma_addr = dma_handle;
234 static int unregister_sba(struct ism_dev *ism)
241 ret = ism_cmd_simple(ism, ISM_UNREG_SBA);
242 if (ret && ret != ISM_ERROR)
245 dma_free_coherent(&ism->pdev->dev, PAGE_SIZE,
246 ism->sba, ism->sba_dma_addr);
249 ism->sba_dma_addr = 0;
254 static int unregister_ieq(struct ism_dev *ism)
261 ret = ism_cmd_simple(ism, ISM_UNREG_IEQ);
262 if (ret && ret != ISM_ERROR)
265 dma_free_coherent(&ism->pdev->dev, PAGE_SIZE,
266 ism->ieq, ism->ieq_dma_addr);
269 ism->ieq_dma_addr = 0;
274 static int ism_read_local_gid(struct ism_dev *ism)
276 union ism_read_gid cmd;
279 memset(&cmd, 0, sizeof(cmd));
280 cmd.request.hdr.cmd = ISM_READ_GID;
281 cmd.request.hdr.len = sizeof(cmd.request);
283 ret = ism_cmd(ism, &cmd);
287 ism->local_gid = cmd.response.gid;
292 static int ism_query_rgid(struct ism_dev *ism, u64 rgid, u32 vid_valid,
295 union ism_query_rgid cmd;
297 memset(&cmd, 0, sizeof(cmd));
298 cmd.request.hdr.cmd = ISM_QUERY_RGID;
299 cmd.request.hdr.len = sizeof(cmd.request);
301 cmd.request.rgid = rgid;
302 cmd.request.vlan_valid = vid_valid;
303 cmd.request.vlan_id = vid;
305 return ism_cmd(ism, &cmd);
308 static void ism_free_dmb(struct ism_dev *ism, struct ism_dmb *dmb)
310 clear_bit(dmb->sba_idx, ism->sba_bitmap);
311 dma_free_coherent(&ism->pdev->dev, dmb->dmb_len,
312 dmb->cpu_addr, dmb->dma_addr);
315 static int ism_alloc_dmb(struct ism_dev *ism, struct ism_dmb *dmb)
319 if (PAGE_ALIGN(dmb->dmb_len) > dma_get_max_seg_size(&ism->pdev->dev))
323 bit = find_next_zero_bit(ism->sba_bitmap, ISM_NR_DMBS,
325 if (bit == ISM_NR_DMBS)
330 if (dmb->sba_idx < ISM_DMB_BIT_OFFSET ||
331 test_and_set_bit(dmb->sba_idx, ism->sba_bitmap))
334 dmb->cpu_addr = dma_alloc_coherent(&ism->pdev->dev, dmb->dmb_len,
336 GFP_KERNEL | __GFP_NOWARN |
337 __GFP_NOMEMALLOC | __GFP_NORETRY);
339 clear_bit(dmb->sba_idx, ism->sba_bitmap);
341 return dmb->cpu_addr ? 0 : -ENOMEM;
344 int ism_register_dmb(struct ism_dev *ism, struct ism_dmb *dmb,
345 struct ism_client *client)
347 union ism_reg_dmb cmd;
351 ret = ism_alloc_dmb(ism, dmb);
355 memset(&cmd, 0, sizeof(cmd));
356 cmd.request.hdr.cmd = ISM_REG_DMB;
357 cmd.request.hdr.len = sizeof(cmd.request);
359 cmd.request.dmb = dmb->dma_addr;
360 cmd.request.dmb_len = dmb->dmb_len;
361 cmd.request.sba_idx = dmb->sba_idx;
362 cmd.request.vlan_valid = dmb->vlan_valid;
363 cmd.request.vlan_id = dmb->vlan_id;
364 cmd.request.rgid = dmb->rgid;
366 ret = ism_cmd(ism, &cmd);
368 ism_free_dmb(ism, dmb);
371 dmb->dmb_tok = cmd.response.dmb_tok;
372 spin_lock_irqsave(&ism->lock, flags);
373 ism->sba_client_arr[dmb->sba_idx - ISM_DMB_BIT_OFFSET] = client->id;
374 spin_unlock_irqrestore(&ism->lock, flags);
378 EXPORT_SYMBOL_GPL(ism_register_dmb);
380 int ism_unregister_dmb(struct ism_dev *ism, struct ism_dmb *dmb)
382 union ism_unreg_dmb cmd;
386 memset(&cmd, 0, sizeof(cmd));
387 cmd.request.hdr.cmd = ISM_UNREG_DMB;
388 cmd.request.hdr.len = sizeof(cmd.request);
390 cmd.request.dmb_tok = dmb->dmb_tok;
392 spin_lock_irqsave(&ism->lock, flags);
393 ism->sba_client_arr[dmb->sba_idx - ISM_DMB_BIT_OFFSET] = NO_CLIENT;
394 spin_unlock_irqrestore(&ism->lock, flags);
396 ret = ism_cmd(ism, &cmd);
397 if (ret && ret != ISM_ERROR)
400 ism_free_dmb(ism, dmb);
404 EXPORT_SYMBOL_GPL(ism_unregister_dmb);
406 static int ism_add_vlan_id(struct ism_dev *ism, u64 vlan_id)
408 union ism_set_vlan_id cmd;
410 memset(&cmd, 0, sizeof(cmd));
411 cmd.request.hdr.cmd = ISM_ADD_VLAN_ID;
412 cmd.request.hdr.len = sizeof(cmd.request);
414 cmd.request.vlan_id = vlan_id;
416 return ism_cmd(ism, &cmd);
419 static int ism_del_vlan_id(struct ism_dev *ism, u64 vlan_id)
421 union ism_set_vlan_id cmd;
423 memset(&cmd, 0, sizeof(cmd));
424 cmd.request.hdr.cmd = ISM_DEL_VLAN_ID;
425 cmd.request.hdr.len = sizeof(cmd.request);
427 cmd.request.vlan_id = vlan_id;
429 return ism_cmd(ism, &cmd);
432 static int ism_signal_ieq(struct ism_dev *ism, u64 rgid, u32 trigger_irq,
433 u32 event_code, u64 info)
435 union ism_sig_ieq cmd;
437 memset(&cmd, 0, sizeof(cmd));
438 cmd.request.hdr.cmd = ISM_SIGNAL_IEQ;
439 cmd.request.hdr.len = sizeof(cmd.request);
441 cmd.request.rgid = rgid;
442 cmd.request.trigger_irq = trigger_irq;
443 cmd.request.event_code = event_code;
444 cmd.request.info = info;
446 return ism_cmd(ism, &cmd);
449 static unsigned int max_bytes(unsigned int start, unsigned int len,
450 unsigned int boundary)
452 return min(boundary - (start & (boundary - 1)), len);
455 int ism_move(struct ism_dev *ism, u64 dmb_tok, unsigned int idx, bool sf,
456 unsigned int offset, void *data, unsigned int size)
463 bytes = max_bytes(offset, size, PAGE_SIZE);
464 dmb_req = ISM_CREATE_REQ(dmb_tok, idx, size == bytes ? sf : 0,
467 ret = __ism_move(ism, dmb_req, data, bytes);
478 EXPORT_SYMBOL_GPL(ism_move);
480 static struct ism_systemeid SYSTEM_EID = {
481 .seid_string = "IBM-SYSZ-ISMSEID00000000",
482 .serial_number = "0000",
486 static void ism_create_system_eid(void)
493 ident_tail = (u16)(id.ident & ISM_IDENT_MASK);
494 snprintf(tmp, 5, "%04X", ident_tail);
495 memcpy(&SYSTEM_EID.serial_number, tmp, 4);
496 snprintf(tmp, 5, "%04X", id.machine);
497 memcpy(&SYSTEM_EID.type, tmp, 4);
500 u8 *ism_get_seid(void)
502 return SYSTEM_EID.seid_string;
504 EXPORT_SYMBOL_GPL(ism_get_seid);
506 static u16 ism_get_chid(struct ism_dev *ism)
508 if (!ism || !ism->pdev)
511 return to_zpci(ism->pdev)->pchid;
514 static void ism_handle_event(struct ism_dev *ism)
516 struct ism_event *entry;
517 struct ism_client *clt;
520 while ((ism->ieq_idx + 1) != READ_ONCE(ism->ieq->header.idx)) {
521 if (++(ism->ieq_idx) == ARRAY_SIZE(ism->ieq->entry))
524 entry = &ism->ieq->entry[ism->ieq_idx];
525 debug_event(ism_debug_info, 2, entry, sizeof(*entry));
526 for (i = 0; i < max_client; ++i) {
529 clt->handle_event(ism, entry);
534 static irqreturn_t ism_handle_irq(int irq, void *data)
536 struct ism_dev *ism = data;
537 unsigned long bit, end;
542 bv = (void *) &ism->sba->dmb_bits[ISM_DMB_WORD_OFFSET];
543 end = sizeof(ism->sba->dmb_bits) * BITS_PER_BYTE - ISM_DMB_BIT_OFFSET;
545 spin_lock(&ism->lock);
549 bit = find_next_bit_inv(bv, end, bit);
553 clear_bit_inv(bit, bv);
554 dmbemask = ism->sba->dmbe_mask[bit + ISM_DMB_BIT_OFFSET];
555 ism->sba->dmbe_mask[bit + ISM_DMB_BIT_OFFSET] = 0;
557 client_id = ism->sba_client_arr[bit];
558 if (unlikely(client_id == NO_CLIENT || !ism->subs[client_id]))
560 ism->subs[client_id]->handle_irq(ism, bit + ISM_DMB_BIT_OFFSET, dmbemask);
566 ism_handle_event(ism);
568 spin_unlock(&ism->lock);
572 static u64 ism_get_local_gid(struct ism_dev *ism)
574 return ism->local_gid;
577 static int ism_dev_init(struct ism_dev *ism)
579 struct pci_dev *pdev = ism->pdev;
582 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
586 ism->sba_client_arr = kzalloc(ISM_NR_DMBS, GFP_KERNEL);
587 if (!ism->sba_client_arr)
589 memset(ism->sba_client_arr, NO_CLIENT, ISM_NR_DMBS);
591 ret = request_irq(pci_irq_vector(pdev, 0), ism_handle_irq, 0,
592 pci_name(pdev), ism);
594 goto free_client_arr;
596 ret = register_sba(ism);
600 ret = register_ieq(ism);
604 ret = ism_read_local_gid(ism);
608 if (!ism_add_vlan_id(ism, ISM_RESERVED_VLANID))
609 /* hardware is V2 capable */
610 ism_create_system_eid();
612 mutex_lock(&ism_dev_list.mutex);
613 mutex_lock(&clients_lock);
614 for (i = 0; i < max_client; ++i) {
616 clients[i]->add(ism);
617 ism_setup_forwarding(clients[i], ism);
620 mutex_unlock(&clients_lock);
622 list_add(&ism->list, &ism_dev_list.list);
623 mutex_unlock(&ism_dev_list.mutex);
633 free_irq(pci_irq_vector(pdev, 0), ism);
635 kfree(ism->sba_client_arr);
637 pci_free_irq_vectors(pdev);
642 static int ism_probe(struct pci_dev *pdev, const struct pci_device_id *id)
647 ism = kzalloc(sizeof(*ism), GFP_KERNEL);
651 spin_lock_init(&ism->lock);
652 dev_set_drvdata(&pdev->dev, ism);
654 ism->dev.parent = &pdev->dev;
655 device_initialize(&ism->dev);
656 dev_set_name(&ism->dev, dev_name(&pdev->dev));
657 ret = device_add(&ism->dev);
661 ret = pci_enable_device_mem(pdev);
665 ret = pci_request_mem_regions(pdev, DRV_NAME);
669 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
673 dma_set_seg_boundary(&pdev->dev, SZ_1M - 1);
674 dma_set_max_seg_size(&pdev->dev, SZ_1M);
675 pci_set_master(pdev);
677 ret = ism_dev_init(ism);
684 pci_release_mem_regions(pdev);
686 pci_disable_device(pdev);
688 device_del(&ism->dev);
690 dev_set_drvdata(&pdev->dev, NULL);
696 static void ism_dev_exit(struct ism_dev *ism)
698 struct pci_dev *pdev = ism->pdev;
702 spin_lock_irqsave(&ism->lock, flags);
703 for (i = 0; i < max_client; ++i)
705 spin_unlock_irqrestore(&ism->lock, flags);
707 mutex_lock(&ism_dev_list.mutex);
708 mutex_lock(&clients_lock);
709 for (i = 0; i < max_client; ++i) {
711 clients[i]->remove(ism);
713 mutex_unlock(&clients_lock);
715 if (SYSTEM_EID.serial_number[0] != '0' ||
716 SYSTEM_EID.type[0] != '0')
717 ism_del_vlan_id(ism, ISM_RESERVED_VLANID);
720 free_irq(pci_irq_vector(pdev, 0), ism);
721 kfree(ism->sba_client_arr);
722 pci_free_irq_vectors(pdev);
723 list_del_init(&ism->list);
724 mutex_unlock(&ism_dev_list.mutex);
727 static void ism_remove(struct pci_dev *pdev)
729 struct ism_dev *ism = dev_get_drvdata(&pdev->dev);
733 pci_release_mem_regions(pdev);
734 pci_disable_device(pdev);
735 device_del(&ism->dev);
736 dev_set_drvdata(&pdev->dev, NULL);
740 static struct pci_driver ism_driver = {
742 .id_table = ism_device_table,
744 .remove = ism_remove,
747 static int __init ism_init(void)
751 ism_debug_info = debug_register("ism", 2, 1, 16);
755 memset(clients, 0, sizeof(clients));
757 debug_register_view(ism_debug_info, &debug_hex_ascii_view);
758 ret = pci_register_driver(&ism_driver);
760 debug_unregister(ism_debug_info);
765 static void __exit ism_exit(void)
767 pci_unregister_driver(&ism_driver);
768 debug_unregister(ism_debug_info);
771 module_init(ism_init);
772 module_exit(ism_exit);
774 /*************************** SMC-D Implementation *****************************/
776 #if IS_ENABLED(CONFIG_SMC)
777 static int smcd_query_rgid(struct smcd_dev *smcd, u64 rgid, u32 vid_valid,
780 return ism_query_rgid(smcd->priv, rgid, vid_valid, vid);
783 static int smcd_register_dmb(struct smcd_dev *smcd, struct smcd_dmb *dmb,
784 struct ism_client *client)
786 return ism_register_dmb(smcd->priv, (struct ism_dmb *)dmb, client);
789 static int smcd_unregister_dmb(struct smcd_dev *smcd, struct smcd_dmb *dmb)
791 return ism_unregister_dmb(smcd->priv, (struct ism_dmb *)dmb);
794 static int smcd_add_vlan_id(struct smcd_dev *smcd, u64 vlan_id)
796 return ism_add_vlan_id(smcd->priv, vlan_id);
799 static int smcd_del_vlan_id(struct smcd_dev *smcd, u64 vlan_id)
801 return ism_del_vlan_id(smcd->priv, vlan_id);
804 static int smcd_set_vlan_required(struct smcd_dev *smcd)
806 return ism_cmd_simple(smcd->priv, ISM_SET_VLAN);
809 static int smcd_reset_vlan_required(struct smcd_dev *smcd)
811 return ism_cmd_simple(smcd->priv, ISM_RESET_VLAN);
814 static int smcd_signal_ieq(struct smcd_dev *smcd, u64 rgid, u32 trigger_irq,
815 u32 event_code, u64 info)
817 return ism_signal_ieq(smcd->priv, rgid, trigger_irq, event_code, info);
820 static int smcd_move(struct smcd_dev *smcd, u64 dmb_tok, unsigned int idx,
821 bool sf, unsigned int offset, void *data,
824 return ism_move(smcd->priv, dmb_tok, idx, sf, offset, data, size);
827 static int smcd_supports_v2(void)
829 return SYSTEM_EID.serial_number[0] != '0' ||
830 SYSTEM_EID.type[0] != '0';
833 static u64 smcd_get_local_gid(struct smcd_dev *smcd)
835 return ism_get_local_gid(smcd->priv);
838 static u16 smcd_get_chid(struct smcd_dev *smcd)
840 return ism_get_chid(smcd->priv);
843 static inline struct device *smcd_get_dev(struct smcd_dev *dev)
845 struct ism_dev *ism = dev->priv;
850 static const struct smcd_ops ism_ops = {
851 .query_remote_gid = smcd_query_rgid,
852 .register_dmb = smcd_register_dmb,
853 .unregister_dmb = smcd_unregister_dmb,
854 .add_vlan_id = smcd_add_vlan_id,
855 .del_vlan_id = smcd_del_vlan_id,
856 .set_vlan_required = smcd_set_vlan_required,
857 .reset_vlan_required = smcd_reset_vlan_required,
858 .signal_event = smcd_signal_ieq,
859 .move_data = smcd_move,
860 .supports_v2 = smcd_supports_v2,
861 .get_system_eid = ism_get_seid,
862 .get_local_gid = smcd_get_local_gid,
863 .get_chid = smcd_get_chid,
864 .get_dev = smcd_get_dev,
867 const struct smcd_ops *ism_get_smcd_ops(void)
871 EXPORT_SYMBOL_GPL(ism_get_smcd_ops);