diagfwd_socket.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  1. /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/device.h>
  15. #include <linux/err.h>
  16. #include <linux/sched.h>
  17. #include <linux/ratelimit.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/msm_ipc.h>
  20. #include <linux/socket.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/delay.h>
  23. #include <linux/diagchar.h>
  24. #include <linux/of.h>
  25. #include <linux/kmemleak.h>
  26. #include <asm/current.h>
  27. #include <net/sock.h>
  28. #include <linux/ipc_router.h>
  29. #include <linux/notifier.h>
  30. #include "diagchar.h"
  31. #include "diagfwd.h"
  32. #include "diagfwd_peripheral.h"
  33. #include "diagfwd_socket.h"
  34. #include "diag_ipc_logging.h"
  35. #include <soc/qcom/subsystem_notif.h>
  36. #include <soc/qcom/subsystem_restart.h>
  37. #define DIAG_SVC_ID 0x1001
  38. #define MODEM_INST_BASE 0
  39. #define LPASS_INST_BASE 64
  40. #define WCNSS_INST_BASE 128
  41. #define SENSORS_INST_BASE 192
  42. #define CDSP_INST_BASE 256
  43. #define WDSP_INST_BASE 320
  44. #define INST_ID_CNTL 0
  45. #define INST_ID_CMD 1
  46. #define INST_ID_DATA 2
  47. #define INST_ID_DCI_CMD 3
  48. #define INST_ID_DCI 4
  49. struct diag_cntl_socket_info *cntl_socket;
  50. static uint64_t bootup_req[NUM_SOCKET_SUBSYSTEMS];
  51. struct diag_socket_info socket_data[NUM_PERIPHERALS] = {
  52. {
  53. .peripheral = PERIPHERAL_MODEM,
  54. .type = TYPE_DATA,
  55. .name = "MODEM_DATA"
  56. },
  57. {
  58. .peripheral = PERIPHERAL_LPASS,
  59. .type = TYPE_DATA,
  60. .name = "LPASS_DATA"
  61. },
  62. {
  63. .peripheral = PERIPHERAL_WCNSS,
  64. .type = TYPE_DATA,
  65. .name = "WCNSS_DATA"
  66. },
  67. {
  68. .peripheral = PERIPHERAL_SENSORS,
  69. .type = TYPE_DATA,
  70. .name = "SENSORS_DATA"
  71. },
  72. {
  73. .peripheral = PERIPHERAL_WDSP,
  74. .type = TYPE_DATA,
  75. .name = "DIAG_DATA"
  76. },
  77. {
  78. .peripheral = PERIPHERAL_CDSP,
  79. .type = TYPE_DATA,
  80. .name = "CDSP_DATA"
  81. }
  82. };
  83. struct diag_socket_info socket_cntl[NUM_PERIPHERALS] = {
  84. {
  85. .peripheral = PERIPHERAL_MODEM,
  86. .type = TYPE_CNTL,
  87. .name = "MODEM_CNTL"
  88. },
  89. {
  90. .peripheral = PERIPHERAL_LPASS,
  91. .type = TYPE_CNTL,
  92. .name = "LPASS_CNTL"
  93. },
  94. {
  95. .peripheral = PERIPHERAL_WCNSS,
  96. .type = TYPE_CNTL,
  97. .name = "WCNSS_CNTL"
  98. },
  99. {
  100. .peripheral = PERIPHERAL_SENSORS,
  101. .type = TYPE_CNTL,
  102. .name = "SENSORS_CNTL"
  103. },
  104. {
  105. .peripheral = PERIPHERAL_WDSP,
  106. .type = TYPE_CNTL,
  107. .name = "DIAG_CTRL"
  108. },
  109. {
  110. .peripheral = PERIPHERAL_CDSP,
  111. .type = TYPE_CNTL,
  112. .name = "CDSP_CNTL"
  113. }
  114. };
  115. struct diag_socket_info socket_dci[NUM_PERIPHERALS] = {
  116. {
  117. .peripheral = PERIPHERAL_MODEM,
  118. .type = TYPE_DCI,
  119. .name = "MODEM_DCI"
  120. },
  121. {
  122. .peripheral = PERIPHERAL_LPASS,
  123. .type = TYPE_DCI,
  124. .name = "LPASS_DCI"
  125. },
  126. {
  127. .peripheral = PERIPHERAL_WCNSS,
  128. .type = TYPE_DCI,
  129. .name = "WCNSS_DCI"
  130. },
  131. {
  132. .peripheral = PERIPHERAL_SENSORS,
  133. .type = TYPE_DCI,
  134. .name = "SENSORS_DCI"
  135. },
  136. {
  137. .peripheral = PERIPHERAL_WDSP,
  138. .type = TYPE_DCI,
  139. .name = "DIAG_DCI_DATA"
  140. },
  141. {
  142. .peripheral = PERIPHERAL_CDSP,
  143. .type = TYPE_DCI,
  144. .name = "CDSP_DCI"
  145. }
  146. };
  147. struct diag_socket_info socket_cmd[NUM_PERIPHERALS] = {
  148. {
  149. .peripheral = PERIPHERAL_MODEM,
  150. .type = TYPE_CMD,
  151. .name = "MODEM_CMD"
  152. },
  153. {
  154. .peripheral = PERIPHERAL_LPASS,
  155. .type = TYPE_CMD,
  156. .name = "LPASS_CMD"
  157. },
  158. {
  159. .peripheral = PERIPHERAL_WCNSS,
  160. .type = TYPE_CMD,
  161. .name = "WCNSS_CMD"
  162. },
  163. {
  164. .peripheral = PERIPHERAL_SENSORS,
  165. .type = TYPE_CMD,
  166. .name = "SENSORS_CMD"
  167. },
  168. {
  169. .peripheral = PERIPHERAL_WDSP,
  170. .type = TYPE_CMD,
  171. .name = "DIAG_CMD"
  172. },
  173. {
  174. .peripheral = PERIPHERAL_CDSP,
  175. .type = TYPE_CMD,
  176. .name = "CDSP_CMD"
  177. }
  178. };
  179. struct diag_socket_info socket_dci_cmd[NUM_PERIPHERALS] = {
  180. {
  181. .peripheral = PERIPHERAL_MODEM,
  182. .type = TYPE_DCI_CMD,
  183. .name = "MODEM_DCI_CMD"
  184. },
  185. {
  186. .peripheral = PERIPHERAL_LPASS,
  187. .type = TYPE_DCI_CMD,
  188. .name = "LPASS_DCI_CMD"
  189. },
  190. {
  191. .peripheral = PERIPHERAL_WCNSS,
  192. .type = TYPE_DCI_CMD,
  193. .name = "WCNSS_DCI_CMD"
  194. },
  195. {
  196. .peripheral = PERIPHERAL_SENSORS,
  197. .type = TYPE_DCI_CMD,
  198. .name = "SENSORS_DCI_CMD"
  199. },
  200. {
  201. .peripheral = PERIPHERAL_WDSP,
  202. .type = TYPE_DCI_CMD,
  203. .name = "DIAG_DCI_CMD"
  204. },
  205. {
  206. .peripheral = PERIPHERAL_CDSP,
  207. .type = TYPE_DCI_CMD,
  208. .name = "CDSP_DCI_CMD"
  209. },
  210. };
  211. static void diag_state_open_socket(void *ctxt);
  212. static void diag_state_close_socket(void *ctxt);
  213. static int diag_socket_write(void *ctxt, unsigned char *buf, int len);
  214. static int diag_socket_read(void *ctxt, unsigned char *buf, int buf_len,
  215. struct diagfwd_buf_t *fwd_buf);
  216. static void diag_socket_queue_read(void *ctxt);
  217. static void socket_init_work_fn(struct work_struct *work);
  218. static int socket_ready_notify(struct notifier_block *nb,
  219. unsigned long action, void *data);
  220. static struct diag_peripheral_ops socket_ops = {
  221. .open = diag_state_open_socket,
  222. .close = diag_state_close_socket,
  223. .write = diag_socket_write,
  224. .read = diag_socket_read,
  225. .queue_read = diag_socket_queue_read
  226. };
  227. static struct notifier_block socket_notify = {
  228. .notifier_call = socket_ready_notify,
  229. };
  230. static void diag_state_open_socket(void *ctxt)
  231. {
  232. struct diag_socket_info *info = NULL;
  233. if (!ctxt)
  234. return;
  235. info = (struct diag_socket_info *)(ctxt);
  236. atomic_set(&info->diag_state, 1);
  237. DIAG_LOG(DIAG_DEBUG_PERIPHERALS,
  238. "%s setting diag state to 1", info->name);
  239. }
  240. static void diag_state_close_socket(void *ctxt)
  241. {
  242. struct diag_socket_info *info = NULL;
  243. if (!ctxt)
  244. return;
  245. info = (struct diag_socket_info *)(ctxt);
  246. atomic_set(&info->diag_state, 0);
  247. DIAG_LOG(DIAG_DEBUG_PERIPHERALS,
  248. "%s setting diag state to 0", info->name);
  249. wake_up_interruptible(&info->read_wait_q);
  250. flush_workqueue(info->wq);
  251. }
  252. static void socket_data_ready(struct sock *sk_ptr)
  253. {
  254. unsigned long flags;
  255. struct diag_socket_info *info = NULL;
  256. if (!sk_ptr) {
  257. pr_err_ratelimited("diag: In %s, invalid sk_ptr", __func__);
  258. return;
  259. }
  260. info = (struct diag_socket_info *)(sk_ptr->sk_user_data);
  261. if (!info) {
  262. pr_err_ratelimited("diag: In %s, invalid info\n", __func__);
  263. return;
  264. }
  265. spin_lock_irqsave(&info->lock, flags);
  266. info->data_ready++;
  267. spin_unlock_irqrestore(&info->lock, flags);
  268. diag_ws_on_notify();
  269. queue_work(info->wq, &(info->read_work));
  270. wake_up_interruptible(&info->read_wait_q);
  271. }
  272. static void cntl_socket_data_ready(struct sock *sk_ptr)
  273. {
  274. if (!sk_ptr || !cntl_socket) {
  275. pr_err_ratelimited("diag: In %s, invalid ptrs. sk_ptr: %pK cntl_socket: %pK\n",
  276. __func__, sk_ptr, cntl_socket);
  277. return;
  278. }
  279. atomic_inc(&cntl_socket->data_ready);
  280. wake_up_interruptible(&cntl_socket->read_wait_q);
  281. queue_work(cntl_socket->wq, &(cntl_socket->read_work));
  282. }
  283. static void socket_flow_cntl(struct sock *sk_ptr)
  284. {
  285. struct diag_socket_info *info = NULL;
  286. if (!sk_ptr)
  287. return;
  288. info = (struct diag_socket_info *)(sk_ptr->sk_user_data);
  289. if (!info) {
  290. pr_err_ratelimited("diag: In %s, invalid info\n", __func__);
  291. return;
  292. }
  293. atomic_inc(&info->flow_cnt);
  294. DIAG_LOG(DIAG_DEBUG_PERIPHERALS, "%s flow controlled\n", info->name);
  295. pr_debug("diag: In %s, channel %s flow controlled\n",
  296. __func__, info->name);
  297. }
  298. static int lookup_server(struct diag_socket_info *info)
  299. {
  300. int ret = 0;
  301. struct server_lookup_args *args = NULL;
  302. struct sockaddr_msm_ipc *srv_addr = NULL;
  303. if (!info)
  304. return -EINVAL;
  305. args = kzalloc((sizeof(struct server_lookup_args) +
  306. sizeof(struct msm_ipc_server_info)), GFP_KERNEL);
  307. if (!args)
  308. return -ENOMEM;
  309. kmemleak_not_leak(args);
  310. args->lookup_mask = 0xFFFFFFFF;
  311. args->port_name.service = info->svc_id;
  312. args->port_name.instance = info->ins_id;
  313. args->num_entries_in_array = 1;
  314. args->num_entries_found = 0;
  315. ret = kernel_sock_ioctl(info->hdl, IPC_ROUTER_IOCTL_LOOKUP_SERVER,
  316. (unsigned long)args);
  317. if (ret < 0) {
  318. pr_err("diag: In %s, cannot find service for %s\n", __func__,
  319. info->name);
  320. kfree(args);
  321. return -EFAULT;
  322. }
  323. srv_addr = &info->remote_addr;
  324. srv_addr->family = AF_MSM_IPC;
  325. srv_addr->address.addrtype = MSM_IPC_ADDR_ID;
  326. srv_addr->address.addr.port_addr.node_id = args->srv_info[0].node_id;
  327. srv_addr->address.addr.port_addr.port_id = args->srv_info[0].port_id;
  328. ret = args->num_entries_found;
  329. kfree(args);
  330. if (ret < 1)
  331. return -EIO;
  332. DIAG_LOG(DIAG_DEBUG_PERIPHERALS, "%s found server node: %d port: %d",
  333. info->name, srv_addr->address.addr.port_addr.node_id,
  334. srv_addr->address.addr.port_addr.port_id);
  335. return 0;
  336. }
  337. static void __socket_open_channel(struct diag_socket_info *info)
  338. {
  339. if (!info)
  340. return;
  341. if (!info->inited) {
  342. pr_debug("diag: In %s, socket %s is not initialized\n",
  343. __func__, info->name);
  344. return;
  345. }
  346. if (atomic_read(&info->opened)) {
  347. pr_debug("diag: In %s, socket %s already opened\n",
  348. __func__, info->name);
  349. return;
  350. }
  351. atomic_set(&info->opened, 1);
  352. diagfwd_channel_open(info->fwd_ctxt);
  353. }
  354. static void socket_open_client(struct diag_socket_info *info)
  355. {
  356. int ret = 0;
  357. if (!info || info->port_type != PORT_TYPE_CLIENT)
  358. return;
  359. ret = sock_create(AF_MSM_IPC, SOCK_DGRAM, 0, &info->hdl);
  360. if (ret < 0 || !info->hdl) {
  361. pr_err("diag: In %s, socket not initialized for %s\n", __func__,
  362. info->name);
  363. return;
  364. }
  365. write_lock_bh(&info->hdl->sk->sk_callback_lock);
  366. info->hdl->sk->sk_user_data = (void *)(info);
  367. info->hdl->sk->sk_data_ready = socket_data_ready;
  368. info->hdl->sk->sk_write_space = socket_flow_cntl;
  369. write_unlock_bh(&info->hdl->sk->sk_callback_lock);
  370. ret = lookup_server(info);
  371. if (ret) {
  372. pr_err("diag: In %s, failed to lookup server, ret: %d\n",
  373. __func__, ret);
  374. return;
  375. }
  376. __socket_open_channel(info);
  377. DIAG_LOG(DIAG_DEBUG_PERIPHERALS, "%s opened client\n", info->name);
  378. }
  379. static void socket_open_server(struct diag_socket_info *info)
  380. {
  381. int ret = 0;
  382. struct sockaddr_msm_ipc srv_addr = { 0 };
  383. if (!info)
  384. return;
  385. ret = sock_create(AF_MSM_IPC, SOCK_DGRAM, 0, &info->hdl);
  386. if (ret < 0 || !info->hdl) {
  387. pr_err("diag: In %s, socket not initialized for %s\n", __func__,
  388. info->name);
  389. return;
  390. }
  391. write_lock_bh(&info->hdl->sk->sk_callback_lock);
  392. info->hdl->sk->sk_user_data = (void *)(info);
  393. info->hdl->sk->sk_data_ready = socket_data_ready;
  394. info->hdl->sk->sk_write_space = socket_flow_cntl;
  395. write_unlock_bh(&info->hdl->sk->sk_callback_lock);
  396. srv_addr.family = AF_MSM_IPC;
  397. srv_addr.address.addrtype = MSM_IPC_ADDR_NAME;
  398. srv_addr.address.addr.port_name.service = info->svc_id;
  399. srv_addr.address.addr.port_name.instance = info->ins_id;
  400. ret = kernel_bind(info->hdl, (struct sockaddr *)&srv_addr,
  401. sizeof(srv_addr));
  402. if (ret) {
  403. pr_err("diag: In %s, failed to bind, ch: %s, svc_id: %d ins_id: %d, err: %d\n",
  404. __func__, info->name, info->svc_id, info->ins_id, ret);
  405. return;
  406. }
  407. DIAG_LOG(DIAG_DEBUG_PERIPHERALS, "%s opened server svc: %d ins: %d",
  408. info->name, info->svc_id, info->ins_id);
  409. }
  410. static void socket_init_work_fn(struct work_struct *work)
  411. {
  412. struct diag_socket_info *info = container_of(work,
  413. struct diag_socket_info,
  414. init_work);
  415. if (!info)
  416. return;
  417. if (!info->inited) {
  418. pr_debug("diag: In %s, socket %s is not initialized\n",
  419. __func__, info->name);
  420. return;
  421. }
  422. switch (info->port_type) {
  423. case PORT_TYPE_SERVER:
  424. socket_open_server(info);
  425. break;
  426. case PORT_TYPE_CLIENT:
  427. socket_open_client(info);
  428. break;
  429. default:
  430. pr_err("diag: In %s, unknown type %d\n", __func__,
  431. info->port_type);
  432. break;
  433. }
  434. }
  435. static void __socket_close_channel(struct diag_socket_info *info)
  436. {
  437. if (!info || !info->hdl)
  438. return;
  439. if (!atomic_read(&info->opened))
  440. return;
  441. if (bootup_req[info->peripheral] == PEPIPHERAL_SSR_UP) {
  442. DIAG_LOG(DIAG_DEBUG_PERIPHERALS,
  443. "diag: %s is up, stopping cleanup: bootup_req = %d\n",
  444. info->name, (int)bootup_req[info->peripheral]);
  445. return;
  446. }
  447. memset(&info->remote_addr, 0, sizeof(struct sockaddr_msm_ipc));
  448. diagfwd_channel_close(info->fwd_ctxt);
  449. atomic_set(&info->opened, 0);
  450. /* Don't close the server. Server should always remain open */
  451. if (info->port_type != PORT_TYPE_SERVER) {
  452. write_lock_bh(&info->hdl->sk->sk_callback_lock);
  453. info->hdl->sk->sk_user_data = NULL;
  454. info->hdl->sk->sk_data_ready = NULL;
  455. write_unlock_bh(&info->hdl->sk->sk_callback_lock);
  456. mutex_lock(&info->socket_info_mutex);
  457. sock_release(info->hdl);
  458. info->hdl = NULL;
  459. mutex_unlock(&info->socket_info_mutex);
  460. wake_up_interruptible(&info->read_wait_q);
  461. }
  462. DIAG_LOG(DIAG_DEBUG_PERIPHERALS, "%s exiting\n", info->name);
  463. }
  464. static void socket_close_channel(struct diag_socket_info *info)
  465. {
  466. if (!info)
  467. return;
  468. __socket_close_channel(info);
  469. DIAG_LOG(DIAG_DEBUG_PERIPHERALS, "%s exiting\n", info->name);
  470. }
  471. static int cntl_socket_process_msg_server(uint32_t cmd, uint32_t svc_id,
  472. uint32_t ins_id)
  473. {
  474. uint8_t peripheral;
  475. uint8_t found = 0;
  476. struct diag_socket_info *info = NULL;
  477. for (peripheral = 0; peripheral < NUM_PERIPHERALS; peripheral++) {
  478. info = &socket_cmd[peripheral];
  479. if ((svc_id == info->svc_id) &&
  480. (ins_id == info->ins_id)) {
  481. found = 1;
  482. break;
  483. }
  484. info = &socket_dci_cmd[peripheral];
  485. if ((svc_id == info->svc_id) &&
  486. (ins_id == info->ins_id)) {
  487. found = 1;
  488. break;
  489. }
  490. }
  491. if (!found)
  492. return -EIO;
  493. switch (cmd) {
  494. case CNTL_CMD_NEW_SERVER:
  495. DIAG_LOG(DIAG_DEBUG_PERIPHERALS, "%s received new server\n",
  496. info->name);
  497. diagfwd_register(TRANSPORT_SOCKET, info->peripheral,
  498. info->type, (void *)info, &socket_ops,
  499. &info->fwd_ctxt);
  500. queue_work(info->wq, &(info->init_work));
  501. break;
  502. case CNTL_CMD_REMOVE_SERVER:
  503. DIAG_LOG(DIAG_DEBUG_PERIPHERALS, "%s received remove server\n",
  504. info->name);
  505. socket_close_channel(info);
  506. break;
  507. default:
  508. return -EINVAL;
  509. }
  510. return 0;
  511. }
  512. static int cntl_socket_process_msg_client(uint32_t cmd, uint32_t node_id,
  513. uint32_t port_id)
  514. {
  515. uint8_t peripheral;
  516. uint8_t found = 0;
  517. struct diag_socket_info *info = NULL;
  518. struct msm_ipc_port_addr remote_port = {0};
  519. for (peripheral = 0; peripheral < NUM_PERIPHERALS; peripheral++) {
  520. info = &socket_data[peripheral];
  521. remote_port = info->remote_addr.address.addr.port_addr;
  522. if ((remote_port.node_id == node_id) &&
  523. (remote_port.port_id == port_id)) {
  524. found = 1;
  525. break;
  526. }
  527. info = &socket_cntl[peripheral];
  528. remote_port = info->remote_addr.address.addr.port_addr;
  529. if ((remote_port.node_id == node_id) &&
  530. (remote_port.port_id == port_id)) {
  531. found = 1;
  532. break;
  533. }
  534. info = &socket_dci[peripheral];
  535. remote_port = info->remote_addr.address.addr.port_addr;
  536. if ((remote_port.node_id == node_id) &&
  537. (remote_port.port_id == port_id)) {
  538. found = 1;
  539. break;
  540. }
  541. }
  542. if (!found)
  543. return -EIO;
  544. switch (cmd) {
  545. case CNTL_CMD_REMOVE_CLIENT:
  546. DIAG_LOG(DIAG_DEBUG_PERIPHERALS, "%s received remove client\n",
  547. info->name);
  548. mutex_lock(&driver->diag_notifier_mutex);
  549. socket_close_channel(info);
  550. mutex_unlock(&driver->diag_notifier_mutex);
  551. break;
  552. default:
  553. return -EINVAL;
  554. }
  555. return 0;
  556. }
  557. static int restart_notifier_cb(struct notifier_block *this,
  558. unsigned long code,
  559. void *data);
  560. struct restart_notifier_block {
  561. unsigned int processor;
  562. char *name;
  563. struct notifier_block nb;
  564. };
  565. static struct restart_notifier_block restart_notifiers[] = {
  566. {SOCKET_MODEM, "modem", .nb.notifier_call = restart_notifier_cb},
  567. {SOCKET_ADSP, "adsp", .nb.notifier_call = restart_notifier_cb},
  568. {SOCKET_WCNSS, "wcnss", .nb.notifier_call = restart_notifier_cb},
  569. {SOCKET_SLPI, "slpi", .nb.notifier_call = restart_notifier_cb},
  570. {SOCKET_CDSP, "cdsp", .nb.notifier_call = restart_notifier_cb},
  571. };
  572. static void cntl_socket_read_work_fn(struct work_struct *work)
  573. {
  574. union cntl_port_msg msg;
  575. int ret = 0;
  576. struct kvec iov = { 0 };
  577. struct msghdr read_msg = { 0 };
  578. if (!cntl_socket)
  579. return;
  580. ret = wait_event_interruptible(cntl_socket->read_wait_q,
  581. (atomic_read(&cntl_socket->data_ready) > 0));
  582. if (ret)
  583. return;
  584. do {
  585. iov.iov_base = &msg;
  586. iov.iov_len = sizeof(msg);
  587. read_msg.msg_name = NULL;
  588. read_msg.msg_namelen = 0;
  589. ret = kernel_recvmsg(cntl_socket->hdl, &read_msg, &iov, 1,
  590. sizeof(msg), MSG_DONTWAIT);
  591. if (ret < 0) {
  592. pr_debug("diag: In %s, Error recving data %d\n",
  593. __func__, ret);
  594. break;
  595. }
  596. atomic_dec(&cntl_socket->data_ready);
  597. switch (msg.srv.cmd) {
  598. case CNTL_CMD_NEW_SERVER:
  599. case CNTL_CMD_REMOVE_SERVER:
  600. cntl_socket_process_msg_server(msg.srv.cmd,
  601. msg.srv.service,
  602. msg.srv.instance);
  603. break;
  604. case CNTL_CMD_REMOVE_CLIENT:
  605. cntl_socket_process_msg_client(msg.cli.cmd,
  606. msg.cli.node_id,
  607. msg.cli.port_id);
  608. break;
  609. }
  610. } while (atomic_read(&cntl_socket->data_ready) > 0);
  611. }
  612. static void socket_read_work_fn(struct work_struct *work)
  613. {
  614. struct diag_socket_info *info = container_of(work,
  615. struct diag_socket_info,
  616. read_work);
  617. if (!info)
  618. return;
  619. if (!atomic_read(&info->opened) && info->port_type == PORT_TYPE_SERVER)
  620. diagfwd_buffers_init(info->fwd_ctxt);
  621. diagfwd_channel_read(info->fwd_ctxt);
  622. }
  623. static void diag_socket_queue_read(void *ctxt)
  624. {
  625. struct diag_socket_info *info = NULL;
  626. if (!ctxt)
  627. return;
  628. info = (struct diag_socket_info *)ctxt;
  629. if (info->hdl && info->wq)
  630. queue_work(info->wq, &(info->read_work));
  631. }
  632. void diag_socket_invalidate(void *ctxt, struct diagfwd_info *fwd_ctxt)
  633. {
  634. struct diag_socket_info *info = NULL;
  635. if (!ctxt || !fwd_ctxt)
  636. return;
  637. info = (struct diag_socket_info *)ctxt;
  638. info->fwd_ctxt = fwd_ctxt;
  639. }
  640. int diag_socket_check_state(void *ctxt)
  641. {
  642. struct diag_socket_info *info = NULL;
  643. if (!ctxt)
  644. return 0;
  645. info = (struct diag_socket_info *)ctxt;
  646. return (int)(atomic_read(&info->diag_state));
  647. }
  648. static void __diag_socket_init(struct diag_socket_info *info)
  649. {
  650. uint16_t ins_base = 0;
  651. uint16_t ins_offset = 0;
  652. char wq_name[DIAG_SOCKET_NAME_SZ + 10];
  653. if (!info)
  654. return;
  655. init_waitqueue_head(&info->wait_q);
  656. info->inited = 0;
  657. atomic_set(&info->opened, 0);
  658. atomic_set(&info->diag_state, 0);
  659. info->pkt_len = 0;
  660. info->pkt_read = 0;
  661. info->hdl = NULL;
  662. info->fwd_ctxt = NULL;
  663. info->data_ready = 0;
  664. atomic_set(&info->flow_cnt, 0);
  665. spin_lock_init(&info->lock);
  666. strlcpy(wq_name, "DIAG_SOCKET_", 10);
  667. strlcat(wq_name, info->name, sizeof(info->name));
  668. init_waitqueue_head(&info->read_wait_q);
  669. info->wq = create_singlethread_workqueue(wq_name);
  670. if (!info->wq) {
  671. pr_err("diag: In %s, unable to create workqueue for socket channel %s\n",
  672. __func__, info->name);
  673. return;
  674. }
  675. INIT_WORK(&(info->init_work), socket_init_work_fn);
  676. INIT_WORK(&(info->read_work), socket_read_work_fn);
  677. switch (info->peripheral) {
  678. case PERIPHERAL_MODEM:
  679. ins_base = MODEM_INST_BASE;
  680. break;
  681. case PERIPHERAL_LPASS:
  682. ins_base = LPASS_INST_BASE;
  683. break;
  684. case PERIPHERAL_WCNSS:
  685. ins_base = WCNSS_INST_BASE;
  686. break;
  687. case PERIPHERAL_SENSORS:
  688. ins_base = SENSORS_INST_BASE;
  689. break;
  690. case PERIPHERAL_WDSP:
  691. ins_base = WDSP_INST_BASE;
  692. break;
  693. case PERIPHERAL_CDSP:
  694. ins_base = CDSP_INST_BASE;
  695. break;
  696. }
  697. switch (info->type) {
  698. case TYPE_DATA:
  699. ins_offset = INST_ID_DATA;
  700. info->port_type = PORT_TYPE_SERVER;
  701. break;
  702. case TYPE_CNTL:
  703. ins_offset = INST_ID_CNTL;
  704. info->port_type = PORT_TYPE_SERVER;
  705. break;
  706. case TYPE_DCI:
  707. ins_offset = INST_ID_DCI;
  708. info->port_type = PORT_TYPE_SERVER;
  709. break;
  710. case TYPE_CMD:
  711. ins_offset = INST_ID_CMD;
  712. info->port_type = PORT_TYPE_CLIENT;
  713. break;
  714. case TYPE_DCI_CMD:
  715. ins_offset = INST_ID_DCI_CMD;
  716. info->port_type = PORT_TYPE_CLIENT;
  717. break;
  718. }
  719. if (info->port_type == PORT_TYPE_CLIENT)
  720. mutex_init(&info->socket_info_mutex);
  721. info->svc_id = DIAG_SVC_ID;
  722. info->ins_id = ins_base + ins_offset;
  723. info->inited = 1;
  724. }
  725. static void cntl_socket_init_work_fn(struct work_struct *work)
  726. {
  727. int ret = 0;
  728. if (!cntl_socket)
  729. return;
  730. ret = sock_create(AF_MSM_IPC, SOCK_DGRAM, 0, &cntl_socket->hdl);
  731. if (ret < 0 || !cntl_socket->hdl) {
  732. pr_err("diag: In %s, cntl socket is not initialized, ret: %d\n",
  733. __func__, ret);
  734. return;
  735. }
  736. write_lock_bh(&cntl_socket->hdl->sk->sk_callback_lock);
  737. cntl_socket->hdl->sk->sk_user_data = (void *)cntl_socket;
  738. cntl_socket->hdl->sk->sk_data_ready = cntl_socket_data_ready;
  739. write_unlock_bh(&cntl_socket->hdl->sk->sk_callback_lock);
  740. ret = kernel_sock_ioctl(cntl_socket->hdl,
  741. IPC_ROUTER_IOCTL_BIND_CONTROL_PORT, 0);
  742. if (ret < 0) {
  743. pr_err("diag: In %s Could not bind as control port, ret: %d\n",
  744. __func__, ret);
  745. }
  746. DIAG_LOG(DIAG_DEBUG_PERIPHERALS, "Initialized control sockets");
  747. }
  748. static int __diag_cntl_socket_init(void)
  749. {
  750. cntl_socket = kzalloc(sizeof(struct diag_cntl_socket_info), GFP_KERNEL);
  751. if (!cntl_socket)
  752. return -ENOMEM;
  753. cntl_socket->svc_id = DIAG_SVC_ID;
  754. cntl_socket->ins_id = 1;
  755. atomic_set(&cntl_socket->data_ready, 0);
  756. init_waitqueue_head(&cntl_socket->read_wait_q);
  757. cntl_socket->wq = create_singlethread_workqueue("DIAG_CNTL_SOCKET");
  758. INIT_WORK(&(cntl_socket->read_work), cntl_socket_read_work_fn);
  759. INIT_WORK(&(cntl_socket->init_work), cntl_socket_init_work_fn);
  760. return 0;
  761. }
  762. int diag_socket_init(void)
  763. {
  764. int err = 0;
  765. int i;
  766. int peripheral = 0;
  767. void *handle;
  768. struct diag_socket_info *info = NULL;
  769. struct restart_notifier_block *nb;
  770. for (peripheral = 0; peripheral < NUM_PERIPHERALS; peripheral++) {
  771. info = &socket_cntl[peripheral];
  772. __diag_socket_init(&socket_cntl[peripheral]);
  773. diagfwd_cntl_register(TRANSPORT_SOCKET, peripheral,
  774. (void *)info, &socket_ops, &(info->fwd_ctxt));
  775. __diag_socket_init(&socket_data[peripheral]);
  776. __diag_socket_init(&socket_cmd[peripheral]);
  777. __diag_socket_init(&socket_dci[peripheral]);
  778. __diag_socket_init(&socket_dci_cmd[peripheral]);
  779. }
  780. err = __diag_cntl_socket_init();
  781. if (err) {
  782. pr_err("diag: Unable to open control sockets, err: %d\n", err);
  783. goto fail;
  784. }
  785. for (i = 0; i < ARRAY_SIZE(restart_notifiers); i++) {
  786. nb = &restart_notifiers[i];
  787. if (nb) {
  788. handle = subsys_notif_register_notifier(nb->name,
  789. &nb->nb);
  790. DIAG_LOG(DIAG_DEBUG_PERIPHERALS,
  791. "%s: registering notifier for '%s', handle=%p\n",
  792. __func__, nb->name, handle);
  793. }
  794. }
  795. register_ipcrtr_af_init_notifier(&socket_notify);
  796. fail:
  797. return err;
  798. }
  799. static int socket_ready_notify(struct notifier_block *nb,
  800. unsigned long action, void *data)
  801. {
  802. uint8_t peripheral;
  803. struct diag_socket_info *info = NULL;
  804. DIAG_LOG(DIAG_DEBUG_PERIPHERALS, "received notification from IPCR");
  805. if (action != IPCRTR_AF_INIT) {
  806. DIAG_LOG(DIAG_DEBUG_PERIPHERALS,
  807. "action not recognized by diag %lu\n", action);
  808. return 0;
  809. }
  810. /* Initialize only the servers */
  811. for (peripheral = 0; peripheral < NUM_PERIPHERALS; peripheral++) {
  812. info = &socket_cntl[peripheral];
  813. queue_work(info->wq, &(info->init_work));
  814. info = &socket_data[peripheral];
  815. queue_work(info->wq, &(info->init_work));
  816. info = &socket_dci[peripheral];
  817. queue_work(info->wq, &(info->init_work));
  818. }
  819. DIAG_LOG(DIAG_DEBUG_PERIPHERALS, "Initialized all servers");
  820. queue_work(cntl_socket->wq, &(cntl_socket->init_work));
  821. return 0;
  822. }
  823. static int restart_notifier_cb(struct notifier_block *this, unsigned long code,
  824. void *_cmd)
  825. {
  826. struct restart_notifier_block *notifier;
  827. notifier = container_of(this,
  828. struct restart_notifier_block, nb);
  829. if (!notifier) {
  830. DIAG_LOG(DIAG_DEBUG_PERIPHERALS,
  831. "diag: %s: invalid notifier block\n", __func__);
  832. return NOTIFY_DONE;
  833. }
  834. mutex_lock(&driver->diag_notifier_mutex);
  835. DIAG_LOG(DIAG_DEBUG_PERIPHERALS,
  836. "%s: ssr for processor %d ('%s')\n",
  837. __func__, notifier->processor, notifier->name);
  838. switch (code) {
  839. case SUBSYS_BEFORE_SHUTDOWN:
  840. DIAG_LOG(DIAG_DEBUG_PERIPHERALS,
  841. "diag: %s: SUBSYS_BEFORE_SHUTDOWN\n", __func__);
  842. bootup_req[notifier->processor] = PEPIPHERAL_SSR_DOWN;
  843. break;
  844. case SUBSYS_AFTER_SHUTDOWN:
  845. DIAG_LOG(DIAG_DEBUG_PERIPHERALS,
  846. "diag: %s: SUBSYS_AFTER_SHUTDOWN\n", __func__);
  847. break;
  848. case SUBSYS_BEFORE_POWERUP:
  849. DIAG_LOG(DIAG_DEBUG_PERIPHERALS,
  850. "diag: %s: SUBSYS_BEFORE_POWERUP\n", __func__);
  851. break;
  852. case SUBSYS_AFTER_POWERUP:
  853. DIAG_LOG(DIAG_DEBUG_PERIPHERALS,
  854. "diag: %s: SUBSYS_AFTER_POWERUP\n", __func__);
  855. if (!bootup_req[notifier->processor]) {
  856. bootup_req[notifier->processor] = PEPIPHERAL_SSR_DOWN;
  857. break;
  858. }
  859. bootup_req[notifier->processor] = PEPIPHERAL_SSR_UP;
  860. break;
  861. default:
  862. DIAG_LOG(DIAG_DEBUG_PERIPHERALS,
  863. "diag: code: %lu\n", code);
  864. break;
  865. }
  866. mutex_unlock(&driver->diag_notifier_mutex);
  867. DIAG_LOG(DIAG_DEBUG_PERIPHERALS,
  868. "diag: bootup_req[%s] = %d\n",
  869. notifier->name, (int)bootup_req[notifier->processor]);
  870. return NOTIFY_DONE;
  871. }
  872. int diag_socket_init_peripheral(uint8_t peripheral)
  873. {
  874. struct diag_socket_info *info = NULL;
  875. if (peripheral >= NUM_PERIPHERALS)
  876. return -EINVAL;
  877. info = &socket_data[peripheral];
  878. diagfwd_register(TRANSPORT_SOCKET, info->peripheral,
  879. info->type, (void *)info, &socket_ops,
  880. &info->fwd_ctxt);
  881. info = &socket_dci[peripheral];
  882. diagfwd_register(TRANSPORT_SOCKET, info->peripheral,
  883. info->type, (void *)info, &socket_ops,
  884. &info->fwd_ctxt);
  885. return 0;
  886. }
  887. static void __diag_socket_exit(struct diag_socket_info *info)
  888. {
  889. if (!info)
  890. return;
  891. diagfwd_deregister(info->peripheral, info->type, (void *)info);
  892. info->fwd_ctxt = NULL;
  893. info->hdl = NULL;
  894. if (info->port_type == PORT_TYPE_CLIENT)
  895. mutex_destroy(&info->socket_info_mutex);
  896. if (info->wq)
  897. destroy_workqueue(info->wq);
  898. }
  899. void diag_socket_early_exit(void)
  900. {
  901. int i = 0;
  902. for (i = 0; i < NUM_PERIPHERALS; i++)
  903. __diag_socket_exit(&socket_cntl[i]);
  904. }
  905. void diag_socket_exit(void)
  906. {
  907. int i = 0;
  908. for (i = 0; i < NUM_PERIPHERALS; i++) {
  909. __diag_socket_exit(&socket_data[i]);
  910. __diag_socket_exit(&socket_cmd[i]);
  911. __diag_socket_exit(&socket_dci[i]);
  912. __diag_socket_exit(&socket_dci_cmd[i]);
  913. }
  914. }
  915. static int diag_socket_read(void *ctxt, unsigned char *buf, int buf_len,
  916. struct diagfwd_buf_t *fwd_buf)
  917. {
  918. int err = 0;
  919. int pkt_len = 0;
  920. int read_len = 0;
  921. int bytes_remaining = 0;
  922. int total_recd = 0;
  923. int loop_count = 0;
  924. uint8_t buf_full = 0;
  925. unsigned char *temp = NULL;
  926. struct kvec iov = {0};
  927. struct msghdr read_msg = {0};
  928. struct sockaddr_msm_ipc src_addr = {0};
  929. struct diag_socket_info *info = NULL;
  930. unsigned long flags;
  931. info = (struct diag_socket_info *)(ctxt);
  932. if (!info)
  933. return -ENODEV;
  934. if (!buf || !ctxt || buf_len <= 0 || !fwd_buf)
  935. return -EINVAL;
  936. temp = buf;
  937. bytes_remaining = buf_len;
  938. err = wait_event_interruptible(info->read_wait_q,
  939. (info->data_ready > 0) || (!info->hdl) ||
  940. (atomic_read(&info->diag_state) == 0));
  941. if (err)
  942. goto fail;
  943. if (atomic_read(&fwd_buf->in_busy) == 0) {
  944. DIAG_LOG(DIAG_DEBUG_PERIPHERALS,
  945. "%s closing read thread. Buffer is already marked freed p: %d t: %d buf_num: %d\n",
  946. info->name, GET_BUF_PERIPHERAL(fwd_buf->ctxt),
  947. GET_BUF_TYPE(fwd_buf->ctxt),
  948. GET_BUF_NUM(fwd_buf->ctxt));
  949. diag_ws_release();
  950. return 0;
  951. }
  952. /*
  953. * There is no need to continue reading over peripheral in this case.
  954. * Release the wake source hold earlier.
  955. */
  956. if (atomic_read(&info->diag_state) == 0) {
  957. DIAG_LOG(DIAG_DEBUG_PERIPHERALS,
  958. "%s closing read thread. diag state is closed\n",
  959. info->name);
  960. goto fail;
  961. }
  962. if (!info->hdl) {
  963. DIAG_LOG(DIAG_DEBUG_PERIPHERALS, "%s closing read thread\n",
  964. info->name);
  965. goto fail;
  966. }
  967. do {
  968. loop_count++;
  969. iov.iov_base = temp;
  970. iov.iov_len = bytes_remaining;
  971. read_msg.msg_name = &src_addr;
  972. read_msg.msg_namelen = sizeof(src_addr);
  973. if (info->port_type != PORT_TYPE_SERVER) {
  974. mutex_lock(&info->socket_info_mutex);
  975. if (!info->hdl) {
  976. DIAG_LOG(DIAG_DEBUG_PERIPHERALS,
  977. "%s closing read thread\n",
  978. info->name);
  979. mutex_unlock(&info->socket_info_mutex);
  980. goto fail;
  981. }
  982. }
  983. pkt_len = kernel_recvmsg(info->hdl, &read_msg, &iov, 1, 0,
  984. MSG_PEEK);
  985. if (pkt_len <= 0) {
  986. if (info->port_type != PORT_TYPE_SERVER)
  987. mutex_unlock(&info->socket_info_mutex);
  988. break;
  989. }
  990. if (pkt_len > bytes_remaining) {
  991. buf_full = 1;
  992. if (info->port_type != PORT_TYPE_SERVER)
  993. mutex_unlock(&info->socket_info_mutex);
  994. break;
  995. }
  996. spin_lock_irqsave(&info->lock, flags);
  997. info->data_ready--;
  998. spin_unlock_irqrestore(&info->lock, flags);
  999. read_len = kernel_recvmsg(info->hdl, &read_msg, &iov, 1,
  1000. pkt_len, 0);
  1001. if (info->port_type != PORT_TYPE_SERVER)
  1002. mutex_unlock(&info->socket_info_mutex);
  1003. if (read_len <= 0)
  1004. goto fail;
  1005. if (!atomic_read(&info->opened) &&
  1006. info->port_type == PORT_TYPE_SERVER) {
  1007. /*
  1008. * This is the first packet from the client. Copy its
  1009. * address to the connection object. Consider this
  1010. * channel open for communication.
  1011. */
  1012. memcpy(&info->remote_addr, &src_addr, sizeof(src_addr));
  1013. if (info->ins_id == INST_ID_DCI)
  1014. atomic_set(&info->opened, 1);
  1015. else
  1016. __socket_open_channel(info);
  1017. }
  1018. if (read_len < 0) {
  1019. pr_err_ratelimited("diag: In %s, error receiving data, err: %d\n",
  1020. __func__, pkt_len);
  1021. err = read_len;
  1022. goto fail;
  1023. }
  1024. temp += read_len;
  1025. total_recd += read_len;
  1026. bytes_remaining -= read_len;
  1027. } while (info->data_ready > 0);
  1028. if (buf_full || (info->type == TYPE_DATA && pkt_len))
  1029. err = queue_work(info->wq, &(info->read_work));
  1030. if (total_recd > 0) {
  1031. DIAG_LOG(DIAG_DEBUG_PERIPHERALS, "%s read total bytes: %d\n",
  1032. info->name, total_recd);
  1033. mutex_lock(&driver->diagfwd_channel_mutex[info->peripheral]);
  1034. err = diagfwd_channel_read_done(info->fwd_ctxt,
  1035. buf, total_recd);
  1036. mutex_unlock(&driver->diagfwd_channel_mutex[info->peripheral]);
  1037. if (err)
  1038. goto fail;
  1039. } else {
  1040. DIAG_LOG(DIAG_DEBUG_PERIPHERALS, "%s error in read, err: %d\n",
  1041. info->name, total_recd);
  1042. goto fail;
  1043. }
  1044. diag_socket_queue_read(info);
  1045. return 0;
  1046. fail:
  1047. mutex_lock(&driver->diagfwd_channel_mutex[info->peripheral]);
  1048. diagfwd_channel_read_done(info->fwd_ctxt, buf, 0);
  1049. mutex_unlock(&driver->diagfwd_channel_mutex[info->peripheral]);
  1050. return 0;
  1051. }
  1052. static int diag_socket_write(void *ctxt, unsigned char *buf, int len)
  1053. {
  1054. int err = 0;
  1055. int write_len = 0;
  1056. struct kvec iov = {0};
  1057. struct msghdr write_msg = {0};
  1058. struct diag_socket_info *info = NULL;
  1059. if (!ctxt || !buf || len <= 0)
  1060. return -EIO;
  1061. info = (struct diag_socket_info *)(ctxt);
  1062. if (!atomic_read(&info->opened) || !info->hdl)
  1063. return -ENODEV;
  1064. iov.iov_base = buf;
  1065. iov.iov_len = len;
  1066. write_msg.msg_name = &info->remote_addr;
  1067. write_msg.msg_namelen = sizeof(info->remote_addr);
  1068. write_msg.msg_flags |= MSG_DONTWAIT;
  1069. if (info->port_type != PORT_TYPE_SERVER) {
  1070. mutex_lock(&info->socket_info_mutex);
  1071. if (!info->hdl) {
  1072. mutex_unlock(&info->socket_info_mutex);
  1073. return -ENODEV;
  1074. }
  1075. }
  1076. write_len = kernel_sendmsg(info->hdl, &write_msg, &iov, 1, len);
  1077. if (info->port_type != PORT_TYPE_SERVER)
  1078. mutex_unlock(&info->socket_info_mutex);
  1079. if (write_len < 0) {
  1080. err = write_len;
  1081. /*
  1082. * -EAGAIN means that the number of packets in flight is at
  1083. * max capactity and the peripheral hasn't read the data.
  1084. */
  1085. if (err != -EAGAIN) {
  1086. pr_err_ratelimited("diag: In %s, error sending data, err: %d, ch: %s\n",
  1087. __func__, err, info->name);
  1088. }
  1089. } else if (write_len != len) {
  1090. err = write_len;
  1091. pr_err_ratelimited("diag: In %s, wrote partial packet to %s, len: %d, wrote: %d\n",
  1092. __func__, info->name, len, write_len);
  1093. }
  1094. DIAG_LOG(DIAG_DEBUG_PERIPHERALS, "%s wrote to socket, len: %d\n",
  1095. info->name, write_len);
  1096. return err;
  1097. }