xfs: switch to NOFS allocation under i_lock in xfs_attr_rmtval_set
[linux-2.6/cjktty.git] / drivers / staging / heci / heci_interface.c
blobc5f51a7ddf2c69f5f74a4ba1f38b8f922b34ab5d
1 /*
2 * Part of Intel(R) Manageability Engine Interface Linux driver
4 * Copyright (c) 2003 - 2008 Intel Corp.
5 * All rights reserved.
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.
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.
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.
42 #include "heci.h"
43 #include "heci_interface.h"
46 /**
47 * heci_set_csr_register - write H_CSR register to the heci device
49 * @dev: device object for our driver
51 void heci_set_csr_register(struct iamt_heci_device *dev)
53 write_heci_register(dev, H_CSR, dev->host_hw_state);
54 dev->host_hw_state = read_heci_register(dev, H_CSR);
57 /**
58 * heci_csr_enable_interrupts - enable heci device interrupts
60 * @dev: device object for our driver
62 void heci_csr_enable_interrupts(struct iamt_heci_device *dev)
64 dev->host_hw_state |= H_IE;
65 heci_set_csr_register(dev);
68 /**
69 * heci_csr_disable_interrupts - disable heci device interrupts
71 * @dev: device object for our driver
73 void heci_csr_disable_interrupts(struct iamt_heci_device *dev)
75 dev->host_hw_state &= ~H_IE;
76 heci_set_csr_register(dev);
80 /**
81 * _host_get_filled_slots - get number of device filled buffer slots
83 * @device: the device structure
85 * returns numer of filled slots
87 static unsigned char _host_get_filled_slots(const struct iamt_heci_device *dev)
89 char read_ptr, write_ptr;
91 read_ptr = (char) ((dev->host_hw_state & H_CBRP) >> 8);
92 write_ptr = (char) ((dev->host_hw_state & H_CBWP) >> 16);
94 return (unsigned char) (write_ptr - read_ptr);
97 /**
98 * host_buffer_is_empty - check if host buffer is empty.
100 * @dev: device object for our driver
102 * returns 1 if empty, 0 - otherwise.
104 int host_buffer_is_empty(struct iamt_heci_device *dev)
106 unsigned char filled_slots;
108 dev->host_hw_state = read_heci_register(dev, H_CSR);
109 filled_slots = _host_get_filled_slots(dev);
111 if (filled_slots > 0)
112 return 0;
114 return 1;
118 * count_empty_write_slots - count write empty slots.
120 * @dev: device object for our driver
122 * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise empty slots count
124 __s32 count_empty_write_slots(const struct iamt_heci_device *dev)
126 unsigned char buffer_depth, filled_slots, empty_slots;
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;
132 if (filled_slots > buffer_depth) {
133 /* overflow */
134 return -ESLOTS_OVERFLOW;
137 return (__s32) empty_slots;
141 * heci_write_message - write a message to heci device.
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
148 * returns 1 if success, 0 - otherwise.
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)
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;
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);
168 dw_to_write = ((write_length + 3) / 4);
170 if (dw_to_write > empty_slots)
171 return 0;
173 write_heci_register(dev, H_CB_WW, *((__u32 *) header));
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;
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);
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;
193 dev->write_hang = 0;
194 return 1;
198 * count_full_read_slots - count read full slots.
200 * @dev: device object for our driver
202 * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise filled slots count
204 __s32 count_full_read_slots(struct iamt_heci_device *dev)
206 char read_ptr, write_ptr;
207 unsigned char buffer_depth, filled_slots;
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);
215 if (filled_slots > buffer_depth) {
216 /* overflow */
217 return -ESLOTS_OVERFLOW;
220 DBG("filled_slots =%08x \n", filled_slots);
221 return (__s32) filled_slots;
225 * heci_read_slots - read a message from heci device.
227 * @dev: device object for our driver
228 * @buffer: message buffer will be write
229 * @buffer_length: message size will be read
231 void heci_read_slots(struct iamt_heci_device *dev,
232 unsigned char *buffer, unsigned long buffer_length)
234 __u32 i = 0;
235 unsigned char temp_buf[sizeof(__u32)];
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);
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);
249 dev->host_hw_state |= H_IG;
250 heci_set_csr_register(dev);
254 * flow_ctrl_creds - check flow_control credentials.
256 * @dev: device object for our driver
257 * @file_ext: private data of the file object
259 * returns 1 if flow_ctrl_creds >0, 0 - otherwise.
261 int flow_ctrl_creds(struct iamt_heci_device *dev,
262 struct heci_file_private *file_ext)
264 __u8 i;
266 if (!dev->num_heci_me_clients)
267 return 0;
269 if (file_ext == NULL)
270 return 0;
272 if (file_ext->flow_ctrl_creds > 0)
273 return 1;
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;
282 return 0;
285 BUG();
286 return 0;
290 * flow_ctrl_reduce - reduce flow_control.
292 * @dev: device object for our driver
293 * @file_ext: private data of the file object
295 void flow_ctrl_reduce(struct iamt_heci_device *dev,
296 struct heci_file_private *file_ext)
298 __u8 i;
300 if (!dev->num_heci_me_clients)
301 return;
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--;
312 return;
315 BUG();
319 * heci_send_flow_control - send flow control to fw.
321 * @dev: device object for our driver
322 * @file_ext: private data of the file object
324 * returns 1 if success, 0 - otherwise.
326 int heci_send_flow_control(struct iamt_heci_device *dev,
327 struct heci_file_private *file_ext)
329 struct heci_msg_hdr *heci_hdr;
330 struct hbm_flow_control *heci_flow_control;
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;
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;
353 return 1;
358 * other_client_is_connecting - check if other
359 * client with the same client id is connected.
361 * @dev: device object for our driver
362 * @file_ext: private data of the file object
364 * returns 1 if other client is connected, 0 - otherwise.
366 int other_client_is_connecting(struct iamt_heci_device *dev,
367 struct heci_file_private *file_ext)
369 struct heci_file_private *file_pos = NULL;
370 struct heci_file_private *file_next = NULL;
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;
379 return 0;
383 * heci_send_wd - send watch dog message to fw.
385 * @dev: device object for our driver
387 * returns 1 if success, 0 - otherwise.
389 int heci_send_wd(struct iamt_heci_device *dev)
391 struct heci_msg_hdr *heci_hdr;
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;
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;
408 if (!heci_write_message(dev, heci_hdr, dev->wd_data, heci_hdr->length))
409 return 0;
411 return 1;
415 * heci_disconnect - send disconnect message to fw.
417 * @dev: device object for our driver
418 * @file_ext: private data of the file object
420 * returns 1 if success, 0 - otherwise.
422 int heci_disconnect(struct iamt_heci_device *dev,
423 struct heci_file_private *file_ext)
425 struct heci_msg_hdr *heci_hdr;
426 struct hbm_client_disconnect_request *heci_cli_disconnect;
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;
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;
443 if (!heci_write_message(dev, heci_hdr,
444 (unsigned char *) heci_cli_disconnect,
445 sizeof(struct hbm_client_disconnect_request)))
446 return 0;
448 return 1;
452 * heci_connect - send connect message to fw.
454 * @dev: device object for our driver
455 * @file_ext: private data of the file object
457 * returns 1 if success, 0 - otherwise.
459 int heci_connect(struct iamt_heci_device *dev,
460 struct heci_file_private *file_ext)
462 struct heci_msg_hdr *heci_hdr;
463 struct hbm_client_connect_request *heci_cli_connect;
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;
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;
479 if (!heci_write_message(dev, heci_hdr,
480 (unsigned char *) heci_cli_connect,
481 sizeof(struct hbm_client_connect_request)))
482 return 0;
484 return 1;