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