manage.c 9.2 KB

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