xref: /openbmc/linux/drivers/s390/net/ism_drv.c (revision 017cdefe)
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 
19 #include "ism.h"
20 
21 MODULE_DESCRIPTION("ISM driver for s390");
22 MODULE_LICENSE("GPL");
23 
24 #define PCI_DEVICE_ID_IBM_ISM 0x04ED
25 #define DRV_NAME "ism"
26 
27 static const struct pci_device_id ism_device_table[] = {
28 	{ PCI_VDEVICE(IBM, PCI_DEVICE_ID_IBM_ISM), 0 },
29 	{ 0, }
30 };
31 MODULE_DEVICE_TABLE(pci, ism_device_table);
32 
33 static debug_info_t *ism_debug_info;
34 static const struct smcd_ops ism_ops;
35 
36 #define NO_CLIENT		0xff		/* must be >= MAX_CLIENTS */
37 static struct ism_client *clients[MAX_CLIENTS];	/* use an array rather than */
38 						/* a list for fast mapping  */
39 static u8 max_client;
40 static DEFINE_SPINLOCK(clients_lock);
41 struct ism_dev_list {
42 	struct list_head list;
43 	struct mutex mutex; /* protects ism device list */
44 };
45 
46 static struct ism_dev_list ism_dev_list = {
47 	.list = LIST_HEAD_INIT(ism_dev_list.list),
48 	.mutex = __MUTEX_INITIALIZER(ism_dev_list.mutex),
49 };
50 
51 int ism_register_client(struct ism_client *client)
52 {
53 	struct ism_dev *ism;
54 	unsigned long flags;
55 	int i, rc = -ENOSPC;
56 
57 	mutex_lock(&ism_dev_list.mutex);
58 	spin_lock_irqsave(&clients_lock, flags);
59 	for (i = 0; i < MAX_CLIENTS; ++i) {
60 		if (!clients[i]) {
61 			clients[i] = client;
62 			client->id = i;
63 			if (i == max_client)
64 				max_client++;
65 			rc = 0;
66 			break;
67 		}
68 	}
69 	spin_unlock_irqrestore(&clients_lock, flags);
70 	if (i < MAX_CLIENTS) {
71 		/* initialize with all devices that we got so far */
72 		list_for_each_entry(ism, &ism_dev_list.list, list) {
73 			ism->priv[i] = NULL;
74 			client->add(ism);
75 		}
76 	}
77 	mutex_unlock(&ism_dev_list.mutex);
78 
79 	return rc;
80 }
81 EXPORT_SYMBOL_GPL(ism_register_client);
82 
83 int ism_unregister_client(struct ism_client *client)
84 {
85 	struct ism_dev *ism;
86 	unsigned long flags;
87 	int rc = 0;
88 
89 	mutex_lock(&ism_dev_list.mutex);
90 	spin_lock_irqsave(&clients_lock, flags);
91 	clients[client->id] = NULL;
92 	if (client->id + 1 == max_client)
93 		max_client--;
94 	spin_unlock_irqrestore(&clients_lock, flags);
95 	list_for_each_entry(ism, &ism_dev_list.list, list) {
96 		for (int i = 0; i < ISM_NR_DMBS; ++i) {
97 			if (ism->sba_client_arr[i] == client->id) {
98 				pr_err("%s: attempt to unregister client '%s'"
99 				       "with registered dmb(s)\n", __func__,
100 				       client->name);
101 				rc = -EBUSY;
102 				goto out;
103 			}
104 		}
105 	}
106 out:
107 	mutex_unlock(&ism_dev_list.mutex);
108 
109 	return rc;
110 }
111 EXPORT_SYMBOL_GPL(ism_unregister_client);
112 
113 static int ism_cmd(struct ism_dev *ism, void *cmd)
114 {
115 	struct ism_req_hdr *req = cmd;
116 	struct ism_resp_hdr *resp = cmd;
117 
118 	__ism_write_cmd(ism, req + 1, sizeof(*req), req->len - sizeof(*req));
119 	__ism_write_cmd(ism, req, 0, sizeof(*req));
120 
121 	WRITE_ONCE(resp->ret, ISM_ERROR);
122 
123 	__ism_read_cmd(ism, resp, 0, sizeof(*resp));
124 	if (resp->ret) {
125 		debug_text_event(ism_debug_info, 0, "cmd failure");
126 		debug_event(ism_debug_info, 0, resp, sizeof(*resp));
127 		goto out;
128 	}
129 	__ism_read_cmd(ism, resp + 1, sizeof(*resp), resp->len - sizeof(*resp));
130 out:
131 	return resp->ret;
132 }
133 
134 static int ism_cmd_simple(struct ism_dev *ism, u32 cmd_code)
135 {
136 	union ism_cmd_simple cmd;
137 
138 	memset(&cmd, 0, sizeof(cmd));
139 	cmd.request.hdr.cmd = cmd_code;
140 	cmd.request.hdr.len = sizeof(cmd.request);
141 
142 	return ism_cmd(ism, &cmd);
143 }
144 
145 static int query_info(struct ism_dev *ism)
146 {
147 	union ism_qi cmd;
148 
149 	memset(&cmd, 0, sizeof(cmd));
150 	cmd.request.hdr.cmd = ISM_QUERY_INFO;
151 	cmd.request.hdr.len = sizeof(cmd.request);
152 
153 	if (ism_cmd(ism, &cmd))
154 		goto out;
155 
156 	debug_text_event(ism_debug_info, 3, "query info");
157 	debug_event(ism_debug_info, 3, &cmd.response, sizeof(cmd.response));
158 out:
159 	return 0;
160 }
161 
162 static int register_sba(struct ism_dev *ism)
163 {
164 	union ism_reg_sba cmd;
165 	dma_addr_t dma_handle;
166 	struct ism_sba *sba;
167 
168 	sba = dma_alloc_coherent(&ism->pdev->dev, PAGE_SIZE, &dma_handle,
169 				 GFP_KERNEL);
170 	if (!sba)
171 		return -ENOMEM;
172 
173 	memset(&cmd, 0, sizeof(cmd));
174 	cmd.request.hdr.cmd = ISM_REG_SBA;
175 	cmd.request.hdr.len = sizeof(cmd.request);
176 	cmd.request.sba = dma_handle;
177 
178 	if (ism_cmd(ism, &cmd)) {
179 		dma_free_coherent(&ism->pdev->dev, PAGE_SIZE, sba, dma_handle);
180 		return -EIO;
181 	}
182 
183 	ism->sba = sba;
184 	ism->sba_dma_addr = dma_handle;
185 
186 	return 0;
187 }
188 
189 static int register_ieq(struct ism_dev *ism)
190 {
191 	union ism_reg_ieq cmd;
192 	dma_addr_t dma_handle;
193 	struct ism_eq *ieq;
194 
195 	ieq = dma_alloc_coherent(&ism->pdev->dev, PAGE_SIZE, &dma_handle,
196 				 GFP_KERNEL);
197 	if (!ieq)
198 		return -ENOMEM;
199 
200 	memset(&cmd, 0, sizeof(cmd));
201 	cmd.request.hdr.cmd = ISM_REG_IEQ;
202 	cmd.request.hdr.len = sizeof(cmd.request);
203 	cmd.request.ieq = dma_handle;
204 	cmd.request.len = sizeof(*ieq);
205 
206 	if (ism_cmd(ism, &cmd)) {
207 		dma_free_coherent(&ism->pdev->dev, PAGE_SIZE, ieq, dma_handle);
208 		return -EIO;
209 	}
210 
211 	ism->ieq = ieq;
212 	ism->ieq_idx = -1;
213 	ism->ieq_dma_addr = dma_handle;
214 
215 	return 0;
216 }
217 
218 static int unregister_sba(struct ism_dev *ism)
219 {
220 	int ret;
221 
222 	if (!ism->sba)
223 		return 0;
224 
225 	ret = ism_cmd_simple(ism, ISM_UNREG_SBA);
226 	if (ret && ret != ISM_ERROR)
227 		return -EIO;
228 
229 	dma_free_coherent(&ism->pdev->dev, PAGE_SIZE,
230 			  ism->sba, ism->sba_dma_addr);
231 
232 	ism->sba = NULL;
233 	ism->sba_dma_addr = 0;
234 
235 	return 0;
236 }
237 
238 static int unregister_ieq(struct ism_dev *ism)
239 {
240 	int ret;
241 
242 	if (!ism->ieq)
243 		return 0;
244 
245 	ret = ism_cmd_simple(ism, ISM_UNREG_IEQ);
246 	if (ret && ret != ISM_ERROR)
247 		return -EIO;
248 
249 	dma_free_coherent(&ism->pdev->dev, PAGE_SIZE,
250 			  ism->ieq, ism->ieq_dma_addr);
251 
252 	ism->ieq = NULL;
253 	ism->ieq_dma_addr = 0;
254 
255 	return 0;
256 }
257 
258 static int ism_read_local_gid(struct ism_dev *ism)
259 {
260 	union ism_read_gid cmd;
261 	int ret;
262 
263 	memset(&cmd, 0, sizeof(cmd));
264 	cmd.request.hdr.cmd = ISM_READ_GID;
265 	cmd.request.hdr.len = sizeof(cmd.request);
266 
267 	ret = ism_cmd(ism, &cmd);
268 	if (ret)
269 		goto out;
270 
271 	ism->local_gid = cmd.response.gid;
272 out:
273 	return ret;
274 }
275 
276 static int ism_query_rgid(struct ism_dev *ism, u64 rgid, u32 vid_valid,
277 			  u32 vid)
278 {
279 	union ism_query_rgid cmd;
280 
281 	memset(&cmd, 0, sizeof(cmd));
282 	cmd.request.hdr.cmd = ISM_QUERY_RGID;
283 	cmd.request.hdr.len = sizeof(cmd.request);
284 
285 	cmd.request.rgid = rgid;
286 	cmd.request.vlan_valid = vid_valid;
287 	cmd.request.vlan_id = vid;
288 
289 	return ism_cmd(ism, &cmd);
290 }
291 
292 static void ism_free_dmb(struct ism_dev *ism, struct ism_dmb *dmb)
293 {
294 	clear_bit(dmb->sba_idx, ism->sba_bitmap);
295 	dma_free_coherent(&ism->pdev->dev, dmb->dmb_len,
296 			  dmb->cpu_addr, dmb->dma_addr);
297 }
298 
299 static int ism_alloc_dmb(struct ism_dev *ism, struct ism_dmb *dmb)
300 {
301 	unsigned long bit;
302 
303 	if (PAGE_ALIGN(dmb->dmb_len) > dma_get_max_seg_size(&ism->pdev->dev))
304 		return -EINVAL;
305 
306 	if (!dmb->sba_idx) {
307 		bit = find_next_zero_bit(ism->sba_bitmap, ISM_NR_DMBS,
308 					 ISM_DMB_BIT_OFFSET);
309 		if (bit == ISM_NR_DMBS)
310 			return -ENOSPC;
311 
312 		dmb->sba_idx = bit;
313 	}
314 	if (dmb->sba_idx < ISM_DMB_BIT_OFFSET ||
315 	    test_and_set_bit(dmb->sba_idx, ism->sba_bitmap))
316 		return -EINVAL;
317 
318 	dmb->cpu_addr = dma_alloc_coherent(&ism->pdev->dev, dmb->dmb_len,
319 					   &dmb->dma_addr,
320 					   GFP_KERNEL | __GFP_NOWARN |
321 					   __GFP_NOMEMALLOC | __GFP_NORETRY);
322 	if (!dmb->cpu_addr)
323 		clear_bit(dmb->sba_idx, ism->sba_bitmap);
324 
325 	return dmb->cpu_addr ? 0 : -ENOMEM;
326 }
327 
328 int ism_register_dmb(struct ism_dev *ism, struct ism_dmb *dmb,
329 		     struct ism_client *client)
330 {
331 	union ism_reg_dmb cmd;
332 	int ret;
333 
334 	ret = ism_alloc_dmb(ism, dmb);
335 	if (ret)
336 		goto out;
337 
338 	memset(&cmd, 0, sizeof(cmd));
339 	cmd.request.hdr.cmd = ISM_REG_DMB;
340 	cmd.request.hdr.len = sizeof(cmd.request);
341 
342 	cmd.request.dmb = dmb->dma_addr;
343 	cmd.request.dmb_len = dmb->dmb_len;
344 	cmd.request.sba_idx = dmb->sba_idx;
345 	cmd.request.vlan_valid = dmb->vlan_valid;
346 	cmd.request.vlan_id = dmb->vlan_id;
347 	cmd.request.rgid = dmb->rgid;
348 
349 	ret = ism_cmd(ism, &cmd);
350 	if (ret) {
351 		ism_free_dmb(ism, dmb);
352 		goto out;
353 	}
354 	dmb->dmb_tok = cmd.response.dmb_tok;
355 	ism->sba_client_arr[dmb->sba_idx - ISM_DMB_BIT_OFFSET] = client->id;
356 out:
357 	return ret;
358 }
359 EXPORT_SYMBOL_GPL(ism_register_dmb);
360 
361 int ism_unregister_dmb(struct ism_dev *ism, struct ism_dmb *dmb)
362 {
363 	union ism_unreg_dmb cmd;
364 	int ret;
365 
366 	memset(&cmd, 0, sizeof(cmd));
367 	cmd.request.hdr.cmd = ISM_UNREG_DMB;
368 	cmd.request.hdr.len = sizeof(cmd.request);
369 
370 	cmd.request.dmb_tok = dmb->dmb_tok;
371 
372 	ism->sba_client_arr[dmb->sba_idx - ISM_DMB_BIT_OFFSET] = NO_CLIENT;
373 
374 	ret = ism_cmd(ism, &cmd);
375 	if (ret && ret != ISM_ERROR)
376 		goto out;
377 
378 	ism_free_dmb(ism, dmb);
379 out:
380 	return ret;
381 }
382 EXPORT_SYMBOL_GPL(ism_unregister_dmb);
383 
384 static int ism_add_vlan_id(struct ism_dev *ism, u64 vlan_id)
385 {
386 	union ism_set_vlan_id cmd;
387 
388 	memset(&cmd, 0, sizeof(cmd));
389 	cmd.request.hdr.cmd = ISM_ADD_VLAN_ID;
390 	cmd.request.hdr.len = sizeof(cmd.request);
391 
392 	cmd.request.vlan_id = vlan_id;
393 
394 	return ism_cmd(ism, &cmd);
395 }
396 
397 static int ism_del_vlan_id(struct ism_dev *ism, u64 vlan_id)
398 {
399 	union ism_set_vlan_id cmd;
400 
401 	memset(&cmd, 0, sizeof(cmd));
402 	cmd.request.hdr.cmd = ISM_DEL_VLAN_ID;
403 	cmd.request.hdr.len = sizeof(cmd.request);
404 
405 	cmd.request.vlan_id = vlan_id;
406 
407 	return ism_cmd(ism, &cmd);
408 }
409 
410 static int ism_signal_ieq(struct ism_dev *ism, u64 rgid, u32 trigger_irq,
411 			  u32 event_code, u64 info)
412 {
413 	union ism_sig_ieq cmd;
414 
415 	memset(&cmd, 0, sizeof(cmd));
416 	cmd.request.hdr.cmd = ISM_SIGNAL_IEQ;
417 	cmd.request.hdr.len = sizeof(cmd.request);
418 
419 	cmd.request.rgid = rgid;
420 	cmd.request.trigger_irq = trigger_irq;
421 	cmd.request.event_code = event_code;
422 	cmd.request.info = info;
423 
424 	return ism_cmd(ism, &cmd);
425 }
426 
427 static unsigned int max_bytes(unsigned int start, unsigned int len,
428 			      unsigned int boundary)
429 {
430 	return min(boundary - (start & (boundary - 1)), len);
431 }
432 
433 int ism_move(struct ism_dev *ism, u64 dmb_tok, unsigned int idx, bool sf,
434 	     unsigned int offset, void *data, unsigned int size)
435 {
436 	unsigned int bytes;
437 	u64 dmb_req;
438 	int ret;
439 
440 	while (size) {
441 		bytes = max_bytes(offset, size, PAGE_SIZE);
442 		dmb_req = ISM_CREATE_REQ(dmb_tok, idx, size == bytes ? sf : 0,
443 					 offset);
444 
445 		ret = __ism_move(ism, dmb_req, data, bytes);
446 		if (ret)
447 			return ret;
448 
449 		size -= bytes;
450 		data += bytes;
451 		offset += bytes;
452 	}
453 
454 	return 0;
455 }
456 EXPORT_SYMBOL_GPL(ism_move);
457 
458 static struct ism_systemeid SYSTEM_EID = {
459 	.seid_string = "IBM-SYSZ-ISMSEID00000000",
460 	.serial_number = "0000",
461 	.type = "0000",
462 };
463 
464 static void ism_create_system_eid(void)
465 {
466 	struct cpuid id;
467 	u16 ident_tail;
468 	char tmp[5];
469 
470 	get_cpu_id(&id);
471 	ident_tail = (u16)(id.ident & ISM_IDENT_MASK);
472 	snprintf(tmp, 5, "%04X", ident_tail);
473 	memcpy(&SYSTEM_EID.serial_number, tmp, 4);
474 	snprintf(tmp, 5, "%04X", id.machine);
475 	memcpy(&SYSTEM_EID.type, tmp, 4);
476 }
477 
478 u8 *ism_get_seid(void)
479 {
480 	return SYSTEM_EID.seid_string;
481 }
482 EXPORT_SYMBOL_GPL(ism_get_seid);
483 
484 static u16 ism_get_chid(struct ism_dev *ism)
485 {
486 	if (!ism || !ism->pdev)
487 		return 0;
488 
489 	return to_zpci(ism->pdev)->pchid;
490 }
491 
492 static void ism_handle_event(struct ism_dev *ism)
493 {
494 	struct ism_event *entry;
495 	int i;
496 
497 	while ((ism->ieq_idx + 1) != READ_ONCE(ism->ieq->header.idx)) {
498 		if (++(ism->ieq_idx) == ARRAY_SIZE(ism->ieq->entry))
499 			ism->ieq_idx = 0;
500 
501 		entry = &ism->ieq->entry[ism->ieq_idx];
502 		debug_event(ism_debug_info, 2, entry, sizeof(*entry));
503 		spin_lock(&clients_lock);
504 		for (i = 0; i < max_client; ++i)
505 			if (clients[i])
506 				clients[i]->handle_event(ism, entry);
507 		spin_unlock(&clients_lock);
508 	}
509 }
510 
511 static irqreturn_t ism_handle_irq(int irq, void *data)
512 {
513 	struct ism_dev *ism = data;
514 	struct ism_client *clt;
515 	unsigned long bit, end;
516 	unsigned long *bv;
517 	u16 dmbemask;
518 
519 	bv = (void *) &ism->sba->dmb_bits[ISM_DMB_WORD_OFFSET];
520 	end = sizeof(ism->sba->dmb_bits) * BITS_PER_BYTE - ISM_DMB_BIT_OFFSET;
521 
522 	spin_lock(&ism->lock);
523 	ism->sba->s = 0;
524 	barrier();
525 	for (bit = 0;;) {
526 		bit = find_next_bit_inv(bv, end, bit);
527 		if (bit >= end)
528 			break;
529 
530 		clear_bit_inv(bit, bv);
531 		dmbemask = ism->sba->dmbe_mask[bit + ISM_DMB_BIT_OFFSET];
532 		ism->sba->dmbe_mask[bit + ISM_DMB_BIT_OFFSET] = 0;
533 		barrier();
534 		clt = clients[ism->sba_client_arr[bit]];
535 		clt->handle_irq(ism, bit + ISM_DMB_BIT_OFFSET, dmbemask);
536 	}
537 
538 	if (ism->sba->e) {
539 		ism->sba->e = 0;
540 		barrier();
541 		ism_handle_event(ism);
542 	}
543 	spin_unlock(&ism->lock);
544 	return IRQ_HANDLED;
545 }
546 
547 static u64 ism_get_local_gid(struct ism_dev *ism)
548 {
549 	return ism->local_gid;
550 }
551 
552 static void ism_dev_add_work_func(struct work_struct *work)
553 {
554 	struct ism_client *client = container_of(work, struct ism_client,
555 						 add_work);
556 
557 	client->add(client->tgt_ism);
558 	atomic_dec(&client->tgt_ism->add_dev_cnt);
559 	wake_up(&client->tgt_ism->waitq);
560 }
561 
562 static int ism_dev_init(struct ism_dev *ism)
563 {
564 	struct pci_dev *pdev = ism->pdev;
565 	unsigned long flags;
566 	int i, ret;
567 
568 	ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
569 	if (ret <= 0)
570 		goto out;
571 
572 	ism->sba_client_arr = kzalloc(ISM_NR_DMBS, GFP_KERNEL);
573 	if (!ism->sba_client_arr)
574 		goto free_vectors;
575 	memset(ism->sba_client_arr, NO_CLIENT, ISM_NR_DMBS);
576 
577 	ret = request_irq(pci_irq_vector(pdev, 0), ism_handle_irq, 0,
578 			  pci_name(pdev), ism);
579 	if (ret)
580 		goto free_client_arr;
581 
582 	ret = register_sba(ism);
583 	if (ret)
584 		goto free_irq;
585 
586 	ret = register_ieq(ism);
587 	if (ret)
588 		goto unreg_sba;
589 
590 	ret = ism_read_local_gid(ism);
591 	if (ret)
592 		goto unreg_ieq;
593 
594 	if (!ism_add_vlan_id(ism, ISM_RESERVED_VLANID))
595 		/* hardware is V2 capable */
596 		ism_create_system_eid();
597 
598 	init_waitqueue_head(&ism->waitq);
599 	atomic_set(&ism->free_clients_cnt, 0);
600 	atomic_set(&ism->add_dev_cnt, 0);
601 
602 	wait_event(ism->waitq, !atomic_read(&ism->add_dev_cnt));
603 	spin_lock_irqsave(&clients_lock, flags);
604 	for (i = 0; i < max_client; ++i)
605 		if (clients[i]) {
606 			INIT_WORK(&clients[i]->add_work,
607 				  ism_dev_add_work_func);
608 			clients[i]->tgt_ism = ism;
609 			atomic_inc(&ism->add_dev_cnt);
610 			schedule_work(&clients[i]->add_work);
611 		}
612 	spin_unlock_irqrestore(&clients_lock, flags);
613 
614 	wait_event(ism->waitq, !atomic_read(&ism->add_dev_cnt));
615 
616 	mutex_lock(&ism_dev_list.mutex);
617 	list_add(&ism->list, &ism_dev_list.list);
618 	mutex_unlock(&ism_dev_list.mutex);
619 
620 	query_info(ism);
621 	return 0;
622 
623 unreg_ieq:
624 	unregister_ieq(ism);
625 unreg_sba:
626 	unregister_sba(ism);
627 free_irq:
628 	free_irq(pci_irq_vector(pdev, 0), ism);
629 free_client_arr:
630 	kfree(ism->sba_client_arr);
631 free_vectors:
632 	pci_free_irq_vectors(pdev);
633 out:
634 	return ret;
635 }
636 
637 static int ism_probe(struct pci_dev *pdev, const struct pci_device_id *id)
638 {
639 	struct ism_dev *ism;
640 	int ret;
641 
642 	ism = kzalloc(sizeof(*ism), GFP_KERNEL);
643 	if (!ism)
644 		return -ENOMEM;
645 
646 	spin_lock_init(&ism->lock);
647 	dev_set_drvdata(&pdev->dev, ism);
648 	ism->pdev = pdev;
649 	ism->dev.parent = &pdev->dev;
650 	device_initialize(&ism->dev);
651 	dev_set_name(&ism->dev, dev_name(&pdev->dev));
652 	ret = device_add(&ism->dev);
653 	if (ret)
654 		goto err_dev;
655 
656 	ret = pci_enable_device_mem(pdev);
657 	if (ret)
658 		goto err;
659 
660 	ret = pci_request_mem_regions(pdev, DRV_NAME);
661 	if (ret)
662 		goto err_disable;
663 
664 	ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
665 	if (ret)
666 		goto err_resource;
667 
668 	dma_set_seg_boundary(&pdev->dev, SZ_1M - 1);
669 	dma_set_max_seg_size(&pdev->dev, SZ_1M);
670 	pci_set_master(pdev);
671 
672 	ret = ism_dev_init(ism);
673 	if (ret)
674 		goto err_resource;
675 
676 	return 0;
677 
678 err_resource:
679 	pci_clear_master(pdev);
680 	pci_release_mem_regions(pdev);
681 err_disable:
682 	pci_disable_device(pdev);
683 err:
684 	device_del(&ism->dev);
685 err_dev:
686 	dev_set_drvdata(&pdev->dev, NULL);
687 	kfree(ism);
688 
689 	return ret;
690 }
691 
692 static void ism_dev_remove_work_func(struct work_struct *work)
693 {
694 	struct ism_client *client = container_of(work, struct ism_client,
695 						 remove_work);
696 
697 	client->remove(client->tgt_ism);
698 	atomic_dec(&client->tgt_ism->free_clients_cnt);
699 	wake_up(&client->tgt_ism->waitq);
700 }
701 
702 /* Callers must hold ism_dev_list.mutex */
703 static void ism_dev_exit(struct ism_dev *ism)
704 {
705 	struct pci_dev *pdev = ism->pdev;
706 	unsigned long flags;
707 	int i;
708 
709 	wait_event(ism->waitq, !atomic_read(&ism->free_clients_cnt));
710 	spin_lock_irqsave(&clients_lock, flags);
711 	for (i = 0; i < max_client; ++i)
712 		if (clients[i]) {
713 			INIT_WORK(&clients[i]->remove_work,
714 				  ism_dev_remove_work_func);
715 			clients[i]->tgt_ism = ism;
716 			atomic_inc(&ism->free_clients_cnt);
717 			schedule_work(&clients[i]->remove_work);
718 		}
719 	spin_unlock_irqrestore(&clients_lock, flags);
720 
721 	wait_event(ism->waitq, !atomic_read(&ism->free_clients_cnt));
722 
723 	if (SYSTEM_EID.serial_number[0] != '0' ||
724 	    SYSTEM_EID.type[0] != '0')
725 		ism_del_vlan_id(ism, ISM_RESERVED_VLANID);
726 	unregister_ieq(ism);
727 	unregister_sba(ism);
728 	free_irq(pci_irq_vector(pdev, 0), ism);
729 	kfree(ism->sba_client_arr);
730 	pci_free_irq_vectors(pdev);
731 	list_del_init(&ism->list);
732 }
733 
734 static void ism_remove(struct pci_dev *pdev)
735 {
736 	struct ism_dev *ism = dev_get_drvdata(&pdev->dev);
737 
738 	mutex_lock(&ism_dev_list.mutex);
739 	ism_dev_exit(ism);
740 	mutex_unlock(&ism_dev_list.mutex);
741 
742 	pci_clear_master(pdev);
743 	pci_release_mem_regions(pdev);
744 	pci_disable_device(pdev);
745 	device_del(&ism->dev);
746 	dev_set_drvdata(&pdev->dev, NULL);
747 	kfree(ism);
748 }
749 
750 static struct pci_driver ism_driver = {
751 	.name	  = DRV_NAME,
752 	.id_table = ism_device_table,
753 	.probe	  = ism_probe,
754 	.remove	  = ism_remove,
755 };
756 
757 static int __init ism_init(void)
758 {
759 	int ret;
760 
761 	ism_debug_info = debug_register("ism", 2, 1, 16);
762 	if (!ism_debug_info)
763 		return -ENODEV;
764 
765 	memset(clients, 0, sizeof(clients));
766 	max_client = 0;
767 	debug_register_view(ism_debug_info, &debug_hex_ascii_view);
768 	ret = pci_register_driver(&ism_driver);
769 	if (ret)
770 		debug_unregister(ism_debug_info);
771 
772 	return ret;
773 }
774 
775 static void __exit ism_exit(void)
776 {
777 	struct ism_dev *ism;
778 
779 	mutex_lock(&ism_dev_list.mutex);
780 	list_for_each_entry(ism, &ism_dev_list.list, list) {
781 		ism_dev_exit(ism);
782 	}
783 	mutex_unlock(&ism_dev_list.mutex);
784 
785 	pci_unregister_driver(&ism_driver);
786 	debug_unregister(ism_debug_info);
787 }
788 
789 module_init(ism_init);
790 module_exit(ism_exit);
791 
792 /*************************** SMC-D Implementation *****************************/
793 
794 #if IS_ENABLED(CONFIG_SMC)
795 static int smcd_query_rgid(struct smcd_dev *smcd, u64 rgid, u32 vid_valid,
796 			   u32 vid)
797 {
798 	return ism_query_rgid(smcd->priv, rgid, vid_valid, vid);
799 }
800 
801 static int smcd_register_dmb(struct smcd_dev *smcd, struct smcd_dmb *dmb,
802 			     struct ism_client *client)
803 {
804 	return ism_register_dmb(smcd->priv, (struct ism_dmb *)dmb, client);
805 }
806 
807 static int smcd_unregister_dmb(struct smcd_dev *smcd, struct smcd_dmb *dmb)
808 {
809 	return ism_unregister_dmb(smcd->priv, (struct ism_dmb *)dmb);
810 }
811 
812 static int smcd_add_vlan_id(struct smcd_dev *smcd, u64 vlan_id)
813 {
814 	return ism_add_vlan_id(smcd->priv, vlan_id);
815 }
816 
817 static int smcd_del_vlan_id(struct smcd_dev *smcd, u64 vlan_id)
818 {
819 	return ism_del_vlan_id(smcd->priv, vlan_id);
820 }
821 
822 static int smcd_set_vlan_required(struct smcd_dev *smcd)
823 {
824 	return ism_cmd_simple(smcd->priv, ISM_SET_VLAN);
825 }
826 
827 static int smcd_reset_vlan_required(struct smcd_dev *smcd)
828 {
829 	return ism_cmd_simple(smcd->priv, ISM_RESET_VLAN);
830 }
831 
832 static int smcd_signal_ieq(struct smcd_dev *smcd, u64 rgid, u32 trigger_irq,
833 			   u32 event_code, u64 info)
834 {
835 	return ism_signal_ieq(smcd->priv, rgid, trigger_irq, event_code, info);
836 }
837 
838 static int smcd_move(struct smcd_dev *smcd, u64 dmb_tok, unsigned int idx,
839 		     bool sf, unsigned int offset, void *data,
840 		     unsigned int size)
841 {
842 	return ism_move(smcd->priv, dmb_tok, idx, sf, offset, data, size);
843 }
844 
845 static u64 smcd_get_local_gid(struct smcd_dev *smcd)
846 {
847 	return ism_get_local_gid(smcd->priv);
848 }
849 
850 static u16 smcd_get_chid(struct smcd_dev *smcd)
851 {
852 	return ism_get_chid(smcd->priv);
853 }
854 
855 static inline struct device *smcd_get_dev(struct smcd_dev *dev)
856 {
857 	struct ism_dev *ism = dev->priv;
858 
859 	return &ism->dev;
860 }
861 
862 static const struct smcd_ops ism_ops = {
863 	.query_remote_gid = smcd_query_rgid,
864 	.register_dmb = smcd_register_dmb,
865 	.unregister_dmb = smcd_unregister_dmb,
866 	.add_vlan_id = smcd_add_vlan_id,
867 	.del_vlan_id = smcd_del_vlan_id,
868 	.set_vlan_required = smcd_set_vlan_required,
869 	.reset_vlan_required = smcd_reset_vlan_required,
870 	.signal_event = smcd_signal_ieq,
871 	.move_data = smcd_move,
872 	.get_system_eid = ism_get_seid,
873 	.get_local_gid = smcd_get_local_gid,
874 	.get_chid = smcd_get_chid,
875 	.get_dev = smcd_get_dev,
876 };
877 
878 const struct smcd_ops *ism_get_smcd_ops(void)
879 {
880 	return &ism_ops;
881 }
882 EXPORT_SYMBOL_GPL(ism_get_smcd_ops);
883 #endif
884