main.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Networking over Thunderbolt/USB4 cables using USB4NET protocol
  4. * (formerly Apple ThunderboltIP).
  5. *
  6. * Copyright (C) 2017, Intel Corporation
  7. * Authors: Amir Levy <amir.jer.levy@intel.com>
  8. * Michael Jamet <michael.jamet@intel.com>
  9. * Mika Westerberg <mika.westerberg@linux.intel.com>
  10. */
  11. #include <linux/atomic.h>
  12. #include <linux/ethtool.h>
  13. #include <linux/highmem.h>
  14. #include <linux/if_vlan.h>
  15. #include <linux/jhash.h>
  16. #include <linux/module.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/rtnetlink.h>
  19. #include <linux/sizes.h>
  20. #include <linux/thunderbolt.h>
  21. #include <linux/uuid.h>
  22. #include <linux/workqueue.h>
  23. #include <net/ip6_checksum.h>
  24. #include "trace.h"
  25. /* Protocol timeouts in ms */
  26. #define TBNET_LOGIN_DELAY 4500
  27. #define TBNET_LOGIN_TIMEOUT 500
  28. #define TBNET_LOGOUT_TIMEOUT 1000
  29. #define TBNET_RING_SIZE 256
  30. #define TBNET_LOGIN_RETRIES 60
  31. #define TBNET_LOGOUT_RETRIES 10
  32. #define TBNET_E2E BIT(0)
  33. #define TBNET_MATCH_FRAGS_ID BIT(1)
  34. #define TBNET_64K_FRAMES BIT(2)
  35. #define TBNET_MAX_MTU SZ_64K
  36. #define TBNET_FRAME_SIZE SZ_4K
  37. #define TBNET_MAX_PAYLOAD_SIZE \
  38. (TBNET_FRAME_SIZE - sizeof(struct thunderbolt_ip_frame_header))
  39. /* Rx packets need to hold space for skb_shared_info */
  40. #define TBNET_RX_MAX_SIZE \
  41. (TBNET_FRAME_SIZE + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
  42. #define TBNET_RX_PAGE_ORDER get_order(TBNET_RX_MAX_SIZE)
  43. #define TBNET_RX_PAGE_SIZE (PAGE_SIZE << TBNET_RX_PAGE_ORDER)
  44. #define TBNET_L0_PORT_NUM(route) ((route) & GENMASK(5, 0))
  45. /**
  46. * struct thunderbolt_ip_frame_header - Header for each Thunderbolt frame
  47. * @frame_size: size of the data with the frame
  48. * @frame_index: running index on the frames
  49. * @frame_id: ID of the frame to match frames to specific packet
  50. * @frame_count: how many frames assembles a full packet
  51. *
  52. * Each data frame passed to the high-speed DMA ring has this header. If
  53. * the XDomain network directory announces that %TBNET_MATCH_FRAGS_ID is
  54. * supported then @frame_id is filled, otherwise it stays %0.
  55. */
  56. struct thunderbolt_ip_frame_header {
  57. __le32 frame_size;
  58. __le16 frame_index;
  59. __le16 frame_id;
  60. __le32 frame_count;
  61. };
  62. enum thunderbolt_ip_frame_pdf {
  63. TBIP_PDF_FRAME_START = 1,
  64. TBIP_PDF_FRAME_END,
  65. };
  66. enum thunderbolt_ip_type {
  67. TBIP_LOGIN,
  68. TBIP_LOGIN_RESPONSE,
  69. TBIP_LOGOUT,
  70. TBIP_STATUS,
  71. };
  72. struct thunderbolt_ip_header {
  73. u32 route_hi;
  74. u32 route_lo;
  75. u32 length_sn;
  76. uuid_t uuid;
  77. uuid_t initiator_uuid;
  78. uuid_t target_uuid;
  79. u32 type;
  80. u32 command_id;
  81. };
  82. #define TBIP_HDR_LENGTH_MASK GENMASK(5, 0)
  83. #define TBIP_HDR_SN_MASK GENMASK(28, 27)
  84. #define TBIP_HDR_SN_SHIFT 27
  85. struct thunderbolt_ip_login {
  86. struct thunderbolt_ip_header hdr;
  87. u32 proto_version;
  88. u32 transmit_path;
  89. u32 reserved[4];
  90. };
  91. #define TBIP_LOGIN_PROTO_VERSION 1
  92. struct thunderbolt_ip_login_response {
  93. struct thunderbolt_ip_header hdr;
  94. u32 status;
  95. u32 receiver_mac[2];
  96. u32 receiver_mac_len;
  97. u32 reserved[4];
  98. };
  99. struct thunderbolt_ip_logout {
  100. struct thunderbolt_ip_header hdr;
  101. };
  102. struct thunderbolt_ip_status {
  103. struct thunderbolt_ip_header hdr;
  104. u32 status;
  105. };
  106. struct tbnet_stats {
  107. u64 tx_packets;
  108. u64 rx_packets;
  109. u64 tx_bytes;
  110. u64 rx_bytes;
  111. u64 rx_errors;
  112. u64 tx_errors;
  113. u64 rx_length_errors;
  114. u64 rx_over_errors;
  115. u64 rx_crc_errors;
  116. u64 rx_missed_errors;
  117. };
  118. struct tbnet_frame {
  119. struct net_device *dev;
  120. struct page *page;
  121. struct ring_frame frame;
  122. };
  123. struct tbnet_ring {
  124. struct tbnet_frame frames[TBNET_RING_SIZE];
  125. unsigned int cons;
  126. unsigned int prod;
  127. struct tb_ring *ring;
  128. };
  129. /**
  130. * struct tbnet - ThunderboltIP network driver private data
  131. * @svc: XDomain service the driver is bound to
  132. * @xd: XDomain the service belongs to
  133. * @handler: ThunderboltIP configuration protocol handler
  134. * @dev: Networking device
  135. * @napi: NAPI structure for Rx polling
  136. * @stats: Network statistics
  137. * @skb: Network packet that is currently processed on Rx path
  138. * @command_id: ID used for next configuration protocol packet
  139. * @login_sent: ThunderboltIP login message successfully sent
  140. * @login_received: ThunderboltIP login message received from the remote
  141. * host
  142. * @local_transmit_path: HopID we are using to send out packets
  143. * @remote_transmit_path: HopID the other end is using to send packets to us
  144. * @connection_lock: Lock serializing access to @login_sent,
  145. * @login_received and @transmit_path.
  146. * @login_retries: Number of login retries currently done
  147. * @login_work: Worker to send ThunderboltIP login packets
  148. * @connected_work: Worker that finalizes the ThunderboltIP connection
  149. * setup and enables DMA paths for high speed data
  150. * transfers
  151. * @disconnect_work: Worker that handles tearing down the ThunderboltIP
  152. * connection
  153. * @rx_hdr: Copy of the currently processed Rx frame. Used when a
  154. * network packet consists of multiple Thunderbolt frames.
  155. * In host byte order.
  156. * @rx_ring: Software ring holding Rx frames
  157. * @frame_id: Frame ID use for next Tx packet
  158. * (if %TBNET_MATCH_FRAGS_ID is supported in both ends)
  159. * @tx_ring: Software ring holding Tx frames
  160. */
  161. struct tbnet {
  162. const struct tb_service *svc;
  163. struct tb_xdomain *xd;
  164. struct tb_protocol_handler handler;
  165. struct net_device *dev;
  166. struct napi_struct napi;
  167. struct tbnet_stats stats;
  168. struct sk_buff *skb;
  169. atomic_t command_id;
  170. bool login_sent;
  171. bool login_received;
  172. int local_transmit_path;
  173. int remote_transmit_path;
  174. struct mutex connection_lock;
  175. int login_retries;
  176. struct delayed_work login_work;
  177. struct work_struct connected_work;
  178. struct work_struct disconnect_work;
  179. struct thunderbolt_ip_frame_header rx_hdr;
  180. struct tbnet_ring rx_ring;
  181. atomic_t frame_id;
  182. struct tbnet_ring tx_ring;
  183. };
  184. /* Network property directory UUID: c66189ca-1cce-4195-bdb8-49592e5f5a4f */
  185. static const uuid_t tbnet_dir_uuid =
  186. UUID_INIT(0xc66189ca, 0x1cce, 0x4195,
  187. 0xbd, 0xb8, 0x49, 0x59, 0x2e, 0x5f, 0x5a, 0x4f);
  188. /* ThunderboltIP protocol UUID: 798f589e-3616-8a47-97c6-5664a920c8dd */
  189. static const uuid_t tbnet_svc_uuid =
  190. UUID_INIT(0x798f589e, 0x3616, 0x8a47,
  191. 0x97, 0xc6, 0x56, 0x64, 0xa9, 0x20, 0xc8, 0xdd);
  192. static struct tb_property_dir *tbnet_dir;
  193. static bool tbnet_e2e = true;
  194. module_param_named(e2e, tbnet_e2e, bool, 0444);
  195. MODULE_PARM_DESC(e2e, "USB4NET full end-to-end flow control (default: true)");
  196. static void tbnet_fill_header(struct thunderbolt_ip_header *hdr, u64 route,
  197. u8 sequence, const uuid_t *initiator_uuid, const uuid_t *target_uuid,
  198. enum thunderbolt_ip_type type, size_t size, u32 command_id)
  199. {
  200. u32 length_sn;
  201. /* Length does not include route_hi/lo and length_sn fields */
  202. length_sn = (size - 3 * 4) / 4;
  203. length_sn |= (sequence << TBIP_HDR_SN_SHIFT) & TBIP_HDR_SN_MASK;
  204. hdr->route_hi = upper_32_bits(route);
  205. hdr->route_lo = lower_32_bits(route);
  206. hdr->length_sn = length_sn;
  207. uuid_copy(&hdr->uuid, &tbnet_svc_uuid);
  208. uuid_copy(&hdr->initiator_uuid, initiator_uuid);
  209. uuid_copy(&hdr->target_uuid, target_uuid);
  210. hdr->type = type;
  211. hdr->command_id = command_id;
  212. }
  213. static int tbnet_login_response(struct tbnet *net, u64 route, u8 sequence,
  214. u32 command_id)
  215. {
  216. struct thunderbolt_ip_login_response reply;
  217. struct tb_xdomain *xd = net->xd;
  218. memset(&reply, 0, sizeof(reply));
  219. tbnet_fill_header(&reply.hdr, route, sequence, xd->local_uuid,
  220. xd->remote_uuid, TBIP_LOGIN_RESPONSE, sizeof(reply),
  221. command_id);
  222. memcpy(reply.receiver_mac, net->dev->dev_addr, ETH_ALEN);
  223. reply.receiver_mac_len = ETH_ALEN;
  224. return tb_xdomain_response(xd, &reply, sizeof(reply),
  225. TB_CFG_PKG_XDOMAIN_RESP);
  226. }
  227. static int tbnet_login_request(struct tbnet *net, u8 sequence)
  228. {
  229. struct thunderbolt_ip_login_response reply;
  230. struct thunderbolt_ip_login request;
  231. struct tb_xdomain *xd = net->xd;
  232. memset(&request, 0, sizeof(request));
  233. tbnet_fill_header(&request.hdr, xd->route, sequence, xd->local_uuid,
  234. xd->remote_uuid, TBIP_LOGIN, sizeof(request),
  235. atomic_inc_return(&net->command_id));
  236. request.proto_version = TBIP_LOGIN_PROTO_VERSION;
  237. request.transmit_path = net->local_transmit_path;
  238. return tb_xdomain_request(xd, &request, sizeof(request),
  239. TB_CFG_PKG_XDOMAIN_RESP, &reply,
  240. sizeof(reply), TB_CFG_PKG_XDOMAIN_RESP,
  241. TBNET_LOGIN_TIMEOUT);
  242. }
  243. static int tbnet_logout_response(struct tbnet *net, u64 route, u8 sequence,
  244. u32 command_id)
  245. {
  246. struct thunderbolt_ip_status reply;
  247. struct tb_xdomain *xd = net->xd;
  248. memset(&reply, 0, sizeof(reply));
  249. tbnet_fill_header(&reply.hdr, route, sequence, xd->local_uuid,
  250. xd->remote_uuid, TBIP_STATUS, sizeof(reply),
  251. atomic_inc_return(&net->command_id));
  252. return tb_xdomain_response(xd, &reply, sizeof(reply),
  253. TB_CFG_PKG_XDOMAIN_RESP);
  254. }
  255. static int tbnet_logout_request(struct tbnet *net)
  256. {
  257. struct thunderbolt_ip_logout request;
  258. struct thunderbolt_ip_status reply;
  259. struct tb_xdomain *xd = net->xd;
  260. memset(&request, 0, sizeof(request));
  261. tbnet_fill_header(&request.hdr, xd->route, 0, xd->local_uuid,
  262. xd->remote_uuid, TBIP_LOGOUT, sizeof(request),
  263. atomic_inc_return(&net->command_id));
  264. return tb_xdomain_request(xd, &request, sizeof(request),
  265. TB_CFG_PKG_XDOMAIN_RESP, &reply,
  266. sizeof(reply), TB_CFG_PKG_XDOMAIN_RESP,
  267. TBNET_LOGOUT_TIMEOUT);
  268. }
  269. static void start_login(struct tbnet *net)
  270. {
  271. netdev_dbg(net->dev, "login started\n");
  272. mutex_lock(&net->connection_lock);
  273. net->login_sent = false;
  274. net->login_received = false;
  275. mutex_unlock(&net->connection_lock);
  276. queue_delayed_work(system_long_wq, &net->login_work,
  277. msecs_to_jiffies(1000));
  278. }
  279. static void stop_login(struct tbnet *net)
  280. {
  281. cancel_delayed_work_sync(&net->login_work);
  282. cancel_work_sync(&net->connected_work);
  283. netdev_dbg(net->dev, "login stopped\n");
  284. }
  285. static inline unsigned int tbnet_frame_size(const struct tbnet_frame *tf)
  286. {
  287. return tf->frame.size ? : TBNET_FRAME_SIZE;
  288. }
  289. static void tbnet_free_buffers(struct tbnet_ring *ring)
  290. {
  291. unsigned int i;
  292. for (i = 0; i < TBNET_RING_SIZE; i++) {
  293. struct device *dma_dev = tb_ring_dma_device(ring->ring);
  294. struct tbnet_frame *tf = &ring->frames[i];
  295. enum dma_data_direction dir;
  296. unsigned int order;
  297. size_t size;
  298. if (!tf->page)
  299. continue;
  300. if (ring->ring->is_tx) {
  301. dir = DMA_TO_DEVICE;
  302. order = 0;
  303. size = TBNET_FRAME_SIZE;
  304. } else {
  305. dir = DMA_FROM_DEVICE;
  306. order = TBNET_RX_PAGE_ORDER;
  307. size = TBNET_RX_PAGE_SIZE;
  308. }
  309. trace_tbnet_free_frame(i, tf->page, tf->frame.buffer_phy, dir);
  310. if (tf->frame.buffer_phy)
  311. dma_unmap_page(dma_dev, tf->frame.buffer_phy, size,
  312. dir);
  313. __free_pages(tf->page, order);
  314. tf->page = NULL;
  315. }
  316. ring->cons = 0;
  317. ring->prod = 0;
  318. }
  319. static void tbnet_tear_down(struct tbnet *net, bool send_logout)
  320. {
  321. netif_carrier_off(net->dev);
  322. netif_stop_queue(net->dev);
  323. stop_login(net);
  324. mutex_lock(&net->connection_lock);
  325. if (net->login_sent && net->login_received) {
  326. int ret, retries = TBNET_LOGOUT_RETRIES;
  327. while (send_logout && retries-- > 0) {
  328. netdev_dbg(net->dev, "sending logout request %u\n",
  329. retries);
  330. ret = tbnet_logout_request(net);
  331. if (ret != -ETIMEDOUT)
  332. break;
  333. }
  334. tb_ring_stop(net->rx_ring.ring);
  335. tb_ring_stop(net->tx_ring.ring);
  336. tbnet_free_buffers(&net->rx_ring);
  337. tbnet_free_buffers(&net->tx_ring);
  338. ret = tb_xdomain_disable_paths(net->xd,
  339. net->local_transmit_path,
  340. net->tx_ring.ring->hop,
  341. net->remote_transmit_path,
  342. net->rx_ring.ring->hop);
  343. if (ret)
  344. netdev_warn(net->dev, "failed to disable DMA paths\n");
  345. tb_xdomain_release_in_hopid(net->xd, net->remote_transmit_path);
  346. net->remote_transmit_path = 0;
  347. }
  348. net->login_retries = 0;
  349. net->login_sent = false;
  350. net->login_received = false;
  351. netdev_dbg(net->dev, "network traffic stopped\n");
  352. mutex_unlock(&net->connection_lock);
  353. }
  354. static int tbnet_handle_packet(const void *buf, size_t size, void *data)
  355. {
  356. const struct thunderbolt_ip_login *pkg = buf;
  357. struct tbnet *net = data;
  358. u32 command_id;
  359. int ret = 0;
  360. u32 sequence;
  361. u64 route;
  362. /* Make sure the packet is for us */
  363. if (size < sizeof(struct thunderbolt_ip_header))
  364. return 0;
  365. if (!uuid_equal(&pkg->hdr.initiator_uuid, net->xd->remote_uuid))
  366. return 0;
  367. if (!uuid_equal(&pkg->hdr.target_uuid, net->xd->local_uuid))
  368. return 0;
  369. route = ((u64)pkg->hdr.route_hi << 32) | pkg->hdr.route_lo;
  370. route &= ~BIT_ULL(63);
  371. if (route != net->xd->route)
  372. return 0;
  373. sequence = pkg->hdr.length_sn & TBIP_HDR_SN_MASK;
  374. sequence >>= TBIP_HDR_SN_SHIFT;
  375. command_id = pkg->hdr.command_id;
  376. switch (pkg->hdr.type) {
  377. case TBIP_LOGIN:
  378. netdev_dbg(net->dev, "remote login request received\n");
  379. if (!netif_running(net->dev))
  380. break;
  381. ret = tbnet_login_response(net, route, sequence,
  382. pkg->hdr.command_id);
  383. if (!ret) {
  384. netdev_dbg(net->dev, "remote login response sent\n");
  385. mutex_lock(&net->connection_lock);
  386. net->login_received = true;
  387. net->remote_transmit_path = pkg->transmit_path;
  388. /* If we reached the number of max retries or
  389. * previous logout, schedule another round of
  390. * login retries
  391. */
  392. if (net->login_retries >= TBNET_LOGIN_RETRIES ||
  393. !net->login_sent) {
  394. net->login_retries = 0;
  395. queue_delayed_work(system_long_wq,
  396. &net->login_work, 0);
  397. }
  398. mutex_unlock(&net->connection_lock);
  399. queue_work(system_long_wq, &net->connected_work);
  400. }
  401. break;
  402. case TBIP_LOGOUT:
  403. netdev_dbg(net->dev, "remote logout request received\n");
  404. ret = tbnet_logout_response(net, route, sequence, command_id);
  405. if (!ret) {
  406. netdev_dbg(net->dev, "remote logout response sent\n");
  407. queue_work(system_long_wq, &net->disconnect_work);
  408. }
  409. break;
  410. default:
  411. return 0;
  412. }
  413. if (ret)
  414. netdev_warn(net->dev, "failed to send ThunderboltIP response\n");
  415. return 1;
  416. }
  417. static unsigned int tbnet_available_buffers(const struct tbnet_ring *ring)
  418. {
  419. return ring->prod - ring->cons;
  420. }
  421. static int tbnet_alloc_rx_buffers(struct tbnet *net, unsigned int nbuffers)
  422. {
  423. struct tbnet_ring *ring = &net->rx_ring;
  424. int ret;
  425. while (nbuffers--) {
  426. struct device *dma_dev = tb_ring_dma_device(ring->ring);
  427. unsigned int index = ring->prod & (TBNET_RING_SIZE - 1);
  428. struct tbnet_frame *tf = &ring->frames[index];
  429. dma_addr_t dma_addr;
  430. if (tf->page)
  431. break;
  432. /* Allocate page (order > 0) so that it can hold maximum
  433. * ThunderboltIP frame (4kB) and the additional room for
  434. * SKB shared info required by build_skb().
  435. */
  436. tf->page = dev_alloc_pages(TBNET_RX_PAGE_ORDER);
  437. if (!tf->page) {
  438. ret = -ENOMEM;
  439. goto err_free;
  440. }
  441. dma_addr = dma_map_page(dma_dev, tf->page, 0,
  442. TBNET_RX_PAGE_SIZE, DMA_FROM_DEVICE);
  443. if (dma_mapping_error(dma_dev, dma_addr)) {
  444. ret = -ENOMEM;
  445. goto err_free;
  446. }
  447. tf->frame.buffer_phy = dma_addr;
  448. tf->dev = net->dev;
  449. trace_tbnet_alloc_rx_frame(index, tf->page, dma_addr,
  450. DMA_FROM_DEVICE);
  451. tb_ring_rx(ring->ring, &tf->frame);
  452. ring->prod++;
  453. }
  454. return 0;
  455. err_free:
  456. tbnet_free_buffers(ring);
  457. return ret;
  458. }
  459. static struct tbnet_frame *tbnet_get_tx_buffer(struct tbnet *net)
  460. {
  461. struct tbnet_ring *ring = &net->tx_ring;
  462. struct device *dma_dev = tb_ring_dma_device(ring->ring);
  463. struct tbnet_frame *tf;
  464. unsigned int index;
  465. if (!tbnet_available_buffers(ring))
  466. return NULL;
  467. index = ring->cons++ & (TBNET_RING_SIZE - 1);
  468. tf = &ring->frames[index];
  469. tf->frame.size = 0;
  470. dma_sync_single_for_cpu(dma_dev, tf->frame.buffer_phy,
  471. tbnet_frame_size(tf), DMA_TO_DEVICE);
  472. return tf;
  473. }
  474. static void tbnet_tx_callback(struct tb_ring *ring, struct ring_frame *frame,
  475. bool canceled)
  476. {
  477. struct tbnet_frame *tf = container_of(frame, typeof(*tf), frame);
  478. struct tbnet *net = netdev_priv(tf->dev);
  479. /* Return buffer to the ring */
  480. net->tx_ring.prod++;
  481. if (tbnet_available_buffers(&net->tx_ring) >= TBNET_RING_SIZE / 2)
  482. netif_wake_queue(net->dev);
  483. }
  484. static int tbnet_alloc_tx_buffers(struct tbnet *net)
  485. {
  486. struct tbnet_ring *ring = &net->tx_ring;
  487. struct device *dma_dev = tb_ring_dma_device(ring->ring);
  488. unsigned int i;
  489. for (i = 0; i < TBNET_RING_SIZE; i++) {
  490. struct tbnet_frame *tf = &ring->frames[i];
  491. dma_addr_t dma_addr;
  492. tf->page = alloc_page(GFP_KERNEL);
  493. if (!tf->page) {
  494. tbnet_free_buffers(ring);
  495. return -ENOMEM;
  496. }
  497. dma_addr = dma_map_page(dma_dev, tf->page, 0, TBNET_FRAME_SIZE,
  498. DMA_TO_DEVICE);
  499. if (dma_mapping_error(dma_dev, dma_addr)) {
  500. __free_page(tf->page);
  501. tf->page = NULL;
  502. tbnet_free_buffers(ring);
  503. return -ENOMEM;
  504. }
  505. tf->dev = net->dev;
  506. tf->frame.buffer_phy = dma_addr;
  507. tf->frame.callback = tbnet_tx_callback;
  508. tf->frame.sof = TBIP_PDF_FRAME_START;
  509. tf->frame.eof = TBIP_PDF_FRAME_END;
  510. trace_tbnet_alloc_tx_frame(i, tf->page, dma_addr, DMA_TO_DEVICE);
  511. }
  512. ring->cons = 0;
  513. ring->prod = TBNET_RING_SIZE - 1;
  514. return 0;
  515. }
  516. static void tbnet_connected_work(struct work_struct *work)
  517. {
  518. struct tbnet *net = container_of(work, typeof(*net), connected_work);
  519. bool connected;
  520. int ret;
  521. if (netif_carrier_ok(net->dev))
  522. return;
  523. mutex_lock(&net->connection_lock);
  524. connected = net->login_sent && net->login_received;
  525. mutex_unlock(&net->connection_lock);
  526. if (!connected)
  527. return;
  528. netdev_dbg(net->dev, "login successful, enabling paths\n");
  529. ret = tb_xdomain_alloc_in_hopid(net->xd, net->remote_transmit_path);
  530. if (ret != net->remote_transmit_path) {
  531. netdev_err(net->dev, "failed to allocate Rx HopID\n");
  532. return;
  533. }
  534. /* Both logins successful so enable the rings, high-speed DMA
  535. * paths and start the network device queue.
  536. *
  537. * Note we enable the DMA paths last to make sure we have primed
  538. * the Rx ring before any incoming packets are allowed to
  539. * arrive.
  540. */
  541. tb_ring_start(net->tx_ring.ring);
  542. tb_ring_start(net->rx_ring.ring);
  543. ret = tbnet_alloc_rx_buffers(net, TBNET_RING_SIZE);
  544. if (ret)
  545. goto err_stop_rings;
  546. ret = tbnet_alloc_tx_buffers(net);
  547. if (ret)
  548. goto err_free_rx_buffers;
  549. ret = tb_xdomain_enable_paths(net->xd, net->local_transmit_path,
  550. net->tx_ring.ring->hop,
  551. net->remote_transmit_path,
  552. net->rx_ring.ring->hop);
  553. if (ret) {
  554. netdev_err(net->dev, "failed to enable DMA paths\n");
  555. goto err_free_tx_buffers;
  556. }
  557. netif_carrier_on(net->dev);
  558. netif_start_queue(net->dev);
  559. netdev_dbg(net->dev, "network traffic started\n");
  560. return;
  561. err_free_tx_buffers:
  562. tbnet_free_buffers(&net->tx_ring);
  563. err_free_rx_buffers:
  564. tbnet_free_buffers(&net->rx_ring);
  565. err_stop_rings:
  566. tb_ring_stop(net->rx_ring.ring);
  567. tb_ring_stop(net->tx_ring.ring);
  568. tb_xdomain_release_in_hopid(net->xd, net->remote_transmit_path);
  569. }
  570. static void tbnet_login_work(struct work_struct *work)
  571. {
  572. struct tbnet *net = container_of(work, typeof(*net), login_work.work);
  573. unsigned long delay = msecs_to_jiffies(TBNET_LOGIN_DELAY);
  574. int ret;
  575. if (netif_carrier_ok(net->dev))
  576. return;
  577. netdev_dbg(net->dev, "sending login request, retries=%u\n",
  578. net->login_retries);
  579. ret = tbnet_login_request(net, net->login_retries % 4);
  580. if (ret) {
  581. netdev_dbg(net->dev, "sending login request failed, ret=%d\n",
  582. ret);
  583. if (net->login_retries++ < TBNET_LOGIN_RETRIES) {
  584. queue_delayed_work(system_long_wq, &net->login_work,
  585. delay);
  586. } else {
  587. netdev_info(net->dev, "ThunderboltIP login timed out\n");
  588. }
  589. } else {
  590. netdev_dbg(net->dev, "received login reply\n");
  591. net->login_retries = 0;
  592. mutex_lock(&net->connection_lock);
  593. net->login_sent = true;
  594. mutex_unlock(&net->connection_lock);
  595. queue_work(system_long_wq, &net->connected_work);
  596. }
  597. }
  598. static void tbnet_disconnect_work(struct work_struct *work)
  599. {
  600. struct tbnet *net = container_of(work, typeof(*net), disconnect_work);
  601. tbnet_tear_down(net, false);
  602. }
  603. static bool tbnet_check_frame(struct tbnet *net, const struct tbnet_frame *tf,
  604. const struct thunderbolt_ip_frame_header *hdr)
  605. {
  606. u32 frame_id, frame_count, frame_size, frame_index;
  607. unsigned int size;
  608. if (tf->frame.flags & RING_DESC_CRC_ERROR) {
  609. net->stats.rx_crc_errors++;
  610. return false;
  611. } else if (tf->frame.flags & RING_DESC_BUFFER_OVERRUN) {
  612. net->stats.rx_over_errors++;
  613. return false;
  614. }
  615. /* Should be greater than just header i.e. contains data */
  616. size = tbnet_frame_size(tf);
  617. if (size <= sizeof(*hdr)) {
  618. net->stats.rx_length_errors++;
  619. return false;
  620. }
  621. frame_count = le32_to_cpu(hdr->frame_count);
  622. frame_size = le32_to_cpu(hdr->frame_size);
  623. frame_index = le16_to_cpu(hdr->frame_index);
  624. frame_id = le16_to_cpu(hdr->frame_id);
  625. if ((frame_size > size - sizeof(*hdr)) || !frame_size) {
  626. net->stats.rx_length_errors++;
  627. return false;
  628. }
  629. /* In case we're in the middle of packet, validate the frame
  630. * header based on first fragment of the packet.
  631. */
  632. if (net->skb && net->rx_hdr.frame_count) {
  633. /* Check the frame count fits the count field */
  634. if (frame_count != le32_to_cpu(net->rx_hdr.frame_count)) {
  635. net->stats.rx_length_errors++;
  636. return false;
  637. }
  638. /* Check the frame identifiers are incremented correctly,
  639. * and id is matching.
  640. */
  641. if (frame_index != le16_to_cpu(net->rx_hdr.frame_index) + 1 ||
  642. frame_id != le16_to_cpu(net->rx_hdr.frame_id)) {
  643. net->stats.rx_missed_errors++;
  644. return false;
  645. }
  646. if (net->skb->len + frame_size > TBNET_MAX_MTU) {
  647. net->stats.rx_length_errors++;
  648. return false;
  649. }
  650. return true;
  651. }
  652. /* Start of packet, validate the frame header */
  653. if (frame_count == 0 || frame_count > TBNET_RING_SIZE / 4) {
  654. net->stats.rx_length_errors++;
  655. return false;
  656. }
  657. if (frame_index != 0) {
  658. net->stats.rx_missed_errors++;
  659. return false;
  660. }
  661. return true;
  662. }
  663. static int tbnet_poll(struct napi_struct *napi, int budget)
  664. {
  665. struct tbnet *net = container_of(napi, struct tbnet, napi);
  666. unsigned int cleaned_count = tbnet_available_buffers(&net->rx_ring);
  667. struct device *dma_dev = tb_ring_dma_device(net->rx_ring.ring);
  668. unsigned int rx_packets = 0;
  669. while (rx_packets < budget) {
  670. const struct thunderbolt_ip_frame_header *hdr;
  671. unsigned int hdr_size = sizeof(*hdr);
  672. struct sk_buff *skb = NULL;
  673. struct ring_frame *frame;
  674. struct tbnet_frame *tf;
  675. struct page *page;
  676. bool last = true;
  677. u32 frame_size;
  678. /* Return some buffers to hardware, one at a time is too
  679. * slow so allocate MAX_SKB_FRAGS buffers at the same
  680. * time.
  681. */
  682. if (cleaned_count >= MAX_SKB_FRAGS) {
  683. tbnet_alloc_rx_buffers(net, cleaned_count);
  684. cleaned_count = 0;
  685. }
  686. frame = tb_ring_poll(net->rx_ring.ring);
  687. if (!frame)
  688. break;
  689. dma_unmap_page(dma_dev, frame->buffer_phy,
  690. TBNET_RX_PAGE_SIZE, DMA_FROM_DEVICE);
  691. tf = container_of(frame, typeof(*tf), frame);
  692. page = tf->page;
  693. tf->page = NULL;
  694. net->rx_ring.cons++;
  695. cleaned_count++;
  696. hdr = page_address(page);
  697. if (!tbnet_check_frame(net, tf, hdr)) {
  698. trace_tbnet_invalid_rx_ip_frame(hdr->frame_size,
  699. hdr->frame_id, hdr->frame_index, hdr->frame_count);
  700. __free_pages(page, TBNET_RX_PAGE_ORDER);
  701. dev_kfree_skb_any(net->skb);
  702. net->skb = NULL;
  703. continue;
  704. }
  705. trace_tbnet_rx_ip_frame(hdr->frame_size, hdr->frame_id,
  706. hdr->frame_index, hdr->frame_count);
  707. frame_size = le32_to_cpu(hdr->frame_size);
  708. skb = net->skb;
  709. if (!skb) {
  710. skb = build_skb(page_address(page),
  711. TBNET_RX_PAGE_SIZE);
  712. if (!skb) {
  713. __free_pages(page, TBNET_RX_PAGE_ORDER);
  714. net->stats.rx_errors++;
  715. break;
  716. }
  717. skb_reserve(skb, hdr_size);
  718. skb_put(skb, frame_size);
  719. net->skb = skb;
  720. } else {
  721. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
  722. page, hdr_size, frame_size,
  723. TBNET_RX_PAGE_SIZE - hdr_size);
  724. }
  725. net->rx_hdr.frame_size = hdr->frame_size;
  726. net->rx_hdr.frame_count = hdr->frame_count;
  727. net->rx_hdr.frame_index = hdr->frame_index;
  728. net->rx_hdr.frame_id = hdr->frame_id;
  729. last = le16_to_cpu(net->rx_hdr.frame_index) ==
  730. le32_to_cpu(net->rx_hdr.frame_count) - 1;
  731. rx_packets++;
  732. net->stats.rx_bytes += frame_size;
  733. if (last) {
  734. skb->protocol = eth_type_trans(skb, net->dev);
  735. trace_tbnet_rx_skb(skb);
  736. napi_gro_receive(&net->napi, skb);
  737. net->skb = NULL;
  738. }
  739. }
  740. net->stats.rx_packets += rx_packets;
  741. if (cleaned_count)
  742. tbnet_alloc_rx_buffers(net, cleaned_count);
  743. if (rx_packets >= budget)
  744. return budget;
  745. napi_complete_done(napi, rx_packets);
  746. /* Re-enable the ring interrupt */
  747. tb_ring_poll_complete(net->rx_ring.ring);
  748. return rx_packets;
  749. }
  750. static void tbnet_start_poll(void *data)
  751. {
  752. struct tbnet *net = data;
  753. napi_schedule(&net->napi);
  754. }
  755. static int tbnet_open(struct net_device *dev)
  756. {
  757. struct tbnet *net = netdev_priv(dev);
  758. struct tb_xdomain *xd = net->xd;
  759. u16 sof_mask, eof_mask;
  760. struct tb_ring *ring;
  761. unsigned int flags;
  762. int hopid;
  763. netif_carrier_off(dev);
  764. flags = RING_FLAG_FRAME;
  765. /* Only enable full E2E if the other end supports it too */
  766. if (tbnet_e2e && net->svc->prtcstns & TBNET_E2E)
  767. flags |= RING_FLAG_E2E;
  768. ring = tb_ring_alloc_tx(xd->tb->nhi, -1, TBNET_RING_SIZE, flags);
  769. if (!ring) {
  770. netdev_err(dev, "failed to allocate Tx ring\n");
  771. return -ENOMEM;
  772. }
  773. net->tx_ring.ring = ring;
  774. hopid = tb_xdomain_alloc_out_hopid(xd, -1);
  775. if (hopid < 0) {
  776. netdev_err(dev, "failed to allocate Tx HopID\n");
  777. tb_ring_free(net->tx_ring.ring);
  778. net->tx_ring.ring = NULL;
  779. return hopid;
  780. }
  781. net->local_transmit_path = hopid;
  782. sof_mask = BIT(TBIP_PDF_FRAME_START);
  783. eof_mask = BIT(TBIP_PDF_FRAME_END);
  784. ring = tb_ring_alloc_rx(xd->tb->nhi, -1, TBNET_RING_SIZE, flags,
  785. net->tx_ring.ring->hop, sof_mask,
  786. eof_mask, tbnet_start_poll, net);
  787. if (!ring) {
  788. netdev_err(dev, "failed to allocate Rx ring\n");
  789. tb_xdomain_release_out_hopid(xd, hopid);
  790. tb_ring_free(net->tx_ring.ring);
  791. net->tx_ring.ring = NULL;
  792. return -ENOMEM;
  793. }
  794. net->rx_ring.ring = ring;
  795. napi_enable(&net->napi);
  796. start_login(net);
  797. return 0;
  798. }
  799. static int tbnet_stop(struct net_device *dev)
  800. {
  801. struct tbnet *net = netdev_priv(dev);
  802. napi_disable(&net->napi);
  803. cancel_work_sync(&net->disconnect_work);
  804. tbnet_tear_down(net, true);
  805. tb_ring_free(net->rx_ring.ring);
  806. net->rx_ring.ring = NULL;
  807. tb_xdomain_release_out_hopid(net->xd, net->local_transmit_path);
  808. tb_ring_free(net->tx_ring.ring);
  809. net->tx_ring.ring = NULL;
  810. return 0;
  811. }
  812. static bool tbnet_xmit_csum_and_map(struct tbnet *net, struct sk_buff *skb,
  813. struct tbnet_frame **frames, u32 frame_count)
  814. {
  815. struct thunderbolt_ip_frame_header *hdr = page_address(frames[0]->page);
  816. struct device *dma_dev = tb_ring_dma_device(net->tx_ring.ring);
  817. unsigned int i, len, offset = skb_transport_offset(skb);
  818. /* Remove payload length from checksum */
  819. u32 paylen = skb->len - skb_transport_offset(skb);
  820. __wsum wsum = (__force __wsum)htonl(paylen);
  821. __be16 protocol = skb->protocol;
  822. void *data = skb->data;
  823. void *dest = hdr + 1;
  824. __sum16 *tucso;
  825. if (skb->ip_summed != CHECKSUM_PARTIAL) {
  826. /* No need to calculate checksum so we just update the
  827. * total frame count and sync the frames for DMA.
  828. */
  829. for (i = 0; i < frame_count; i++) {
  830. hdr = page_address(frames[i]->page);
  831. hdr->frame_count = cpu_to_le32(frame_count);
  832. trace_tbnet_tx_ip_frame(hdr->frame_size, hdr->frame_id,
  833. hdr->frame_index, hdr->frame_count);
  834. dma_sync_single_for_device(dma_dev,
  835. frames[i]->frame.buffer_phy,
  836. tbnet_frame_size(frames[i]), DMA_TO_DEVICE);
  837. }
  838. return true;
  839. }
  840. if (protocol == htons(ETH_P_8021Q)) {
  841. struct vlan_hdr *vhdr, vh;
  842. vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(vh), &vh);
  843. if (!vhdr)
  844. return false;
  845. protocol = vhdr->h_vlan_encapsulated_proto;
  846. }
  847. /* Data points on the beginning of packet.
  848. * Check is the checksum absolute place in the packet.
  849. * ipcso will update IP checksum.
  850. * tucso will update TCP/UDP checksum.
  851. */
  852. if (protocol == htons(ETH_P_IP)) {
  853. __sum16 *ipcso = dest + ((void *)&(ip_hdr(skb)->check) - data);
  854. *ipcso = 0;
  855. *ipcso = ip_fast_csum(dest + skb_network_offset(skb),
  856. ip_hdr(skb)->ihl);
  857. if (ip_hdr(skb)->protocol == IPPROTO_TCP)
  858. tucso = dest + ((void *)&(tcp_hdr(skb)->check) - data);
  859. else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
  860. tucso = dest + ((void *)&(udp_hdr(skb)->check) - data);
  861. else
  862. return false;
  863. *tucso = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  864. ip_hdr(skb)->daddr, 0,
  865. ip_hdr(skb)->protocol, 0);
  866. } else if (skb_is_gso(skb) && skb_is_gso_v6(skb)) {
  867. tucso = dest + ((void *)&(tcp_hdr(skb)->check) - data);
  868. *tucso = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  869. &ipv6_hdr(skb)->daddr, 0,
  870. IPPROTO_TCP, 0);
  871. } else if (protocol == htons(ETH_P_IPV6)) {
  872. tucso = dest + skb_checksum_start_offset(skb) + skb->csum_offset;
  873. *tucso = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  874. &ipv6_hdr(skb)->daddr, 0,
  875. ipv6_hdr(skb)->nexthdr, 0);
  876. } else {
  877. return false;
  878. }
  879. /* First frame was headers, rest of the frames contain data.
  880. * Calculate checksum over each frame.
  881. */
  882. for (i = 0; i < frame_count; i++) {
  883. hdr = page_address(frames[i]->page);
  884. dest = (void *)(hdr + 1) + offset;
  885. len = le32_to_cpu(hdr->frame_size) - offset;
  886. wsum = csum_partial(dest, len, wsum);
  887. hdr->frame_count = cpu_to_le32(frame_count);
  888. trace_tbnet_tx_ip_frame(hdr->frame_size, hdr->frame_id,
  889. hdr->frame_index, hdr->frame_count);
  890. offset = 0;
  891. }
  892. *tucso = csum_fold(wsum);
  893. /* Checksum is finally calculated and we don't touch the memory
  894. * anymore, so DMA sync the frames now.
  895. */
  896. for (i = 0; i < frame_count; i++) {
  897. dma_sync_single_for_device(dma_dev, frames[i]->frame.buffer_phy,
  898. tbnet_frame_size(frames[i]), DMA_TO_DEVICE);
  899. }
  900. return true;
  901. }
  902. static void *tbnet_kmap_frag(struct sk_buff *skb, unsigned int frag_num,
  903. unsigned int *len)
  904. {
  905. const skb_frag_t *frag = &skb_shinfo(skb)->frags[frag_num];
  906. *len = skb_frag_size(frag);
  907. return kmap_local_page(skb_frag_page(frag)) + skb_frag_off(frag);
  908. }
  909. static netdev_tx_t tbnet_start_xmit(struct sk_buff *skb,
  910. struct net_device *dev)
  911. {
  912. struct tbnet *net = netdev_priv(dev);
  913. struct tbnet_frame *frames[MAX_SKB_FRAGS];
  914. u16 frame_id = atomic_read(&net->frame_id);
  915. struct thunderbolt_ip_frame_header *hdr;
  916. unsigned int len = skb_headlen(skb);
  917. unsigned int data_len = skb->len;
  918. unsigned int nframes, i;
  919. unsigned int frag = 0;
  920. void *src = skb->data;
  921. u32 frame_index = 0;
  922. bool unmap = false;
  923. void *dest;
  924. trace_tbnet_tx_skb(skb);
  925. nframes = DIV_ROUND_UP(data_len, TBNET_MAX_PAYLOAD_SIZE);
  926. if (tbnet_available_buffers(&net->tx_ring) < nframes) {
  927. netif_stop_queue(net->dev);
  928. return NETDEV_TX_BUSY;
  929. }
  930. frames[frame_index] = tbnet_get_tx_buffer(net);
  931. if (!frames[frame_index])
  932. goto err_drop;
  933. hdr = page_address(frames[frame_index]->page);
  934. dest = hdr + 1;
  935. /* If overall packet is bigger than the frame data size */
  936. while (data_len > TBNET_MAX_PAYLOAD_SIZE) {
  937. unsigned int size_left = TBNET_MAX_PAYLOAD_SIZE;
  938. hdr->frame_size = cpu_to_le32(TBNET_MAX_PAYLOAD_SIZE);
  939. hdr->frame_index = cpu_to_le16(frame_index);
  940. hdr->frame_id = cpu_to_le16(frame_id);
  941. do {
  942. if (len > size_left) {
  943. /* Copy data onto Tx buffer data with
  944. * full frame size then break and go to
  945. * next frame
  946. */
  947. memcpy(dest, src, size_left);
  948. len -= size_left;
  949. dest += size_left;
  950. src += size_left;
  951. break;
  952. }
  953. memcpy(dest, src, len);
  954. size_left -= len;
  955. dest += len;
  956. if (unmap) {
  957. kunmap_local(src);
  958. unmap = false;
  959. }
  960. /* Ensure all fragments have been processed */
  961. if (frag < skb_shinfo(skb)->nr_frags) {
  962. /* Map and then unmap quickly */
  963. src = tbnet_kmap_frag(skb, frag++, &len);
  964. unmap = true;
  965. } else if (unlikely(size_left > 0)) {
  966. goto err_drop;
  967. }
  968. } while (size_left > 0);
  969. data_len -= TBNET_MAX_PAYLOAD_SIZE;
  970. frame_index++;
  971. frames[frame_index] = tbnet_get_tx_buffer(net);
  972. if (!frames[frame_index])
  973. goto err_drop;
  974. hdr = page_address(frames[frame_index]->page);
  975. dest = hdr + 1;
  976. }
  977. hdr->frame_size = cpu_to_le32(data_len);
  978. hdr->frame_index = cpu_to_le16(frame_index);
  979. hdr->frame_id = cpu_to_le16(frame_id);
  980. frames[frame_index]->frame.size = data_len + sizeof(*hdr);
  981. /* In case the remaining data_len is smaller than a frame */
  982. while (len < data_len) {
  983. memcpy(dest, src, len);
  984. data_len -= len;
  985. dest += len;
  986. if (unmap) {
  987. kunmap_local(src);
  988. unmap = false;
  989. }
  990. if (frag < skb_shinfo(skb)->nr_frags) {
  991. src = tbnet_kmap_frag(skb, frag++, &len);
  992. unmap = true;
  993. } else if (unlikely(data_len > 0)) {
  994. goto err_drop;
  995. }
  996. }
  997. memcpy(dest, src, data_len);
  998. if (unmap)
  999. kunmap_local(src);
  1000. if (!tbnet_xmit_csum_and_map(net, skb, frames, frame_index + 1))
  1001. goto err_drop;
  1002. for (i = 0; i < frame_index + 1; i++)
  1003. tb_ring_tx(net->tx_ring.ring, &frames[i]->frame);
  1004. if (net->svc->prtcstns & TBNET_MATCH_FRAGS_ID)
  1005. atomic_inc(&net->frame_id);
  1006. net->stats.tx_packets++;
  1007. net->stats.tx_bytes += skb->len;
  1008. trace_tbnet_consume_skb(skb);
  1009. dev_consume_skb_any(skb);
  1010. return NETDEV_TX_OK;
  1011. err_drop:
  1012. /* We can re-use the buffers */
  1013. net->tx_ring.cons -= frame_index;
  1014. dev_kfree_skb_any(skb);
  1015. net->stats.tx_errors++;
  1016. return NETDEV_TX_OK;
  1017. }
  1018. static void tbnet_get_stats64(struct net_device *dev,
  1019. struct rtnl_link_stats64 *stats)
  1020. {
  1021. struct tbnet *net = netdev_priv(dev);
  1022. stats->tx_packets = net->stats.tx_packets;
  1023. stats->rx_packets = net->stats.rx_packets;
  1024. stats->tx_bytes = net->stats.tx_bytes;
  1025. stats->rx_bytes = net->stats.rx_bytes;
  1026. stats->rx_errors = net->stats.rx_errors + net->stats.rx_length_errors +
  1027. net->stats.rx_over_errors + net->stats.rx_crc_errors +
  1028. net->stats.rx_missed_errors;
  1029. stats->tx_errors = net->stats.tx_errors;
  1030. stats->rx_length_errors = net->stats.rx_length_errors;
  1031. stats->rx_over_errors = net->stats.rx_over_errors;
  1032. stats->rx_crc_errors = net->stats.rx_crc_errors;
  1033. stats->rx_missed_errors = net->stats.rx_missed_errors;
  1034. }
  1035. static const struct net_device_ops tbnet_netdev_ops = {
  1036. .ndo_open = tbnet_open,
  1037. .ndo_stop = tbnet_stop,
  1038. .ndo_start_xmit = tbnet_start_xmit,
  1039. .ndo_set_mac_address = eth_mac_addr,
  1040. .ndo_get_stats64 = tbnet_get_stats64,
  1041. };
  1042. static int tbnet_get_link_ksettings(struct net_device *dev,
  1043. struct ethtool_link_ksettings *cmd)
  1044. {
  1045. const struct tbnet *net = netdev_priv(dev);
  1046. const struct tb_xdomain *xd = net->xd;
  1047. int speed;
  1048. ethtool_link_ksettings_zero_link_mode(cmd, supported);
  1049. ethtool_link_ksettings_zero_link_mode(cmd, advertising);
  1050. /* Figure out the current link speed and width */
  1051. switch (xd->link_speed) {
  1052. case 40:
  1053. speed = SPEED_80000;
  1054. break;
  1055. case 20:
  1056. if (xd->link_width == 2)
  1057. speed = SPEED_40000;
  1058. else
  1059. speed = SPEED_20000;
  1060. break;
  1061. case 10:
  1062. if (xd->link_width == 2) {
  1063. speed = SPEED_20000;
  1064. break;
  1065. }
  1066. fallthrough;
  1067. default:
  1068. speed = SPEED_10000;
  1069. break;
  1070. }
  1071. cmd->base.speed = speed;
  1072. cmd->base.duplex = DUPLEX_FULL;
  1073. cmd->base.autoneg = AUTONEG_DISABLE;
  1074. cmd->base.port = PORT_OTHER;
  1075. return 0;
  1076. }
  1077. static const struct ethtool_ops tbnet_ethtool_ops = {
  1078. .get_link_ksettings = tbnet_get_link_ksettings,
  1079. };
  1080. static void tbnet_generate_mac(struct net_device *dev)
  1081. {
  1082. const struct tbnet *net = netdev_priv(dev);
  1083. const struct tb_xdomain *xd = net->xd;
  1084. u8 addr[ETH_ALEN];
  1085. u8 phy_port;
  1086. u32 hash;
  1087. phy_port = tb_phy_port_from_link(TBNET_L0_PORT_NUM(xd->route));
  1088. /* Unicast and locally administered MAC */
  1089. addr[0] = phy_port << 4 | 0x02;
  1090. hash = jhash2((u32 *)xd->local_uuid, 4, 0);
  1091. memcpy(addr + 1, &hash, sizeof(hash));
  1092. hash = jhash2((u32 *)xd->local_uuid, 4, hash);
  1093. addr[5] = hash & 0xff;
  1094. eth_hw_addr_set(dev, addr);
  1095. /* Allow changing it if needed */
  1096. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1097. }
  1098. static int tbnet_probe(struct tb_service *svc, const struct tb_service_id *id)
  1099. {
  1100. struct tb_xdomain *xd = tb_service_parent(svc);
  1101. struct net_device *dev;
  1102. struct tbnet *net;
  1103. int ret;
  1104. dev = alloc_etherdev(sizeof(*net));
  1105. if (!dev)
  1106. return -ENOMEM;
  1107. SET_NETDEV_DEV(dev, &svc->dev);
  1108. net = netdev_priv(dev);
  1109. INIT_DELAYED_WORK(&net->login_work, tbnet_login_work);
  1110. INIT_WORK(&net->connected_work, tbnet_connected_work);
  1111. INIT_WORK(&net->disconnect_work, tbnet_disconnect_work);
  1112. mutex_init(&net->connection_lock);
  1113. atomic_set(&net->command_id, 0);
  1114. atomic_set(&net->frame_id, 0);
  1115. net->svc = svc;
  1116. net->dev = dev;
  1117. net->xd = xd;
  1118. tbnet_generate_mac(dev);
  1119. strcpy(dev->name, "thunderbolt%d");
  1120. dev->netdev_ops = &tbnet_netdev_ops;
  1121. dev->ethtool_ops = &tbnet_ethtool_ops;
  1122. /* ThunderboltIP takes advantage of TSO packets but instead of
  1123. * segmenting them we just split the packet into Thunderbolt
  1124. * frames (maximum payload size of each frame is 4084 bytes) and
  1125. * calculate checksum over the whole packet here.
  1126. *
  1127. * The receiving side does the opposite if the host OS supports
  1128. * LRO, otherwise it needs to split the large packet into MTU
  1129. * sized smaller packets.
  1130. *
  1131. * In order to receive large packets from the networking stack,
  1132. * we need to announce support for most of the offloading
  1133. * features here.
  1134. */
  1135. dev->hw_features = NETIF_F_SG | NETIF_F_ALL_TSO | NETIF_F_GRO |
  1136. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  1137. dev->features = dev->hw_features | NETIF_F_HIGHDMA;
  1138. dev->hard_header_len += sizeof(struct thunderbolt_ip_frame_header);
  1139. netif_napi_add(dev, &net->napi, tbnet_poll);
  1140. /* MTU range: 68 - 65522 */
  1141. dev->min_mtu = ETH_MIN_MTU;
  1142. dev->max_mtu = TBNET_MAX_MTU - ETH_HLEN;
  1143. net->handler.uuid = &tbnet_svc_uuid;
  1144. net->handler.callback = tbnet_handle_packet;
  1145. net->handler.data = net;
  1146. tb_register_protocol_handler(&net->handler);
  1147. tb_service_set_drvdata(svc, net);
  1148. ret = register_netdev(dev);
  1149. if (ret) {
  1150. tb_unregister_protocol_handler(&net->handler);
  1151. free_netdev(dev);
  1152. return ret;
  1153. }
  1154. return 0;
  1155. }
  1156. static void tbnet_remove(struct tb_service *svc)
  1157. {
  1158. struct tbnet *net = tb_service_get_drvdata(svc);
  1159. unregister_netdev(net->dev);
  1160. tb_unregister_protocol_handler(&net->handler);
  1161. free_netdev(net->dev);
  1162. }
  1163. static void tbnet_shutdown(struct tb_service *svc)
  1164. {
  1165. tbnet_tear_down(tb_service_get_drvdata(svc), true);
  1166. }
  1167. static int tbnet_suspend(struct device *dev)
  1168. {
  1169. struct tb_service *svc = tb_to_service(dev);
  1170. struct tbnet *net = tb_service_get_drvdata(svc);
  1171. stop_login(net);
  1172. if (netif_running(net->dev)) {
  1173. netif_device_detach(net->dev);
  1174. tbnet_tear_down(net, true);
  1175. }
  1176. tb_unregister_protocol_handler(&net->handler);
  1177. return 0;
  1178. }
  1179. static int tbnet_resume(struct device *dev)
  1180. {
  1181. struct tb_service *svc = tb_to_service(dev);
  1182. struct tbnet *net = tb_service_get_drvdata(svc);
  1183. tb_register_protocol_handler(&net->handler);
  1184. netif_carrier_off(net->dev);
  1185. if (netif_running(net->dev)) {
  1186. netif_device_attach(net->dev);
  1187. start_login(net);
  1188. }
  1189. return 0;
  1190. }
  1191. static DEFINE_SIMPLE_DEV_PM_OPS(tbnet_pm_ops, tbnet_suspend, tbnet_resume);
  1192. static const struct tb_service_id tbnet_ids[] = {
  1193. { TB_SERVICE("network", 1) },
  1194. { },
  1195. };
  1196. MODULE_DEVICE_TABLE(tbsvc, tbnet_ids);
  1197. static struct tb_service_driver tbnet_driver = {
  1198. .driver = {
  1199. .owner = THIS_MODULE,
  1200. .name = "thunderbolt-net",
  1201. .pm = pm_sleep_ptr(&tbnet_pm_ops),
  1202. },
  1203. .probe = tbnet_probe,
  1204. .remove = tbnet_remove,
  1205. .shutdown = tbnet_shutdown,
  1206. .id_table = tbnet_ids,
  1207. };
  1208. static int __init tbnet_init(void)
  1209. {
  1210. unsigned int flags;
  1211. int ret;
  1212. tbnet_dir = tb_property_create_dir(&tbnet_dir_uuid);
  1213. if (!tbnet_dir)
  1214. return -ENOMEM;
  1215. tb_property_add_immediate(tbnet_dir, "prtcid", 1);
  1216. tb_property_add_immediate(tbnet_dir, "prtcvers", 1);
  1217. tb_property_add_immediate(tbnet_dir, "prtcrevs", 1);
  1218. flags = TBNET_MATCH_FRAGS_ID | TBNET_64K_FRAMES;
  1219. if (tbnet_e2e)
  1220. flags |= TBNET_E2E;
  1221. tb_property_add_immediate(tbnet_dir, "prtcstns", flags);
  1222. ret = tb_register_property_dir("network", tbnet_dir);
  1223. if (ret)
  1224. goto err_free_dir;
  1225. ret = tb_register_service_driver(&tbnet_driver);
  1226. if (ret)
  1227. goto err_unregister;
  1228. return 0;
  1229. err_unregister:
  1230. tb_unregister_property_dir("network", tbnet_dir);
  1231. err_free_dir:
  1232. tb_property_free_dir(tbnet_dir);
  1233. return ret;
  1234. }
  1235. module_init(tbnet_init);
  1236. static void __exit tbnet_exit(void)
  1237. {
  1238. tb_unregister_service_driver(&tbnet_driver);
  1239. tb_unregister_property_dir("network", tbnet_dir);
  1240. tb_property_free_dir(tbnet_dir);
  1241. }
  1242. module_exit(tbnet_exit);
  1243. MODULE_AUTHOR("Amir Levy <amir.jer.levy@intel.com>");
  1244. MODULE_AUTHOR("Michael Jamet <michael.jamet@intel.com>");
  1245. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  1246. MODULE_DESCRIPTION("Thunderbolt/USB4 network driver");
  1247. MODULE_LICENSE("GPL v2");