mad_rmpp.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. /*
  2. * Copyright (c) 2005 Intel Inc. All rights reserved.
  3. * Copyright (c) 2005-2006 Voltaire, Inc. All rights reserved.
  4. * Copyright (c) 2014 Intel Corporation. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/slab.h>
  35. #include "mad_priv.h"
  36. #include "mad_rmpp.h"
  37. enum rmpp_state {
  38. RMPP_STATE_ACTIVE,
  39. RMPP_STATE_TIMEOUT,
  40. RMPP_STATE_COMPLETE,
  41. RMPP_STATE_CANCELING
  42. };
  43. struct mad_rmpp_recv {
  44. struct ib_mad_agent_private *agent;
  45. struct list_head list;
  46. struct delayed_work timeout_work;
  47. struct delayed_work cleanup_work;
  48. struct completion comp;
  49. enum rmpp_state state;
  50. spinlock_t lock;
  51. atomic_t refcount;
  52. struct ib_ah *ah;
  53. struct ib_mad_recv_wc *rmpp_wc;
  54. struct ib_mad_recv_buf *cur_seg_buf;
  55. int last_ack;
  56. int seg_num;
  57. int newwin;
  58. int repwin;
  59. __be64 tid;
  60. u32 src_qp;
  61. u16 slid;
  62. u8 mgmt_class;
  63. u8 class_version;
  64. u8 method;
  65. u8 base_version;
  66. };
  67. static inline void deref_rmpp_recv(struct mad_rmpp_recv *rmpp_recv)
  68. {
  69. if (atomic_dec_and_test(&rmpp_recv->refcount))
  70. complete(&rmpp_recv->comp);
  71. }
  72. static void destroy_rmpp_recv(struct mad_rmpp_recv *rmpp_recv)
  73. {
  74. deref_rmpp_recv(rmpp_recv);
  75. wait_for_completion(&rmpp_recv->comp);
  76. ib_destroy_ah(rmpp_recv->ah);
  77. kfree(rmpp_recv);
  78. }
  79. void ib_cancel_rmpp_recvs(struct ib_mad_agent_private *agent)
  80. {
  81. struct mad_rmpp_recv *rmpp_recv, *temp_rmpp_recv;
  82. unsigned long flags;
  83. spin_lock_irqsave(&agent->lock, flags);
  84. list_for_each_entry(rmpp_recv, &agent->rmpp_list, list) {
  85. if (rmpp_recv->state != RMPP_STATE_COMPLETE)
  86. ib_free_recv_mad(rmpp_recv->rmpp_wc);
  87. rmpp_recv->state = RMPP_STATE_CANCELING;
  88. }
  89. spin_unlock_irqrestore(&agent->lock, flags);
  90. list_for_each_entry(rmpp_recv, &agent->rmpp_list, list) {
  91. cancel_delayed_work(&rmpp_recv->timeout_work);
  92. cancel_delayed_work(&rmpp_recv->cleanup_work);
  93. }
  94. flush_workqueue(agent->qp_info->port_priv->wq);
  95. list_for_each_entry_safe(rmpp_recv, temp_rmpp_recv,
  96. &agent->rmpp_list, list) {
  97. list_del(&rmpp_recv->list);
  98. destroy_rmpp_recv(rmpp_recv);
  99. }
  100. }
  101. static void format_ack(struct ib_mad_send_buf *msg,
  102. struct ib_rmpp_mad *data,
  103. struct mad_rmpp_recv *rmpp_recv)
  104. {
  105. struct ib_rmpp_mad *ack = msg->mad;
  106. unsigned long flags;
  107. memcpy(ack, &data->mad_hdr, msg->hdr_len);
  108. ack->mad_hdr.method ^= IB_MGMT_METHOD_RESP;
  109. ack->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_ACK;
  110. ib_set_rmpp_flags(&ack->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
  111. spin_lock_irqsave(&rmpp_recv->lock, flags);
  112. rmpp_recv->last_ack = rmpp_recv->seg_num;
  113. ack->rmpp_hdr.seg_num = cpu_to_be32(rmpp_recv->seg_num);
  114. ack->rmpp_hdr.paylen_newwin = cpu_to_be32(rmpp_recv->newwin);
  115. spin_unlock_irqrestore(&rmpp_recv->lock, flags);
  116. }
  117. static void ack_recv(struct mad_rmpp_recv *rmpp_recv,
  118. struct ib_mad_recv_wc *recv_wc)
  119. {
  120. struct ib_mad_send_buf *msg;
  121. int ret, hdr_len;
  122. hdr_len = ib_get_mad_data_offset(recv_wc->recv_buf.mad->mad_hdr.mgmt_class);
  123. msg = ib_create_send_mad(&rmpp_recv->agent->agent, recv_wc->wc->src_qp,
  124. recv_wc->wc->pkey_index, 1, hdr_len,
  125. 0, GFP_KERNEL,
  126. IB_MGMT_BASE_VERSION);
  127. if (IS_ERR(msg))
  128. return;
  129. format_ack(msg, (struct ib_rmpp_mad *) recv_wc->recv_buf.mad, rmpp_recv);
  130. msg->ah = rmpp_recv->ah;
  131. ret = ib_post_send_mad(msg, NULL);
  132. if (ret)
  133. ib_free_send_mad(msg);
  134. }
  135. static struct ib_mad_send_buf *alloc_response_msg(struct ib_mad_agent *agent,
  136. struct ib_mad_recv_wc *recv_wc)
  137. {
  138. struct ib_mad_send_buf *msg;
  139. struct ib_ah *ah;
  140. int hdr_len;
  141. ah = ib_create_ah_from_wc(agent->qp->pd, recv_wc->wc,
  142. recv_wc->recv_buf.grh, agent->port_num);
  143. if (IS_ERR(ah))
  144. return (void *) ah;
  145. hdr_len = ib_get_mad_data_offset(recv_wc->recv_buf.mad->mad_hdr.mgmt_class);
  146. msg = ib_create_send_mad(agent, recv_wc->wc->src_qp,
  147. recv_wc->wc->pkey_index, 1,
  148. hdr_len, 0, GFP_KERNEL,
  149. IB_MGMT_BASE_VERSION);
  150. if (IS_ERR(msg))
  151. ib_destroy_ah(ah);
  152. else {
  153. msg->ah = ah;
  154. msg->context[0] = ah;
  155. }
  156. return msg;
  157. }
  158. static void ack_ds_ack(struct ib_mad_agent_private *agent,
  159. struct ib_mad_recv_wc *recv_wc)
  160. {
  161. struct ib_mad_send_buf *msg;
  162. struct ib_rmpp_mad *rmpp_mad;
  163. int ret;
  164. msg = alloc_response_msg(&agent->agent, recv_wc);
  165. if (IS_ERR(msg))
  166. return;
  167. rmpp_mad = msg->mad;
  168. memcpy(rmpp_mad, recv_wc->recv_buf.mad, msg->hdr_len);
  169. rmpp_mad->mad_hdr.method ^= IB_MGMT_METHOD_RESP;
  170. ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
  171. rmpp_mad->rmpp_hdr.seg_num = 0;
  172. rmpp_mad->rmpp_hdr.paylen_newwin = cpu_to_be32(1);
  173. ret = ib_post_send_mad(msg, NULL);
  174. if (ret) {
  175. ib_destroy_ah(msg->ah);
  176. ib_free_send_mad(msg);
  177. }
  178. }
  179. void ib_rmpp_send_handler(struct ib_mad_send_wc *mad_send_wc)
  180. {
  181. if (mad_send_wc->send_buf->context[0] == mad_send_wc->send_buf->ah)
  182. ib_destroy_ah(mad_send_wc->send_buf->ah);
  183. ib_free_send_mad(mad_send_wc->send_buf);
  184. }
  185. static void nack_recv(struct ib_mad_agent_private *agent,
  186. struct ib_mad_recv_wc *recv_wc, u8 rmpp_status)
  187. {
  188. struct ib_mad_send_buf *msg;
  189. struct ib_rmpp_mad *rmpp_mad;
  190. int ret;
  191. msg = alloc_response_msg(&agent->agent, recv_wc);
  192. if (IS_ERR(msg))
  193. return;
  194. rmpp_mad = msg->mad;
  195. memcpy(rmpp_mad, recv_wc->recv_buf.mad, msg->hdr_len);
  196. rmpp_mad->mad_hdr.method ^= IB_MGMT_METHOD_RESP;
  197. rmpp_mad->rmpp_hdr.rmpp_version = IB_MGMT_RMPP_VERSION;
  198. rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_ABORT;
  199. ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
  200. rmpp_mad->rmpp_hdr.rmpp_status = rmpp_status;
  201. rmpp_mad->rmpp_hdr.seg_num = 0;
  202. rmpp_mad->rmpp_hdr.paylen_newwin = 0;
  203. ret = ib_post_send_mad(msg, NULL);
  204. if (ret) {
  205. ib_destroy_ah(msg->ah);
  206. ib_free_send_mad(msg);
  207. }
  208. }
  209. static void recv_timeout_handler(struct work_struct *work)
  210. {
  211. struct mad_rmpp_recv *rmpp_recv =
  212. container_of(work, struct mad_rmpp_recv, timeout_work.work);
  213. struct ib_mad_recv_wc *rmpp_wc;
  214. unsigned long flags;
  215. spin_lock_irqsave(&rmpp_recv->agent->lock, flags);
  216. if (rmpp_recv->state != RMPP_STATE_ACTIVE) {
  217. spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
  218. return;
  219. }
  220. rmpp_recv->state = RMPP_STATE_TIMEOUT;
  221. list_del(&rmpp_recv->list);
  222. spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
  223. rmpp_wc = rmpp_recv->rmpp_wc;
  224. nack_recv(rmpp_recv->agent, rmpp_wc, IB_MGMT_RMPP_STATUS_T2L);
  225. destroy_rmpp_recv(rmpp_recv);
  226. ib_free_recv_mad(rmpp_wc);
  227. }
  228. static void recv_cleanup_handler(struct work_struct *work)
  229. {
  230. struct mad_rmpp_recv *rmpp_recv =
  231. container_of(work, struct mad_rmpp_recv, cleanup_work.work);
  232. unsigned long flags;
  233. spin_lock_irqsave(&rmpp_recv->agent->lock, flags);
  234. if (rmpp_recv->state == RMPP_STATE_CANCELING) {
  235. spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
  236. return;
  237. }
  238. list_del(&rmpp_recv->list);
  239. spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
  240. destroy_rmpp_recv(rmpp_recv);
  241. }
  242. static struct mad_rmpp_recv *
  243. create_rmpp_recv(struct ib_mad_agent_private *agent,
  244. struct ib_mad_recv_wc *mad_recv_wc)
  245. {
  246. struct mad_rmpp_recv *rmpp_recv;
  247. struct ib_mad_hdr *mad_hdr;
  248. rmpp_recv = kmalloc(sizeof *rmpp_recv, GFP_KERNEL);
  249. if (!rmpp_recv)
  250. return NULL;
  251. rmpp_recv->ah = ib_create_ah_from_wc(agent->agent.qp->pd,
  252. mad_recv_wc->wc,
  253. mad_recv_wc->recv_buf.grh,
  254. agent->agent.port_num);
  255. if (IS_ERR(rmpp_recv->ah))
  256. goto error;
  257. rmpp_recv->agent = agent;
  258. init_completion(&rmpp_recv->comp);
  259. INIT_DELAYED_WORK(&rmpp_recv->timeout_work, recv_timeout_handler);
  260. INIT_DELAYED_WORK(&rmpp_recv->cleanup_work, recv_cleanup_handler);
  261. spin_lock_init(&rmpp_recv->lock);
  262. rmpp_recv->state = RMPP_STATE_ACTIVE;
  263. atomic_set(&rmpp_recv->refcount, 1);
  264. rmpp_recv->rmpp_wc = mad_recv_wc;
  265. rmpp_recv->cur_seg_buf = &mad_recv_wc->recv_buf;
  266. rmpp_recv->newwin = 1;
  267. rmpp_recv->seg_num = 1;
  268. rmpp_recv->last_ack = 0;
  269. rmpp_recv->repwin = 1;
  270. mad_hdr = &mad_recv_wc->recv_buf.mad->mad_hdr;
  271. rmpp_recv->tid = mad_hdr->tid;
  272. rmpp_recv->src_qp = mad_recv_wc->wc->src_qp;
  273. rmpp_recv->slid = mad_recv_wc->wc->slid;
  274. rmpp_recv->mgmt_class = mad_hdr->mgmt_class;
  275. rmpp_recv->class_version = mad_hdr->class_version;
  276. rmpp_recv->method = mad_hdr->method;
  277. rmpp_recv->base_version = mad_hdr->base_version;
  278. return rmpp_recv;
  279. error: kfree(rmpp_recv);
  280. return NULL;
  281. }
  282. static struct mad_rmpp_recv *
  283. find_rmpp_recv(struct ib_mad_agent_private *agent,
  284. struct ib_mad_recv_wc *mad_recv_wc)
  285. {
  286. struct mad_rmpp_recv *rmpp_recv;
  287. struct ib_mad_hdr *mad_hdr = &mad_recv_wc->recv_buf.mad->mad_hdr;
  288. list_for_each_entry(rmpp_recv, &agent->rmpp_list, list) {
  289. if (rmpp_recv->tid == mad_hdr->tid &&
  290. rmpp_recv->src_qp == mad_recv_wc->wc->src_qp &&
  291. rmpp_recv->slid == mad_recv_wc->wc->slid &&
  292. rmpp_recv->mgmt_class == mad_hdr->mgmt_class &&
  293. rmpp_recv->class_version == mad_hdr->class_version &&
  294. rmpp_recv->method == mad_hdr->method)
  295. return rmpp_recv;
  296. }
  297. return NULL;
  298. }
  299. static struct mad_rmpp_recv *
  300. acquire_rmpp_recv(struct ib_mad_agent_private *agent,
  301. struct ib_mad_recv_wc *mad_recv_wc)
  302. {
  303. struct mad_rmpp_recv *rmpp_recv;
  304. unsigned long flags;
  305. spin_lock_irqsave(&agent->lock, flags);
  306. rmpp_recv = find_rmpp_recv(agent, mad_recv_wc);
  307. if (rmpp_recv)
  308. atomic_inc(&rmpp_recv->refcount);
  309. spin_unlock_irqrestore(&agent->lock, flags);
  310. return rmpp_recv;
  311. }
  312. static struct mad_rmpp_recv *
  313. insert_rmpp_recv(struct ib_mad_agent_private *agent,
  314. struct mad_rmpp_recv *rmpp_recv)
  315. {
  316. struct mad_rmpp_recv *cur_rmpp_recv;
  317. cur_rmpp_recv = find_rmpp_recv(agent, rmpp_recv->rmpp_wc);
  318. if (!cur_rmpp_recv)
  319. list_add_tail(&rmpp_recv->list, &agent->rmpp_list);
  320. return cur_rmpp_recv;
  321. }
  322. static inline int get_last_flag(struct ib_mad_recv_buf *seg)
  323. {
  324. struct ib_rmpp_mad *rmpp_mad;
  325. rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
  326. return ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & IB_MGMT_RMPP_FLAG_LAST;
  327. }
  328. static inline int get_seg_num(struct ib_mad_recv_buf *seg)
  329. {
  330. struct ib_rmpp_mad *rmpp_mad;
  331. rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
  332. return be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
  333. }
  334. static inline struct ib_mad_recv_buf * get_next_seg(struct list_head *rmpp_list,
  335. struct ib_mad_recv_buf *seg)
  336. {
  337. if (seg->list.next == rmpp_list)
  338. return NULL;
  339. return container_of(seg->list.next, struct ib_mad_recv_buf, list);
  340. }
  341. static inline int window_size(struct ib_mad_agent_private *agent)
  342. {
  343. return max(agent->qp_info->recv_queue.max_active >> 3, 1);
  344. }
  345. static struct ib_mad_recv_buf * find_seg_location(struct list_head *rmpp_list,
  346. int seg_num)
  347. {
  348. struct ib_mad_recv_buf *seg_buf;
  349. int cur_seg_num;
  350. list_for_each_entry_reverse(seg_buf, rmpp_list, list) {
  351. cur_seg_num = get_seg_num(seg_buf);
  352. if (seg_num > cur_seg_num)
  353. return seg_buf;
  354. if (seg_num == cur_seg_num)
  355. break;
  356. }
  357. return NULL;
  358. }
  359. static void update_seg_num(struct mad_rmpp_recv *rmpp_recv,
  360. struct ib_mad_recv_buf *new_buf)
  361. {
  362. struct list_head *rmpp_list = &rmpp_recv->rmpp_wc->rmpp_list;
  363. while (new_buf && (get_seg_num(new_buf) == rmpp_recv->seg_num + 1)) {
  364. rmpp_recv->cur_seg_buf = new_buf;
  365. rmpp_recv->seg_num++;
  366. new_buf = get_next_seg(rmpp_list, new_buf);
  367. }
  368. }
  369. static inline int get_mad_len(struct mad_rmpp_recv *rmpp_recv)
  370. {
  371. struct ib_rmpp_mad *rmpp_mad;
  372. int hdr_size, data_size, pad;
  373. bool opa = rdma_cap_opa_mad(rmpp_recv->agent->qp_info->port_priv->device,
  374. rmpp_recv->agent->qp_info->port_priv->port_num);
  375. rmpp_mad = (struct ib_rmpp_mad *)rmpp_recv->cur_seg_buf->mad;
  376. hdr_size = ib_get_mad_data_offset(rmpp_mad->mad_hdr.mgmt_class);
  377. if (opa && rmpp_recv->base_version == OPA_MGMT_BASE_VERSION) {
  378. data_size = sizeof(struct opa_rmpp_mad) - hdr_size;
  379. pad = OPA_MGMT_RMPP_DATA - be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
  380. if (pad > OPA_MGMT_RMPP_DATA || pad < 0)
  381. pad = 0;
  382. } else {
  383. data_size = sizeof(struct ib_rmpp_mad) - hdr_size;
  384. pad = IB_MGMT_RMPP_DATA - be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
  385. if (pad > IB_MGMT_RMPP_DATA || pad < 0)
  386. pad = 0;
  387. }
  388. return hdr_size + rmpp_recv->seg_num * data_size - pad;
  389. }
  390. static struct ib_mad_recv_wc * complete_rmpp(struct mad_rmpp_recv *rmpp_recv)
  391. {
  392. struct ib_mad_recv_wc *rmpp_wc;
  393. ack_recv(rmpp_recv, rmpp_recv->rmpp_wc);
  394. if (rmpp_recv->seg_num > 1)
  395. cancel_delayed_work(&rmpp_recv->timeout_work);
  396. rmpp_wc = rmpp_recv->rmpp_wc;
  397. rmpp_wc->mad_len = get_mad_len(rmpp_recv);
  398. /* 10 seconds until we can find the packet lifetime */
  399. queue_delayed_work(rmpp_recv->agent->qp_info->port_priv->wq,
  400. &rmpp_recv->cleanup_work, msecs_to_jiffies(10000));
  401. return rmpp_wc;
  402. }
  403. static struct ib_mad_recv_wc *
  404. continue_rmpp(struct ib_mad_agent_private *agent,
  405. struct ib_mad_recv_wc *mad_recv_wc)
  406. {
  407. struct mad_rmpp_recv *rmpp_recv;
  408. struct ib_mad_recv_buf *prev_buf;
  409. struct ib_mad_recv_wc *done_wc;
  410. int seg_num;
  411. unsigned long flags;
  412. rmpp_recv = acquire_rmpp_recv(agent, mad_recv_wc);
  413. if (!rmpp_recv)
  414. goto drop1;
  415. seg_num = get_seg_num(&mad_recv_wc->recv_buf);
  416. spin_lock_irqsave(&rmpp_recv->lock, flags);
  417. if ((rmpp_recv->state == RMPP_STATE_TIMEOUT) ||
  418. (seg_num > rmpp_recv->newwin))
  419. goto drop3;
  420. if ((seg_num <= rmpp_recv->last_ack) ||
  421. (rmpp_recv->state == RMPP_STATE_COMPLETE)) {
  422. spin_unlock_irqrestore(&rmpp_recv->lock, flags);
  423. ack_recv(rmpp_recv, mad_recv_wc);
  424. goto drop2;
  425. }
  426. prev_buf = find_seg_location(&rmpp_recv->rmpp_wc->rmpp_list, seg_num);
  427. if (!prev_buf)
  428. goto drop3;
  429. done_wc = NULL;
  430. list_add(&mad_recv_wc->recv_buf.list, &prev_buf->list);
  431. if (rmpp_recv->cur_seg_buf == prev_buf) {
  432. update_seg_num(rmpp_recv, &mad_recv_wc->recv_buf);
  433. if (get_last_flag(rmpp_recv->cur_seg_buf)) {
  434. rmpp_recv->state = RMPP_STATE_COMPLETE;
  435. spin_unlock_irqrestore(&rmpp_recv->lock, flags);
  436. done_wc = complete_rmpp(rmpp_recv);
  437. goto out;
  438. } else if (rmpp_recv->seg_num == rmpp_recv->newwin) {
  439. rmpp_recv->newwin += window_size(agent);
  440. spin_unlock_irqrestore(&rmpp_recv->lock, flags);
  441. ack_recv(rmpp_recv, mad_recv_wc);
  442. goto out;
  443. }
  444. }
  445. spin_unlock_irqrestore(&rmpp_recv->lock, flags);
  446. out:
  447. deref_rmpp_recv(rmpp_recv);
  448. return done_wc;
  449. drop3: spin_unlock_irqrestore(&rmpp_recv->lock, flags);
  450. drop2: deref_rmpp_recv(rmpp_recv);
  451. drop1: ib_free_recv_mad(mad_recv_wc);
  452. return NULL;
  453. }
  454. static struct ib_mad_recv_wc *
  455. start_rmpp(struct ib_mad_agent_private *agent,
  456. struct ib_mad_recv_wc *mad_recv_wc)
  457. {
  458. struct mad_rmpp_recv *rmpp_recv;
  459. unsigned long flags;
  460. rmpp_recv = create_rmpp_recv(agent, mad_recv_wc);
  461. if (!rmpp_recv) {
  462. ib_free_recv_mad(mad_recv_wc);
  463. return NULL;
  464. }
  465. spin_lock_irqsave(&agent->lock, flags);
  466. if (insert_rmpp_recv(agent, rmpp_recv)) {
  467. spin_unlock_irqrestore(&agent->lock, flags);
  468. /* duplicate first MAD */
  469. destroy_rmpp_recv(rmpp_recv);
  470. return continue_rmpp(agent, mad_recv_wc);
  471. }
  472. atomic_inc(&rmpp_recv->refcount);
  473. if (get_last_flag(&mad_recv_wc->recv_buf)) {
  474. rmpp_recv->state = RMPP_STATE_COMPLETE;
  475. spin_unlock_irqrestore(&agent->lock, flags);
  476. complete_rmpp(rmpp_recv);
  477. } else {
  478. spin_unlock_irqrestore(&agent->lock, flags);
  479. /* 40 seconds until we can find the packet lifetimes */
  480. queue_delayed_work(agent->qp_info->port_priv->wq,
  481. &rmpp_recv->timeout_work,
  482. msecs_to_jiffies(40000));
  483. rmpp_recv->newwin += window_size(agent);
  484. ack_recv(rmpp_recv, mad_recv_wc);
  485. mad_recv_wc = NULL;
  486. }
  487. deref_rmpp_recv(rmpp_recv);
  488. return mad_recv_wc;
  489. }
  490. static int send_next_seg(struct ib_mad_send_wr_private *mad_send_wr)
  491. {
  492. struct ib_rmpp_mad *rmpp_mad;
  493. int timeout;
  494. u32 paylen = 0;
  495. rmpp_mad = mad_send_wr->send_buf.mad;
  496. ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
  497. rmpp_mad->rmpp_hdr.seg_num = cpu_to_be32(++mad_send_wr->seg_num);
  498. if (mad_send_wr->seg_num == 1) {
  499. rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_FIRST;
  500. paylen = (mad_send_wr->send_buf.seg_count *
  501. mad_send_wr->send_buf.seg_rmpp_size) -
  502. mad_send_wr->pad;
  503. }
  504. if (mad_send_wr->seg_num == mad_send_wr->send_buf.seg_count) {
  505. rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_LAST;
  506. paylen = mad_send_wr->send_buf.seg_rmpp_size - mad_send_wr->pad;
  507. }
  508. rmpp_mad->rmpp_hdr.paylen_newwin = cpu_to_be32(paylen);
  509. /* 2 seconds for an ACK until we can find the packet lifetime */
  510. timeout = mad_send_wr->send_buf.timeout_ms;
  511. if (!timeout || timeout > 2000)
  512. mad_send_wr->timeout = msecs_to_jiffies(2000);
  513. return ib_send_mad(mad_send_wr);
  514. }
  515. static void abort_send(struct ib_mad_agent_private *agent,
  516. struct ib_mad_recv_wc *mad_recv_wc, u8 rmpp_status)
  517. {
  518. struct ib_mad_send_wr_private *mad_send_wr;
  519. struct ib_mad_send_wc wc;
  520. unsigned long flags;
  521. spin_lock_irqsave(&agent->lock, flags);
  522. mad_send_wr = ib_find_send_mad(agent, mad_recv_wc);
  523. if (!mad_send_wr)
  524. goto out; /* Unmatched send */
  525. if ((mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) ||
  526. (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
  527. goto out; /* Send is already done */
  528. ib_mark_mad_done(mad_send_wr);
  529. spin_unlock_irqrestore(&agent->lock, flags);
  530. wc.status = IB_WC_REM_ABORT_ERR;
  531. wc.vendor_err = rmpp_status;
  532. wc.send_buf = &mad_send_wr->send_buf;
  533. ib_mad_complete_send_wr(mad_send_wr, &wc);
  534. return;
  535. out:
  536. spin_unlock_irqrestore(&agent->lock, flags);
  537. }
  538. static inline void adjust_last_ack(struct ib_mad_send_wr_private *wr,
  539. int seg_num)
  540. {
  541. struct list_head *list;
  542. wr->last_ack = seg_num;
  543. list = &wr->last_ack_seg->list;
  544. list_for_each_entry(wr->last_ack_seg, list, list)
  545. if (wr->last_ack_seg->num == seg_num)
  546. break;
  547. }
  548. static void process_ds_ack(struct ib_mad_agent_private *agent,
  549. struct ib_mad_recv_wc *mad_recv_wc, int newwin)
  550. {
  551. struct mad_rmpp_recv *rmpp_recv;
  552. rmpp_recv = find_rmpp_recv(agent, mad_recv_wc);
  553. if (rmpp_recv && rmpp_recv->state == RMPP_STATE_COMPLETE)
  554. rmpp_recv->repwin = newwin;
  555. }
  556. static void process_rmpp_ack(struct ib_mad_agent_private *agent,
  557. struct ib_mad_recv_wc *mad_recv_wc)
  558. {
  559. struct ib_mad_send_wr_private *mad_send_wr;
  560. struct ib_rmpp_mad *rmpp_mad;
  561. unsigned long flags;
  562. int seg_num, newwin, ret;
  563. rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
  564. if (rmpp_mad->rmpp_hdr.rmpp_status) {
  565. abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
  566. nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
  567. return;
  568. }
  569. seg_num = be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
  570. newwin = be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
  571. if (newwin < seg_num) {
  572. abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_W2S);
  573. nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_W2S);
  574. return;
  575. }
  576. spin_lock_irqsave(&agent->lock, flags);
  577. mad_send_wr = ib_find_send_mad(agent, mad_recv_wc);
  578. if (!mad_send_wr) {
  579. if (!seg_num)
  580. process_ds_ack(agent, mad_recv_wc, newwin);
  581. goto out; /* Unmatched or DS RMPP ACK */
  582. }
  583. if ((mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) &&
  584. (mad_send_wr->timeout)) {
  585. spin_unlock_irqrestore(&agent->lock, flags);
  586. ack_ds_ack(agent, mad_recv_wc);
  587. return; /* Repeated ACK for DS RMPP transaction */
  588. }
  589. if ((mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) ||
  590. (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
  591. goto out; /* Send is already done */
  592. if (seg_num > mad_send_wr->send_buf.seg_count ||
  593. seg_num > mad_send_wr->newwin) {
  594. spin_unlock_irqrestore(&agent->lock, flags);
  595. abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_S2B);
  596. nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_S2B);
  597. return;
  598. }
  599. if (newwin < mad_send_wr->newwin || seg_num < mad_send_wr->last_ack)
  600. goto out; /* Old ACK */
  601. if (seg_num > mad_send_wr->last_ack) {
  602. adjust_last_ack(mad_send_wr, seg_num);
  603. mad_send_wr->retries_left = mad_send_wr->max_retries;
  604. }
  605. mad_send_wr->newwin = newwin;
  606. if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) {
  607. /* If no response is expected, the ACK completes the send */
  608. if (!mad_send_wr->send_buf.timeout_ms) {
  609. struct ib_mad_send_wc wc;
  610. ib_mark_mad_done(mad_send_wr);
  611. spin_unlock_irqrestore(&agent->lock, flags);
  612. wc.status = IB_WC_SUCCESS;
  613. wc.vendor_err = 0;
  614. wc.send_buf = &mad_send_wr->send_buf;
  615. ib_mad_complete_send_wr(mad_send_wr, &wc);
  616. return;
  617. }
  618. if (mad_send_wr->refcount == 1)
  619. ib_reset_mad_timeout(mad_send_wr,
  620. mad_send_wr->send_buf.timeout_ms);
  621. spin_unlock_irqrestore(&agent->lock, flags);
  622. ack_ds_ack(agent, mad_recv_wc);
  623. return;
  624. } else if (mad_send_wr->refcount == 1 &&
  625. mad_send_wr->seg_num < mad_send_wr->newwin &&
  626. mad_send_wr->seg_num < mad_send_wr->send_buf.seg_count) {
  627. /* Send failure will just result in a timeout/retry */
  628. ret = send_next_seg(mad_send_wr);
  629. if (ret)
  630. goto out;
  631. mad_send_wr->refcount++;
  632. list_move_tail(&mad_send_wr->agent_list,
  633. &mad_send_wr->mad_agent_priv->send_list);
  634. }
  635. out:
  636. spin_unlock_irqrestore(&agent->lock, flags);
  637. }
  638. static struct ib_mad_recv_wc *
  639. process_rmpp_data(struct ib_mad_agent_private *agent,
  640. struct ib_mad_recv_wc *mad_recv_wc)
  641. {
  642. struct ib_rmpp_hdr *rmpp_hdr;
  643. u8 rmpp_status;
  644. rmpp_hdr = &((struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad)->rmpp_hdr;
  645. if (rmpp_hdr->rmpp_status) {
  646. rmpp_status = IB_MGMT_RMPP_STATUS_BAD_STATUS;
  647. goto bad;
  648. }
  649. if (rmpp_hdr->seg_num == cpu_to_be32(1)) {
  650. if (!(ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST)) {
  651. rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
  652. goto bad;
  653. }
  654. return start_rmpp(agent, mad_recv_wc);
  655. } else {
  656. if (ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST) {
  657. rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
  658. goto bad;
  659. }
  660. return continue_rmpp(agent, mad_recv_wc);
  661. }
  662. bad:
  663. nack_recv(agent, mad_recv_wc, rmpp_status);
  664. ib_free_recv_mad(mad_recv_wc);
  665. return NULL;
  666. }
  667. static void process_rmpp_stop(struct ib_mad_agent_private *agent,
  668. struct ib_mad_recv_wc *mad_recv_wc)
  669. {
  670. struct ib_rmpp_mad *rmpp_mad;
  671. rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
  672. if (rmpp_mad->rmpp_hdr.rmpp_status != IB_MGMT_RMPP_STATUS_RESX) {
  673. abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
  674. nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
  675. } else
  676. abort_send(agent, mad_recv_wc, rmpp_mad->rmpp_hdr.rmpp_status);
  677. }
  678. static void process_rmpp_abort(struct ib_mad_agent_private *agent,
  679. struct ib_mad_recv_wc *mad_recv_wc)
  680. {
  681. struct ib_rmpp_mad *rmpp_mad;
  682. rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
  683. if (rmpp_mad->rmpp_hdr.rmpp_status < IB_MGMT_RMPP_STATUS_ABORT_MIN ||
  684. rmpp_mad->rmpp_hdr.rmpp_status > IB_MGMT_RMPP_STATUS_ABORT_MAX) {
  685. abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
  686. nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
  687. } else
  688. abort_send(agent, mad_recv_wc, rmpp_mad->rmpp_hdr.rmpp_status);
  689. }
  690. struct ib_mad_recv_wc *
  691. ib_process_rmpp_recv_wc(struct ib_mad_agent_private *agent,
  692. struct ib_mad_recv_wc *mad_recv_wc)
  693. {
  694. struct ib_rmpp_mad *rmpp_mad;
  695. rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
  696. if (!(rmpp_mad->rmpp_hdr.rmpp_rtime_flags & IB_MGMT_RMPP_FLAG_ACTIVE))
  697. return mad_recv_wc;
  698. if (rmpp_mad->rmpp_hdr.rmpp_version != IB_MGMT_RMPP_VERSION) {
  699. abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_UNV);
  700. nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_UNV);
  701. goto out;
  702. }
  703. switch (rmpp_mad->rmpp_hdr.rmpp_type) {
  704. case IB_MGMT_RMPP_TYPE_DATA:
  705. return process_rmpp_data(agent, mad_recv_wc);
  706. case IB_MGMT_RMPP_TYPE_ACK:
  707. process_rmpp_ack(agent, mad_recv_wc);
  708. break;
  709. case IB_MGMT_RMPP_TYPE_STOP:
  710. process_rmpp_stop(agent, mad_recv_wc);
  711. break;
  712. case IB_MGMT_RMPP_TYPE_ABORT:
  713. process_rmpp_abort(agent, mad_recv_wc);
  714. break;
  715. default:
  716. abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BADT);
  717. nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BADT);
  718. break;
  719. }
  720. out:
  721. ib_free_recv_mad(mad_recv_wc);
  722. return NULL;
  723. }
  724. static int init_newwin(struct ib_mad_send_wr_private *mad_send_wr)
  725. {
  726. struct ib_mad_agent_private *agent = mad_send_wr->mad_agent_priv;
  727. struct ib_mad_hdr *mad_hdr = mad_send_wr->send_buf.mad;
  728. struct mad_rmpp_recv *rmpp_recv;
  729. struct ib_ah_attr ah_attr;
  730. unsigned long flags;
  731. int newwin = 1;
  732. if (!(mad_hdr->method & IB_MGMT_METHOD_RESP))
  733. goto out;
  734. spin_lock_irqsave(&agent->lock, flags);
  735. list_for_each_entry(rmpp_recv, &agent->rmpp_list, list) {
  736. if (rmpp_recv->tid != mad_hdr->tid ||
  737. rmpp_recv->mgmt_class != mad_hdr->mgmt_class ||
  738. rmpp_recv->class_version != mad_hdr->class_version ||
  739. (rmpp_recv->method & IB_MGMT_METHOD_RESP))
  740. continue;
  741. if (ib_query_ah(mad_send_wr->send_buf.ah, &ah_attr))
  742. continue;
  743. if (rmpp_recv->slid == ah_attr.dlid) {
  744. newwin = rmpp_recv->repwin;
  745. break;
  746. }
  747. }
  748. spin_unlock_irqrestore(&agent->lock, flags);
  749. out:
  750. return newwin;
  751. }
  752. int ib_send_rmpp_mad(struct ib_mad_send_wr_private *mad_send_wr)
  753. {
  754. struct ib_rmpp_mad *rmpp_mad;
  755. int ret;
  756. rmpp_mad = mad_send_wr->send_buf.mad;
  757. if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
  758. IB_MGMT_RMPP_FLAG_ACTIVE))
  759. return IB_RMPP_RESULT_UNHANDLED;
  760. if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA) {
  761. mad_send_wr->seg_num = 1;
  762. return IB_RMPP_RESULT_INTERNAL;
  763. }
  764. mad_send_wr->newwin = init_newwin(mad_send_wr);
  765. /* We need to wait for the final ACK even if there isn't a response */
  766. mad_send_wr->refcount += (mad_send_wr->timeout == 0);
  767. ret = send_next_seg(mad_send_wr);
  768. if (!ret)
  769. return IB_RMPP_RESULT_CONSUMED;
  770. return ret;
  771. }
  772. int ib_process_rmpp_send_wc(struct ib_mad_send_wr_private *mad_send_wr,
  773. struct ib_mad_send_wc *mad_send_wc)
  774. {
  775. struct ib_rmpp_mad *rmpp_mad;
  776. int ret;
  777. rmpp_mad = mad_send_wr->send_buf.mad;
  778. if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
  779. IB_MGMT_RMPP_FLAG_ACTIVE))
  780. return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
  781. if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA)
  782. return IB_RMPP_RESULT_INTERNAL; /* ACK, STOP, or ABORT */
  783. if (mad_send_wc->status != IB_WC_SUCCESS ||
  784. mad_send_wr->status != IB_WC_SUCCESS)
  785. return IB_RMPP_RESULT_PROCESSED; /* Canceled or send error */
  786. if (!mad_send_wr->timeout)
  787. return IB_RMPP_RESULT_PROCESSED; /* Response received */
  788. if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) {
  789. mad_send_wr->timeout =
  790. msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
  791. return IB_RMPP_RESULT_PROCESSED; /* Send done */
  792. }
  793. if (mad_send_wr->seg_num == mad_send_wr->newwin ||
  794. mad_send_wr->seg_num == mad_send_wr->send_buf.seg_count)
  795. return IB_RMPP_RESULT_PROCESSED; /* Wait for ACK */
  796. ret = send_next_seg(mad_send_wr);
  797. if (ret) {
  798. mad_send_wc->status = IB_WC_GENERAL_ERR;
  799. return IB_RMPP_RESULT_PROCESSED;
  800. }
  801. return IB_RMPP_RESULT_CONSUMED;
  802. }
  803. int ib_retry_rmpp(struct ib_mad_send_wr_private *mad_send_wr)
  804. {
  805. struct ib_rmpp_mad *rmpp_mad;
  806. int ret;
  807. rmpp_mad = mad_send_wr->send_buf.mad;
  808. if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
  809. IB_MGMT_RMPP_FLAG_ACTIVE))
  810. return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
  811. if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count)
  812. return IB_RMPP_RESULT_PROCESSED;
  813. mad_send_wr->seg_num = mad_send_wr->last_ack;
  814. mad_send_wr->cur_seg = mad_send_wr->last_ack_seg;
  815. ret = send_next_seg(mad_send_wr);
  816. if (ret)
  817. return IB_RMPP_RESULT_PROCESSED;
  818. return IB_RMPP_RESULT_CONSUMED;
  819. }