mad_rmpp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /*
  2. * Copyright (c) 2005 Intel Inc. All rights reserved.
  3. * Copyright (c) 2005 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. u64 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 recv_timeout_handler(void *data)
  92. {
  93. struct mad_rmpp_recv *rmpp_recv = data;
  94. struct ib_mad_recv_wc *rmpp_wc;
  95. unsigned long flags;
  96. spin_lock_irqsave(&rmpp_recv->agent->lock, flags);
  97. if (rmpp_recv->state != RMPP_STATE_ACTIVE) {
  98. spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
  99. return;
  100. }
  101. rmpp_recv->state = RMPP_STATE_TIMEOUT;
  102. list_del(&rmpp_recv->list);
  103. spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
  104. /* TODO: send abort. */
  105. rmpp_wc = rmpp_recv->rmpp_wc;
  106. destroy_rmpp_recv(rmpp_recv);
  107. ib_free_recv_mad(rmpp_wc);
  108. }
  109. static void recv_cleanup_handler(void *data)
  110. {
  111. struct mad_rmpp_recv *rmpp_recv = data;
  112. unsigned long flags;
  113. spin_lock_irqsave(&rmpp_recv->agent->lock, flags);
  114. list_del(&rmpp_recv->list);
  115. spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
  116. destroy_rmpp_recv(rmpp_recv);
  117. }
  118. static struct mad_rmpp_recv *
  119. create_rmpp_recv(struct ib_mad_agent_private *agent,
  120. struct ib_mad_recv_wc *mad_recv_wc)
  121. {
  122. struct mad_rmpp_recv *rmpp_recv;
  123. struct ib_mad_hdr *mad_hdr;
  124. rmpp_recv = kmalloc(sizeof *rmpp_recv, GFP_KERNEL);
  125. if (!rmpp_recv)
  126. return NULL;
  127. rmpp_recv->ah = ib_create_ah_from_wc(agent->agent.qp->pd,
  128. mad_recv_wc->wc,
  129. mad_recv_wc->recv_buf.grh,
  130. agent->agent.port_num);
  131. if (IS_ERR(rmpp_recv->ah))
  132. goto error;
  133. rmpp_recv->agent = agent;
  134. init_waitqueue_head(&rmpp_recv->wait);
  135. INIT_WORK(&rmpp_recv->timeout_work, recv_timeout_handler, rmpp_recv);
  136. INIT_WORK(&rmpp_recv->cleanup_work, recv_cleanup_handler, rmpp_recv);
  137. spin_lock_init(&rmpp_recv->lock);
  138. rmpp_recv->state = RMPP_STATE_ACTIVE;
  139. atomic_set(&rmpp_recv->refcount, 1);
  140. rmpp_recv->rmpp_wc = mad_recv_wc;
  141. rmpp_recv->cur_seg_buf = &mad_recv_wc->recv_buf;
  142. rmpp_recv->newwin = 1;
  143. rmpp_recv->seg_num = 1;
  144. rmpp_recv->last_ack = 0;
  145. mad_hdr = &mad_recv_wc->recv_buf.mad->mad_hdr;
  146. rmpp_recv->tid = mad_hdr->tid;
  147. rmpp_recv->src_qp = mad_recv_wc->wc->src_qp;
  148. rmpp_recv->slid = mad_recv_wc->wc->slid;
  149. rmpp_recv->mgmt_class = mad_hdr->mgmt_class;
  150. rmpp_recv->class_version = mad_hdr->class_version;
  151. rmpp_recv->method = mad_hdr->method;
  152. return rmpp_recv;
  153. error: kfree(rmpp_recv);
  154. return NULL;
  155. }
  156. static inline void deref_rmpp_recv(struct mad_rmpp_recv *rmpp_recv)
  157. {
  158. if (atomic_dec_and_test(&rmpp_recv->refcount))
  159. wake_up(&rmpp_recv->wait);
  160. }
  161. static struct mad_rmpp_recv *
  162. find_rmpp_recv(struct ib_mad_agent_private *agent,
  163. struct ib_mad_recv_wc *mad_recv_wc)
  164. {
  165. struct mad_rmpp_recv *rmpp_recv;
  166. struct ib_mad_hdr *mad_hdr = &mad_recv_wc->recv_buf.mad->mad_hdr;
  167. list_for_each_entry(rmpp_recv, &agent->rmpp_list, list) {
  168. if (rmpp_recv->tid == mad_hdr->tid &&
  169. rmpp_recv->src_qp == mad_recv_wc->wc->src_qp &&
  170. rmpp_recv->slid == mad_recv_wc->wc->slid &&
  171. rmpp_recv->mgmt_class == mad_hdr->mgmt_class &&
  172. rmpp_recv->class_version == mad_hdr->class_version &&
  173. rmpp_recv->method == mad_hdr->method)
  174. return rmpp_recv;
  175. }
  176. return NULL;
  177. }
  178. static struct mad_rmpp_recv *
  179. acquire_rmpp_recv(struct ib_mad_agent_private *agent,
  180. struct ib_mad_recv_wc *mad_recv_wc)
  181. {
  182. struct mad_rmpp_recv *rmpp_recv;
  183. unsigned long flags;
  184. spin_lock_irqsave(&agent->lock, flags);
  185. rmpp_recv = find_rmpp_recv(agent, mad_recv_wc);
  186. if (rmpp_recv)
  187. atomic_inc(&rmpp_recv->refcount);
  188. spin_unlock_irqrestore(&agent->lock, flags);
  189. return rmpp_recv;
  190. }
  191. static struct mad_rmpp_recv *
  192. insert_rmpp_recv(struct ib_mad_agent_private *agent,
  193. struct mad_rmpp_recv *rmpp_recv)
  194. {
  195. struct mad_rmpp_recv *cur_rmpp_recv;
  196. cur_rmpp_recv = find_rmpp_recv(agent, rmpp_recv->rmpp_wc);
  197. if (!cur_rmpp_recv)
  198. list_add_tail(&rmpp_recv->list, &agent->rmpp_list);
  199. return cur_rmpp_recv;
  200. }
  201. static int data_offset(u8 mgmt_class)
  202. {
  203. if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
  204. return offsetof(struct ib_sa_mad, data);
  205. else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
  206. (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
  207. return offsetof(struct ib_vendor_mad, data);
  208. else
  209. return offsetof(struct ib_rmpp_mad, data);
  210. }
  211. static void format_ack(struct ib_rmpp_mad *ack,
  212. struct ib_rmpp_mad *data,
  213. struct mad_rmpp_recv *rmpp_recv)
  214. {
  215. unsigned long flags;
  216. memcpy(&ack->mad_hdr, &data->mad_hdr,
  217. data_offset(data->mad_hdr.mgmt_class));
  218. ack->mad_hdr.method ^= IB_MGMT_METHOD_RESP;
  219. ack->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_ACK;
  220. ib_set_rmpp_flags(&ack->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
  221. spin_lock_irqsave(&rmpp_recv->lock, flags);
  222. rmpp_recv->last_ack = rmpp_recv->seg_num;
  223. ack->rmpp_hdr.seg_num = cpu_to_be32(rmpp_recv->seg_num);
  224. ack->rmpp_hdr.paylen_newwin = cpu_to_be32(rmpp_recv->newwin);
  225. spin_unlock_irqrestore(&rmpp_recv->lock, flags);
  226. }
  227. static void ack_recv(struct mad_rmpp_recv *rmpp_recv,
  228. struct ib_mad_recv_wc *recv_wc)
  229. {
  230. struct ib_mad_send_buf *msg;
  231. struct ib_send_wr *bad_send_wr;
  232. int hdr_len, ret;
  233. hdr_len = sizeof(struct ib_mad_hdr) + sizeof(struct ib_rmpp_hdr);
  234. msg = ib_create_send_mad(&rmpp_recv->agent->agent, recv_wc->wc->src_qp,
  235. recv_wc->wc->pkey_index, rmpp_recv->ah, 1,
  236. hdr_len, sizeof(struct ib_rmpp_mad) - hdr_len,
  237. GFP_KERNEL);
  238. if (!msg)
  239. return;
  240. format_ack((struct ib_rmpp_mad *) msg->mad,
  241. (struct ib_rmpp_mad *) recv_wc->recv_buf.mad, rmpp_recv);
  242. ret = ib_post_send_mad(&rmpp_recv->agent->agent, &msg->send_wr,
  243. &bad_send_wr);
  244. if (ret)
  245. ib_free_send_mad(msg);
  246. }
  247. static inline int get_last_flag(struct ib_mad_recv_buf *seg)
  248. {
  249. struct ib_rmpp_mad *rmpp_mad;
  250. rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
  251. return ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & IB_MGMT_RMPP_FLAG_LAST;
  252. }
  253. static inline int get_seg_num(struct ib_mad_recv_buf *seg)
  254. {
  255. struct ib_rmpp_mad *rmpp_mad;
  256. rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
  257. return be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
  258. }
  259. static inline struct ib_mad_recv_buf * get_next_seg(struct list_head *rmpp_list,
  260. struct ib_mad_recv_buf *seg)
  261. {
  262. if (seg->list.next == rmpp_list)
  263. return NULL;
  264. return container_of(seg->list.next, struct ib_mad_recv_buf, list);
  265. }
  266. static inline int window_size(struct ib_mad_agent_private *agent)
  267. {
  268. return max(agent->qp_info->recv_queue.max_active >> 3, 1);
  269. }
  270. static struct ib_mad_recv_buf * find_seg_location(struct list_head *rmpp_list,
  271. int seg_num)
  272. {
  273. struct ib_mad_recv_buf *seg_buf;
  274. int cur_seg_num;
  275. list_for_each_entry_reverse(seg_buf, rmpp_list, list) {
  276. cur_seg_num = get_seg_num(seg_buf);
  277. if (seg_num > cur_seg_num)
  278. return seg_buf;
  279. if (seg_num == cur_seg_num)
  280. break;
  281. }
  282. return NULL;
  283. }
  284. static void update_seg_num(struct mad_rmpp_recv *rmpp_recv,
  285. struct ib_mad_recv_buf *new_buf)
  286. {
  287. struct list_head *rmpp_list = &rmpp_recv->rmpp_wc->rmpp_list;
  288. while (new_buf && (get_seg_num(new_buf) == rmpp_recv->seg_num + 1)) {
  289. rmpp_recv->cur_seg_buf = new_buf;
  290. rmpp_recv->seg_num++;
  291. new_buf = get_next_seg(rmpp_list, new_buf);
  292. }
  293. }
  294. static inline int get_mad_len(struct mad_rmpp_recv *rmpp_recv)
  295. {
  296. struct ib_rmpp_mad *rmpp_mad;
  297. int hdr_size, data_size, pad;
  298. rmpp_mad = (struct ib_rmpp_mad *)rmpp_recv->cur_seg_buf->mad;
  299. hdr_size = data_offset(rmpp_mad->mad_hdr.mgmt_class);
  300. data_size = sizeof(struct ib_rmpp_mad) - hdr_size;
  301. pad = data_size - be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
  302. if (pad > data_size || pad < 0)
  303. pad = 0;
  304. return hdr_size + rmpp_recv->seg_num * data_size - pad;
  305. }
  306. static struct ib_mad_recv_wc * complete_rmpp(struct mad_rmpp_recv *rmpp_recv)
  307. {
  308. struct ib_mad_recv_wc *rmpp_wc;
  309. ack_recv(rmpp_recv, rmpp_recv->rmpp_wc);
  310. if (rmpp_recv->seg_num > 1)
  311. cancel_delayed_work(&rmpp_recv->timeout_work);
  312. rmpp_wc = rmpp_recv->rmpp_wc;
  313. rmpp_wc->mad_len = get_mad_len(rmpp_recv);
  314. /* 10 seconds until we can find the packet lifetime */
  315. queue_delayed_work(rmpp_recv->agent->qp_info->port_priv->wq,
  316. &rmpp_recv->cleanup_work, msecs_to_jiffies(10000));
  317. return rmpp_wc;
  318. }
  319. void ib_coalesce_recv_mad(struct ib_mad_recv_wc *mad_recv_wc, void *buf)
  320. {
  321. struct ib_mad_recv_buf *seg_buf;
  322. struct ib_rmpp_mad *rmpp_mad;
  323. void *data;
  324. int size, len, offset;
  325. u8 flags;
  326. len = mad_recv_wc->mad_len;
  327. if (len <= sizeof(struct ib_mad)) {
  328. memcpy(buf, mad_recv_wc->recv_buf.mad, len);
  329. return;
  330. }
  331. offset = data_offset(mad_recv_wc->recv_buf.mad->mad_hdr.mgmt_class);
  332. list_for_each_entry(seg_buf, &mad_recv_wc->rmpp_list, list) {
  333. rmpp_mad = (struct ib_rmpp_mad *)seg_buf->mad;
  334. flags = ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr);
  335. if (flags & IB_MGMT_RMPP_FLAG_FIRST) {
  336. data = rmpp_mad;
  337. size = sizeof(*rmpp_mad);
  338. } else {
  339. data = (void *) rmpp_mad + offset;
  340. if (flags & IB_MGMT_RMPP_FLAG_LAST)
  341. size = len;
  342. else
  343. size = sizeof(*rmpp_mad) - offset;
  344. }
  345. memcpy(buf, data, size);
  346. len -= size;
  347. buf += size;
  348. }
  349. }
  350. EXPORT_SYMBOL(ib_coalesce_recv_mad);
  351. static struct ib_mad_recv_wc *
  352. continue_rmpp(struct ib_mad_agent_private *agent,
  353. struct ib_mad_recv_wc *mad_recv_wc)
  354. {
  355. struct mad_rmpp_recv *rmpp_recv;
  356. struct ib_mad_recv_buf *prev_buf;
  357. struct ib_mad_recv_wc *done_wc;
  358. int seg_num;
  359. unsigned long flags;
  360. rmpp_recv = acquire_rmpp_recv(agent, mad_recv_wc);
  361. if (!rmpp_recv)
  362. goto drop1;
  363. seg_num = get_seg_num(&mad_recv_wc->recv_buf);
  364. spin_lock_irqsave(&rmpp_recv->lock, flags);
  365. if ((rmpp_recv->state == RMPP_STATE_TIMEOUT) ||
  366. (seg_num > rmpp_recv->newwin))
  367. goto drop3;
  368. if ((seg_num <= rmpp_recv->last_ack) ||
  369. (rmpp_recv->state == RMPP_STATE_COMPLETE)) {
  370. spin_unlock_irqrestore(&rmpp_recv->lock, flags);
  371. ack_recv(rmpp_recv, mad_recv_wc);
  372. goto drop2;
  373. }
  374. prev_buf = find_seg_location(&rmpp_recv->rmpp_wc->rmpp_list, seg_num);
  375. if (!prev_buf)
  376. goto drop3;
  377. done_wc = NULL;
  378. list_add(&mad_recv_wc->recv_buf.list, &prev_buf->list);
  379. if (rmpp_recv->cur_seg_buf == prev_buf) {
  380. update_seg_num(rmpp_recv, &mad_recv_wc->recv_buf);
  381. if (get_last_flag(rmpp_recv->cur_seg_buf)) {
  382. rmpp_recv->state = RMPP_STATE_COMPLETE;
  383. spin_unlock_irqrestore(&rmpp_recv->lock, flags);
  384. done_wc = complete_rmpp(rmpp_recv);
  385. goto out;
  386. } else if (rmpp_recv->seg_num == rmpp_recv->newwin) {
  387. rmpp_recv->newwin += window_size(agent);
  388. spin_unlock_irqrestore(&rmpp_recv->lock, flags);
  389. ack_recv(rmpp_recv, mad_recv_wc);
  390. goto out;
  391. }
  392. }
  393. spin_unlock_irqrestore(&rmpp_recv->lock, flags);
  394. out:
  395. deref_rmpp_recv(rmpp_recv);
  396. return done_wc;
  397. drop3: spin_unlock_irqrestore(&rmpp_recv->lock, flags);
  398. drop2: deref_rmpp_recv(rmpp_recv);
  399. drop1: ib_free_recv_mad(mad_recv_wc);
  400. return NULL;
  401. }
  402. static struct ib_mad_recv_wc *
  403. start_rmpp(struct ib_mad_agent_private *agent,
  404. struct ib_mad_recv_wc *mad_recv_wc)
  405. {
  406. struct mad_rmpp_recv *rmpp_recv;
  407. unsigned long flags;
  408. rmpp_recv = create_rmpp_recv(agent, mad_recv_wc);
  409. if (!rmpp_recv) {
  410. ib_free_recv_mad(mad_recv_wc);
  411. return NULL;
  412. }
  413. spin_lock_irqsave(&agent->lock, flags);
  414. if (insert_rmpp_recv(agent, rmpp_recv)) {
  415. spin_unlock_irqrestore(&agent->lock, flags);
  416. /* duplicate first MAD */
  417. destroy_rmpp_recv(rmpp_recv);
  418. return continue_rmpp(agent, mad_recv_wc);
  419. }
  420. atomic_inc(&rmpp_recv->refcount);
  421. if (get_last_flag(&mad_recv_wc->recv_buf)) {
  422. rmpp_recv->state = RMPP_STATE_COMPLETE;
  423. spin_unlock_irqrestore(&agent->lock, flags);
  424. complete_rmpp(rmpp_recv);
  425. } else {
  426. spin_unlock_irqrestore(&agent->lock, flags);
  427. /* 40 seconds until we can find the packet lifetimes */
  428. queue_delayed_work(agent->qp_info->port_priv->wq,
  429. &rmpp_recv->timeout_work,
  430. msecs_to_jiffies(40000));
  431. rmpp_recv->newwin += window_size(agent);
  432. ack_recv(rmpp_recv, mad_recv_wc);
  433. mad_recv_wc = NULL;
  434. }
  435. deref_rmpp_recv(rmpp_recv);
  436. return mad_recv_wc;
  437. }
  438. static inline u64 get_seg_addr(struct ib_mad_send_wr_private *mad_send_wr)
  439. {
  440. return mad_send_wr->sg_list[0].addr + mad_send_wr->data_offset +
  441. (sizeof(struct ib_rmpp_mad) - mad_send_wr->data_offset) *
  442. (mad_send_wr->seg_num - 1);
  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. rmpp_mad = (struct ib_rmpp_mad *)mad_send_wr->send_wr.wr.ud.mad_hdr;
  449. ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
  450. rmpp_mad->rmpp_hdr.seg_num = cpu_to_be32(mad_send_wr->seg_num);
  451. if (mad_send_wr->seg_num == 1) {
  452. rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_FIRST;
  453. rmpp_mad->rmpp_hdr.paylen_newwin =
  454. cpu_to_be32(mad_send_wr->total_seg *
  455. (sizeof(struct ib_rmpp_mad) -
  456. offsetof(struct ib_rmpp_mad, data)));
  457. mad_send_wr->sg_list[0].length = sizeof(struct ib_rmpp_mad);
  458. } else {
  459. mad_send_wr->send_wr.num_sge = 2;
  460. mad_send_wr->sg_list[0].length = mad_send_wr->data_offset;
  461. mad_send_wr->sg_list[1].addr = get_seg_addr(mad_send_wr);
  462. mad_send_wr->sg_list[1].length = sizeof(struct ib_rmpp_mad) -
  463. mad_send_wr->data_offset;
  464. mad_send_wr->sg_list[1].lkey = mad_send_wr->sg_list[0].lkey;
  465. }
  466. if (mad_send_wr->seg_num == mad_send_wr->total_seg) {
  467. rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_LAST;
  468. rmpp_mad->rmpp_hdr.paylen_newwin =
  469. cpu_to_be32(sizeof(struct ib_rmpp_mad) -
  470. offsetof(struct ib_rmpp_mad, data) -
  471. mad_send_wr->pad);
  472. }
  473. /* 2 seconds for an ACK until we can find the packet lifetime */
  474. timeout = mad_send_wr->send_wr.wr.ud.timeout_ms;
  475. if (!timeout || timeout > 2000)
  476. mad_send_wr->timeout = msecs_to_jiffies(2000);
  477. mad_send_wr->seg_num++;
  478. return ib_send_mad(mad_send_wr);
  479. }
  480. static void process_rmpp_ack(struct ib_mad_agent_private *agent,
  481. struct ib_mad_recv_wc *mad_recv_wc)
  482. {
  483. struct ib_mad_send_wr_private *mad_send_wr;
  484. struct ib_rmpp_mad *rmpp_mad;
  485. unsigned long flags;
  486. int seg_num, newwin, ret;
  487. rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
  488. if (rmpp_mad->rmpp_hdr.rmpp_status)
  489. return;
  490. seg_num = be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
  491. newwin = be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
  492. spin_lock_irqsave(&agent->lock, flags);
  493. mad_send_wr = ib_find_send_mad(agent, rmpp_mad->mad_hdr.tid);
  494. if (!mad_send_wr)
  495. goto out; /* Unmatched ACK */
  496. if ((mad_send_wr->last_ack == mad_send_wr->total_seg) ||
  497. (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
  498. goto out; /* Send is already done */
  499. if (seg_num > mad_send_wr->total_seg)
  500. goto out; /* Bad ACK */
  501. if (newwin < mad_send_wr->newwin || seg_num < mad_send_wr->last_ack)
  502. goto out; /* Old ACK */
  503. if (seg_num > mad_send_wr->last_ack) {
  504. mad_send_wr->last_ack = seg_num;
  505. mad_send_wr->retries = mad_send_wr->send_wr.wr.ud.retries;
  506. }
  507. mad_send_wr->newwin = newwin;
  508. if (mad_send_wr->last_ack == mad_send_wr->total_seg) {
  509. /* If no response is expected, the ACK completes the send */
  510. if (!mad_send_wr->send_wr.wr.ud.timeout_ms) {
  511. struct ib_mad_send_wc wc;
  512. ib_mark_mad_done(mad_send_wr);
  513. spin_unlock_irqrestore(&agent->lock, flags);
  514. wc.status = IB_WC_SUCCESS;
  515. wc.vendor_err = 0;
  516. wc.wr_id = mad_send_wr->wr_id;
  517. ib_mad_complete_send_wr(mad_send_wr, &wc);
  518. return;
  519. }
  520. if (mad_send_wr->refcount == 1)
  521. ib_reset_mad_timeout(mad_send_wr, mad_send_wr->
  522. send_wr.wr.ud.timeout_ms);
  523. } else if (mad_send_wr->refcount == 1 &&
  524. mad_send_wr->seg_num < mad_send_wr->newwin &&
  525. mad_send_wr->seg_num <= mad_send_wr->total_seg) {
  526. /* Send failure will just result in a timeout/retry */
  527. ret = send_next_seg(mad_send_wr);
  528. if (ret)
  529. goto out;
  530. mad_send_wr->refcount++;
  531. list_del(&mad_send_wr->agent_list);
  532. list_add_tail(&mad_send_wr->agent_list,
  533. &mad_send_wr->mad_agent_priv->send_list);
  534. }
  535. out:
  536. spin_unlock_irqrestore(&agent->lock, flags);
  537. }
  538. struct ib_mad_recv_wc *
  539. ib_process_rmpp_recv_wc(struct ib_mad_agent_private *agent,
  540. struct ib_mad_recv_wc *mad_recv_wc)
  541. {
  542. struct ib_rmpp_mad *rmpp_mad;
  543. rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
  544. if (!(rmpp_mad->rmpp_hdr.rmpp_rtime_flags & IB_MGMT_RMPP_FLAG_ACTIVE))
  545. return mad_recv_wc;
  546. if (rmpp_mad->rmpp_hdr.rmpp_version != IB_MGMT_RMPP_VERSION)
  547. goto out;
  548. switch (rmpp_mad->rmpp_hdr.rmpp_type) {
  549. case IB_MGMT_RMPP_TYPE_DATA:
  550. if (rmpp_mad->rmpp_hdr.seg_num == __constant_htonl(1))
  551. return start_rmpp(agent, mad_recv_wc);
  552. else
  553. return continue_rmpp(agent, mad_recv_wc);
  554. case IB_MGMT_RMPP_TYPE_ACK:
  555. process_rmpp_ack(agent, mad_recv_wc);
  556. break;
  557. case IB_MGMT_RMPP_TYPE_STOP:
  558. case IB_MGMT_RMPP_TYPE_ABORT:
  559. /* TODO: process_rmpp_nack(agent, mad_recv_wc); */
  560. break;
  561. default:
  562. break;
  563. }
  564. out:
  565. ib_free_recv_mad(mad_recv_wc);
  566. return NULL;
  567. }
  568. int ib_send_rmpp_mad(struct ib_mad_send_wr_private *mad_send_wr)
  569. {
  570. struct ib_rmpp_mad *rmpp_mad;
  571. int i, total_len, ret;
  572. rmpp_mad = (struct ib_rmpp_mad *)mad_send_wr->send_wr.wr.ud.mad_hdr;
  573. if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
  574. IB_MGMT_RMPP_FLAG_ACTIVE))
  575. return IB_RMPP_RESULT_UNHANDLED;
  576. if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA)
  577. return IB_RMPP_RESULT_INTERNAL;
  578. if (mad_send_wr->send_wr.num_sge > 1)
  579. return -EINVAL; /* TODO: support num_sge > 1 */
  580. mad_send_wr->seg_num = 1;
  581. mad_send_wr->newwin = 1;
  582. mad_send_wr->data_offset = data_offset(rmpp_mad->mad_hdr.mgmt_class);
  583. total_len = 0;
  584. for (i = 0; i < mad_send_wr->send_wr.num_sge; i++)
  585. total_len += mad_send_wr->send_wr.sg_list[i].length;
  586. mad_send_wr->total_seg = (total_len - mad_send_wr->data_offset) /
  587. (sizeof(struct ib_rmpp_mad) - mad_send_wr->data_offset);
  588. mad_send_wr->pad = total_len - offsetof(struct ib_rmpp_mad, data) -
  589. be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
  590. /* We need to wait for the final ACK even if there isn't a response */
  591. mad_send_wr->refcount += (mad_send_wr->timeout == 0);
  592. ret = send_next_seg(mad_send_wr);
  593. if (!ret)
  594. return IB_RMPP_RESULT_CONSUMED;
  595. return ret;
  596. }
  597. int ib_process_rmpp_send_wc(struct ib_mad_send_wr_private *mad_send_wr,
  598. struct ib_mad_send_wc *mad_send_wc)
  599. {
  600. struct ib_rmpp_mad *rmpp_mad;
  601. struct ib_mad_send_buf *msg;
  602. int ret;
  603. rmpp_mad = (struct ib_rmpp_mad *)mad_send_wr->send_wr.wr.ud.mad_hdr;
  604. if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
  605. IB_MGMT_RMPP_FLAG_ACTIVE))
  606. return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
  607. if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA) {
  608. msg = (struct ib_mad_send_buf *) (unsigned long)
  609. mad_send_wc->wr_id;
  610. ib_free_send_mad(msg);
  611. return IB_RMPP_RESULT_INTERNAL; /* ACK, STOP, or ABORT */
  612. }
  613. if (mad_send_wc->status != IB_WC_SUCCESS ||
  614. mad_send_wr->status != IB_WC_SUCCESS)
  615. return IB_RMPP_RESULT_PROCESSED; /* Canceled or send error */
  616. if (!mad_send_wr->timeout)
  617. return IB_RMPP_RESULT_PROCESSED; /* Response received */
  618. if (mad_send_wr->last_ack == mad_send_wr->total_seg) {
  619. mad_send_wr->timeout =
  620. msecs_to_jiffies(mad_send_wr->send_wr.wr.ud.timeout_ms);
  621. return IB_RMPP_RESULT_PROCESSED; /* Send done */
  622. }
  623. if (mad_send_wr->seg_num > mad_send_wr->newwin ||
  624. mad_send_wr->seg_num > mad_send_wr->total_seg)
  625. return IB_RMPP_RESULT_PROCESSED; /* Wait for ACK */
  626. ret = send_next_seg(mad_send_wr);
  627. if (ret) {
  628. mad_send_wc->status = IB_WC_GENERAL_ERR;
  629. return IB_RMPP_RESULT_PROCESSED;
  630. }
  631. return IB_RMPP_RESULT_CONSUMED;
  632. }
  633. int ib_retry_rmpp(struct ib_mad_send_wr_private *mad_send_wr)
  634. {
  635. struct ib_rmpp_mad *rmpp_mad;
  636. int ret;
  637. rmpp_mad = (struct ib_rmpp_mad *)mad_send_wr->send_wr.wr.ud.mad_hdr;
  638. if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
  639. IB_MGMT_RMPP_FLAG_ACTIVE))
  640. return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
  641. if (mad_send_wr->last_ack == mad_send_wr->total_seg)
  642. return IB_RMPP_RESULT_PROCESSED;
  643. mad_send_wr->seg_num = mad_send_wr->last_ack + 1;
  644. ret = send_next_seg(mad_send_wr);
  645. if (ret)
  646. return IB_RMPP_RESULT_PROCESSED;
  647. return IB_RMPP_RESULT_CONSUMED;
  648. }