thunderbolt.c 34 KB

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