thunderbolt.c 36 KB

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