main.c 38 KB

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