manage.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 1999 - 2018 Intel Corporation. */
  3. #include "e1000.h"
  4. /**
  5. * e1000_calculate_checksum - Calculate checksum for buffer
  6. * @buffer: pointer to EEPROM
  7. * @length: size of EEPROM to calculate a checksum for
  8. *
  9. * Calculates the checksum for some buffer on a specified length. The
  10. * checksum calculated is returned.
  11. **/
  12. static u8 e1000_calculate_checksum(u8 *buffer, u32 length)
  13. {
  14. u32 i;
  15. u8 sum = 0;
  16. if (!buffer)
  17. return 0;
  18. for (i = 0; i < length; i++)
  19. sum += buffer[i];
  20. return (u8)(0 - sum);
  21. }
  22. /**
  23. * e1000_mng_enable_host_if - Checks host interface is enabled
  24. * @hw: pointer to the HW structure
  25. *
  26. * Returns 0 upon success, else -E1000_ERR_HOST_INTERFACE_COMMAND
  27. *
  28. * This function checks whether the HOST IF is enabled for command operation
  29. * and also checks whether the previous command is completed. It busy waits
  30. * in case of previous command is not completed.
  31. **/
  32. static s32 e1000_mng_enable_host_if(struct e1000_hw *hw)
  33. {
  34. u32 hicr;
  35. u8 i;
  36. if (!hw->mac.arc_subsystem_valid) {
  37. e_dbg("ARC subsystem not valid.\n");
  38. return -E1000_ERR_HOST_INTERFACE_COMMAND;
  39. }
  40. /* Check that the host interface is enabled. */
  41. hicr = er32(HICR);
  42. if (!(hicr & E1000_HICR_EN)) {
  43. e_dbg("E1000_HOST_EN bit disabled.\n");
  44. return -E1000_ERR_HOST_INTERFACE_COMMAND;
  45. }
  46. /* check the previous command is completed */
  47. for (i = 0; i < E1000_MNG_DHCP_COMMAND_TIMEOUT; i++) {
  48. hicr = er32(HICR);
  49. if (!(hicr & E1000_HICR_C))
  50. break;
  51. mdelay(1);
  52. }
  53. if (i == E1000_MNG_DHCP_COMMAND_TIMEOUT) {
  54. e_dbg("Previous command timeout failed.\n");
  55. return -E1000_ERR_HOST_INTERFACE_COMMAND;
  56. }
  57. return 0;
  58. }
  59. /**
  60. * e1000e_check_mng_mode_generic - Generic check management mode
  61. * @hw: pointer to the HW structure
  62. *
  63. * Reads the firmware semaphore register and returns true (>0) if
  64. * manageability is enabled, else false (0).
  65. **/
  66. bool e1000e_check_mng_mode_generic(struct e1000_hw *hw)
  67. {
  68. u32 fwsm = er32(FWSM);
  69. return (fwsm & E1000_FWSM_MODE_MASK) ==
  70. (E1000_MNG_IAMT_MODE << E1000_FWSM_MODE_SHIFT);
  71. }
  72. /**
  73. * e1000e_enable_tx_pkt_filtering - Enable packet filtering on Tx
  74. * @hw: pointer to the HW structure
  75. *
  76. * Enables packet filtering on transmit packets if manageability is enabled
  77. * and host interface is enabled.
  78. **/
  79. bool e1000e_enable_tx_pkt_filtering(struct e1000_hw *hw)
  80. {
  81. struct e1000_host_mng_dhcp_cookie *hdr = &hw->mng_cookie;
  82. u32 *buffer = (u32 *)&hw->mng_cookie;
  83. u32 offset;
  84. s32 ret_val, hdr_csum, csum;
  85. u8 i, len;
  86. hw->mac.tx_pkt_filtering = true;
  87. /* No manageability, no filtering */
  88. if (!hw->mac.ops.check_mng_mode(hw)) {
  89. hw->mac.tx_pkt_filtering = false;
  90. return hw->mac.tx_pkt_filtering;
  91. }
  92. /* If we can't read from the host interface for whatever
  93. * reason, disable filtering.
  94. */
  95. ret_val = e1000_mng_enable_host_if(hw);
  96. if (ret_val) {
  97. hw->mac.tx_pkt_filtering = false;
  98. return hw->mac.tx_pkt_filtering;
  99. }
  100. /* Read in the header. Length and offset are in dwords. */
  101. len = E1000_MNG_DHCP_COOKIE_LENGTH >> 2;
  102. offset = E1000_MNG_DHCP_COOKIE_OFFSET >> 2;
  103. for (i = 0; i < len; i++)
  104. *(buffer + i) = E1000_READ_REG_ARRAY(hw, E1000_HOST_IF,
  105. offset + i);
  106. hdr_csum = hdr->checksum;
  107. hdr->checksum = 0;
  108. csum = e1000_calculate_checksum((u8 *)hdr,
  109. E1000_MNG_DHCP_COOKIE_LENGTH);
  110. /* If either the checksums or signature don't match, then
  111. * the cookie area isn't considered valid, in which case we
  112. * take the safe route of assuming Tx filtering is enabled.
  113. */
  114. if ((hdr_csum != csum) || (hdr->signature != E1000_IAMT_SIGNATURE)) {
  115. hw->mac.tx_pkt_filtering = true;
  116. return hw->mac.tx_pkt_filtering;
  117. }
  118. /* Cookie area is valid, make the final check for filtering. */
  119. if (!(hdr->status & E1000_MNG_DHCP_COOKIE_STATUS_PARSING))
  120. hw->mac.tx_pkt_filtering = false;
  121. return hw->mac.tx_pkt_filtering;
  122. }
  123. /**
  124. * e1000_mng_write_cmd_header - Writes manageability command header
  125. * @hw: pointer to the HW structure
  126. * @hdr: pointer to the host interface command header
  127. *
  128. * Writes the command header after does the checksum calculation.
  129. **/
  130. static s32 e1000_mng_write_cmd_header(struct e1000_hw *hw,
  131. struct e1000_host_mng_command_header *hdr)
  132. {
  133. u16 i, length = sizeof(struct e1000_host_mng_command_header);
  134. /* Write the whole command header structure with new checksum. */
  135. hdr->checksum = e1000_calculate_checksum((u8 *)hdr, length);
  136. length >>= 2;
  137. /* Write the relevant command block into the ram area. */
  138. for (i = 0; i < length; i++) {
  139. E1000_WRITE_REG_ARRAY(hw, E1000_HOST_IF, i, *((u32 *)hdr + i));
  140. e1e_flush();
  141. }
  142. return 0;
  143. }
  144. /**
  145. * e1000_mng_host_if_write - Write to the manageability host interface
  146. * @hw: pointer to the HW structure
  147. * @buffer: pointer to the host interface buffer
  148. * @length: size of the buffer
  149. * @offset: location in the buffer to write to
  150. * @sum: sum of the data (not checksum)
  151. *
  152. * This function writes the buffer content at the offset given on the host if.
  153. * It also does alignment considerations to do the writes in most efficient
  154. * way. Also fills up the sum of the buffer in *buffer parameter.
  155. **/
  156. static s32 e1000_mng_host_if_write(struct e1000_hw *hw, u8 *buffer,
  157. u16 length, u16 offset, u8 *sum)
  158. {
  159. u8 *tmp;
  160. u8 *bufptr = buffer;
  161. u32 data = 0;
  162. u16 remaining, i, j, prev_bytes;
  163. /* sum = only sum of the data and it is not checksum */
  164. if (length == 0 || offset + length > E1000_HI_MAX_MNG_DATA_LENGTH)
  165. return -E1000_ERR_PARAM;
  166. tmp = (u8 *)&data;
  167. prev_bytes = offset & 0x3;
  168. offset >>= 2;
  169. if (prev_bytes) {
  170. data = E1000_READ_REG_ARRAY(hw, E1000_HOST_IF, offset);
  171. for (j = prev_bytes; j < sizeof(u32); j++) {
  172. *(tmp + j) = *bufptr++;
  173. *sum += *(tmp + j);
  174. }
  175. E1000_WRITE_REG_ARRAY(hw, E1000_HOST_IF, offset, data);
  176. length -= j - prev_bytes;
  177. offset++;
  178. }
  179. remaining = length & 0x3;
  180. length -= remaining;
  181. /* Calculate length in DWORDs */
  182. length >>= 2;
  183. /* The device driver writes the relevant command block into the
  184. * ram area.
  185. */
  186. for (i = 0; i < length; i++) {
  187. for (j = 0; j < sizeof(u32); j++) {
  188. *(tmp + j) = *bufptr++;
  189. *sum += *(tmp + j);
  190. }
  191. E1000_WRITE_REG_ARRAY(hw, E1000_HOST_IF, offset + i, data);
  192. }
  193. if (remaining) {
  194. for (j = 0; j < sizeof(u32); j++) {
  195. if (j < remaining)
  196. *(tmp + j) = *bufptr++;
  197. else
  198. *(tmp + j) = 0;
  199. *sum += *(tmp + j);
  200. }
  201. E1000_WRITE_REG_ARRAY(hw, E1000_HOST_IF, offset + i, data);
  202. }
  203. return 0;
  204. }
  205. /**
  206. * e1000e_mng_write_dhcp_info - Writes DHCP info to host interface
  207. * @hw: pointer to the HW structure
  208. * @buffer: pointer to the host interface
  209. * @length: size of the buffer
  210. *
  211. * Writes the DHCP information to the host interface.
  212. **/
  213. s32 e1000e_mng_write_dhcp_info(struct e1000_hw *hw, u8 *buffer, u16 length)
  214. {
  215. struct e1000_host_mng_command_header hdr;
  216. s32 ret_val;
  217. u32 hicr;
  218. hdr.command_id = E1000_MNG_DHCP_TX_PAYLOAD_CMD;
  219. hdr.command_length = length;
  220. hdr.reserved1 = 0;
  221. hdr.reserved2 = 0;
  222. hdr.checksum = 0;
  223. /* Enable the host interface */
  224. ret_val = e1000_mng_enable_host_if(hw);
  225. if (ret_val)
  226. return ret_val;
  227. /* Populate the host interface with the contents of "buffer". */
  228. ret_val = e1000_mng_host_if_write(hw, buffer, length,
  229. sizeof(hdr), &(hdr.checksum));
  230. if (ret_val)
  231. return ret_val;
  232. /* Write the manageability command header */
  233. ret_val = e1000_mng_write_cmd_header(hw, &hdr);
  234. if (ret_val)
  235. return ret_val;
  236. /* Tell the ARC a new command is pending. */
  237. hicr = er32(HICR);
  238. ew32(HICR, hicr | E1000_HICR_C);
  239. return 0;
  240. }
  241. /**
  242. * e1000e_enable_mng_pass_thru - Check if management passthrough is needed
  243. * @hw: pointer to the HW structure
  244. *
  245. * Verifies the hardware needs to leave interface enabled so that frames can
  246. * be directed to and from the management interface.
  247. **/
  248. bool e1000e_enable_mng_pass_thru(struct e1000_hw *hw)
  249. {
  250. u32 manc;
  251. u32 fwsm, factps;
  252. manc = er32(MANC);
  253. if (!(manc & E1000_MANC_RCV_TCO_EN))
  254. return false;
  255. if (hw->mac.has_fwsm) {
  256. fwsm = er32(FWSM);
  257. factps = er32(FACTPS);
  258. if (!(factps & E1000_FACTPS_MNGCG) &&
  259. ((fwsm & E1000_FWSM_MODE_MASK) ==
  260. (e1000_mng_mode_pt << E1000_FWSM_MODE_SHIFT)))
  261. return true;
  262. } else if ((hw->mac.type == e1000_82574) ||
  263. (hw->mac.type == e1000_82583)) {
  264. u16 data;
  265. s32 ret_val;
  266. factps = er32(FACTPS);
  267. ret_val = e1000_read_nvm(hw, NVM_INIT_CONTROL2_REG, 1, &data);
  268. if (ret_val)
  269. return false;
  270. if (!(factps & E1000_FACTPS_MNGCG) &&
  271. ((data & E1000_NVM_INIT_CTRL2_MNGM) ==
  272. (e1000_mng_mode_pt << 13)))
  273. return true;
  274. } else if ((manc & E1000_MANC_SMBUS_EN) &&
  275. !(manc & E1000_MANC_ASF_EN)) {
  276. return true;
  277. }
  278. return false;
  279. }