manage.c 9.2 KB

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