mad_rmpp.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. * $Id: mad_rmpp.c 1921 2005-03-02 22:58:44Z sean.hefty $
  34. */
  35. #include <linux/dma-mapping.h>
  36. #include "mad_priv.h"
  37. #include "mad_rmpp.h"
  38. enum rmpp_state {
  39. RMPP_STATE_ACTIVE,
  40. RMPP_STATE_TIMEOUT,
  41. RMPP_STATE_COMPLETE
  42. };
  43. struct mad_rmpp_recv {
  44. struct ib_mad_agent_private *agent;
  45. struct list_head list;
  46. struct work_struct timeout_work;
  47. struct work_struct cleanup_work;
  48. wait_queue_head_t wait;
  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. __be64 tid;
  59. u32 src_qp;
  60. u16 slid;
  61. u8 mgmt_class;
  62. u8 class_version;
  63. u8 method;
  64. };
  65. static void destroy_rmpp_recv(struct mad_rmpp_recv *rmpp_recv)
  66. {
  67. atomic_dec(&rmpp_recv->refcount);
  68. wait_event(rmpp_recv->wait, !atomic_read(&rmpp_recv->refcount));
  69. ib_destroy_ah(rmpp_recv->ah);
  70. kfree(rmpp_recv);
  71. }
  72. void ib_cancel_rmpp_recvs(struct ib_mad_agent_private *agent)
  73. {
  74. struct mad_rmpp_recv *rmpp_recv, *temp_rmpp_recv;
  75. unsigned long flags;
  76. spin_lock_irqsave(&agent->lock, flags);
  77. list_for_each_entry(rmpp_recv, &agent->rmpp_list, list) {
  78. cancel_delayed_work(&rmpp_recv->timeout_work);
  79. cancel_delayed_work(&rmpp_recv->cleanup_work);
  80. }
  81. spin_unlock_irqrestore(&agent->lock, flags);
  82. flush_workqueue(agent->qp_info->port_priv->wq);
  83. list_for_each_entry_safe(rmpp_recv, temp_rmpp_recv,
  84. &agent->rmpp_list, list) {
  85. list_del(&rmpp_recv->list);
  86. if (rmpp_recv->state != RMPP_STATE_COMPLETE)
  87. ib_free_recv_mad(rmpp_recv->rmpp_wc);
  88. destroy_rmpp_recv(rmpp_recv);
  89. }
  90. }
  91. static void format_ack(struct ib_mad_send_buf *msg,
  92. struct ib_rmpp_mad *data,
  93. struct mad_rmpp_recv *rmpp_recv)
  94. {
  95. struct ib_rmpp_mad *ack = msg->mad;
  96. unsigned long flags;
  97. memcpy(ack, &data->mad_hdr, msg->hdr_len);
  98. ack->mad_hdr.method ^= IB_MGMT_METHOD_RESP;
  99. ack->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_ACK;
  100. ib_set_rmpp_flags(&ack->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
  101. spin_lock_irqsave(&rmpp_recv->lock, flags);
  102. rmpp_recv->last_ack = rmpp_recv->seg_num;
  103. ack->rmpp_hdr.seg_num = cpu_to_be32(rmpp_recv->seg_num);
  104. ack->rmpp_hdr.paylen_newwin = cpu_to_be32(rmpp_recv->newwin);
  105. spin_unlock_irqrestore(&rmpp_recv->lock, flags);
  106. }
  107. static void ack_recv(struct mad_rmpp_recv *rmpp_recv,
  108. struct ib_mad_recv_wc *recv_wc)
  109. {
  110. struct ib_mad_send_buf *msg;
  111. int ret, hdr_len;
  112. hdr_len = ib_get_mad_data_offset(recv_wc->recv_buf.mad->mad_hdr.mgmt_class);
  113. msg = ib_create_send_mad(&rmpp_recv->agent->agent, recv_wc->wc->src_qp,
  114. recv_wc->wc->pkey_index, 1, hdr_len,
  115. 0, GFP_KERNEL);
  116. if (!msg)
  117. return;
  118. format_ack(msg, (struct ib_rmpp_mad *) recv_wc->recv_buf.mad, rmpp_recv);
  119. msg->ah = rmpp_recv->ah;
  120. ret = ib_post_send_mad(msg, NULL);
  121. if (ret)
  122. ib_free_send_mad(msg);
  123. }
  124. static struct ib_mad_send_buf *alloc_response_msg(struct ib_mad_agent *agent,
  125. struct ib_mad_recv_wc *recv_wc)
  126. {
  127. struct ib_mad_send_buf *msg;
  128. struct ib_ah *ah;
  129. int hdr_len;
  130. ah = ib_create_ah_from_wc(agent->qp->pd, recv_wc->wc,
  131. recv_wc->recv_buf.grh, agent->port_num);
  132. if (IS_ERR(ah))
  133. return (void *) ah;
  134. hdr_len = ib_get_mad_data_offset(recv_wc->recv_buf.mad->mad_hdr.mgmt_class);
  135. msg = ib_create_send_mad(agent, recv_wc->wc->src_qp,
  136. recv_wc->wc->pkey_index, 1,
  137. hdr_len, 0, GFP_KERNEL);
  138. if (IS_ERR(msg))
  139. ib_destroy_ah(ah);
  140. else
  141. msg->ah = ah;
  142. return msg;
  143. }
  144. void ib_rmpp_send_handler(struct ib_mad_send_wc *mad_send_wc)
  145. {
  146. struct ib_rmpp_mad *rmpp_mad = mad_send_wc->send_buf->mad;
  147. if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_ACK)
  148. ib_destroy_ah(mad_send_wc->send_buf->ah);
  149. ib_free_send_mad(mad_send_wc->send_buf);
  150. }
  151. static void nack_recv(struct ib_mad_agent_private *agent,
  152. struct ib_mad_recv_wc *recv_wc, u8 rmpp_status)
  153. {
  154. struct ib_mad_send_buf *msg;
  155. struct ib_rmpp_mad *rmpp_mad;
  156. int ret;
  157. msg = alloc_response_msg(&agent->agent, recv_wc);
  158. if (IS_ERR(msg))
  159. return;
  160. rmpp_mad = msg->mad;
  161. memcpy(rmpp_mad, recv_wc->recv_buf.mad, msg->hdr_len);
  162. rmpp_mad->mad_hdr.method ^= IB_MGMT_METHOD_RESP;
  163. rmpp_mad->rmpp_hdr.rmpp_version = IB_MGMT_RMPP_VERSION;
  164. rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_ABORT;
  165. ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
  166. rmpp_mad->rmpp_hdr.rmpp_status = rmpp_status;
  167. rmpp_mad->rmpp_hdr.seg_num = 0;
  168. rmpp_mad->rmpp_hdr.paylen_newwin = 0;
  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. static void recv_timeout_handler(void *data)
  176. {
  177. struct mad_rmpp_recv *rmpp_recv = data;
  178. struct ib_mad_recv_wc *rmpp_wc;
  179. unsigned long flags;
  180. spin_lock_irqsave(&rmpp_recv->agent->lock, flags);
  181. if (rmpp_recv->state != RMPP_STATE_ACTIVE) {
  182. spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
  183. return;
  184. }
  185. rmpp_recv->state = RMPP_STATE_TIMEOUT;
  186. list_del(&rmpp_recv->list);
  187. spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
  188. rmpp_wc = rmpp_recv->rmpp_wc;
  189. nack_recv(rmpp_recv->agent, rmpp_wc, IB_MGMT_RMPP_STATUS_T2L);
  190. destroy_rmpp_recv(rmpp_recv);
  191. ib_free_recv_mad(rmpp_wc);
  192. }
  193. static void recv_cleanup_handler(void *data)
  194. {
  195. struct mad_rmpp_recv *rmpp_recv = data;
  196. unsigned long flags;
  197. spin_lock_irqsave(&rmpp_recv->agent->lock, flags);
  198. list_del(&rmpp_recv->list);
  199. spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
  200. destroy_rmpp_recv(rmpp_recv);
  201. }
  202. static struct mad_rmpp_recv *
  203. create_rmpp_recv(struct ib_mad_agent_private *agent,
  204. struct ib_mad_recv_wc *mad_recv_wc)
  205. {
  206. struct mad_rmpp_recv *rmpp_recv;
  207. struct ib_mad_hdr *mad_hdr;
  208. rmpp_recv = kmalloc(sizeof *rmpp_recv, GFP_KERNEL);
  209. if (!rmpp_recv)
  210. return NULL;
  211. rmpp_recv->ah = ib_create_ah_from_wc(agent->agent.qp->pd,
  212. mad_recv_wc->wc,
  213. mad_recv_wc->recv_buf.grh,
  214. agent->agent.port_num);
  215. if (IS_ERR(rmpp_recv->ah))
  216. goto error;
  217. rmpp_recv->agent = agent;
  218. init_waitqueue_head(&rmpp_recv->wait);
  219. INIT_WORK(&rmpp_recv->timeout_work, recv_timeout_handler, rmpp_recv);
  220. INIT_WORK(&rmpp_recv->cleanup_work, recv_cleanup_handler, rmpp_recv);
  221. spin_lock_init(&rmpp_recv->lock);
  222. rmpp_recv->state = RMPP_STATE_ACTIVE;
  223. atomic_set(&rmpp_recv->refcount, 1);
  224. rmpp_recv->rmpp_wc = mad_recv_wc;
  225. rmpp_recv->cur_seg_buf = &mad_recv_wc->recv_buf;
  226. rmpp_recv->newwin = 1;
  227. rmpp_recv->seg_num = 1;
  228. rmpp_recv->last_ack = 0;
  229. mad_hdr = &mad_recv_wc->recv_buf.mad->mad_hdr;
  230. rmpp_recv->tid = mad_hdr->tid;
  231. rmpp_recv->src_qp = mad_recv_wc->wc->src_qp;
  232. rmpp_recv->slid = mad_recv_wc->wc->slid;
  233. rmpp_recv->mgmt_class = mad_hdr->mgmt_class;
  234. rmpp_recv->class_version = mad_hdr->class_version;
  235. rmpp_recv->method = mad_hdr->method;
  236. return rmpp_recv;
  237. error: kfree(rmpp_recv);
  238. return NULL;
  239. }
  240. static inline void deref_rmpp_recv(struct mad_rmpp_recv *rmpp_recv)
  241. {
  242. if (atomic_dec_and_test(&rmpp_recv->refcount))
  243. wake_up(&rmpp_recv->wait);
  244. }
  245. static struct mad_rmpp_recv *
  246. find_rmpp_recv(struct ib_mad_agent_private *agent,
  247. struct ib_mad_recv_wc *mad_recv_wc)
  248. {
  249. struct mad_rmpp_recv *rmpp_recv;
  250. struct ib_mad_hdr *mad_hdr = &mad_recv_wc->recv_buf.mad->mad_hdr;
  251. list_for_each_entry(rmpp_recv, &agent->rmpp_list, list) {
  252. if (rmpp_recv->tid == mad_hdr->tid &&
  253. rmpp_recv->src_qp == mad_recv_wc->wc->src_qp &&
  254. rmpp_recv->slid == mad_recv_wc->wc->slid &&
  255. rmpp_recv->mgmt_class == mad_hdr->mgmt_class &&
  256. rmpp_recv->class_version == mad_hdr->class_version &&
  257. rmpp_recv->method == mad_hdr->method)
  258. return rmpp_recv;
  259. }
  260. return NULL;
  261. }
  262. static struct mad_rmpp_recv *
  263. acquire_rmpp_recv(struct ib_mad_agent_private *agent,
  264. struct ib_mad_recv_wc *mad_recv_wc)
  265. {
  266. struct mad_rmpp_recv *rmpp_recv;
  267. unsigned long flags;
  268. spin_lock_irqsave(&agent->lock, flags);
  269. rmpp_recv = find_rmpp_recv(agent, mad_recv_wc);
  270. if (rmpp_recv)
  271. atomic_inc(&rmpp_recv->refcount);
  272. spin_unlock_irqrestore(&agent->lock, flags);
  273. return rmpp_recv;
  274. }
  275. static struct mad_rmpp_recv *
  276. insert_rmpp_recv(struct ib_mad_agent_private *agent,
  277. struct mad_rmpp_recv *rmpp_recv)
  278. {
  279. struct mad_rmpp_recv *cur_rmpp_recv;
  280. cur_rmpp_recv = find_rmpp_recv(agent, rmpp_recv->rmpp_wc);
  281. if (!cur_rmpp_recv)
  282. list_add_tail(&rmpp_recv->list, &agent->rmpp_list);
  283. return cur_rmpp_recv;
  284. }
  285. static inline int get_last_flag(struct ib_mad_recv_buf *seg)
  286. {
  287. struct ib_rmpp_mad *rmpp_mad;
  288. rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
  289. return ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & IB_MGMT_RMPP_FLAG_LAST;
  290. }
  291. static inline int get_seg_num(struct ib_mad_recv_buf *seg)
  292. {
  293. struct ib_rmpp_mad *rmpp_mad;
  294. rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
  295. return be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
  296. }
  297. static inline struct ib_mad_recv_buf * get_next_seg(struct list_head *rmpp_list,
  298. struct ib_mad_recv_buf *seg)
  299. {
  300. if (seg->list.next == rmpp_list)
  301. return NULL;
  302. return container_of(seg->list.next, struct ib_mad_recv_buf, list);
  303. }
  304. static inline int window_size(struct ib_mad_agent_private *agent)
  305. {
  306. return max(agent->qp_info->recv_queue.max_active >> 3, 1);
  307. }
  308. static struct ib_mad_recv_buf * find_seg_location(struct list_head *rmpp_list,
  309. int seg_num)
  310. {
  311. struct ib_mad_recv_buf *seg_buf;
  312. int cur_seg_num;
  313. list_for_each_entry_reverse(seg_buf, rmpp_list, list) {
  314. cur_seg_num = get_seg_num(seg_buf);
  315. if (seg_num > cur_seg_num)
  316. return seg_buf;
  317. if (seg_num == cur_seg_num)
  318. break;
  319. }
  320. return NULL;
  321. }
  322. static void update_seg_num(struct mad_rmpp_recv *rmpp_recv,
  323. struct ib_mad_recv_buf *new_buf)
  324. {
  325. struct list_head *rmpp_list = &rmpp_recv->rmpp_wc->rmpp_list;
  326. while (new_buf && (get_seg_num(new_buf) == rmpp_recv->seg_num + 1)) {
  327. rmpp_recv->cur_seg_buf = new_buf;
  328. rmpp_recv->seg_num++;
  329. new_buf = get_next_seg(rmpp_list, new_buf);
  330. }
  331. }
  332. static inline int get_mad_len(struct mad_rmpp_recv *rmpp_recv)
  333. {
  334. struct ib_rmpp_mad *rmpp_mad;
  335. int hdr_size, data_size, pad;
  336. rmpp_mad = (struct ib_rmpp_mad *)rmpp_recv->cur_seg_buf->mad;
  337. hdr_size = ib_get_mad_data_offset(rmpp_mad->mad_hdr.mgmt_class);
  338. data_size = sizeof(struct ib_rmpp_mad) - hdr_size;
  339. pad = IB_MGMT_RMPP_DATA - be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
  340. if (pad > IB_MGMT_RMPP_DATA || pad < 0)
  341. pad = 0;
  342. return hdr_size + rmpp_recv->seg_num * data_size - pad;
  343. }
  344. static struct ib_mad_recv_wc * complete_rmpp(struct mad_rmpp_recv *rmpp_recv)
  345. {
  346. struct ib_mad_recv_wc *rmpp_wc;
  347. ack_recv(rmpp_recv, rmpp_recv->rmpp_wc);
  348. if (rmpp_recv->seg_num > 1)
  349. cancel_delayed_work(&rmpp_recv->timeout_work);
  350. rmpp_wc = rmpp_recv->rmpp_wc;
  351. rmpp_wc->mad_len = get_mad_len(rmpp_recv);
  352. /* 10 seconds until we can find the packet lifetime */
  353. queue_delayed_work(rmpp_recv->agent->qp_info->port_priv->wq,
  354. &rmpp_recv->cleanup_work, msecs_to_jiffies(10000));
  355. return rmpp_wc;
  356. }
  357. static struct ib_mad_recv_wc *
  358. continue_rmpp(struct ib_mad_agent_private *agent,
  359. struct ib_mad_recv_wc *mad_recv_wc)
  360. {
  361. struct mad_rmpp_recv *rmpp_recv;
  362. struct ib_mad_recv_buf *prev_buf;
  363. struct ib_mad_recv_wc *done_wc;
  364. int seg_num;
  365. unsigned long flags;
  366. rmpp_recv = acquire_rmpp_recv(agent, mad_recv_wc);
  367. if (!rmpp_recv)
  368. goto drop1;
  369. seg_num = get_seg_num(&mad_recv_wc->recv_buf);
  370. spin_lock_irqsave(&rmpp_recv->lock, flags);
  371. if ((rmpp_recv->state == RMPP_STATE_TIMEOUT) ||
  372. (seg_num > rmpp_recv->newwin))
  373. goto drop3;
  374. if ((seg_num <= rmpp_recv->last_ack) ||
  375. (rmpp_recv->state == RMPP_STATE_COMPLETE)) {
  376. spin_unlock_irqrestore(&rmpp_recv->lock, flags);
  377. ack_recv(rmpp_recv, mad_recv_wc);
  378. goto drop2;
  379. }
  380. prev_buf = find_seg_location(&rmpp_recv->rmpp_wc->rmpp_list, seg_num);
  381. if (!prev_buf)
  382. goto drop3;
  383. done_wc = NULL;
  384. list_add(&mad_recv_wc->recv_buf.list, &prev_buf->list);
  385. if (rmpp_recv->cur_seg_buf == prev_buf) {
  386. update_seg_num(rmpp_recv, &mad_recv_wc->recv_buf);
  387. if (get_last_flag(rmpp_recv->cur_seg_buf)) {
  388. rmpp_recv->state = RMPP_STATE_COMPLETE;
  389. spin_unlock_irqrestore(&rmpp_recv->lock, flags);
  390. done_wc = complete_rmpp(rmpp_recv);
  391. goto out;
  392. } else if (rmpp_recv->seg_num == rmpp_recv->newwin) {
  393. rmpp_recv->newwin += window_size(agent);
  394. spin_unlock_irqrestore(&rmpp_recv->lock, flags);
  395. ack_recv(rmpp_recv, mad_recv_wc);
  396. goto out;
  397. }
  398. }
  399. spin_unlock_irqrestore(&rmpp_recv->lock, flags);
  400. out:
  401. deref_rmpp_recv(rmpp_recv);
  402. return done_wc;
  403. drop3: spin_unlock_irqrestore(&rmpp_recv->lock, flags);
  404. drop2: deref_rmpp_recv(rmpp_recv);
  405. drop1: ib_free_recv_mad(mad_recv_wc);
  406. return NULL;
  407. }
  408. static struct ib_mad_recv_wc *
  409. start_rmpp(struct ib_mad_agent_private *agent,
  410. struct ib_mad_recv_wc *mad_recv_wc)
  411. {
  412. struct mad_rmpp_recv *rmpp_recv;
  413. unsigned long flags;
  414. rmpp_recv = create_rmpp_recv(agent, mad_recv_wc);
  415. if (!rmpp_recv) {
  416. ib_free_recv_mad(mad_recv_wc);
  417. return NULL;
  418. }
  419. spin_lock_irqsave(&agent->lock, flags);
  420. if (insert_rmpp_recv(agent, rmpp_recv)) {
  421. spin_unlock_irqrestore(&agent->lock, flags);
  422. /* duplicate first MAD */
  423. destroy_rmpp_recv(rmpp_recv);
  424. return continue_rmpp(agent, mad_recv_wc);
  425. }
  426. atomic_inc(&rmpp_recv->refcount);
  427. if (get_last_flag(&mad_recv_wc->recv_buf)) {
  428. rmpp_recv->state = RMPP_STATE_COMPLETE;
  429. spin_unlock_irqrestore(&agent->lock, flags);
  430. complete_rmpp(rmpp_recv);
  431. } else {
  432. spin_unlock_irqrestore(&agent->lock, flags);
  433. /* 40 seconds until we can find the packet lifetimes */
  434. queue_delayed_work(agent->qp_info->port_priv->wq,
  435. &rmpp_recv->timeout_work,
  436. msecs_to_jiffies(40000));
  437. rmpp_recv->newwin += window_size(agent);
  438. ack_recv(rmpp_recv, mad_recv_wc);
  439. mad_recv_wc = NULL;
  440. }
  441. deref_rmpp_recv(rmpp_recv);
  442. return mad_recv_wc;
  443. }
  444. static int send_next_seg(struct ib_mad_send_wr_private *mad_send_wr)
  445. {
  446. struct ib_rmpp_mad *rmpp_mad;
  447. int timeout;
  448. u32 paylen = 0;
  449. rmpp_mad = mad_send_wr->send_buf.mad;
  450. ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
  451. rmpp_mad->rmpp_hdr.seg_num = cpu_to_be32(++mad_send_wr->seg_num);
  452. if (mad_send_wr->seg_num == 1) {
  453. rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_FIRST;
  454. paylen = mad_send_wr->send_buf.seg_count * IB_MGMT_RMPP_DATA -
  455. mad_send_wr->pad;
  456. }
  457. if (mad_send_wr->seg_num == mad_send_wr->send_buf.seg_count) {
  458. rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_LAST;
  459. paylen = IB_MGMT_RMPP_DATA - mad_send_wr->pad;
  460. }
  461. rmpp_mad->rmpp_hdr.paylen_newwin = cpu_to_be32(paylen);
  462. /* 2 seconds for an ACK until we can find the packet lifetime */
  463. timeout = mad_send_wr->send_buf.timeout_ms;
  464. if (!timeout || timeout > 2000)
  465. mad_send_wr->timeout = msecs_to_jiffies(2000);
  466. return ib_send_mad(mad_send_wr);
  467. }
  468. static void abort_send(struct ib_mad_agent_private *agent,
  469. struct ib_mad_recv_wc *mad_recv_wc, u8 rmpp_status)
  470. {
  471. struct ib_mad_send_wr_private *mad_send_wr;
  472. struct ib_mad_send_wc wc;
  473. unsigned long flags;
  474. spin_lock_irqsave(&agent->lock, flags);
  475. mad_send_wr = ib_find_send_mad(agent, mad_recv_wc);
  476. if (!mad_send_wr)
  477. goto out; /* Unmatched send */
  478. if ((mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) ||
  479. (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
  480. goto out; /* Send is already done */
  481. ib_mark_mad_done(mad_send_wr);
  482. spin_unlock_irqrestore(&agent->lock, flags);
  483. wc.status = IB_WC_REM_ABORT_ERR;
  484. wc.vendor_err = rmpp_status;
  485. wc.send_buf = &mad_send_wr->send_buf;
  486. ib_mad_complete_send_wr(mad_send_wr, &wc);
  487. return;
  488. out:
  489. spin_unlock_irqrestore(&agent->lock, flags);
  490. }
  491. static inline void adjust_last_ack(struct ib_mad_send_wr_private *wr,
  492. int seg_num)
  493. {
  494. struct list_head *list;
  495. wr->last_ack = seg_num;
  496. list = &wr->last_ack_seg->list;
  497. list_for_each_entry(wr->last_ack_seg, list, list)
  498. if (wr->last_ack_seg->num == seg_num)
  499. break;
  500. }
  501. static void process_rmpp_ack(struct ib_mad_agent_private *agent,
  502. struct ib_mad_recv_wc *mad_recv_wc)
  503. {
  504. struct ib_mad_send_wr_private *mad_send_wr;
  505. struct ib_rmpp_mad *rmpp_mad;
  506. unsigned long flags;
  507. int seg_num, newwin, ret;
  508. rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
  509. if (rmpp_mad->rmpp_hdr.rmpp_status) {
  510. abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
  511. nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
  512. return;
  513. }
  514. seg_num = be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
  515. newwin = be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
  516. if (newwin < seg_num) {
  517. abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_W2S);
  518. nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_W2S);
  519. return;
  520. }
  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 ACK */
  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. if (seg_num > mad_send_wr->send_buf.seg_count ||
  529. seg_num > mad_send_wr->newwin) {
  530. spin_unlock_irqrestore(&agent->lock, flags);
  531. abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_S2B);
  532. nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_S2B);
  533. return;
  534. }
  535. if (newwin < mad_send_wr->newwin || seg_num < mad_send_wr->last_ack)
  536. goto out; /* Old ACK */
  537. if (seg_num > mad_send_wr->last_ack) {
  538. adjust_last_ack(mad_send_wr, seg_num);
  539. mad_send_wr->retries = mad_send_wr->send_buf.retries;
  540. }
  541. mad_send_wr->newwin = newwin;
  542. if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) {
  543. /* If no response is expected, the ACK completes the send */
  544. if (!mad_send_wr->send_buf.timeout_ms) {
  545. struct ib_mad_send_wc wc;
  546. ib_mark_mad_done(mad_send_wr);
  547. spin_unlock_irqrestore(&agent->lock, flags);
  548. wc.status = IB_WC_SUCCESS;
  549. wc.vendor_err = 0;
  550. wc.send_buf = &mad_send_wr->send_buf;
  551. ib_mad_complete_send_wr(mad_send_wr, &wc);
  552. return;
  553. }
  554. if (mad_send_wr->refcount == 1)
  555. ib_reset_mad_timeout(mad_send_wr,
  556. mad_send_wr->send_buf.timeout_ms);
  557. } else if (mad_send_wr->refcount == 1 &&
  558. mad_send_wr->seg_num < mad_send_wr->newwin &&
  559. mad_send_wr->seg_num < mad_send_wr->send_buf.seg_count) {
  560. /* Send failure will just result in a timeout/retry */
  561. ret = send_next_seg(mad_send_wr);
  562. if (ret)
  563. goto out;
  564. mad_send_wr->refcount++;
  565. list_del(&mad_send_wr->agent_list);
  566. list_add_tail(&mad_send_wr->agent_list,
  567. &mad_send_wr->mad_agent_priv->send_list);
  568. }
  569. out:
  570. spin_unlock_irqrestore(&agent->lock, flags);
  571. }
  572. static struct ib_mad_recv_wc *
  573. process_rmpp_data(struct ib_mad_agent_private *agent,
  574. struct ib_mad_recv_wc *mad_recv_wc)
  575. {
  576. struct ib_rmpp_hdr *rmpp_hdr;
  577. u8 rmpp_status;
  578. rmpp_hdr = &((struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad)->rmpp_hdr;
  579. if (rmpp_hdr->rmpp_status) {
  580. rmpp_status = IB_MGMT_RMPP_STATUS_BAD_STATUS;
  581. goto bad;
  582. }
  583. if (rmpp_hdr->seg_num == __constant_htonl(1)) {
  584. if (!(ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST)) {
  585. rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
  586. goto bad;
  587. }
  588. return start_rmpp(agent, mad_recv_wc);
  589. } else {
  590. if (ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST) {
  591. rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
  592. goto bad;
  593. }
  594. return continue_rmpp(agent, mad_recv_wc);
  595. }
  596. bad:
  597. nack_recv(agent, mad_recv_wc, rmpp_status);
  598. ib_free_recv_mad(mad_recv_wc);
  599. return NULL;
  600. }
  601. static void process_rmpp_stop(struct ib_mad_agent_private *agent,
  602. struct ib_mad_recv_wc *mad_recv_wc)
  603. {
  604. struct ib_rmpp_mad *rmpp_mad;
  605. rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
  606. if (rmpp_mad->rmpp_hdr.rmpp_status != IB_MGMT_RMPP_STATUS_RESX) {
  607. abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
  608. nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
  609. } else
  610. abort_send(agent, mad_recv_wc, rmpp_mad->rmpp_hdr.rmpp_status);
  611. }
  612. static void process_rmpp_abort(struct ib_mad_agent_private *agent,
  613. struct ib_mad_recv_wc *mad_recv_wc)
  614. {
  615. struct ib_rmpp_mad *rmpp_mad;
  616. rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
  617. if (rmpp_mad->rmpp_hdr.rmpp_status < IB_MGMT_RMPP_STATUS_ABORT_MIN ||
  618. rmpp_mad->rmpp_hdr.rmpp_status > IB_MGMT_RMPP_STATUS_ABORT_MAX) {
  619. abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
  620. nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
  621. } else
  622. abort_send(agent, mad_recv_wc, rmpp_mad->rmpp_hdr.rmpp_status);
  623. }
  624. struct ib_mad_recv_wc *
  625. ib_process_rmpp_recv_wc(struct ib_mad_agent_private *agent,
  626. struct ib_mad_recv_wc *mad_recv_wc)
  627. {
  628. struct ib_rmpp_mad *rmpp_mad;
  629. rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
  630. if (!(rmpp_mad->rmpp_hdr.rmpp_rtime_flags & IB_MGMT_RMPP_FLAG_ACTIVE))
  631. return mad_recv_wc;
  632. if (rmpp_mad->rmpp_hdr.rmpp_version != IB_MGMT_RMPP_VERSION) {
  633. abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_UNV);
  634. nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_UNV);
  635. goto out;
  636. }
  637. switch (rmpp_mad->rmpp_hdr.rmpp_type) {
  638. case IB_MGMT_RMPP_TYPE_DATA:
  639. return process_rmpp_data(agent, mad_recv_wc);
  640. case IB_MGMT_RMPP_TYPE_ACK:
  641. process_rmpp_ack(agent, mad_recv_wc);
  642. break;
  643. case IB_MGMT_RMPP_TYPE_STOP:
  644. process_rmpp_stop(agent, mad_recv_wc);
  645. break;
  646. case IB_MGMT_RMPP_TYPE_ABORT:
  647. process_rmpp_abort(agent, mad_recv_wc);
  648. break;
  649. default:
  650. abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BADT);
  651. nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BADT);
  652. break;
  653. }
  654. out:
  655. ib_free_recv_mad(mad_recv_wc);
  656. return NULL;
  657. }
  658. int ib_send_rmpp_mad(struct ib_mad_send_wr_private *mad_send_wr)
  659. {
  660. struct ib_rmpp_mad *rmpp_mad;
  661. int ret;
  662. rmpp_mad = mad_send_wr->send_buf.mad;
  663. if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
  664. IB_MGMT_RMPP_FLAG_ACTIVE))
  665. return IB_RMPP_RESULT_UNHANDLED;
  666. if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA) {
  667. mad_send_wr->seg_num = 1;
  668. return IB_RMPP_RESULT_INTERNAL;
  669. }
  670. mad_send_wr->newwin = 1;
  671. /* We need to wait for the final ACK even if there isn't a response */
  672. mad_send_wr->refcount += (mad_send_wr->timeout == 0);
  673. ret = send_next_seg(mad_send_wr);
  674. if (!ret)
  675. return IB_RMPP_RESULT_CONSUMED;
  676. return ret;
  677. }
  678. int ib_process_rmpp_send_wc(struct ib_mad_send_wr_private *mad_send_wr,
  679. struct ib_mad_send_wc *mad_send_wc)
  680. {
  681. struct ib_rmpp_mad *rmpp_mad;
  682. int ret;
  683. rmpp_mad = mad_send_wr->send_buf.mad;
  684. if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
  685. IB_MGMT_RMPP_FLAG_ACTIVE))
  686. return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
  687. if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA)
  688. return IB_RMPP_RESULT_INTERNAL; /* ACK, STOP, or ABORT */
  689. if (mad_send_wc->status != IB_WC_SUCCESS ||
  690. mad_send_wr->status != IB_WC_SUCCESS)
  691. return IB_RMPP_RESULT_PROCESSED; /* Canceled or send error */
  692. if (!mad_send_wr->timeout)
  693. return IB_RMPP_RESULT_PROCESSED; /* Response received */
  694. if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) {
  695. mad_send_wr->timeout =
  696. msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
  697. return IB_RMPP_RESULT_PROCESSED; /* Send done */
  698. }
  699. if (mad_send_wr->seg_num == mad_send_wr->newwin ||
  700. mad_send_wr->seg_num == mad_send_wr->send_buf.seg_count)
  701. return IB_RMPP_RESULT_PROCESSED; /* Wait for ACK */
  702. ret = send_next_seg(mad_send_wr);
  703. if (ret) {
  704. mad_send_wc->status = IB_WC_GENERAL_ERR;
  705. return IB_RMPP_RESULT_PROCESSED;
  706. }
  707. return IB_RMPP_RESULT_CONSUMED;
  708. }
  709. int ib_retry_rmpp(struct ib_mad_send_wr_private *mad_send_wr)
  710. {
  711. struct ib_rmpp_mad *rmpp_mad;
  712. int ret;
  713. rmpp_mad = mad_send_wr->send_buf.mad;
  714. if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
  715. IB_MGMT_RMPP_FLAG_ACTIVE))
  716. return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
  717. if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count)
  718. return IB_RMPP_RESULT_PROCESSED;
  719. mad_send_wr->seg_num = mad_send_wr->last_ack;
  720. mad_send_wr->cur_seg = mad_send_wr->last_ack_seg;
  721. ret = send_next_seg(mad_send_wr);
  722. if (ret)
  723. return IB_RMPP_RESULT_PROCESSED;
  724. return IB_RMPP_RESULT_CONSUMED;
  725. }