xref: /openbmc/linux/net/caif/cfctrl.c (revision fe69230f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) ST-Ericsson AB 2010
4  * Author:	Sjur Brendeland
5  */
6 
7 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
8 
9 #include <linux/stddef.h>
10 #include <linux/spinlock.h>
11 #include <linux/slab.h>
12 #include <linux/pkt_sched.h>
13 #include <net/caif/caif_layer.h>
14 #include <net/caif/cfpkt.h>
15 #include <net/caif/cfctrl.h>
16 
17 #define container_obj(layr) container_of(layr, struct cfctrl, serv.layer)
18 #define UTILITY_NAME_LENGTH 16
19 #define CFPKT_CTRL_PKT_LEN 20
20 
21 #ifdef CAIF_NO_LOOP
handle_loop(struct cfctrl * ctrl,int cmd,struct cfpkt * pkt)22 static int handle_loop(struct cfctrl *ctrl,
23 		       int cmd, struct cfpkt *pkt){
24 	return -1;
25 }
26 #else
27 static int handle_loop(struct cfctrl *ctrl,
28 		       int cmd, struct cfpkt *pkt);
29 #endif
30 static int cfctrl_recv(struct cflayer *layr, struct cfpkt *pkt);
31 static void cfctrl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
32 			   int phyid);
33 
34 
cfctrl_create(void)35 struct cflayer *cfctrl_create(void)
36 {
37 	struct dev_info dev_info;
38 	struct cfctrl *this =
39 		kzalloc(sizeof(struct cfctrl), GFP_ATOMIC);
40 	if (!this)
41 		return NULL;
42 	caif_assert(offsetof(struct cfctrl, serv.layer) == 0);
43 	memset(&dev_info, 0, sizeof(dev_info));
44 	dev_info.id = 0xff;
45 	cfsrvl_init(&this->serv, 0, &dev_info, false);
46 	atomic_set(&this->req_seq_no, 1);
47 	atomic_set(&this->rsp_seq_no, 1);
48 	this->serv.layer.receive = cfctrl_recv;
49 	sprintf(this->serv.layer.name, "ctrl");
50 	this->serv.layer.ctrlcmd = cfctrl_ctrlcmd;
51 #ifndef CAIF_NO_LOOP
52 	spin_lock_init(&this->loop_linkid_lock);
53 	this->loop_linkid = 1;
54 #endif
55 	spin_lock_init(&this->info_list_lock);
56 	INIT_LIST_HEAD(&this->list);
57 	return &this->serv.layer;
58 }
59 
cfctrl_remove(struct cflayer * layer)60 void cfctrl_remove(struct cflayer *layer)
61 {
62 	struct cfctrl_request_info *p, *tmp;
63 	struct cfctrl *ctrl = container_obj(layer);
64 
65 	spin_lock_bh(&ctrl->info_list_lock);
66 	list_for_each_entry_safe(p, tmp, &ctrl->list, list) {
67 		list_del(&p->list);
68 		kfree(p);
69 	}
70 	spin_unlock_bh(&ctrl->info_list_lock);
71 	kfree(layer);
72 }
73 
param_eq(const struct cfctrl_link_param * p1,const struct cfctrl_link_param * p2)74 static bool param_eq(const struct cfctrl_link_param *p1,
75 		     const struct cfctrl_link_param *p2)
76 {
77 	bool eq =
78 	    p1->linktype == p2->linktype &&
79 	    p1->priority == p2->priority &&
80 	    p1->phyid == p2->phyid &&
81 	    p1->endpoint == p2->endpoint && p1->chtype == p2->chtype;
82 
83 	if (!eq)
84 		return false;
85 
86 	switch (p1->linktype) {
87 	case CFCTRL_SRV_VEI:
88 		return true;
89 	case CFCTRL_SRV_DATAGRAM:
90 		return p1->u.datagram.connid == p2->u.datagram.connid;
91 	case CFCTRL_SRV_RFM:
92 		return
93 		    p1->u.rfm.connid == p2->u.rfm.connid &&
94 		    strcmp(p1->u.rfm.volume, p2->u.rfm.volume) == 0;
95 	case CFCTRL_SRV_UTIL:
96 		return
97 		    p1->u.utility.fifosize_kb == p2->u.utility.fifosize_kb
98 		    && p1->u.utility.fifosize_bufs ==
99 		    p2->u.utility.fifosize_bufs
100 		    && strcmp(p1->u.utility.name, p2->u.utility.name) == 0
101 		    && p1->u.utility.paramlen == p2->u.utility.paramlen
102 		    && memcmp(p1->u.utility.params, p2->u.utility.params,
103 			      p1->u.utility.paramlen) == 0;
104 
105 	case CFCTRL_SRV_VIDEO:
106 		return p1->u.video.connid == p2->u.video.connid;
107 	case CFCTRL_SRV_DBG:
108 		return true;
109 	case CFCTRL_SRV_DECM:
110 		return false;
111 	default:
112 		return false;
113 	}
114 	return false;
115 }
116 
cfctrl_req_eq(const struct cfctrl_request_info * r1,const struct cfctrl_request_info * r2)117 static bool cfctrl_req_eq(const struct cfctrl_request_info *r1,
118 			  const struct cfctrl_request_info *r2)
119 {
120 	if (r1->cmd != r2->cmd)
121 		return false;
122 	if (r1->cmd == CFCTRL_CMD_LINK_SETUP)
123 		return param_eq(&r1->param, &r2->param);
124 	else
125 		return r1->channel_id == r2->channel_id;
126 }
127 
128 /* Insert request at the end */
cfctrl_insert_req(struct cfctrl * ctrl,struct cfctrl_request_info * req)129 static void cfctrl_insert_req(struct cfctrl *ctrl,
130 			      struct cfctrl_request_info *req)
131 {
132 	spin_lock_bh(&ctrl->info_list_lock);
133 	atomic_inc(&ctrl->req_seq_no);
134 	req->sequence_no = atomic_read(&ctrl->req_seq_no);
135 	list_add_tail(&req->list, &ctrl->list);
136 	spin_unlock_bh(&ctrl->info_list_lock);
137 }
138 
139 /* Compare and remove request */
cfctrl_remove_req(struct cfctrl * ctrl,struct cfctrl_request_info * req)140 static struct cfctrl_request_info *cfctrl_remove_req(struct cfctrl *ctrl,
141 						struct cfctrl_request_info *req)
142 {
143 	struct cfctrl_request_info *p, *tmp, *first;
144 
145 	first = list_first_entry(&ctrl->list, struct cfctrl_request_info, list);
146 
147 	list_for_each_entry_safe(p, tmp, &ctrl->list, list) {
148 		if (cfctrl_req_eq(req, p)) {
149 			if (p != first)
150 				pr_warn("Requests are not received in order\n");
151 
152 			atomic_set(&ctrl->rsp_seq_no,
153 					 p->sequence_no);
154 			list_del(&p->list);
155 			goto out;
156 		}
157 	}
158 	p = NULL;
159 out:
160 	return p;
161 }
162 
cfctrl_get_respfuncs(struct cflayer * layer)163 struct cfctrl_rsp *cfctrl_get_respfuncs(struct cflayer *layer)
164 {
165 	struct cfctrl *this = container_obj(layer);
166 	return &this->res;
167 }
168 
init_info(struct caif_payload_info * info,struct cfctrl * cfctrl)169 static void init_info(struct caif_payload_info *info, struct cfctrl *cfctrl)
170 {
171 	info->hdr_len = 0;
172 	info->channel_id = cfctrl->serv.layer.id;
173 	info->dev_info = &cfctrl->serv.dev_info;
174 }
175 
cfctrl_enum_req(struct cflayer * layer,u8 physlinkid)176 void cfctrl_enum_req(struct cflayer *layer, u8 physlinkid)
177 {
178 	struct cfpkt *pkt;
179 	struct cfctrl *cfctrl = container_obj(layer);
180 	struct cflayer *dn = cfctrl->serv.layer.dn;
181 
182 	if (!dn) {
183 		pr_debug("not able to send enum request\n");
184 		return;
185 	}
186 	pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
187 	if (!pkt)
188 		return;
189 	caif_assert(offsetof(struct cfctrl, serv.layer) == 0);
190 	init_info(cfpkt_info(pkt), cfctrl);
191 	cfpkt_info(pkt)->dev_info->id = physlinkid;
192 	cfctrl->serv.dev_info.id = physlinkid;
193 	cfpkt_addbdy(pkt, CFCTRL_CMD_ENUM);
194 	cfpkt_addbdy(pkt, physlinkid);
195 	cfpkt_set_prio(pkt, TC_PRIO_CONTROL);
196 	dn->transmit(dn, pkt);
197 }
198 
cfctrl_linkup_request(struct cflayer * layer,struct cfctrl_link_param * param,struct cflayer * user_layer)199 int cfctrl_linkup_request(struct cflayer *layer,
200 			  struct cfctrl_link_param *param,
201 			  struct cflayer *user_layer)
202 {
203 	struct cfctrl *cfctrl = container_obj(layer);
204 	u32 tmp32;
205 	u16 tmp16;
206 	u8 tmp8;
207 	struct cfctrl_request_info *req;
208 	int ret;
209 	char utility_name[16];
210 	struct cfpkt *pkt;
211 	struct cflayer *dn = cfctrl->serv.layer.dn;
212 
213 	if (!dn) {
214 		pr_debug("not able to send linkup request\n");
215 		return -ENODEV;
216 	}
217 
218 	if (cfctrl_cancel_req(layer, user_layer) > 0) {
219 		/* Slight Paranoia, check if already connecting */
220 		pr_err("Duplicate connect request for same client\n");
221 		WARN_ON(1);
222 		return -EALREADY;
223 	}
224 
225 	pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
226 	if (!pkt)
227 		return -ENOMEM;
228 	cfpkt_addbdy(pkt, CFCTRL_CMD_LINK_SETUP);
229 	cfpkt_addbdy(pkt, (param->chtype << 4) | param->linktype);
230 	cfpkt_addbdy(pkt, (param->priority << 3) | param->phyid);
231 	cfpkt_addbdy(pkt, param->endpoint & 0x03);
232 
233 	switch (param->linktype) {
234 	case CFCTRL_SRV_VEI:
235 		break;
236 	case CFCTRL_SRV_VIDEO:
237 		cfpkt_addbdy(pkt, (u8) param->u.video.connid);
238 		break;
239 	case CFCTRL_SRV_DBG:
240 		break;
241 	case CFCTRL_SRV_DATAGRAM:
242 		tmp32 = cpu_to_le32(param->u.datagram.connid);
243 		cfpkt_add_body(pkt, &tmp32, 4);
244 		break;
245 	case CFCTRL_SRV_RFM:
246 		/* Construct a frame, convert DatagramConnectionID to network
247 		 * format long and copy it out...
248 		 */
249 		tmp32 = cpu_to_le32(param->u.rfm.connid);
250 		cfpkt_add_body(pkt, &tmp32, 4);
251 		/* Add volume name, including zero termination... */
252 		cfpkt_add_body(pkt, param->u.rfm.volume,
253 			       strlen(param->u.rfm.volume) + 1);
254 		break;
255 	case CFCTRL_SRV_UTIL:
256 		tmp16 = cpu_to_le16(param->u.utility.fifosize_kb);
257 		cfpkt_add_body(pkt, &tmp16, 2);
258 		tmp16 = cpu_to_le16(param->u.utility.fifosize_bufs);
259 		cfpkt_add_body(pkt, &tmp16, 2);
260 		memset(utility_name, 0, sizeof(utility_name));
261 		strscpy(utility_name, param->u.utility.name,
262 			UTILITY_NAME_LENGTH);
263 		cfpkt_add_body(pkt, utility_name, UTILITY_NAME_LENGTH);
264 		tmp8 = param->u.utility.paramlen;
265 		cfpkt_add_body(pkt, &tmp8, 1);
266 		cfpkt_add_body(pkt, param->u.utility.params,
267 			       param->u.utility.paramlen);
268 		break;
269 	default:
270 		pr_warn("Request setup of bad link type = %d\n",
271 			param->linktype);
272 		cfpkt_destroy(pkt);
273 		return -EINVAL;
274 	}
275 	req = kzalloc(sizeof(*req), GFP_KERNEL);
276 	if (!req) {
277 		cfpkt_destroy(pkt);
278 		return -ENOMEM;
279 	}
280 
281 	req->client_layer = user_layer;
282 	req->cmd = CFCTRL_CMD_LINK_SETUP;
283 	req->param = *param;
284 	cfctrl_insert_req(cfctrl, req);
285 	init_info(cfpkt_info(pkt), cfctrl);
286 	/*
287 	 * NOTE:Always send linkup and linkdown request on the same
288 	 *	device as the payload. Otherwise old queued up payload
289 	 *	might arrive with the newly allocated channel ID.
290 	 */
291 	cfpkt_info(pkt)->dev_info->id = param->phyid;
292 	cfpkt_set_prio(pkt, TC_PRIO_CONTROL);
293 	ret =
294 	    dn->transmit(dn, pkt);
295 	if (ret < 0) {
296 		int count;
297 
298 		count = cfctrl_cancel_req(&cfctrl->serv.layer,
299 						user_layer);
300 		if (count != 1) {
301 			pr_err("Could not remove request (%d)", count);
302 			return -ENODEV;
303 		}
304 	}
305 	return 0;
306 }
307 
cfctrl_linkdown_req(struct cflayer * layer,u8 channelid,struct cflayer * client)308 int cfctrl_linkdown_req(struct cflayer *layer, u8 channelid,
309 			struct cflayer *client)
310 {
311 	int ret;
312 	struct cfpkt *pkt;
313 	struct cfctrl *cfctrl = container_obj(layer);
314 	struct cflayer *dn = cfctrl->serv.layer.dn;
315 
316 	if (!dn) {
317 		pr_debug("not able to send link-down request\n");
318 		return -ENODEV;
319 	}
320 	pkt = cfpkt_create(CFPKT_CTRL_PKT_LEN);
321 	if (!pkt)
322 		return -ENOMEM;
323 	cfpkt_addbdy(pkt, CFCTRL_CMD_LINK_DESTROY);
324 	cfpkt_addbdy(pkt, channelid);
325 	init_info(cfpkt_info(pkt), cfctrl);
326 	cfpkt_set_prio(pkt, TC_PRIO_CONTROL);
327 	ret =
328 	    dn->transmit(dn, pkt);
329 #ifndef CAIF_NO_LOOP
330 	cfctrl->loop_linkused[channelid] = 0;
331 #endif
332 	return ret;
333 }
334 
cfctrl_cancel_req(struct cflayer * layr,struct cflayer * adap_layer)335 int cfctrl_cancel_req(struct cflayer *layr, struct cflayer *adap_layer)
336 {
337 	struct cfctrl_request_info *p, *tmp;
338 	struct cfctrl *ctrl = container_obj(layr);
339 	int found = 0;
340 	spin_lock_bh(&ctrl->info_list_lock);
341 
342 	list_for_each_entry_safe(p, tmp, &ctrl->list, list) {
343 		if (p->client_layer == adap_layer) {
344 			list_del(&p->list);
345 			kfree(p);
346 			found++;
347 		}
348 	}
349 
350 	spin_unlock_bh(&ctrl->info_list_lock);
351 	return found;
352 }
353 
cfctrl_recv(struct cflayer * layer,struct cfpkt * pkt)354 static int cfctrl_recv(struct cflayer *layer, struct cfpkt *pkt)
355 {
356 	u8 cmdrsp;
357 	u8 cmd;
358 	int ret = -1;
359 	u8 len;
360 	u8 param[255];
361 	u8 linkid = 0;
362 	struct cfctrl *cfctrl = container_obj(layer);
363 	struct cfctrl_request_info rsp, *req;
364 
365 
366 	cmdrsp = cfpkt_extr_head_u8(pkt);
367 	cmd = cmdrsp & CFCTRL_CMD_MASK;
368 	if (cmd != CFCTRL_CMD_LINK_ERR
369 	    && CFCTRL_RSP_BIT != (CFCTRL_RSP_BIT & cmdrsp)
370 		&& CFCTRL_ERR_BIT != (CFCTRL_ERR_BIT & cmdrsp)) {
371 		if (handle_loop(cfctrl, cmd, pkt) != 0)
372 			cmdrsp |= CFCTRL_ERR_BIT;
373 	}
374 
375 	switch (cmd) {
376 	case CFCTRL_CMD_LINK_SETUP:
377 		{
378 			enum cfctrl_srv serv;
379 			enum cfctrl_srv servtype;
380 			u8 endpoint;
381 			u8 physlinkid;
382 			u8 prio;
383 			u8 tmp;
384 			u8 *cp;
385 			int i;
386 			struct cfctrl_link_param linkparam;
387 			memset(&linkparam, 0, sizeof(linkparam));
388 
389 			tmp = cfpkt_extr_head_u8(pkt);
390 
391 			serv = tmp & CFCTRL_SRV_MASK;
392 			linkparam.linktype = serv;
393 
394 			servtype = tmp >> 4;
395 			linkparam.chtype = servtype;
396 
397 			tmp = cfpkt_extr_head_u8(pkt);
398 			physlinkid = tmp & 0x07;
399 			prio = tmp >> 3;
400 
401 			linkparam.priority = prio;
402 			linkparam.phyid = physlinkid;
403 			endpoint = cfpkt_extr_head_u8(pkt);
404 			linkparam.endpoint = endpoint & 0x03;
405 
406 			switch (serv) {
407 			case CFCTRL_SRV_VEI:
408 			case CFCTRL_SRV_DBG:
409 				if (CFCTRL_ERR_BIT & cmdrsp)
410 					break;
411 				/* Link ID */
412 				linkid = cfpkt_extr_head_u8(pkt);
413 				break;
414 			case CFCTRL_SRV_VIDEO:
415 				tmp = cfpkt_extr_head_u8(pkt);
416 				linkparam.u.video.connid = tmp;
417 				if (CFCTRL_ERR_BIT & cmdrsp)
418 					break;
419 				/* Link ID */
420 				linkid = cfpkt_extr_head_u8(pkt);
421 				break;
422 
423 			case CFCTRL_SRV_DATAGRAM:
424 				linkparam.u.datagram.connid =
425 				    cfpkt_extr_head_u32(pkt);
426 				if (CFCTRL_ERR_BIT & cmdrsp)
427 					break;
428 				/* Link ID */
429 				linkid = cfpkt_extr_head_u8(pkt);
430 				break;
431 			case CFCTRL_SRV_RFM:
432 				/* Construct a frame, convert
433 				 * DatagramConnectionID
434 				 * to network format long and copy it out...
435 				 */
436 				linkparam.u.rfm.connid =
437 				    cfpkt_extr_head_u32(pkt);
438 				cp = (u8 *) linkparam.u.rfm.volume;
439 				for (tmp = cfpkt_extr_head_u8(pkt);
440 				     cfpkt_more(pkt) && tmp != '\0';
441 				     tmp = cfpkt_extr_head_u8(pkt))
442 					*cp++ = tmp;
443 				*cp = '\0';
444 
445 				if (CFCTRL_ERR_BIT & cmdrsp)
446 					break;
447 				/* Link ID */
448 				linkid = cfpkt_extr_head_u8(pkt);
449 
450 				break;
451 			case CFCTRL_SRV_UTIL:
452 				/* Construct a frame, convert
453 				 * DatagramConnectionID
454 				 * to network format long and copy it out...
455 				 */
456 				/* Fifosize KB */
457 				linkparam.u.utility.fifosize_kb =
458 				    cfpkt_extr_head_u16(pkt);
459 				/* Fifosize bufs */
460 				linkparam.u.utility.fifosize_bufs =
461 				    cfpkt_extr_head_u16(pkt);
462 				/* name */
463 				cp = (u8 *) linkparam.u.utility.name;
464 				caif_assert(sizeof(linkparam.u.utility.name)
465 					     >= UTILITY_NAME_LENGTH);
466 				for (i = 0;
467 				     i < UTILITY_NAME_LENGTH
468 				     && cfpkt_more(pkt); i++) {
469 					tmp = cfpkt_extr_head_u8(pkt);
470 					*cp++ = tmp;
471 				}
472 				/* Length */
473 				len = cfpkt_extr_head_u8(pkt);
474 				linkparam.u.utility.paramlen = len;
475 				/* Param Data */
476 				cp = linkparam.u.utility.params;
477 				while (cfpkt_more(pkt) && len--) {
478 					tmp = cfpkt_extr_head_u8(pkt);
479 					*cp++ = tmp;
480 				}
481 				if (CFCTRL_ERR_BIT & cmdrsp)
482 					break;
483 				/* Link ID */
484 				linkid = cfpkt_extr_head_u8(pkt);
485 				/* Length */
486 				len = cfpkt_extr_head_u8(pkt);
487 				/* Param Data */
488 				cfpkt_extr_head(pkt, &param, len);
489 				break;
490 			default:
491 				pr_warn("Request setup, invalid type (%d)\n",
492 					serv);
493 				goto error;
494 			}
495 
496 			rsp.cmd = cmd;
497 			rsp.param = linkparam;
498 			spin_lock_bh(&cfctrl->info_list_lock);
499 			req = cfctrl_remove_req(cfctrl, &rsp);
500 
501 			if (CFCTRL_ERR_BIT == (CFCTRL_ERR_BIT & cmdrsp) ||
502 				cfpkt_erroneous(pkt)) {
503 				pr_err("Invalid O/E bit or parse error "
504 						"on CAIF control channel\n");
505 				cfctrl->res.reject_rsp(cfctrl->serv.layer.up,
506 						       0,
507 						       req ? req->client_layer
508 						       : NULL);
509 			} else {
510 				cfctrl->res.linksetup_rsp(cfctrl->serv.
511 							  layer.up, linkid,
512 							  serv, physlinkid,
513 							  req ? req->
514 							  client_layer : NULL);
515 			}
516 
517 			kfree(req);
518 
519 			spin_unlock_bh(&cfctrl->info_list_lock);
520 		}
521 		break;
522 	case CFCTRL_CMD_LINK_DESTROY:
523 		linkid = cfpkt_extr_head_u8(pkt);
524 		cfctrl->res.linkdestroy_rsp(cfctrl->serv.layer.up, linkid);
525 		break;
526 	case CFCTRL_CMD_LINK_ERR:
527 		pr_err("Frame Error Indication received\n");
528 		cfctrl->res.linkerror_ind();
529 		break;
530 	case CFCTRL_CMD_ENUM:
531 		cfctrl->res.enum_rsp();
532 		break;
533 	case CFCTRL_CMD_SLEEP:
534 		cfctrl->res.sleep_rsp();
535 		break;
536 	case CFCTRL_CMD_WAKE:
537 		cfctrl->res.wake_rsp();
538 		break;
539 	case CFCTRL_CMD_LINK_RECONF:
540 		cfctrl->res.restart_rsp();
541 		break;
542 	case CFCTRL_CMD_RADIO_SET:
543 		cfctrl->res.radioset_rsp();
544 		break;
545 	default:
546 		pr_err("Unrecognized Control Frame\n");
547 		goto error;
548 	}
549 	ret = 0;
550 error:
551 	cfpkt_destroy(pkt);
552 	return ret;
553 }
554 
cfctrl_ctrlcmd(struct cflayer * layr,enum caif_ctrlcmd ctrl,int phyid)555 static void cfctrl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
556 			   int phyid)
557 {
558 	struct cfctrl *this = container_obj(layr);
559 	switch (ctrl) {
560 	case _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND:
561 	case CAIF_CTRLCMD_FLOW_OFF_IND:
562 		spin_lock_bh(&this->info_list_lock);
563 		if (!list_empty(&this->list))
564 			pr_debug("Received flow off in control layer\n");
565 		spin_unlock_bh(&this->info_list_lock);
566 		break;
567 	case _CAIF_CTRLCMD_PHYIF_DOWN_IND: {
568 		struct cfctrl_request_info *p, *tmp;
569 
570 		/* Find all connect request and report failure */
571 		spin_lock_bh(&this->info_list_lock);
572 		list_for_each_entry_safe(p, tmp, &this->list, list) {
573 			if (p->param.phyid == phyid) {
574 				list_del(&p->list);
575 				p->client_layer->ctrlcmd(p->client_layer,
576 						CAIF_CTRLCMD_INIT_FAIL_RSP,
577 						phyid);
578 				kfree(p);
579 			}
580 		}
581 		spin_unlock_bh(&this->info_list_lock);
582 		break;
583 	}
584 	default:
585 		break;
586 	}
587 }
588 
589 #ifndef CAIF_NO_LOOP
handle_loop(struct cfctrl * ctrl,int cmd,struct cfpkt * pkt)590 static int handle_loop(struct cfctrl *ctrl, int cmd, struct cfpkt *pkt)
591 {
592 	static int last_linkid;
593 	static int dec;
594 	u8 linkid, linktype, tmp;
595 	switch (cmd) {
596 	case CFCTRL_CMD_LINK_SETUP:
597 		spin_lock_bh(&ctrl->loop_linkid_lock);
598 		if (!dec) {
599 			for (linkid = last_linkid + 1; linkid < 254; linkid++)
600 				if (!ctrl->loop_linkused[linkid])
601 					goto found;
602 		}
603 		dec = 1;
604 		for (linkid = last_linkid - 1; linkid > 1; linkid--)
605 			if (!ctrl->loop_linkused[linkid])
606 				goto found;
607 		spin_unlock_bh(&ctrl->loop_linkid_lock);
608 		return -1;
609 found:
610 		if (linkid < 10)
611 			dec = 0;
612 
613 		if (!ctrl->loop_linkused[linkid])
614 			ctrl->loop_linkused[linkid] = 1;
615 
616 		last_linkid = linkid;
617 
618 		cfpkt_add_trail(pkt, &linkid, 1);
619 		spin_unlock_bh(&ctrl->loop_linkid_lock);
620 		cfpkt_peek_head(pkt, &linktype, 1);
621 		if (linktype ==  CFCTRL_SRV_UTIL) {
622 			tmp = 0x01;
623 			cfpkt_add_trail(pkt, &tmp, 1);
624 			cfpkt_add_trail(pkt, &tmp, 1);
625 		}
626 		break;
627 
628 	case CFCTRL_CMD_LINK_DESTROY:
629 		spin_lock_bh(&ctrl->loop_linkid_lock);
630 		cfpkt_peek_head(pkt, &linkid, 1);
631 		ctrl->loop_linkused[linkid] = 0;
632 		spin_unlock_bh(&ctrl->loop_linkid_lock);
633 		break;
634 	default:
635 		break;
636 	}
637 	return 0;
638 }
639 #endif
640