mei: me: d0i3: enable d0i3 interrupts
[linux-2.6/btrfs-unstable.git] / drivers / misc / mei / hw-me.c
blob910af88b3214c1f0f53ff9f826ea8e5718b4b640
1 /*
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
17 #include <linux/pci.h>
19 #include <linux/kthread.h>
20 #include <linux/interrupt.h>
22 #include "mei_dev.h"
23 #include "hbm.h"
25 #include "hw-me.h"
26 #include "hw-me-regs.h"
28 #include "mei-trace.h"
30 /**
31 * mei_me_reg_read - Reads 32bit data from the mei device
33 * @hw: the me hardware structure
34 * @offset: offset from which to read the data
36 * Return: register value (u32)
38 static inline u32 mei_me_reg_read(const struct mei_me_hw *hw,
39 unsigned long offset)
41 return ioread32(hw->mem_addr + offset);
45 /**
46 * mei_me_reg_write - Writes 32bit data to the mei device
48 * @hw: the me hardware structure
49 * @offset: offset from which to write the data
50 * @value: register value to write (u32)
52 static inline void mei_me_reg_write(const struct mei_me_hw *hw,
53 unsigned long offset, u32 value)
55 iowrite32(value, hw->mem_addr + offset);
58 /**
59 * mei_me_mecbrw_read - Reads 32bit data from ME circular buffer
60 * read window register
62 * @dev: the device structure
64 * Return: ME_CB_RW register value (u32)
66 static inline u32 mei_me_mecbrw_read(const struct mei_device *dev)
68 return mei_me_reg_read(to_me_hw(dev), ME_CB_RW);
71 /**
72 * mei_me_hcbww_write - write 32bit data to the host circular buffer
74 * @dev: the device structure
75 * @data: 32bit data to be written to the host circular buffer
77 static inline void mei_me_hcbww_write(struct mei_device *dev, u32 data)
79 mei_me_reg_write(to_me_hw(dev), H_CB_WW, data);
82 /**
83 * mei_me_mecsr_read - Reads 32bit data from the ME CSR
85 * @dev: the device structure
87 * Return: ME_CSR_HA register value (u32)
89 static inline u32 mei_me_mecsr_read(const struct mei_device *dev)
91 u32 reg;
93 reg = mei_me_reg_read(to_me_hw(dev), ME_CSR_HA);
94 trace_mei_reg_read(dev->dev, "ME_CSR_HA", ME_CSR_HA, reg);
96 return reg;
99 /**
100 * mei_hcsr_read - Reads 32bit data from the host CSR
102 * @dev: the device structure
104 * Return: H_CSR register value (u32)
106 static inline u32 mei_hcsr_read(const struct mei_device *dev)
108 u32 reg;
110 reg = mei_me_reg_read(to_me_hw(dev), H_CSR);
111 trace_mei_reg_read(dev->dev, "H_CSR", H_CSR, reg);
113 return reg;
117 * mei_hcsr_write - writes H_CSR register to the mei device
119 * @dev: the device structure
120 * @reg: new register value
122 static inline void mei_hcsr_write(struct mei_device *dev, u32 reg)
124 trace_mei_reg_write(dev->dev, "H_CSR", H_CSR, reg);
125 mei_me_reg_write(to_me_hw(dev), H_CSR, reg);
129 * mei_hcsr_set - writes H_CSR register to the mei device,
130 * and ignores the H_IS bit for it is write-one-to-zero.
132 * @dev: the device structure
133 * @reg: new register value
135 static inline void mei_hcsr_set(struct mei_device *dev, u32 reg)
137 reg &= ~H_CSR_IS_MASK;
138 mei_hcsr_write(dev, reg);
142 * mei_me_fw_status - read fw status register from pci config space
144 * @dev: mei device
145 * @fw_status: fw status register values
147 * Return: 0 on success, error otherwise
149 static int mei_me_fw_status(struct mei_device *dev,
150 struct mei_fw_status *fw_status)
152 struct pci_dev *pdev = to_pci_dev(dev->dev);
153 struct mei_me_hw *hw = to_me_hw(dev);
154 const struct mei_fw_status *fw_src = &hw->cfg->fw_status;
155 int ret;
156 int i;
158 if (!fw_status)
159 return -EINVAL;
161 fw_status->count = fw_src->count;
162 for (i = 0; i < fw_src->count && i < MEI_FW_STATUS_MAX; i++) {
163 ret = pci_read_config_dword(pdev,
164 fw_src->status[i], &fw_status->status[i]);
165 if (ret)
166 return ret;
169 return 0;
173 * mei_me_hw_config - configure hw dependent settings
175 * @dev: mei device
177 static void mei_me_hw_config(struct mei_device *dev)
179 struct pci_dev *pdev = to_pci_dev(dev->dev);
180 struct mei_me_hw *hw = to_me_hw(dev);
181 u32 hcsr, reg;
183 /* Doesn't change in runtime */
184 hcsr = mei_hcsr_read(dev);
185 dev->hbuf_depth = (hcsr & H_CBD) >> 24;
187 hw->pg_state = MEI_PG_OFF;
189 reg = 0;
190 pci_read_config_dword(pdev, PCI_CFG_HFS_1, &reg);
191 hw->d0i3_supported =
192 ((reg & PCI_CFG_HFS_1_D0I3_MSK) == PCI_CFG_HFS_1_D0I3_MSK);
196 * mei_me_pg_state - translate internal pg state
197 * to the mei power gating state
199 * @dev: mei device
201 * Return: MEI_PG_OFF if aliveness is on and MEI_PG_ON otherwise
203 static inline enum mei_pg_state mei_me_pg_state(struct mei_device *dev)
205 struct mei_me_hw *hw = to_me_hw(dev);
207 return hw->pg_state;
211 * mei_me_intr_clear - clear and stop interrupts
213 * @dev: the device structure
215 static void mei_me_intr_clear(struct mei_device *dev)
217 u32 hcsr = mei_hcsr_read(dev);
219 if (hcsr & H_CSR_IS_MASK)
220 mei_hcsr_write(dev, hcsr);
223 * mei_me_intr_enable - enables mei device interrupts
225 * @dev: the device structure
227 static void mei_me_intr_enable(struct mei_device *dev)
229 u32 hcsr = mei_hcsr_read(dev);
231 hcsr |= H_CSR_IE_MASK;
232 mei_hcsr_set(dev, hcsr);
236 * mei_me_intr_disable - disables mei device interrupts
238 * @dev: the device structure
240 static void mei_me_intr_disable(struct mei_device *dev)
242 u32 hcsr = mei_hcsr_read(dev);
244 hcsr &= ~H_CSR_IE_MASK;
245 mei_hcsr_set(dev, hcsr);
249 * mei_me_hw_reset_release - release device from the reset
251 * @dev: the device structure
253 static void mei_me_hw_reset_release(struct mei_device *dev)
255 u32 hcsr = mei_hcsr_read(dev);
257 hcsr |= H_IG;
258 hcsr &= ~H_RST;
259 mei_hcsr_set(dev, hcsr);
261 /* complete this write before we set host ready on another CPU */
262 mmiowb();
265 * mei_me_hw_reset - resets fw via mei csr register.
267 * @dev: the device structure
268 * @intr_enable: if interrupt should be enabled after reset.
270 * Return: always 0
272 static int mei_me_hw_reset(struct mei_device *dev, bool intr_enable)
274 u32 hcsr = mei_hcsr_read(dev);
276 /* H_RST may be found lit before reset is started,
277 * for example if preceding reset flow hasn't completed.
278 * In that case asserting H_RST will be ignored, therefore
279 * we need to clean H_RST bit to start a successful reset sequence.
281 if ((hcsr & H_RST) == H_RST) {
282 dev_warn(dev->dev, "H_RST is set = 0x%08X", hcsr);
283 hcsr &= ~H_RST;
284 mei_hcsr_set(dev, hcsr);
285 hcsr = mei_hcsr_read(dev);
288 hcsr |= H_RST | H_IG | H_CSR_IS_MASK;
290 if (intr_enable)
291 hcsr |= H_CSR_IE_MASK;
292 else
293 hcsr &= ~H_CSR_IE_MASK;
295 dev->recvd_hw_ready = false;
296 mei_hcsr_write(dev, hcsr);
299 * Host reads the H_CSR once to ensure that the
300 * posted write to H_CSR completes.
302 hcsr = mei_hcsr_read(dev);
304 if ((hcsr & H_RST) == 0)
305 dev_warn(dev->dev, "H_RST is not set = 0x%08X", hcsr);
307 if ((hcsr & H_RDY) == H_RDY)
308 dev_warn(dev->dev, "H_RDY is not cleared 0x%08X", hcsr);
310 if (intr_enable == false)
311 mei_me_hw_reset_release(dev);
313 return 0;
317 * mei_me_host_set_ready - enable device
319 * @dev: mei device
321 static void mei_me_host_set_ready(struct mei_device *dev)
323 u32 hcsr = mei_hcsr_read(dev);
325 hcsr |= H_CSR_IE_MASK | H_IG | H_RDY;
326 mei_hcsr_set(dev, hcsr);
330 * mei_me_host_is_ready - check whether the host has turned ready
332 * @dev: mei device
333 * Return: bool
335 static bool mei_me_host_is_ready(struct mei_device *dev)
337 u32 hcsr = mei_hcsr_read(dev);
339 return (hcsr & H_RDY) == H_RDY;
343 * mei_me_hw_is_ready - check whether the me(hw) has turned ready
345 * @dev: mei device
346 * Return: bool
348 static bool mei_me_hw_is_ready(struct mei_device *dev)
350 u32 mecsr = mei_me_mecsr_read(dev);
352 return (mecsr & ME_RDY_HRA) == ME_RDY_HRA;
356 * mei_me_hw_ready_wait - wait until the me(hw) has turned ready
357 * or timeout is reached
359 * @dev: mei device
360 * Return: 0 on success, error otherwise
362 static int mei_me_hw_ready_wait(struct mei_device *dev)
364 mutex_unlock(&dev->device_lock);
365 wait_event_timeout(dev->wait_hw_ready,
366 dev->recvd_hw_ready,
367 mei_secs_to_jiffies(MEI_HW_READY_TIMEOUT));
368 mutex_lock(&dev->device_lock);
369 if (!dev->recvd_hw_ready) {
370 dev_err(dev->dev, "wait hw ready failed\n");
371 return -ETIME;
374 mei_me_hw_reset_release(dev);
375 dev->recvd_hw_ready = false;
376 return 0;
380 * mei_me_hw_start - hw start routine
382 * @dev: mei device
383 * Return: 0 on success, error otherwise
385 static int mei_me_hw_start(struct mei_device *dev)
387 int ret = mei_me_hw_ready_wait(dev);
389 if (ret)
390 return ret;
391 dev_dbg(dev->dev, "hw is ready\n");
393 mei_me_host_set_ready(dev);
394 return ret;
399 * mei_hbuf_filled_slots - gets number of device filled buffer slots
401 * @dev: the device structure
403 * Return: number of filled slots
405 static unsigned char mei_hbuf_filled_slots(struct mei_device *dev)
407 u32 hcsr;
408 char read_ptr, write_ptr;
410 hcsr = mei_hcsr_read(dev);
412 read_ptr = (char) ((hcsr & H_CBRP) >> 8);
413 write_ptr = (char) ((hcsr & H_CBWP) >> 16);
415 return (unsigned char) (write_ptr - read_ptr);
419 * mei_me_hbuf_is_empty - checks if host buffer is empty.
421 * @dev: the device structure
423 * Return: true if empty, false - otherwise.
425 static bool mei_me_hbuf_is_empty(struct mei_device *dev)
427 return mei_hbuf_filled_slots(dev) == 0;
431 * mei_me_hbuf_empty_slots - counts write empty slots.
433 * @dev: the device structure
435 * Return: -EOVERFLOW if overflow, otherwise empty slots count
437 static int mei_me_hbuf_empty_slots(struct mei_device *dev)
439 unsigned char filled_slots, empty_slots;
441 filled_slots = mei_hbuf_filled_slots(dev);
442 empty_slots = dev->hbuf_depth - filled_slots;
444 /* check for overflow */
445 if (filled_slots > dev->hbuf_depth)
446 return -EOVERFLOW;
448 return empty_slots;
452 * mei_me_hbuf_max_len - returns size of hw buffer.
454 * @dev: the device structure
456 * Return: size of hw buffer in bytes
458 static size_t mei_me_hbuf_max_len(const struct mei_device *dev)
460 return dev->hbuf_depth * sizeof(u32) - sizeof(struct mei_msg_hdr);
465 * mei_me_write_message - writes a message to mei device.
467 * @dev: the device structure
468 * @header: mei HECI header of message
469 * @buf: message payload will be written
471 * Return: -EIO if write has failed
473 static int mei_me_write_message(struct mei_device *dev,
474 struct mei_msg_hdr *header,
475 unsigned char *buf)
477 unsigned long rem;
478 unsigned long length = header->length;
479 u32 *reg_buf = (u32 *)buf;
480 u32 hcsr;
481 u32 dw_cnt;
482 int i;
483 int empty_slots;
485 dev_dbg(dev->dev, MEI_HDR_FMT, MEI_HDR_PRM(header));
487 empty_slots = mei_hbuf_empty_slots(dev);
488 dev_dbg(dev->dev, "empty slots = %hu.\n", empty_slots);
490 dw_cnt = mei_data2slots(length);
491 if (empty_slots < 0 || dw_cnt > empty_slots)
492 return -EMSGSIZE;
494 mei_me_hcbww_write(dev, *((u32 *) header));
496 for (i = 0; i < length / 4; i++)
497 mei_me_hcbww_write(dev, reg_buf[i]);
499 rem = length & 0x3;
500 if (rem > 0) {
501 u32 reg = 0;
503 memcpy(&reg, &buf[length - rem], rem);
504 mei_me_hcbww_write(dev, reg);
507 hcsr = mei_hcsr_read(dev) | H_IG;
508 mei_hcsr_set(dev, hcsr);
509 if (!mei_me_hw_is_ready(dev))
510 return -EIO;
512 return 0;
516 * mei_me_count_full_read_slots - counts read full slots.
518 * @dev: the device structure
520 * Return: -EOVERFLOW if overflow, otherwise filled slots count
522 static int mei_me_count_full_read_slots(struct mei_device *dev)
524 u32 me_csr;
525 char read_ptr, write_ptr;
526 unsigned char buffer_depth, filled_slots;
528 me_csr = mei_me_mecsr_read(dev);
529 buffer_depth = (unsigned char)((me_csr & ME_CBD_HRA) >> 24);
530 read_ptr = (char) ((me_csr & ME_CBRP_HRA) >> 8);
531 write_ptr = (char) ((me_csr & ME_CBWP_HRA) >> 16);
532 filled_slots = (unsigned char) (write_ptr - read_ptr);
534 /* check for overflow */
535 if (filled_slots > buffer_depth)
536 return -EOVERFLOW;
538 dev_dbg(dev->dev, "filled_slots =%08x\n", filled_slots);
539 return (int)filled_slots;
543 * mei_me_read_slots - reads a message from mei device.
545 * @dev: the device structure
546 * @buffer: message buffer will be written
547 * @buffer_length: message size will be read
549 * Return: always 0
551 static int mei_me_read_slots(struct mei_device *dev, unsigned char *buffer,
552 unsigned long buffer_length)
554 u32 *reg_buf = (u32 *)buffer;
555 u32 hcsr;
557 for (; buffer_length >= sizeof(u32); buffer_length -= sizeof(u32))
558 *reg_buf++ = mei_me_mecbrw_read(dev);
560 if (buffer_length > 0) {
561 u32 reg = mei_me_mecbrw_read(dev);
563 memcpy(reg_buf, &reg, buffer_length);
566 hcsr = mei_hcsr_read(dev) | H_IG;
567 mei_hcsr_set(dev, hcsr);
568 return 0;
572 * mei_me_pg_set - write pg enter register
574 * @dev: the device structure
576 static void mei_me_pg_set(struct mei_device *dev)
578 struct mei_me_hw *hw = to_me_hw(dev);
579 u32 reg;
581 reg = mei_me_reg_read(hw, H_HPG_CSR);
582 trace_mei_reg_read(dev->dev, "H_HPG_CSR", H_HPG_CSR, reg);
584 reg |= H_HPG_CSR_PGI;
586 trace_mei_reg_write(dev->dev, "H_HPG_CSR", H_HPG_CSR, reg);
587 mei_me_reg_write(hw, H_HPG_CSR, reg);
591 * mei_me_pg_unset - write pg exit register
593 * @dev: the device structure
595 static void mei_me_pg_unset(struct mei_device *dev)
597 struct mei_me_hw *hw = to_me_hw(dev);
598 u32 reg;
600 reg = mei_me_reg_read(hw, H_HPG_CSR);
601 trace_mei_reg_read(dev->dev, "H_HPG_CSR", H_HPG_CSR, reg);
603 WARN(!(reg & H_HPG_CSR_PGI), "PGI is not set\n");
605 reg |= H_HPG_CSR_PGIHEXR;
607 trace_mei_reg_write(dev->dev, "H_HPG_CSR", H_HPG_CSR, reg);
608 mei_me_reg_write(hw, H_HPG_CSR, reg);
612 * mei_me_pg_enter_sync - perform pg entry procedure
614 * @dev: the device structure
616 * Return: 0 on success an error code otherwise
618 int mei_me_pg_enter_sync(struct mei_device *dev)
620 struct mei_me_hw *hw = to_me_hw(dev);
621 unsigned long timeout = mei_secs_to_jiffies(MEI_PGI_TIMEOUT);
622 int ret;
624 dev->pg_event = MEI_PG_EVENT_WAIT;
626 ret = mei_hbm_pg(dev, MEI_PG_ISOLATION_ENTRY_REQ_CMD);
627 if (ret)
628 return ret;
630 mutex_unlock(&dev->device_lock);
631 wait_event_timeout(dev->wait_pg,
632 dev->pg_event == MEI_PG_EVENT_RECEIVED, timeout);
633 mutex_lock(&dev->device_lock);
635 if (dev->pg_event == MEI_PG_EVENT_RECEIVED) {
636 mei_me_pg_set(dev);
637 ret = 0;
638 } else {
639 ret = -ETIME;
642 dev->pg_event = MEI_PG_EVENT_IDLE;
643 hw->pg_state = MEI_PG_ON;
645 return ret;
649 * mei_me_pg_exit_sync - perform pg exit procedure
651 * @dev: the device structure
653 * Return: 0 on success an error code otherwise
655 int mei_me_pg_exit_sync(struct mei_device *dev)
657 struct mei_me_hw *hw = to_me_hw(dev);
658 unsigned long timeout = mei_secs_to_jiffies(MEI_PGI_TIMEOUT);
659 int ret;
661 if (dev->pg_event == MEI_PG_EVENT_RECEIVED)
662 goto reply;
664 dev->pg_event = MEI_PG_EVENT_WAIT;
666 mei_me_pg_unset(dev);
668 mutex_unlock(&dev->device_lock);
669 wait_event_timeout(dev->wait_pg,
670 dev->pg_event == MEI_PG_EVENT_RECEIVED, timeout);
671 mutex_lock(&dev->device_lock);
673 reply:
674 if (dev->pg_event != MEI_PG_EVENT_RECEIVED) {
675 ret = -ETIME;
676 goto out;
679 dev->pg_event = MEI_PG_EVENT_INTR_WAIT;
680 ret = mei_hbm_pg(dev, MEI_PG_ISOLATION_EXIT_RES_CMD);
681 if (ret)
682 return ret;
684 mutex_unlock(&dev->device_lock);
685 wait_event_timeout(dev->wait_pg,
686 dev->pg_event == MEI_PG_EVENT_INTR_RECEIVED, timeout);
687 mutex_lock(&dev->device_lock);
689 if (dev->pg_event == MEI_PG_EVENT_INTR_RECEIVED)
690 ret = 0;
691 else
692 ret = -ETIME;
694 out:
695 dev->pg_event = MEI_PG_EVENT_IDLE;
696 hw->pg_state = MEI_PG_OFF;
698 return ret;
702 * mei_me_pg_in_transition - is device now in pg transition
704 * @dev: the device structure
706 * Return: true if in pg transition, false otherwise
708 static bool mei_me_pg_in_transition(struct mei_device *dev)
710 return dev->pg_event >= MEI_PG_EVENT_WAIT &&
711 dev->pg_event <= MEI_PG_EVENT_INTR_WAIT;
715 * mei_me_pg_is_enabled - detect if PG is supported by HW
717 * @dev: the device structure
719 * Return: true is pg supported, false otherwise
721 static bool mei_me_pg_is_enabled(struct mei_device *dev)
723 u32 reg = mei_me_mecsr_read(dev);
725 if ((reg & ME_PGIC_HRA) == 0)
726 goto notsupported;
728 if (!dev->hbm_f_pg_supported)
729 goto notsupported;
731 return true;
733 notsupported:
734 dev_dbg(dev->dev, "pg: not supported: HGP = %d hbm version %d.%d ?= %d.%d\n",
735 !!(reg & ME_PGIC_HRA),
736 dev->version.major_version,
737 dev->version.minor_version,
738 HBM_MAJOR_VERSION_PGI,
739 HBM_MINOR_VERSION_PGI);
741 return false;
745 * mei_me_pg_intr - perform pg processing in interrupt thread handler
747 * @dev: the device structure
749 static void mei_me_pg_intr(struct mei_device *dev)
751 struct mei_me_hw *hw = to_me_hw(dev);
753 if (dev->pg_event != MEI_PG_EVENT_INTR_WAIT)
754 return;
756 dev->pg_event = MEI_PG_EVENT_INTR_RECEIVED;
757 hw->pg_state = MEI_PG_OFF;
758 if (waitqueue_active(&dev->wait_pg))
759 wake_up(&dev->wait_pg);
763 * mei_me_irq_quick_handler - The ISR of the MEI device
765 * @irq: The irq number
766 * @dev_id: pointer to the device structure
768 * Return: irqreturn_t
770 irqreturn_t mei_me_irq_quick_handler(int irq, void *dev_id)
772 struct mei_device *dev = (struct mei_device *)dev_id;
773 struct mei_me_hw *hw = to_me_hw(dev);
774 u32 hcsr;
776 hcsr = mei_hcsr_read(dev);
777 if (!(hcsr & H_CSR_IS_MASK))
778 return IRQ_NONE;
780 hw->intr_source = hcsr & H_CSR_IS_MASK;
781 dev_dbg(dev->dev, "interrupt source 0x%08X.\n", hw->intr_source);
783 /* clear H_IS and H_D0I3C_IS bits in H_CSR to clear the interrupts */
784 mei_hcsr_write(dev, hcsr);
786 return IRQ_WAKE_THREAD;
790 * mei_me_irq_thread_handler - function called after ISR to handle the interrupt
791 * processing.
793 * @irq: The irq number
794 * @dev_id: pointer to the device structure
796 * Return: irqreturn_t
799 irqreturn_t mei_me_irq_thread_handler(int irq, void *dev_id)
801 struct mei_device *dev = (struct mei_device *) dev_id;
802 struct mei_cl_cb complete_list;
803 s32 slots;
804 int rets = 0;
806 dev_dbg(dev->dev, "function called after ISR to handle the interrupt processing.\n");
807 /* initialize our complete list */
808 mutex_lock(&dev->device_lock);
809 mei_io_list_init(&complete_list);
811 /* check if ME wants a reset */
812 if (!mei_hw_is_ready(dev) && dev->dev_state != MEI_DEV_RESETTING) {
813 dev_warn(dev->dev, "FW not ready: resetting.\n");
814 schedule_work(&dev->reset_work);
815 goto end;
818 mei_me_pg_intr(dev);
820 /* check if we need to start the dev */
821 if (!mei_host_is_ready(dev)) {
822 if (mei_hw_is_ready(dev)) {
823 dev_dbg(dev->dev, "we need to start the dev.\n");
824 dev->recvd_hw_ready = true;
825 wake_up(&dev->wait_hw_ready);
826 } else {
827 dev_dbg(dev->dev, "Spurious Interrupt\n");
829 goto end;
831 /* check slots available for reading */
832 slots = mei_count_full_read_slots(dev);
833 while (slots > 0) {
834 dev_dbg(dev->dev, "slots to read = %08x\n", slots);
835 rets = mei_irq_read_handler(dev, &complete_list, &slots);
836 /* There is a race between ME write and interrupt delivery:
837 * Not all data is always available immediately after the
838 * interrupt, so try to read again on the next interrupt.
840 if (rets == -ENODATA)
841 break;
843 if (rets && dev->dev_state != MEI_DEV_RESETTING) {
844 dev_err(dev->dev, "mei_irq_read_handler ret = %d.\n",
845 rets);
846 schedule_work(&dev->reset_work);
847 goto end;
851 dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
854 * During PG handshake only allowed write is the replay to the
855 * PG exit message, so block calling write function
856 * if the pg event is in PG handshake
858 if (dev->pg_event != MEI_PG_EVENT_WAIT &&
859 dev->pg_event != MEI_PG_EVENT_RECEIVED) {
860 rets = mei_irq_write_handler(dev, &complete_list);
861 dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
864 mei_irq_compl_handler(dev, &complete_list);
866 end:
867 dev_dbg(dev->dev, "interrupt thread end ret = %d\n", rets);
868 mutex_unlock(&dev->device_lock);
869 return IRQ_HANDLED;
872 static const struct mei_hw_ops mei_me_hw_ops = {
874 .fw_status = mei_me_fw_status,
875 .pg_state = mei_me_pg_state,
877 .host_is_ready = mei_me_host_is_ready,
879 .hw_is_ready = mei_me_hw_is_ready,
880 .hw_reset = mei_me_hw_reset,
881 .hw_config = mei_me_hw_config,
882 .hw_start = mei_me_hw_start,
884 .pg_in_transition = mei_me_pg_in_transition,
885 .pg_is_enabled = mei_me_pg_is_enabled,
887 .intr_clear = mei_me_intr_clear,
888 .intr_enable = mei_me_intr_enable,
889 .intr_disable = mei_me_intr_disable,
891 .hbuf_free_slots = mei_me_hbuf_empty_slots,
892 .hbuf_is_ready = mei_me_hbuf_is_empty,
893 .hbuf_max_len = mei_me_hbuf_max_len,
895 .write = mei_me_write_message,
897 .rdbuf_full_slots = mei_me_count_full_read_slots,
898 .read_hdr = mei_me_mecbrw_read,
899 .read = mei_me_read_slots
902 static bool mei_me_fw_type_nm(struct pci_dev *pdev)
904 u32 reg;
906 pci_read_config_dword(pdev, PCI_CFG_HFS_2, &reg);
907 /* make sure that bit 9 (NM) is up and bit 10 (DM) is down */
908 return (reg & 0x600) == 0x200;
911 #define MEI_CFG_FW_NM \
912 .quirk_probe = mei_me_fw_type_nm
914 static bool mei_me_fw_type_sps(struct pci_dev *pdev)
916 u32 reg;
917 /* Read ME FW Status check for SPS Firmware */
918 pci_read_config_dword(pdev, PCI_CFG_HFS_1, &reg);
919 /* if bits [19:16] = 15, running SPS Firmware */
920 return (reg & 0xf0000) == 0xf0000;
923 #define MEI_CFG_FW_SPS \
924 .quirk_probe = mei_me_fw_type_sps
927 #define MEI_CFG_LEGACY_HFS \
928 .fw_status.count = 0
930 #define MEI_CFG_ICH_HFS \
931 .fw_status.count = 1, \
932 .fw_status.status[0] = PCI_CFG_HFS_1
934 #define MEI_CFG_PCH_HFS \
935 .fw_status.count = 2, \
936 .fw_status.status[0] = PCI_CFG_HFS_1, \
937 .fw_status.status[1] = PCI_CFG_HFS_2
939 #define MEI_CFG_PCH8_HFS \
940 .fw_status.count = 6, \
941 .fw_status.status[0] = PCI_CFG_HFS_1, \
942 .fw_status.status[1] = PCI_CFG_HFS_2, \
943 .fw_status.status[2] = PCI_CFG_HFS_3, \
944 .fw_status.status[3] = PCI_CFG_HFS_4, \
945 .fw_status.status[4] = PCI_CFG_HFS_5, \
946 .fw_status.status[5] = PCI_CFG_HFS_6
948 /* ICH Legacy devices */
949 const struct mei_cfg mei_me_legacy_cfg = {
950 MEI_CFG_LEGACY_HFS,
953 /* ICH devices */
954 const struct mei_cfg mei_me_ich_cfg = {
955 MEI_CFG_ICH_HFS,
958 /* PCH devices */
959 const struct mei_cfg mei_me_pch_cfg = {
960 MEI_CFG_PCH_HFS,
964 /* PCH Cougar Point and Patsburg with quirk for Node Manager exclusion */
965 const struct mei_cfg mei_me_pch_cpt_pbg_cfg = {
966 MEI_CFG_PCH_HFS,
967 MEI_CFG_FW_NM,
970 /* PCH8 Lynx Point and newer devices */
971 const struct mei_cfg mei_me_pch8_cfg = {
972 MEI_CFG_PCH8_HFS,
975 /* PCH8 Lynx Point with quirk for SPS Firmware exclusion */
976 const struct mei_cfg mei_me_pch8_sps_cfg = {
977 MEI_CFG_PCH8_HFS,
978 MEI_CFG_FW_SPS,
982 * mei_me_dev_init - allocates and initializes the mei device structure
984 * @pdev: The pci device structure
985 * @cfg: per device generation config
987 * Return: The mei_device_device pointer on success, NULL on failure.
989 struct mei_device *mei_me_dev_init(struct pci_dev *pdev,
990 const struct mei_cfg *cfg)
992 struct mei_device *dev;
993 struct mei_me_hw *hw;
995 dev = kzalloc(sizeof(struct mei_device) +
996 sizeof(struct mei_me_hw), GFP_KERNEL);
997 if (!dev)
998 return NULL;
999 hw = to_me_hw(dev);
1001 mei_device_init(dev, &pdev->dev, &mei_me_hw_ops);
1002 hw->cfg = cfg;
1003 return dev;