mad_rmpp.c 27 KB

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