Merge branch 'sched-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6] / drivers / staging / heci / heci_interface.c
1 /*
2  * Part of Intel(R) Manageability Engine Interface Linux driver
3  *
4  * Copyright (c) 2003 - 2008 Intel Corp.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification.
13  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14  *    substantially similar to the "NO WARRANTY" disclaimer below
15  *    ("Disclaimer") and any redistribution must be conditioned upon
16  *    including a substantially similar Disclaimer requirement for further
17  *    binary redistribution.
18  * 3. Neither the names of the above-listed copyright holders nor the names
19  *    of any contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * Alternatively, this software may be distributed under the terms of the
23  * GNU General Public License ("GPL") version 2 as published by the Free
24  * Software Foundation.
25  *
26  * NO WARRANTY
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
36  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGES.
38  *
39  */
40
41
42 #include "heci.h"
43 #include "heci_interface.h"
44
45
46 /**
47  * heci_set_csr_register - write H_CSR register to the heci device,
48  * and ignore the H_IS bit for it is write-one-to-zero.
49  *
50  * @dev: device object for our driver
51  */
52 void heci_set_csr_register(struct iamt_heci_device *dev)
53 {
54         if ((dev->host_hw_state & H_IS) == H_IS)
55                 dev->host_hw_state &= ~H_IS;
56         write_heci_register(dev, H_CSR, dev->host_hw_state);
57         dev->host_hw_state = read_heci_register(dev, H_CSR);
58 }
59
60 /**
61  * heci_csr_enable_interrupts - enable heci device interrupts
62  *
63  * @dev: device object for our driver
64  */
65 void heci_csr_enable_interrupts(struct iamt_heci_device *dev)
66 {
67         dev->host_hw_state |= H_IE;
68         heci_set_csr_register(dev);
69 }
70
71 /**
72  * heci_csr_disable_interrupts - disable heci device interrupts
73  *
74  * @dev: device object for our driver
75  */
76 void heci_csr_disable_interrupts(struct iamt_heci_device *dev)
77 {
78         dev->host_hw_state &= ~H_IE;
79         heci_set_csr_register(dev);
80 }
81
82 /**
83  * heci_csr_clear_his - clear H_IS bit in H_CSR
84  *
85  * @dev: device object for our driver
86  */
87 void heci_csr_clear_his(struct iamt_heci_device *dev)
88 {
89         write_heci_register(dev, H_CSR, dev->host_hw_state);
90         dev->host_hw_state = read_heci_register(dev, H_CSR);
91 }
92
93 /**
94  * _host_get_filled_slots - get number of device filled buffer slots
95  *
96  * @device: the device structure
97  *
98  * returns numer of filled slots
99  */
100 static unsigned char _host_get_filled_slots(const struct iamt_heci_device *dev)
101 {
102         char read_ptr, write_ptr;
103
104         read_ptr = (char) ((dev->host_hw_state & H_CBRP) >> 8);
105         write_ptr = (char) ((dev->host_hw_state & H_CBWP) >> 16);
106
107         return (unsigned char) (write_ptr - read_ptr);
108 }
109
110 /**
111  * host_buffer_is_empty  - check if host buffer is empty.
112  *
113  * @dev: device object for our driver
114  *
115  * returns  1 if empty, 0 - otherwise.
116  */
117 int host_buffer_is_empty(struct iamt_heci_device *dev)
118 {
119         unsigned char filled_slots;
120
121         dev->host_hw_state = read_heci_register(dev, H_CSR);
122         filled_slots = _host_get_filled_slots(dev);
123
124         if (filled_slots > 0)
125                 return 0;
126
127         return 1;
128 }
129
130 /**
131  * count_empty_write_slots  - count write empty slots.
132  *
133  * @dev: device object for our driver
134  *
135  * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise empty slots count
136  */
137 __s32 count_empty_write_slots(const struct iamt_heci_device *dev)
138 {
139         unsigned char buffer_depth, filled_slots, empty_slots;
140
141         buffer_depth = (unsigned char) ((dev->host_hw_state & H_CBD) >> 24);
142         filled_slots = _host_get_filled_slots(dev);
143         empty_slots = buffer_depth - filled_slots;
144
145         if (filled_slots > buffer_depth) {
146                 /* overflow */
147                 return -ESLOTS_OVERFLOW;
148         }
149
150         return (__s32) empty_slots;
151 }
152
153 /**
154  * heci_write_message  - write a message to heci device.
155  *
156  * @dev: device object for our driver
157  * @heci_hdr: header of  message
158  * @write_buffer: message buffer will be write
159  * @write_length: message size will be write
160  *
161  * returns 1 if success, 0 - otherwise.
162  */
163 int heci_write_message(struct iamt_heci_device *dev,
164                              struct heci_msg_hdr *header,
165                              unsigned char *write_buffer,
166                              unsigned long write_length)
167 {
168         __u32 temp_msg = 0;
169         unsigned long bytes_written = 0;
170         unsigned char buffer_depth, filled_slots, empty_slots;
171         unsigned long dw_to_write;
172
173         dev->host_hw_state = read_heci_register(dev, H_CSR);
174         DBG("host_hw_state = 0x%08x.\n", dev->host_hw_state);
175         DBG("heci_write_message header=%08x.\n", *((__u32 *) header));
176         buffer_depth = (unsigned char) ((dev->host_hw_state & H_CBD) >> 24);
177         filled_slots = _host_get_filled_slots(dev);
178         empty_slots = buffer_depth - filled_slots;
179         DBG("filled = %hu, empty = %hu.\n", filled_slots, empty_slots);
180
181         dw_to_write = ((write_length + 3) / 4);
182
183         if (dw_to_write > empty_slots)
184                 return 0;
185
186         write_heci_register(dev, H_CB_WW, *((__u32 *) header));
187
188         while (write_length >= 4) {
189                 write_heci_register(dev, H_CB_WW,
190                                 *(__u32 *) (write_buffer + bytes_written));
191                 bytes_written += 4;
192                 write_length -= 4;
193         }
194
195         if (write_length > 0) {
196                 memcpy(&temp_msg, &write_buffer[bytes_written], write_length);
197                 write_heci_register(dev, H_CB_WW, temp_msg);
198         }
199
200         dev->host_hw_state |= H_IG;
201         heci_set_csr_register(dev);
202         dev->me_hw_state = read_heci_register(dev, ME_CSR_HA);
203         if ((dev->me_hw_state & ME_RDY_HRA) != ME_RDY_HRA)
204                 return 0;
205
206         dev->write_hang = 0;
207         return 1;
208 }
209
210 /**
211  * count_full_read_slots  - count read full slots.
212  *
213  * @dev: device object for our driver
214  *
215  * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise filled slots count
216  */
217 __s32 count_full_read_slots(struct iamt_heci_device *dev)
218 {
219         char read_ptr, write_ptr;
220         unsigned char buffer_depth, filled_slots;
221
222         dev->me_hw_state = read_heci_register(dev, ME_CSR_HA);
223         buffer_depth = (unsigned char)((dev->me_hw_state & ME_CBD_HRA) >> 24);
224         read_ptr = (char) ((dev->me_hw_state & ME_CBRP_HRA) >> 8);
225         write_ptr = (char) ((dev->me_hw_state & ME_CBWP_HRA) >> 16);
226         filled_slots = (unsigned char) (write_ptr - read_ptr);
227
228         if (filled_slots > buffer_depth) {
229                 /* overflow */
230                 return -ESLOTS_OVERFLOW;
231         }
232
233         DBG("filled_slots =%08x  \n", filled_slots);
234         return (__s32) filled_slots;
235 }
236
237 /**
238  * heci_read_slots  - read a message from heci device.
239  *
240  * @dev: device object for our driver
241  * @buffer: message buffer will be write
242  * @buffer_length: message size will be read
243  */
244 void heci_read_slots(struct iamt_heci_device *dev,
245                      unsigned char *buffer, unsigned long buffer_length)
246 {
247         __u32 i = 0;
248         unsigned char temp_buf[sizeof(__u32)];
249
250         while (buffer_length >= sizeof(__u32)) {
251                 ((__u32 *) buffer)[i] = read_heci_register(dev, ME_CB_RW);
252                 DBG("buffer[%d]= %d\n", i, ((__u32 *) buffer)[i]);
253                 i++;
254                 buffer_length -= sizeof(__u32);
255         }
256
257         if (buffer_length > 0) {
258                 *((__u32 *) &temp_buf) = read_heci_register(dev, ME_CB_RW);
259                 memcpy(&buffer[i * 4], temp_buf, buffer_length);
260         }
261
262         dev->host_hw_state |= H_IG;
263         heci_set_csr_register(dev);
264 }
265
266 /**
267  * flow_ctrl_creds  - check flow_control credentials.
268  *
269  * @dev: device object for our driver
270  * @file_ext: private data of the file object
271  *
272  * returns 1 if flow_ctrl_creds >0, 0 - otherwise.
273  */
274 int flow_ctrl_creds(struct iamt_heci_device *dev,
275                                    struct heci_file_private *file_ext)
276 {
277         __u8 i;
278
279         if (!dev->num_heci_me_clients)
280                 return 0;
281
282         if (file_ext == NULL)
283                 return 0;
284
285         if (file_ext->flow_ctrl_creds > 0)
286                 return 1;
287
288         for (i = 0; i < dev->num_heci_me_clients; i++) {
289                 if (dev->me_clients[i].client_id == file_ext->me_client_id) {
290                         if (dev->me_clients[i].flow_ctrl_creds > 0) {
291                                 BUG_ON(dev->me_clients[i].props.single_recv_buf
292                                          == 0);
293                                 return 1;
294                         }
295                         return 0;
296                 }
297         }
298         BUG();
299         return 0;
300 }
301
302 /**
303  * flow_ctrl_reduce  - reduce flow_control.
304  *
305  * @dev: device object for our driver
306  * @file_ext: private data of the file object
307  */
308 void flow_ctrl_reduce(struct iamt_heci_device *dev,
309                          struct heci_file_private *file_ext)
310 {
311         __u8 i;
312
313         if (!dev->num_heci_me_clients)
314                 return;
315
316         for (i = 0; i < dev->num_heci_me_clients; i++) {
317                 if (dev->me_clients[i].client_id == file_ext->me_client_id) {
318                         if (dev->me_clients[i].props.single_recv_buf != 0) {
319                                 BUG_ON(dev->me_clients[i].flow_ctrl_creds <= 0);
320                                 dev->me_clients[i].flow_ctrl_creds--;
321                         } else {
322                                 BUG_ON(file_ext->flow_ctrl_creds <= 0);
323                                 file_ext->flow_ctrl_creds--;
324                         }
325                         return;
326                 }
327         }
328         BUG();
329 }
330
331 /**
332  * heci_send_flow_control - send flow control to fw.
333  *
334  * @dev: device object for our driver
335  * @file_ext: private data of the file object
336  *
337  * returns 1 if success, 0 - otherwise.
338  */
339 int heci_send_flow_control(struct iamt_heci_device *dev,
340                                  struct heci_file_private *file_ext)
341 {
342         struct heci_msg_hdr *heci_hdr;
343         struct hbm_flow_control *heci_flow_control;
344
345         heci_hdr = (struct heci_msg_hdr *) &dev->wr_msg_buf[0];
346         heci_hdr->host_addr = 0;
347         heci_hdr->me_addr = 0;
348         heci_hdr->length = sizeof(struct hbm_flow_control);
349         heci_hdr->msg_complete = 1;
350         heci_hdr->reserved = 0;
351
352         heci_flow_control = (struct hbm_flow_control *) &dev->wr_msg_buf[1];
353         memset(heci_flow_control, 0, sizeof(heci_flow_control));
354         heci_flow_control->host_addr = file_ext->host_client_id;
355         heci_flow_control->me_addr = file_ext->me_client_id;
356         heci_flow_control->cmd.cmd = HECI_FLOW_CONTROL_CMD;
357         memset(heci_flow_control->reserved, 0,
358                         sizeof(heci_flow_control->reserved));
359         DBG("sending flow control host client = %d, me client = %d\n",
360             file_ext->host_client_id, file_ext->me_client_id);
361         if (!heci_write_message(dev, heci_hdr,
362                                 (unsigned char *) heci_flow_control,
363                                 sizeof(struct hbm_flow_control)))
364                 return 0;
365
366         return 1;
367
368 }
369
370 /**
371  * other_client_is_connecting  - check if other
372  *    client with the same client id is connected.
373  *
374  * @dev: device object for our driver
375  * @file_ext: private data of the file object
376  *
377  * returns 1 if other client is connected, 0 - otherwise.
378  */
379 int other_client_is_connecting(struct iamt_heci_device *dev,
380                 struct heci_file_private *file_ext)
381 {
382         struct heci_file_private *file_pos = NULL;
383         struct heci_file_private *file_next = NULL;
384
385         list_for_each_entry_safe(file_pos, file_next, &dev->file_list, link) {
386                 if ((file_pos->state == HECI_FILE_CONNECTING)
387                         && (file_pos != file_ext)
388                         && file_ext->me_client_id == file_pos->me_client_id)
389                         return 1;
390
391         }
392         return 0;
393 }
394
395 /**
396  * heci_send_wd  - send watch dog message to fw.
397  *
398  * @dev: device object for our driver
399  *
400  * returns 1 if success, 0 - otherwise.
401  */
402 int heci_send_wd(struct iamt_heci_device *dev)
403 {
404         struct heci_msg_hdr *heci_hdr;
405
406         heci_hdr = (struct heci_msg_hdr *) &dev->wr_msg_buf[0];
407         heci_hdr->host_addr = dev->wd_file_ext.host_client_id;
408         heci_hdr->me_addr = dev->wd_file_ext.me_client_id;
409         heci_hdr->msg_complete = 1;
410         heci_hdr->reserved = 0;
411
412         if (!memcmp(dev->wd_data, heci_start_wd_params,
413                         HECI_WD_PARAMS_SIZE)) {
414                 heci_hdr->length = HECI_START_WD_DATA_SIZE;
415         } else {
416                 BUG_ON(memcmp(dev->wd_data, heci_stop_wd_params,
417                         HECI_WD_PARAMS_SIZE));
418                 heci_hdr->length = HECI_WD_PARAMS_SIZE;
419         }
420
421         if (!heci_write_message(dev, heci_hdr, dev->wd_data, heci_hdr->length))
422                 return 0;
423
424         return 1;
425 }
426
427 /**
428  * heci_disconnect  - send disconnect message to fw.
429  *
430  * @dev: device object for our driver
431  * @file_ext: private data of the file object
432  *
433  * returns 1 if success, 0 - otherwise.
434  */
435 int heci_disconnect(struct iamt_heci_device *dev,
436                           struct heci_file_private *file_ext)
437 {
438         struct heci_msg_hdr *heci_hdr;
439         struct hbm_client_disconnect_request *heci_cli_disconnect;
440
441         heci_hdr = (struct heci_msg_hdr *) &dev->wr_msg_buf[0];
442         heci_hdr->host_addr = 0;
443         heci_hdr->me_addr = 0;
444         heci_hdr->length = sizeof(struct hbm_client_disconnect_request);
445         heci_hdr->msg_complete = 1;
446         heci_hdr->reserved = 0;
447
448         heci_cli_disconnect =
449             (struct hbm_client_disconnect_request *) &dev->wr_msg_buf[1];
450         memset(heci_cli_disconnect, 0, sizeof(heci_cli_disconnect));
451         heci_cli_disconnect->host_addr = file_ext->host_client_id;
452         heci_cli_disconnect->me_addr = file_ext->me_client_id;
453         heci_cli_disconnect->cmd.cmd = CLIENT_DISCONNECT_REQ_CMD;
454         heci_cli_disconnect->reserved[0] = 0;
455
456         if (!heci_write_message(dev, heci_hdr,
457                                 (unsigned char *) heci_cli_disconnect,
458                                 sizeof(struct hbm_client_disconnect_request)))
459                 return 0;
460
461         return 1;
462 }
463
464 /**
465  * heci_connect - send connect message to fw.
466  *
467  * @dev: device object for our driver
468  * @file_ext: private data of the file object
469  *
470  * returns 1 if success, 0 - otherwise.
471  */
472 int heci_connect(struct iamt_heci_device *dev,
473                        struct heci_file_private *file_ext)
474 {
475         struct heci_msg_hdr *heci_hdr;
476         struct hbm_client_connect_request *heci_cli_connect;
477
478         heci_hdr = (struct heci_msg_hdr *) &dev->wr_msg_buf[0];
479         heci_hdr->host_addr = 0;
480         heci_hdr->me_addr = 0;
481         heci_hdr->length = sizeof(struct hbm_client_connect_request);
482         heci_hdr->msg_complete = 1;
483         heci_hdr->reserved = 0;
484
485         heci_cli_connect =
486             (struct hbm_client_connect_request *) &dev->wr_msg_buf[1];
487         heci_cli_connect->host_addr = file_ext->host_client_id;
488         heci_cli_connect->me_addr = file_ext->me_client_id;
489         heci_cli_connect->cmd.cmd = CLIENT_CONNECT_REQ_CMD;
490         heci_cli_connect->reserved = 0;
491
492         if (!heci_write_message(dev, heci_hdr,
493                                 (unsigned char *) heci_cli_connect,
494                                 sizeof(struct hbm_client_connect_request)))
495                 return 0;
496
497         return 1;
498 }