1 /*
2  * disp.c
3  *
4  * DSP-BIOS Bridge driver support functions for TI OMAP processors.
5  *
6  * Node Dispatcher interface. Communicates with Resource Manager Server
7  * (RMS) on DSP. Access to RMS is synchronized in NODE.
8  *
9  * Copyright (C) 2005-2006 Texas Instruments, Inc.
10  *
11  * This package is free software;  you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  *
15  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18  */
19 #include <linux/types.h>
20 
21 /*  ----------------------------------- Host OS */
22 #include <dspbridge/host_os.h>
23 
24 /*  ----------------------------------- DSP/BIOS Bridge */
25 #include <dspbridge/dbdefs.h>
26 
27 /*  ----------------------------------- Trace & Debug */
28 #include <dspbridge/dbc.h>
29 
30 /*  ----------------------------------- OS Adaptation Layer */
31 #include <dspbridge/sync.h>
32 
33 /*  ----------------------------------- Link Driver */
34 #include <dspbridge/dspdefs.h>
35 
36 /*  ----------------------------------- Platform Manager */
37 #include <dspbridge/dev.h>
38 #include <dspbridge/chnldefs.h>
39 
40 /*  ----------------------------------- Resource Manager */
41 #include <dspbridge/nodedefs.h>
42 #include <dspbridge/nodepriv.h>
43 #include <dspbridge/rms_sh.h>
44 
45 /*  ----------------------------------- This */
46 #include <dspbridge/disp.h>
47 
48 /* Size of a reply from RMS */
49 #define REPLYSIZE (3 * sizeof(rms_word))
50 
51 /* Reserved channel offsets for communication with RMS */
52 #define CHNLTORMSOFFSET       0
53 #define CHNLFROMRMSOFFSET     1
54 
55 #define CHNLIOREQS      1
56 
57 /*
58  *  ======== disp_object ========
59  */
60 struct disp_object {
61 	struct dev_object *dev_obj;	/* Device for this processor */
62 	/* Function interface to Bridge driver */
63 	struct bridge_drv_interface *intf_fxns;
64 	struct chnl_mgr *chnl_mgr;	/* Channel manager */
65 	struct chnl_object *chnl_to_dsp;	/* Chnl for commands to RMS */
66 	struct chnl_object *chnl_from_dsp;	/* Chnl for replies from RMS */
67 	u8 *buf;		/* Buffer for commands, replies */
68 	u32 bufsize;		/* buf size in bytes */
69 	u32 bufsize_rms;	/* buf size in RMS words */
70 	u32 char_size;		/* Size of DSP character */
71 	u32 word_size;		/* Size of DSP word */
72 	u32 data_mau_size;	/* Size of DSP Data MAU */
73 };
74 
75 static u32 refs;
76 
77 static void delete_disp(struct disp_object *disp_obj);
78 static int fill_stream_def(rms_word *pdw_buf, u32 *ptotal, u32 offset,
79 				  struct node_strmdef strm_def, u32 max,
80 				  u32 chars_in_rms_word);
81 static int send_message(struct disp_object *disp_obj, u32 timeout,
82 			       u32 ul_bytes, u32 *pdw_arg);
83 
84 /*
85  *  ======== disp_create ========
86  *  Create a NODE Dispatcher object.
87  */
disp_create(struct disp_object ** dispatch_obj,struct dev_object * hdev_obj,const struct disp_attr * disp_attrs)88 int disp_create(struct disp_object **dispatch_obj,
89 		       struct dev_object *hdev_obj,
90 		       const struct disp_attr *disp_attrs)
91 {
92 	struct disp_object *disp_obj;
93 	struct bridge_drv_interface *intf_fxns;
94 	u32 ul_chnl_id;
95 	struct chnl_attr chnl_attr_obj;
96 	int status = 0;
97 	u8 dev_type;
98 
99 	DBC_REQUIRE(refs > 0);
100 	DBC_REQUIRE(dispatch_obj != NULL);
101 	DBC_REQUIRE(disp_attrs != NULL);
102 	DBC_REQUIRE(hdev_obj != NULL);
103 
104 	*dispatch_obj = NULL;
105 
106 	/* Allocate Node Dispatcher object */
107 	disp_obj = kzalloc(sizeof(struct disp_object), GFP_KERNEL);
108 	if (disp_obj == NULL)
109 		status = -ENOMEM;
110 	else
111 		disp_obj->dev_obj = hdev_obj;
112 
113 	/* Get Channel manager and Bridge function interface */
114 	if (!status) {
115 		status = dev_get_chnl_mgr(hdev_obj, &(disp_obj->chnl_mgr));
116 		if (!status) {
117 			(void)dev_get_intf_fxns(hdev_obj, &intf_fxns);
118 			disp_obj->intf_fxns = intf_fxns;
119 		}
120 	}
121 
122 	/* check device type and decide if streams or messag'ing is used for
123 	 * RMS/EDS */
124 	if (status)
125 		goto func_cont;
126 
127 	status = dev_get_dev_type(hdev_obj, &dev_type);
128 
129 	if (status)
130 		goto func_cont;
131 
132 	if (dev_type != DSP_UNIT) {
133 		status = -EPERM;
134 		goto func_cont;
135 	}
136 
137 	disp_obj->char_size = DSPWORDSIZE;
138 	disp_obj->word_size = DSPWORDSIZE;
139 	disp_obj->data_mau_size = DSPWORDSIZE;
140 	/* Open channels for communicating with the RMS */
141 	chnl_attr_obj.uio_reqs = CHNLIOREQS;
142 	chnl_attr_obj.event_obj = NULL;
143 	ul_chnl_id = disp_attrs->chnl_offset + CHNLTORMSOFFSET;
144 	status = (*intf_fxns->chnl_open) (&(disp_obj->chnl_to_dsp),
145 					      disp_obj->chnl_mgr,
146 					      CHNL_MODETODSP, ul_chnl_id,
147 					      &chnl_attr_obj);
148 
149 	if (!status) {
150 		ul_chnl_id = disp_attrs->chnl_offset + CHNLFROMRMSOFFSET;
151 		status =
152 		    (*intf_fxns->chnl_open) (&(disp_obj->chnl_from_dsp),
153 						 disp_obj->chnl_mgr,
154 						 CHNL_MODEFROMDSP, ul_chnl_id,
155 						 &chnl_attr_obj);
156 	}
157 	if (!status) {
158 		/* Allocate buffer for commands, replies */
159 		disp_obj->bufsize = disp_attrs->chnl_buf_size;
160 		disp_obj->bufsize_rms = RMS_COMMANDBUFSIZE;
161 		disp_obj->buf = kzalloc(disp_obj->bufsize, GFP_KERNEL);
162 		if (disp_obj->buf == NULL)
163 			status = -ENOMEM;
164 	}
165 func_cont:
166 	if (!status)
167 		*dispatch_obj = disp_obj;
168 	else
169 		delete_disp(disp_obj);
170 
171 	DBC_ENSURE((status && *dispatch_obj == NULL) ||
172 				(!status && *dispatch_obj));
173 	return status;
174 }
175 
176 /*
177  *  ======== disp_delete ========
178  *  Delete the NODE Dispatcher.
179  */
disp_delete(struct disp_object * disp_obj)180 void disp_delete(struct disp_object *disp_obj)
181 {
182 	DBC_REQUIRE(refs > 0);
183 	DBC_REQUIRE(disp_obj);
184 
185 	delete_disp(disp_obj);
186 }
187 
188 /*
189  *  ======== disp_exit ========
190  *  Discontinue usage of DISP module.
191  */
disp_exit(void)192 void disp_exit(void)
193 {
194 	DBC_REQUIRE(refs > 0);
195 
196 	refs--;
197 
198 	DBC_ENSURE(refs >= 0);
199 }
200 
201 /*
202  *  ======== disp_init ========
203  *  Initialize the DISP module.
204  */
disp_init(void)205 bool disp_init(void)
206 {
207 	bool ret = true;
208 
209 	DBC_REQUIRE(refs >= 0);
210 
211 	if (ret)
212 		refs++;
213 
214 	DBC_ENSURE((ret && (refs > 0)) || (!ret && (refs >= 0)));
215 	return ret;
216 }
217 
218 /*
219  *  ======== disp_node_change_priority ========
220  *  Change the priority of a node currently running on the target.
221  */
disp_node_change_priority(struct disp_object * disp_obj,struct node_object * hnode,u32 rms_fxn,nodeenv node_env,s32 prio)222 int disp_node_change_priority(struct disp_object *disp_obj,
223 				     struct node_object *hnode,
224 				     u32 rms_fxn, nodeenv node_env, s32 prio)
225 {
226 	u32 dw_arg;
227 	struct rms_command *rms_cmd;
228 	int status = 0;
229 
230 	DBC_REQUIRE(refs > 0);
231 	DBC_REQUIRE(disp_obj);
232 	DBC_REQUIRE(hnode != NULL);
233 
234 	/* Send message to RMS to change priority */
235 	rms_cmd = (struct rms_command *)(disp_obj->buf);
236 	rms_cmd->fxn = (rms_word) (rms_fxn);
237 	rms_cmd->arg1 = (rms_word) node_env;
238 	rms_cmd->arg2 = prio;
239 	status = send_message(disp_obj, node_get_timeout(hnode),
240 			      sizeof(struct rms_command), &dw_arg);
241 
242 	return status;
243 }
244 
245 /*
246  *  ======== disp_node_create ========
247  *  Create a node on the DSP by remotely calling the node's create function.
248  */
disp_node_create(struct disp_object * disp_obj,struct node_object * hnode,u32 rms_fxn,u32 ul_create_fxn,const struct node_createargs * pargs,nodeenv * node_env)249 int disp_node_create(struct disp_object *disp_obj,
250 			    struct node_object *hnode, u32 rms_fxn,
251 			    u32 ul_create_fxn,
252 			    const struct node_createargs *pargs,
253 			    nodeenv *node_env)
254 {
255 	struct node_msgargs node_msg_args;
256 	struct node_taskargs task_arg_obj;
257 	struct rms_command *rms_cmd;
258 	struct rms_msg_args *pmsg_args;
259 	struct rms_more_task_args *more_task_args;
260 	enum node_type node_type;
261 	u32 dw_length;
262 	rms_word *pdw_buf = NULL;
263 	u32 ul_bytes;
264 	u32 i;
265 	u32 total;
266 	u32 chars_in_rms_word;
267 	s32 task_args_offset;
268 	s32 sio_in_def_offset;
269 	s32 sio_out_def_offset;
270 	s32 sio_defs_offset;
271 	s32 args_offset = -1;
272 	s32 offset;
273 	struct node_strmdef strm_def;
274 	u32 max;
275 	int status = 0;
276 	struct dsp_nodeinfo node_info;
277 	u8 dev_type;
278 
279 	DBC_REQUIRE(refs > 0);
280 	DBC_REQUIRE(disp_obj);
281 	DBC_REQUIRE(hnode != NULL);
282 	DBC_REQUIRE(node_get_type(hnode) != NODE_DEVICE);
283 	DBC_REQUIRE(node_env != NULL);
284 
285 	status = dev_get_dev_type(disp_obj->dev_obj, &dev_type);
286 
287 	if (status)
288 		goto func_end;
289 
290 	if (dev_type != DSP_UNIT) {
291 		dev_dbg(bridge, "%s: unknown device type = 0x%x\n",
292 			__func__, dev_type);
293 		goto func_end;
294 	}
295 	DBC_REQUIRE(pargs != NULL);
296 	node_type = node_get_type(hnode);
297 	node_msg_args = pargs->asa.node_msg_args;
298 	max = disp_obj->bufsize_rms;	/*Max # of RMS words that can be sent */
299 	DBC_ASSERT(max == RMS_COMMANDBUFSIZE);
300 	chars_in_rms_word = sizeof(rms_word) / disp_obj->char_size;
301 	/* Number of RMS words needed to hold arg data */
302 	dw_length =
303 	    (node_msg_args.arg_length + chars_in_rms_word -
304 	     1) / chars_in_rms_word;
305 	/* Make sure msg args and command fit in buffer */
306 	total = sizeof(struct rms_command) / sizeof(rms_word) +
307 	    sizeof(struct rms_msg_args)
308 	    / sizeof(rms_word) - 1 + dw_length;
309 	if (total >= max) {
310 		status = -EPERM;
311 		dev_dbg(bridge, "%s: Message args too large for buffer! size "
312 			"= %d, max = %d\n", __func__, total, max);
313 	}
314 	/*
315 	 *  Fill in buffer to send to RMS.
316 	 *  The buffer will have the following  format:
317 	 *
318 	 *  RMS command:
319 	 *      Address of RMS_CreateNode()
320 	 *      Address of node's create function
321 	 *      dummy argument
322 	 *      node type
323 	 *
324 	 *  Message Args:
325 	 *      max number of messages
326 	 *      segid for message buffer allocation
327 	 *      notification type to use when message is received
328 	 *      length of message arg data
329 	 *      message args data
330 	 *
331 	 *  Task Args (if task or socket node):
332 	 *      priority
333 	 *      stack size
334 	 *      system stack size
335 	 *      stack segment
336 	 *      misc
337 	 *      number of input streams
338 	 *      pSTRMInDef[] - offsets of STRM definitions for input streams
339 	 *      number of output streams
340 	 *      pSTRMOutDef[] - offsets of STRM definitions for output
341 	 *      streams
342 	 *      STRMInDef[] - array of STRM definitions for input streams
343 	 *      STRMOutDef[] - array of STRM definitions for output streams
344 	 *
345 	 *  Socket Args (if DAIS socket node):
346 	 *
347 	 */
348 	if (!status) {
349 		total = 0;	/* Total number of words in buffer so far */
350 		pdw_buf = (rms_word *) disp_obj->buf;
351 		rms_cmd = (struct rms_command *)pdw_buf;
352 		rms_cmd->fxn = (rms_word) (rms_fxn);
353 		rms_cmd->arg1 = (rms_word) (ul_create_fxn);
354 		if (node_get_load_type(hnode) == NLDR_DYNAMICLOAD) {
355 			/* Flush ICACHE on Load */
356 			rms_cmd->arg2 = 1;	/* dummy argument */
357 		} else {
358 			/* Do not flush ICACHE */
359 			rms_cmd->arg2 = 0;	/* dummy argument */
360 		}
361 		rms_cmd->data = node_get_type(hnode);
362 		/*
363 		 *  args_offset is the offset of the data field in struct
364 		 *  rms_command structure. We need this to calculate stream
365 		 *  definition offsets.
366 		 */
367 		args_offset = 3;
368 		total += sizeof(struct rms_command) / sizeof(rms_word);
369 		/* Message args */
370 		pmsg_args = (struct rms_msg_args *)(pdw_buf + total);
371 		pmsg_args->max_msgs = node_msg_args.max_msgs;
372 		pmsg_args->segid = node_msg_args.seg_id;
373 		pmsg_args->notify_type = node_msg_args.notify_type;
374 		pmsg_args->arg_length = node_msg_args.arg_length;
375 		total += sizeof(struct rms_msg_args) / sizeof(rms_word) - 1;
376 		memcpy(pdw_buf + total, node_msg_args.pdata,
377 		       node_msg_args.arg_length);
378 		total += dw_length;
379 	}
380 	if (status)
381 		goto func_end;
382 
383 	/* If node is a task node, copy task create arguments into  buffer */
384 	if (node_type == NODE_TASK || node_type == NODE_DAISSOCKET) {
385 		task_arg_obj = pargs->asa.task_arg_obj;
386 		task_args_offset = total;
387 		total += sizeof(struct rms_more_task_args) / sizeof(rms_word) +
388 		    1 + task_arg_obj.num_inputs + task_arg_obj.num_outputs;
389 		/* Copy task arguments */
390 		if (total < max) {
391 			total = task_args_offset;
392 			more_task_args = (struct rms_more_task_args *)(pdw_buf +
393 								       total);
394 			/*
395 			 * Get some important info about the node. Note that we
396 			 * don't just reach into the hnode struct because
397 			 * that would break the node object's abstraction.
398 			 */
399 			get_node_info(hnode, &node_info);
400 			more_task_args->priority = node_info.execution_priority;
401 			more_task_args->stack_size = task_arg_obj.stack_size;
402 			more_task_args->sysstack_size =
403 			    task_arg_obj.sys_stack_size;
404 			more_task_args->stack_seg = task_arg_obj.stack_seg;
405 			more_task_args->heap_addr = task_arg_obj.dsp_heap_addr;
406 			more_task_args->heap_size = task_arg_obj.heap_size;
407 			more_task_args->misc = task_arg_obj.dais_arg;
408 			more_task_args->num_input_streams =
409 			    task_arg_obj.num_inputs;
410 			total +=
411 			    sizeof(struct rms_more_task_args) /
412 			    sizeof(rms_word);
413 			dev_dbg(bridge, "%s: dsp_heap_addr %x, heap_size %x\n",
414 				__func__, task_arg_obj.dsp_heap_addr,
415 				task_arg_obj.heap_size);
416 			/* Keep track of pSIOInDef[] and pSIOOutDef[]
417 			 * positions in the buffer, since this needs to be
418 			 * filled in later. */
419 			sio_in_def_offset = total;
420 			total += task_arg_obj.num_inputs;
421 			pdw_buf[total++] = task_arg_obj.num_outputs;
422 			sio_out_def_offset = total;
423 			total += task_arg_obj.num_outputs;
424 			sio_defs_offset = total;
425 			/* Fill SIO defs and offsets */
426 			offset = sio_defs_offset;
427 			for (i = 0; i < task_arg_obj.num_inputs; i++) {
428 				if (status)
429 					break;
430 
431 				pdw_buf[sio_in_def_offset + i] =
432 				    (offset - args_offset)
433 				    * (sizeof(rms_word) / DSPWORDSIZE);
434 				strm_def = task_arg_obj.strm_in_def[i];
435 				status =
436 				    fill_stream_def(pdw_buf, &total, offset,
437 						    strm_def, max,
438 						    chars_in_rms_word);
439 				offset = total;
440 			}
441 			for (i = 0; (i < task_arg_obj.num_outputs) &&
442 			     (!status); i++) {
443 				pdw_buf[sio_out_def_offset + i] =
444 				    (offset - args_offset)
445 				    * (sizeof(rms_word) / DSPWORDSIZE);
446 				strm_def = task_arg_obj.strm_out_def[i];
447 				status =
448 				    fill_stream_def(pdw_buf, &total, offset,
449 						    strm_def, max,
450 						    chars_in_rms_word);
451 				offset = total;
452 			}
453 		} else {
454 			/* Args won't fit */
455 			status = -EPERM;
456 		}
457 	}
458 	if (!status) {
459 		ul_bytes = total * sizeof(rms_word);
460 		DBC_ASSERT(ul_bytes < (RMS_COMMANDBUFSIZE * sizeof(rms_word)));
461 		status = send_message(disp_obj, node_get_timeout(hnode),
462 				      ul_bytes, node_env);
463 	}
464 func_end:
465 	return status;
466 }
467 
468 /*
469  *  ======== disp_node_delete ========
470  *  purpose:
471  *      Delete a node on the DSP by remotely calling the node's delete function.
472  *
473  */
disp_node_delete(struct disp_object * disp_obj,struct node_object * hnode,u32 rms_fxn,u32 ul_delete_fxn,nodeenv node_env)474 int disp_node_delete(struct disp_object *disp_obj,
475 			    struct node_object *hnode, u32 rms_fxn,
476 			    u32 ul_delete_fxn, nodeenv node_env)
477 {
478 	u32 dw_arg;
479 	struct rms_command *rms_cmd;
480 	int status = 0;
481 	u8 dev_type;
482 
483 	DBC_REQUIRE(refs > 0);
484 	DBC_REQUIRE(disp_obj);
485 	DBC_REQUIRE(hnode != NULL);
486 
487 	status = dev_get_dev_type(disp_obj->dev_obj, &dev_type);
488 
489 	if (!status) {
490 
491 		if (dev_type == DSP_UNIT) {
492 
493 			/*
494 			 *  Fill in buffer to send to RMS
495 			 */
496 			rms_cmd = (struct rms_command *)disp_obj->buf;
497 			rms_cmd->fxn = (rms_word) (rms_fxn);
498 			rms_cmd->arg1 = (rms_word) node_env;
499 			rms_cmd->arg2 = (rms_word) (ul_delete_fxn);
500 			rms_cmd->data = node_get_type(hnode);
501 
502 			status = send_message(disp_obj, node_get_timeout(hnode),
503 					      sizeof(struct rms_command),
504 					      &dw_arg);
505 		}
506 	}
507 	return status;
508 }
509 
510 /*
511  *  ======== disp_node_run ========
512  *  purpose:
513  *      Start execution of a node's execute phase, or resume execution of a node
514  *      that has been suspended (via DISP_NodePause()) on the DSP.
515  */
disp_node_run(struct disp_object * disp_obj,struct node_object * hnode,u32 rms_fxn,u32 ul_execute_fxn,nodeenv node_env)516 int disp_node_run(struct disp_object *disp_obj,
517 			 struct node_object *hnode, u32 rms_fxn,
518 			 u32 ul_execute_fxn, nodeenv node_env)
519 {
520 	u32 dw_arg;
521 	struct rms_command *rms_cmd;
522 	int status = 0;
523 	u8 dev_type;
524 	DBC_REQUIRE(refs > 0);
525 	DBC_REQUIRE(disp_obj);
526 	DBC_REQUIRE(hnode != NULL);
527 
528 	status = dev_get_dev_type(disp_obj->dev_obj, &dev_type);
529 
530 	if (!status) {
531 
532 		if (dev_type == DSP_UNIT) {
533 
534 			/*
535 			 *  Fill in buffer to send to RMS.
536 			 */
537 			rms_cmd = (struct rms_command *)disp_obj->buf;
538 			rms_cmd->fxn = (rms_word) (rms_fxn);
539 			rms_cmd->arg1 = (rms_word) node_env;
540 			rms_cmd->arg2 = (rms_word) (ul_execute_fxn);
541 			rms_cmd->data = node_get_type(hnode);
542 
543 			status = send_message(disp_obj, node_get_timeout(hnode),
544 					      sizeof(struct rms_command),
545 					      &dw_arg);
546 		}
547 	}
548 
549 	return status;
550 }
551 
552 /*
553  *  ======== delete_disp ========
554  *  purpose:
555  *      Frees the resources allocated for the dispatcher.
556  */
delete_disp(struct disp_object * disp_obj)557 static void delete_disp(struct disp_object *disp_obj)
558 {
559 	int status = 0;
560 	struct bridge_drv_interface *intf_fxns;
561 
562 	if (disp_obj) {
563 		intf_fxns = disp_obj->intf_fxns;
564 
565 		/* Free Node Dispatcher resources */
566 		if (disp_obj->chnl_from_dsp) {
567 			/* Channel close can fail only if the channel handle
568 			 * is invalid. */
569 			status = (*intf_fxns->chnl_close)
570 			    (disp_obj->chnl_from_dsp);
571 			if (status) {
572 				dev_dbg(bridge, "%s: Failed to close channel "
573 					"from RMS: 0x%x\n", __func__, status);
574 			}
575 		}
576 		if (disp_obj->chnl_to_dsp) {
577 			status =
578 			    (*intf_fxns->chnl_close) (disp_obj->
579 							  chnl_to_dsp);
580 			if (status) {
581 				dev_dbg(bridge, "%s: Failed to close channel to"
582 					" RMS: 0x%x\n", __func__, status);
583 			}
584 		}
585 		kfree(disp_obj->buf);
586 
587 		kfree(disp_obj);
588 	}
589 }
590 
591 /*
592  *  ======== fill_stream_def ========
593  *  purpose:
594  *      Fills stream definitions.
595  */
fill_stream_def(rms_word * pdw_buf,u32 * ptotal,u32 offset,struct node_strmdef strm_def,u32 max,u32 chars_in_rms_word)596 static int fill_stream_def(rms_word *pdw_buf, u32 *ptotal, u32 offset,
597 				  struct node_strmdef strm_def, u32 max,
598 				  u32 chars_in_rms_word)
599 {
600 	struct rms_strm_def *strm_def_obj;
601 	u32 total = *ptotal;
602 	u32 name_len;
603 	u32 dw_length;
604 	int status = 0;
605 
606 	if (total + sizeof(struct rms_strm_def) / sizeof(rms_word) >= max) {
607 		status = -EPERM;
608 	} else {
609 		strm_def_obj = (struct rms_strm_def *)(pdw_buf + total);
610 		strm_def_obj->bufsize = strm_def.buf_size;
611 		strm_def_obj->nbufs = strm_def.num_bufs;
612 		strm_def_obj->segid = strm_def.seg_id;
613 		strm_def_obj->align = strm_def.buf_alignment;
614 		strm_def_obj->timeout = strm_def.timeout;
615 	}
616 
617 	if (!status) {
618 		/*
619 		 *  Since we haven't added the device name yet, subtract
620 		 *  1 from total.
621 		 */
622 		total += sizeof(struct rms_strm_def) / sizeof(rms_word) - 1;
623 		DBC_REQUIRE(strm_def.sz_device);
624 		dw_length = strlen(strm_def.sz_device) + 1;
625 
626 		/* Number of RMS_WORDS needed to hold device name */
627 		name_len =
628 		    (dw_length + chars_in_rms_word - 1) / chars_in_rms_word;
629 
630 		if (total + name_len >= max) {
631 			status = -EPERM;
632 		} else {
633 			/*
634 			 *  Zero out last word, since the device name may not
635 			 *  extend to completely fill this word.
636 			 */
637 			pdw_buf[total + name_len - 1] = 0;
638 			/** TODO USE SERVICES * */
639 			memcpy(pdw_buf + total, strm_def.sz_device, dw_length);
640 			total += name_len;
641 			*ptotal = total;
642 		}
643 	}
644 
645 	return status;
646 }
647 
648 /*
649  *  ======== send_message ======
650  *  Send command message to RMS, get reply from RMS.
651  */
send_message(struct disp_object * disp_obj,u32 timeout,u32 ul_bytes,u32 * pdw_arg)652 static int send_message(struct disp_object *disp_obj, u32 timeout,
653 			       u32 ul_bytes, u32 *pdw_arg)
654 {
655 	struct bridge_drv_interface *intf_fxns;
656 	struct chnl_object *chnl_obj;
657 	u32 dw_arg = 0;
658 	u8 *pbuf;
659 	struct chnl_ioc chnl_ioc_obj;
660 	int status = 0;
661 
662 	DBC_REQUIRE(pdw_arg != NULL);
663 
664 	*pdw_arg = (u32) NULL;
665 	intf_fxns = disp_obj->intf_fxns;
666 	chnl_obj = disp_obj->chnl_to_dsp;
667 	pbuf = disp_obj->buf;
668 
669 	/* Send the command */
670 	status = (*intf_fxns->chnl_add_io_req) (chnl_obj, pbuf, ul_bytes, 0,
671 						    0L, dw_arg);
672 	if (status)
673 		goto func_end;
674 
675 	status =
676 	    (*intf_fxns->chnl_get_ioc) (chnl_obj, timeout, &chnl_ioc_obj);
677 	if (!status) {
678 		if (!CHNL_IS_IO_COMPLETE(chnl_ioc_obj)) {
679 			if (CHNL_IS_TIMED_OUT(chnl_ioc_obj))
680 				status = -ETIME;
681 			else
682 				status = -EPERM;
683 		}
684 	}
685 	/* Get the reply */
686 	if (status)
687 		goto func_end;
688 
689 	chnl_obj = disp_obj->chnl_from_dsp;
690 	ul_bytes = REPLYSIZE;
691 	status = (*intf_fxns->chnl_add_io_req) (chnl_obj, pbuf, ul_bytes,
692 						    0, 0L, dw_arg);
693 	if (status)
694 		goto func_end;
695 
696 	status =
697 	    (*intf_fxns->chnl_get_ioc) (chnl_obj, timeout, &chnl_ioc_obj);
698 	if (!status) {
699 		if (CHNL_IS_TIMED_OUT(chnl_ioc_obj)) {
700 			status = -ETIME;
701 		} else if (chnl_ioc_obj.byte_size < ul_bytes) {
702 			/* Did not get all of the reply from the RMS */
703 			status = -EPERM;
704 		} else {
705 			if (CHNL_IS_IO_COMPLETE(chnl_ioc_obj)) {
706 				DBC_ASSERT(chnl_ioc_obj.buf == pbuf);
707 				if (*((int *)chnl_ioc_obj.buf) < 0) {
708 					/* Translate DSP's to kernel error */
709 					status = -EREMOTEIO;
710 					dev_dbg(bridge, "%s: DSP-side failed:"
711 						" DSP errcode = 0x%x, Kernel "
712 						"errcode = %d\n", __func__,
713 						*(int *)pbuf, status);
714 				}
715 				*pdw_arg =
716 				    (((rms_word *) (chnl_ioc_obj.buf))[1]);
717 			} else {
718 				status = -EPERM;
719 			}
720 		}
721 	}
722 func_end:
723 	return status;
724 }
725