Merge branch 'next' of master.kernel.org:/pub/scm/linux/kernel/git/galak/powerpc...
[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  *
49  * @dev: device object for our driver
50  */
51 void heci_set_csr_register(struct iamt_heci_device *dev)
52 {
53         write_heci_register(dev, H_CSR, dev->host_hw_state);
54         dev->host_hw_state = read_heci_register(dev, H_CSR);
55 }
56
57 /**
58  * heci_csr_enable_interrupts - enable heci device interrupts
59  *
60  * @dev: device object for our driver
61  */
62 void heci_csr_enable_interrupts(struct iamt_heci_device *dev)
63 {
64         dev->host_hw_state |= H_IE;
65         heci_set_csr_register(dev);
66 }
67
68 /**
69  * heci_csr_disable_interrupts - disable heci device interrupts
70  *
71  * @dev: device object for our driver
72  */
73 void heci_csr_disable_interrupts(struct iamt_heci_device *dev)
74 {
75         dev->host_hw_state &= ~H_IE;
76         heci_set_csr_register(dev);
77 }
78
79
80 /**
81  * _host_get_filled_slots - get number of device filled buffer slots
82  *
83  * @device: the device structure
84  *
85  * returns numer of filled slots
86  */
87 static unsigned char _host_get_filled_slots(const struct iamt_heci_device *dev)
88 {
89         char read_ptr, write_ptr;
90
91         read_ptr = (char) ((dev->host_hw_state & H_CBRP) >> 8);
92         write_ptr = (char) ((dev->host_hw_state & H_CBWP) >> 16);
93
94         return (unsigned char) (write_ptr - read_ptr);
95 }
96
97 /**
98  * host_buffer_is_empty  - check if host buffer is empty.
99  *
100  * @dev: device object for our driver
101  *
102  * returns  1 if empty, 0 - otherwise.
103  */
104 int host_buffer_is_empty(struct iamt_heci_device *dev)
105 {
106         unsigned char filled_slots;
107
108         dev->host_hw_state = read_heci_register(dev, H_CSR);
109         filled_slots = _host_get_filled_slots(dev);
110
111         if (filled_slots > 0)
112                 return 0;
113
114         return 1;
115 }
116
117 /**
118  * count_empty_write_slots  - count write empty slots.
119  *
120  * @dev: device object for our driver
121  *
122  * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise empty slots count
123  */
124 __s32 count_empty_write_slots(const struct iamt_heci_device *dev)
125 {
126         unsigned char buffer_depth, filled_slots, empty_slots;
127
128         buffer_depth = (unsigned char) ((dev->host_hw_state & H_CBD) >> 24);
129         filled_slots = _host_get_filled_slots(dev);
130         empty_slots = buffer_depth - filled_slots;
131
132         if (filled_slots > buffer_depth) {
133                 /* overflow */
134                 return -ESLOTS_OVERFLOW;
135         }
136
137         return (__s32) empty_slots;
138 }
139
140 /**
141  * heci_write_message  - write a message to heci device.
142  *
143  * @dev: device object for our driver
144  * @heci_hdr: header of  message
145  * @write_buffer: message buffer will be write
146  * @write_length: message size will be write
147  *
148  * returns 1 if success, 0 - otherwise.
149  */
150 int heci_write_message(struct iamt_heci_device *dev,
151                              struct heci_msg_hdr *header,
152                              unsigned char *write_buffer,
153                              unsigned long write_length)
154 {
155         __u32 temp_msg = 0;
156         unsigned long bytes_written = 0;
157         unsigned char buffer_depth, filled_slots, empty_slots;
158         unsigned long dw_to_write;
159
160         dev->host_hw_state = read_heci_register(dev, H_CSR);
161         DBG("host_hw_state = 0x%08x.\n", dev->host_hw_state);
162         DBG("heci_write_message header=%08x.\n", *((__u32 *) header));
163         buffer_depth = (unsigned char) ((dev->host_hw_state & H_CBD) >> 24);
164         filled_slots = _host_get_filled_slots(dev);
165         empty_slots = buffer_depth - filled_slots;
166         DBG("filled = %hu, empty = %hu.\n", filled_slots, empty_slots);
167
168         dw_to_write = ((write_length + 3) / 4);
169
170         if (dw_to_write > empty_slots)
171                 return 0;
172
173         write_heci_register(dev, H_CB_WW, *((__u32 *) header));
174
175         while (write_length >= 4) {
176                 write_heci_register(dev, H_CB_WW,
177                                 *(__u32 *) (write_buffer + bytes_written));
178                 bytes_written += 4;
179                 write_length -= 4;
180         }
181
182         if (write_length > 0) {
183                 memcpy(&temp_msg, &write_buffer[bytes_written], write_length);
184                 write_heci_register(dev, H_CB_WW, temp_msg);
185         }
186
187         dev->host_hw_state |= H_IG;
188         write_heci_register(dev, H_CSR, dev->host_hw_state);
189         dev->me_hw_state = read_heci_register(dev, ME_CSR_HA);
190         if ((dev->me_hw_state & ME_RDY_HRA) != ME_RDY_HRA)
191                 return 0;
192
193         dev->write_hang = 0;
194         return 1;
195 }
196
197 /**
198  * count_full_read_slots  - count read full slots.
199  *
200  * @dev: device object for our driver
201  *
202  * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise filled slots count
203  */
204 __s32 count_full_read_slots(struct iamt_heci_device *dev)
205 {
206         char read_ptr, write_ptr;
207         unsigned char buffer_depth, filled_slots;
208
209         dev->me_hw_state = read_heci_register(dev, ME_CSR_HA);
210         buffer_depth = (unsigned char)((dev->me_hw_state & ME_CBD_HRA) >> 24);
211         read_ptr = (char) ((dev->me_hw_state & ME_CBRP_HRA) >> 8);
212         write_ptr = (char) ((dev->me_hw_state & ME_CBWP_HRA) >> 16);
213         filled_slots = (unsigned char) (write_ptr - read_ptr);
214
215         if (filled_slots > buffer_depth) {
216                 /* overflow */
217                 return -ESLOTS_OVERFLOW;
218         }
219
220         DBG("filled_slots =%08x  \n", filled_slots);
221         return (__s32) filled_slots;
222 }
223
224 /**
225  * heci_read_slots  - read a message from heci device.
226  *
227  * @dev: device object for our driver
228  * @buffer: message buffer will be write
229  * @buffer_length: message size will be read
230  */
231 void heci_read_slots(struct iamt_heci_device *dev,
232                      unsigned char *buffer, unsigned long buffer_length)
233 {
234         __u32 i = 0;
235         unsigned char temp_buf[sizeof(__u32)];
236
237         while (buffer_length >= sizeof(__u32)) {
238                 ((__u32 *) buffer)[i] = read_heci_register(dev, ME_CB_RW);
239                 DBG("buffer[%d]= %d\n", i, ((__u32 *) buffer)[i]);
240                 i++;
241                 buffer_length -= sizeof(__u32);
242         }
243
244         if (buffer_length > 0) {
245                 *((__u32 *) &temp_buf) = read_heci_register(dev, ME_CB_RW);
246                 memcpy(&buffer[i * 4], temp_buf, buffer_length);
247         }
248
249         dev->host_hw_state |= H_IG;
250         heci_set_csr_register(dev);
251 }
252
253 /**
254  * flow_ctrl_creds  - check flow_control credentials.
255  *
256  * @dev: device object for our driver
257  * @file_ext: private data of the file object
258  *
259  * returns 1 if flow_ctrl_creds >0, 0 - otherwise.
260  */
261 int flow_ctrl_creds(struct iamt_heci_device *dev,
262                                    struct heci_file_private *file_ext)
263 {
264         __u8 i;
265
266         if (!dev->num_heci_me_clients)
267                 return 0;
268
269         if (file_ext == NULL)
270                 return 0;
271
272         if (file_ext->flow_ctrl_creds > 0)
273                 return 1;
274
275         for (i = 0; i < dev->num_heci_me_clients; i++) {
276                 if (dev->me_clients[i].client_id == file_ext->me_client_id) {
277                         if (dev->me_clients[i].flow_ctrl_creds > 0) {
278                                 BUG_ON(dev->me_clients[i].props.single_recv_buf
279                                          == 0);
280                                 return 1;
281                         }
282                         return 0;
283                 }
284         }
285         BUG();
286         return 0;
287 }
288
289 /**
290  * flow_ctrl_reduce  - reduce flow_control.
291  *
292  * @dev: device object for our driver
293  * @file_ext: private data of the file object
294  */
295 void flow_ctrl_reduce(struct iamt_heci_device *dev,
296                          struct heci_file_private *file_ext)
297 {
298         __u8 i;
299
300         if (!dev->num_heci_me_clients)
301                 return;
302
303         for (i = 0; i < dev->num_heci_me_clients; i++) {
304                 if (dev->me_clients[i].client_id == file_ext->me_client_id) {
305                         if (dev->me_clients[i].props.single_recv_buf != 0) {
306                                 BUG_ON(dev->me_clients[i].flow_ctrl_creds <= 0);
307                                 dev->me_clients[i].flow_ctrl_creds--;
308                         } else {
309                                 BUG_ON(file_ext->flow_ctrl_creds <= 0);
310                                 file_ext->flow_ctrl_creds--;
311                         }
312                         return;
313                 }
314         }
315         BUG();
316 }
317
318 /**
319  * heci_send_flow_control - send flow control to fw.
320  *
321  * @dev: device object for our driver
322  * @file_ext: private data of the file object
323  *
324  * returns 1 if success, 0 - otherwise.
325  */
326 int heci_send_flow_control(struct iamt_heci_device *dev,
327                                  struct heci_file_private *file_ext)
328 {
329         struct heci_msg_hdr *heci_hdr;
330         struct hbm_flow_control *heci_flow_control;
331
332         heci_hdr = (struct heci_msg_hdr *) &dev->wr_msg_buf[0];
333         heci_hdr->host_addr = 0;
334         heci_hdr->me_addr = 0;
335         heci_hdr->length = sizeof(struct hbm_flow_control);
336         heci_hdr->msg_complete = 1;
337         heci_hdr->reserved = 0;
338
339         heci_flow_control = (struct hbm_flow_control *) &dev->wr_msg_buf[1];
340         memset(heci_flow_control, 0, sizeof(heci_flow_control));
341         heci_flow_control->host_addr = file_ext->host_client_id;
342         heci_flow_control->me_addr = file_ext->me_client_id;
343         heci_flow_control->cmd.cmd = HECI_FLOW_CONTROL_CMD;
344         memset(heci_flow_control->reserved, 0,
345                         sizeof(heci_flow_control->reserved));
346         DBG("sending flow control host client = %d, me client = %d\n",
347             file_ext->host_client_id, file_ext->me_client_id);
348         if (!heci_write_message(dev, heci_hdr,
349                                 (unsigned char *) heci_flow_control,
350                                 sizeof(struct hbm_flow_control)))
351                 return 0;
352
353         return 1;
354
355 }
356
357 /**
358  * other_client_is_connecting  - check if other
359  *    client with the same client id is connected.
360  *
361  * @dev: device object for our driver
362  * @file_ext: private data of the file object
363  *
364  * returns 1 if other client is connected, 0 - otherwise.
365  */
366 int other_client_is_connecting(struct iamt_heci_device *dev,
367                 struct heci_file_private *file_ext)
368 {
369         struct heci_file_private *file_pos = NULL;
370         struct heci_file_private *file_next = NULL;
371
372         list_for_each_entry_safe(file_pos, file_next, &dev->file_list, link) {
373                 if ((file_pos->state == HECI_FILE_CONNECTING)
374                         && (file_pos != file_ext)
375                         && file_ext->me_client_id == file_pos->me_client_id)
376                         return 1;
377
378         }
379         return 0;
380 }
381
382 /**
383  * heci_send_wd  - send watch dog message to fw.
384  *
385  * @dev: device object for our driver
386  *
387  * returns 1 if success, 0 - otherwise.
388  */
389 int heci_send_wd(struct iamt_heci_device *dev)
390 {
391         struct heci_msg_hdr *heci_hdr;
392
393         heci_hdr = (struct heci_msg_hdr *) &dev->wr_msg_buf[0];
394         heci_hdr->host_addr = dev->wd_file_ext.host_client_id;
395         heci_hdr->me_addr = dev->wd_file_ext.me_client_id;
396         heci_hdr->msg_complete = 1;
397         heci_hdr->reserved = 0;
398
399         if (!memcmp(dev->wd_data, heci_start_wd_params,
400                         HECI_WD_PARAMS_SIZE)) {
401                 heci_hdr->length = HECI_START_WD_DATA_SIZE;
402         } else {
403                 BUG_ON(memcmp(dev->wd_data, heci_stop_wd_params,
404                         HECI_WD_PARAMS_SIZE));
405                 heci_hdr->length = HECI_WD_PARAMS_SIZE;
406         }
407
408         if (!heci_write_message(dev, heci_hdr, dev->wd_data, heci_hdr->length))
409                 return 0;
410
411         return 1;
412 }
413
414 /**
415  * heci_disconnect  - send disconnect message to fw.
416  *
417  * @dev: device object for our driver
418  * @file_ext: private data of the file object
419  *
420  * returns 1 if success, 0 - otherwise.
421  */
422 int heci_disconnect(struct iamt_heci_device *dev,
423                           struct heci_file_private *file_ext)
424 {
425         struct heci_msg_hdr *heci_hdr;
426         struct hbm_client_disconnect_request *heci_cli_disconnect;
427
428         heci_hdr = (struct heci_msg_hdr *) &dev->wr_msg_buf[0];
429         heci_hdr->host_addr = 0;
430         heci_hdr->me_addr = 0;
431         heci_hdr->length = sizeof(struct hbm_client_disconnect_request);
432         heci_hdr->msg_complete = 1;
433         heci_hdr->reserved = 0;
434
435         heci_cli_disconnect =
436             (struct hbm_client_disconnect_request *) &dev->wr_msg_buf[1];
437         memset(heci_cli_disconnect, 0, sizeof(heci_cli_disconnect));
438         heci_cli_disconnect->host_addr = file_ext->host_client_id;
439         heci_cli_disconnect->me_addr = file_ext->me_client_id;
440         heci_cli_disconnect->cmd.cmd = CLIENT_DISCONNECT_REQ_CMD;
441         heci_cli_disconnect->reserved[0] = 0;
442
443         if (!heci_write_message(dev, heci_hdr,
444                                 (unsigned char *) heci_cli_disconnect,
445                                 sizeof(struct hbm_client_disconnect_request)))
446                 return 0;
447
448         return 1;
449 }
450
451 /**
452  * heci_connect - send connect message to fw.
453  *
454  * @dev: device object for our driver
455  * @file_ext: private data of the file object
456  *
457  * returns 1 if success, 0 - otherwise.
458  */
459 int heci_connect(struct iamt_heci_device *dev,
460                        struct heci_file_private *file_ext)
461 {
462         struct heci_msg_hdr *heci_hdr;
463         struct hbm_client_connect_request *heci_cli_connect;
464
465         heci_hdr = (struct heci_msg_hdr *) &dev->wr_msg_buf[0];
466         heci_hdr->host_addr = 0;
467         heci_hdr->me_addr = 0;
468         heci_hdr->length = sizeof(struct hbm_client_connect_request);
469         heci_hdr->msg_complete = 1;
470         heci_hdr->reserved = 0;
471
472         heci_cli_connect =
473             (struct hbm_client_connect_request *) &dev->wr_msg_buf[1];
474         heci_cli_connect->host_addr = file_ext->host_client_id;
475         heci_cli_connect->me_addr = file_ext->me_client_id;
476         heci_cli_connect->cmd.cmd = CLIENT_CONNECT_REQ_CMD;
477         heci_cli_connect->reserved = 0;
478
479         if (!heci_write_message(dev, heci_hdr,
480                                 (unsigned char *) heci_cli_connect,
481                                 sizeof(struct hbm_client_connect_request)))
482                 return 0;
483
484         return 1;
485 }