user_mad.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
  4. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  5. * Copyright (c) 2008 Cisco. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. */
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. #include <linux/device.h>
  38. #include <linux/err.h>
  39. #include <linux/fs.h>
  40. #include <linux/cdev.h>
  41. #include <linux/dma-mapping.h>
  42. #include <linux/poll.h>
  43. #include <linux/mutex.h>
  44. #include <linux/kref.h>
  45. #include <linux/compat.h>
  46. #include <linux/sched.h>
  47. #include <linux/semaphore.h>
  48. #include <linux/slab.h>
  49. #include <asm/uaccess.h>
  50. #include <rdma/ib_mad.h>
  51. #include <rdma/ib_user_mad.h>
  52. MODULE_AUTHOR("Roland Dreier");
  53. MODULE_DESCRIPTION("InfiniBand userspace MAD packet access");
  54. MODULE_LICENSE("Dual BSD/GPL");
  55. enum {
  56. IB_UMAD_MAX_PORTS = 64,
  57. IB_UMAD_MAX_AGENTS = 32,
  58. IB_UMAD_MAJOR = 231,
  59. IB_UMAD_MINOR_BASE = 0
  60. };
  61. /*
  62. * Our lifetime rules for these structs are the following:
  63. * device special file is opened, we take a reference on the
  64. * ib_umad_port's struct ib_umad_device. We drop these
  65. * references in the corresponding close().
  66. *
  67. * In addition to references coming from open character devices, there
  68. * is one more reference to each ib_umad_device representing the
  69. * module's reference taken when allocating the ib_umad_device in
  70. * ib_umad_add_one().
  71. *
  72. * When destroying an ib_umad_device, we drop the module's reference.
  73. */
  74. struct ib_umad_port {
  75. struct cdev cdev;
  76. struct device *dev;
  77. struct cdev sm_cdev;
  78. struct device *sm_dev;
  79. struct semaphore sm_sem;
  80. struct mutex file_mutex;
  81. struct list_head file_list;
  82. struct ib_device *ib_dev;
  83. struct ib_umad_device *umad_dev;
  84. int dev_num;
  85. u8 port_num;
  86. };
  87. struct ib_umad_device {
  88. int start_port, end_port;
  89. struct kobject kobj;
  90. struct ib_umad_port port[0];
  91. };
  92. struct ib_umad_file {
  93. struct mutex mutex;
  94. struct ib_umad_port *port;
  95. struct list_head recv_list;
  96. struct list_head send_list;
  97. struct list_head port_list;
  98. spinlock_t send_lock;
  99. wait_queue_head_t recv_wait;
  100. struct ib_mad_agent *agent[IB_UMAD_MAX_AGENTS];
  101. int agents_dead;
  102. u8 use_pkey_index;
  103. u8 already_used;
  104. };
  105. struct ib_umad_packet {
  106. struct ib_mad_send_buf *msg;
  107. struct ib_mad_recv_wc *recv_wc;
  108. struct list_head list;
  109. int length;
  110. struct ib_user_mad mad;
  111. };
  112. static struct class *umad_class;
  113. static const dev_t base_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE);
  114. static DEFINE_SPINLOCK(port_lock);
  115. static DECLARE_BITMAP(dev_map, IB_UMAD_MAX_PORTS);
  116. static void ib_umad_add_one(struct ib_device *device);
  117. static void ib_umad_remove_one(struct ib_device *device);
  118. static void ib_umad_release_dev(struct kobject *kobj)
  119. {
  120. struct ib_umad_device *dev =
  121. container_of(kobj, struct ib_umad_device, kobj);
  122. kfree(dev);
  123. }
  124. static struct kobj_type ib_umad_dev_ktype = {
  125. .release = ib_umad_release_dev,
  126. };
  127. static int hdr_size(struct ib_umad_file *file)
  128. {
  129. return file->use_pkey_index ? sizeof (struct ib_user_mad_hdr) :
  130. sizeof (struct ib_user_mad_hdr_old);
  131. }
  132. /* caller must hold file->mutex */
  133. static struct ib_mad_agent *__get_agent(struct ib_umad_file *file, int id)
  134. {
  135. return file->agents_dead ? NULL : file->agent[id];
  136. }
  137. static int queue_packet(struct ib_umad_file *file,
  138. struct ib_mad_agent *agent,
  139. struct ib_umad_packet *packet)
  140. {
  141. int ret = 1;
  142. mutex_lock(&file->mutex);
  143. for (packet->mad.hdr.id = 0;
  144. packet->mad.hdr.id < IB_UMAD_MAX_AGENTS;
  145. packet->mad.hdr.id++)
  146. if (agent == __get_agent(file, packet->mad.hdr.id)) {
  147. list_add_tail(&packet->list, &file->recv_list);
  148. wake_up_interruptible(&file->recv_wait);
  149. ret = 0;
  150. break;
  151. }
  152. mutex_unlock(&file->mutex);
  153. return ret;
  154. }
  155. static void dequeue_send(struct ib_umad_file *file,
  156. struct ib_umad_packet *packet)
  157. {
  158. spin_lock_irq(&file->send_lock);
  159. list_del(&packet->list);
  160. spin_unlock_irq(&file->send_lock);
  161. }
  162. static void send_handler(struct ib_mad_agent *agent,
  163. struct ib_mad_send_wc *send_wc)
  164. {
  165. struct ib_umad_file *file = agent->context;
  166. struct ib_umad_packet *packet = send_wc->send_buf->context[0];
  167. dequeue_send(file, packet);
  168. ib_destroy_ah(packet->msg->ah);
  169. ib_free_send_mad(packet->msg);
  170. if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) {
  171. packet->length = IB_MGMT_MAD_HDR;
  172. packet->mad.hdr.status = ETIMEDOUT;
  173. if (!queue_packet(file, agent, packet))
  174. return;
  175. }
  176. kfree(packet);
  177. }
  178. static void recv_handler(struct ib_mad_agent *agent,
  179. struct ib_mad_recv_wc *mad_recv_wc)
  180. {
  181. struct ib_umad_file *file = agent->context;
  182. struct ib_umad_packet *packet;
  183. if (mad_recv_wc->wc->status != IB_WC_SUCCESS)
  184. goto err1;
  185. packet = kzalloc(sizeof *packet, GFP_KERNEL);
  186. if (!packet)
  187. goto err1;
  188. packet->length = mad_recv_wc->mad_len;
  189. packet->recv_wc = mad_recv_wc;
  190. packet->mad.hdr.status = 0;
  191. packet->mad.hdr.length = hdr_size(file) + mad_recv_wc->mad_len;
  192. packet->mad.hdr.qpn = cpu_to_be32(mad_recv_wc->wc->src_qp);
  193. packet->mad.hdr.lid = cpu_to_be16(mad_recv_wc->wc->slid);
  194. packet->mad.hdr.sl = mad_recv_wc->wc->sl;
  195. packet->mad.hdr.path_bits = mad_recv_wc->wc->dlid_path_bits;
  196. packet->mad.hdr.pkey_index = mad_recv_wc->wc->pkey_index;
  197. packet->mad.hdr.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH);
  198. if (packet->mad.hdr.grh_present) {
  199. struct ib_ah_attr ah_attr;
  200. ib_init_ah_from_wc(agent->device, agent->port_num,
  201. mad_recv_wc->wc, mad_recv_wc->recv_buf.grh,
  202. &ah_attr);
  203. packet->mad.hdr.gid_index = ah_attr.grh.sgid_index;
  204. packet->mad.hdr.hop_limit = ah_attr.grh.hop_limit;
  205. packet->mad.hdr.traffic_class = ah_attr.grh.traffic_class;
  206. memcpy(packet->mad.hdr.gid, &ah_attr.grh.dgid, 16);
  207. packet->mad.hdr.flow_label = cpu_to_be32(ah_attr.grh.flow_label);
  208. }
  209. if (queue_packet(file, agent, packet))
  210. goto err2;
  211. return;
  212. err2:
  213. kfree(packet);
  214. err1:
  215. ib_free_recv_mad(mad_recv_wc);
  216. }
  217. static ssize_t copy_recv_mad(struct ib_umad_file *file, char __user *buf,
  218. struct ib_umad_packet *packet, size_t count)
  219. {
  220. struct ib_mad_recv_buf *recv_buf;
  221. int left, seg_payload, offset, max_seg_payload;
  222. /* We need enough room to copy the first (or only) MAD segment. */
  223. recv_buf = &packet->recv_wc->recv_buf;
  224. if ((packet->length <= sizeof (*recv_buf->mad) &&
  225. count < hdr_size(file) + packet->length) ||
  226. (packet->length > sizeof (*recv_buf->mad) &&
  227. count < hdr_size(file) + sizeof (*recv_buf->mad)))
  228. return -EINVAL;
  229. if (copy_to_user(buf, &packet->mad, hdr_size(file)))
  230. return -EFAULT;
  231. buf += hdr_size(file);
  232. seg_payload = min_t(int, packet->length, sizeof (*recv_buf->mad));
  233. if (copy_to_user(buf, recv_buf->mad, seg_payload))
  234. return -EFAULT;
  235. if (seg_payload < packet->length) {
  236. /*
  237. * Multipacket RMPP MAD message. Copy remainder of message.
  238. * Note that last segment may have a shorter payload.
  239. */
  240. if (count < hdr_size(file) + packet->length) {
  241. /*
  242. * The buffer is too small, return the first RMPP segment,
  243. * which includes the RMPP message length.
  244. */
  245. return -ENOSPC;
  246. }
  247. offset = ib_get_mad_data_offset(recv_buf->mad->mad_hdr.mgmt_class);
  248. max_seg_payload = sizeof (struct ib_mad) - offset;
  249. for (left = packet->length - seg_payload, buf += seg_payload;
  250. left; left -= seg_payload, buf += seg_payload) {
  251. recv_buf = container_of(recv_buf->list.next,
  252. struct ib_mad_recv_buf, list);
  253. seg_payload = min(left, max_seg_payload);
  254. if (copy_to_user(buf, ((void *) recv_buf->mad) + offset,
  255. seg_payload))
  256. return -EFAULT;
  257. }
  258. }
  259. return hdr_size(file) + packet->length;
  260. }
  261. static ssize_t copy_send_mad(struct ib_umad_file *file, char __user *buf,
  262. struct ib_umad_packet *packet, size_t count)
  263. {
  264. ssize_t size = hdr_size(file) + packet->length;
  265. if (count < size)
  266. return -EINVAL;
  267. if (copy_to_user(buf, &packet->mad, hdr_size(file)))
  268. return -EFAULT;
  269. buf += hdr_size(file);
  270. if (copy_to_user(buf, packet->mad.data, packet->length))
  271. return -EFAULT;
  272. return size;
  273. }
  274. static ssize_t ib_umad_read(struct file *filp, char __user *buf,
  275. size_t count, loff_t *pos)
  276. {
  277. struct ib_umad_file *file = filp->private_data;
  278. struct ib_umad_packet *packet;
  279. ssize_t ret;
  280. if (count < hdr_size(file))
  281. return -EINVAL;
  282. mutex_lock(&file->mutex);
  283. while (list_empty(&file->recv_list)) {
  284. mutex_unlock(&file->mutex);
  285. if (filp->f_flags & O_NONBLOCK)
  286. return -EAGAIN;
  287. if (wait_event_interruptible(file->recv_wait,
  288. !list_empty(&file->recv_list)))
  289. return -ERESTARTSYS;
  290. mutex_lock(&file->mutex);
  291. }
  292. packet = list_entry(file->recv_list.next, struct ib_umad_packet, list);
  293. list_del(&packet->list);
  294. mutex_unlock(&file->mutex);
  295. if (packet->recv_wc)
  296. ret = copy_recv_mad(file, buf, packet, count);
  297. else
  298. ret = copy_send_mad(file, buf, packet, count);
  299. if (ret < 0) {
  300. /* Requeue packet */
  301. mutex_lock(&file->mutex);
  302. list_add(&packet->list, &file->recv_list);
  303. mutex_unlock(&file->mutex);
  304. } else {
  305. if (packet->recv_wc)
  306. ib_free_recv_mad(packet->recv_wc);
  307. kfree(packet);
  308. }
  309. return ret;
  310. }
  311. static int copy_rmpp_mad(struct ib_mad_send_buf *msg, const char __user *buf)
  312. {
  313. int left, seg;
  314. /* Copy class specific header */
  315. if ((msg->hdr_len > IB_MGMT_RMPP_HDR) &&
  316. copy_from_user(msg->mad + IB_MGMT_RMPP_HDR, buf + IB_MGMT_RMPP_HDR,
  317. msg->hdr_len - IB_MGMT_RMPP_HDR))
  318. return -EFAULT;
  319. /* All headers are in place. Copy data segments. */
  320. for (seg = 1, left = msg->data_len, buf += msg->hdr_len; left > 0;
  321. seg++, left -= msg->seg_size, buf += msg->seg_size) {
  322. if (copy_from_user(ib_get_rmpp_segment(msg, seg), buf,
  323. min(left, msg->seg_size)))
  324. return -EFAULT;
  325. }
  326. return 0;
  327. }
  328. static int same_destination(struct ib_user_mad_hdr *hdr1,
  329. struct ib_user_mad_hdr *hdr2)
  330. {
  331. if (!hdr1->grh_present && !hdr2->grh_present)
  332. return (hdr1->lid == hdr2->lid);
  333. if (hdr1->grh_present && hdr2->grh_present)
  334. return !memcmp(hdr1->gid, hdr2->gid, 16);
  335. return 0;
  336. }
  337. static int is_duplicate(struct ib_umad_file *file,
  338. struct ib_umad_packet *packet)
  339. {
  340. struct ib_umad_packet *sent_packet;
  341. struct ib_mad_hdr *sent_hdr, *hdr;
  342. hdr = (struct ib_mad_hdr *) packet->mad.data;
  343. list_for_each_entry(sent_packet, &file->send_list, list) {
  344. sent_hdr = (struct ib_mad_hdr *) sent_packet->mad.data;
  345. if ((hdr->tid != sent_hdr->tid) ||
  346. (hdr->mgmt_class != sent_hdr->mgmt_class))
  347. continue;
  348. /*
  349. * No need to be overly clever here. If two new operations have
  350. * the same TID, reject the second as a duplicate. This is more
  351. * restrictive than required by the spec.
  352. */
  353. if (!ib_response_mad((struct ib_mad *) hdr)) {
  354. if (!ib_response_mad((struct ib_mad *) sent_hdr))
  355. return 1;
  356. continue;
  357. } else if (!ib_response_mad((struct ib_mad *) sent_hdr))
  358. continue;
  359. if (same_destination(&packet->mad.hdr, &sent_packet->mad.hdr))
  360. return 1;
  361. }
  362. return 0;
  363. }
  364. static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
  365. size_t count, loff_t *pos)
  366. {
  367. struct ib_umad_file *file = filp->private_data;
  368. struct ib_umad_packet *packet;
  369. struct ib_mad_agent *agent;
  370. struct ib_ah_attr ah_attr;
  371. struct ib_ah *ah;
  372. struct ib_rmpp_mad *rmpp_mad;
  373. __be64 *tid;
  374. int ret, data_len, hdr_len, copy_offset, rmpp_active;
  375. if (count < hdr_size(file) + IB_MGMT_RMPP_HDR)
  376. return -EINVAL;
  377. packet = kzalloc(sizeof *packet + IB_MGMT_RMPP_HDR, GFP_KERNEL);
  378. if (!packet)
  379. return -ENOMEM;
  380. if (copy_from_user(&packet->mad, buf, hdr_size(file))) {
  381. ret = -EFAULT;
  382. goto err;
  383. }
  384. if (packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) {
  385. ret = -EINVAL;
  386. goto err;
  387. }
  388. buf += hdr_size(file);
  389. if (copy_from_user(packet->mad.data, buf, IB_MGMT_RMPP_HDR)) {
  390. ret = -EFAULT;
  391. goto err;
  392. }
  393. mutex_lock(&file->mutex);
  394. agent = __get_agent(file, packet->mad.hdr.id);
  395. if (!agent) {
  396. ret = -EINVAL;
  397. goto err_up;
  398. }
  399. memset(&ah_attr, 0, sizeof ah_attr);
  400. ah_attr.dlid = be16_to_cpu(packet->mad.hdr.lid);
  401. ah_attr.sl = packet->mad.hdr.sl;
  402. ah_attr.src_path_bits = packet->mad.hdr.path_bits;
  403. ah_attr.port_num = file->port->port_num;
  404. if (packet->mad.hdr.grh_present) {
  405. ah_attr.ah_flags = IB_AH_GRH;
  406. memcpy(ah_attr.grh.dgid.raw, packet->mad.hdr.gid, 16);
  407. ah_attr.grh.sgid_index = packet->mad.hdr.gid_index;
  408. ah_attr.grh.flow_label = be32_to_cpu(packet->mad.hdr.flow_label);
  409. ah_attr.grh.hop_limit = packet->mad.hdr.hop_limit;
  410. ah_attr.grh.traffic_class = packet->mad.hdr.traffic_class;
  411. }
  412. ah = ib_create_ah(agent->qp->pd, &ah_attr);
  413. if (IS_ERR(ah)) {
  414. ret = PTR_ERR(ah);
  415. goto err_up;
  416. }
  417. rmpp_mad = (struct ib_rmpp_mad *) packet->mad.data;
  418. hdr_len = ib_get_mad_data_offset(rmpp_mad->mad_hdr.mgmt_class);
  419. if (!ib_is_mad_class_rmpp(rmpp_mad->mad_hdr.mgmt_class)) {
  420. copy_offset = IB_MGMT_MAD_HDR;
  421. rmpp_active = 0;
  422. } else {
  423. copy_offset = IB_MGMT_RMPP_HDR;
  424. rmpp_active = ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
  425. IB_MGMT_RMPP_FLAG_ACTIVE;
  426. }
  427. data_len = count - hdr_size(file) - hdr_len;
  428. packet->msg = ib_create_send_mad(agent,
  429. be32_to_cpu(packet->mad.hdr.qpn),
  430. packet->mad.hdr.pkey_index, rmpp_active,
  431. hdr_len, data_len, GFP_KERNEL);
  432. if (IS_ERR(packet->msg)) {
  433. ret = PTR_ERR(packet->msg);
  434. goto err_ah;
  435. }
  436. packet->msg->ah = ah;
  437. packet->msg->timeout_ms = packet->mad.hdr.timeout_ms;
  438. packet->msg->retries = packet->mad.hdr.retries;
  439. packet->msg->context[0] = packet;
  440. /* Copy MAD header. Any RMPP header is already in place. */
  441. memcpy(packet->msg->mad, packet->mad.data, IB_MGMT_MAD_HDR);
  442. if (!rmpp_active) {
  443. if (copy_from_user(packet->msg->mad + copy_offset,
  444. buf + copy_offset,
  445. hdr_len + data_len - copy_offset)) {
  446. ret = -EFAULT;
  447. goto err_msg;
  448. }
  449. } else {
  450. ret = copy_rmpp_mad(packet->msg, buf);
  451. if (ret)
  452. goto err_msg;
  453. }
  454. /*
  455. * Set the high-order part of the transaction ID to make MADs from
  456. * different agents unique, and allow routing responses back to the
  457. * original requestor.
  458. */
  459. if (!ib_response_mad(packet->msg->mad)) {
  460. tid = &((struct ib_mad_hdr *) packet->msg->mad)->tid;
  461. *tid = cpu_to_be64(((u64) agent->hi_tid) << 32 |
  462. (be64_to_cpup(tid) & 0xffffffff));
  463. rmpp_mad->mad_hdr.tid = *tid;
  464. }
  465. spin_lock_irq(&file->send_lock);
  466. ret = is_duplicate(file, packet);
  467. if (!ret)
  468. list_add_tail(&packet->list, &file->send_list);
  469. spin_unlock_irq(&file->send_lock);
  470. if (ret) {
  471. ret = -EINVAL;
  472. goto err_msg;
  473. }
  474. ret = ib_post_send_mad(packet->msg, NULL);
  475. if (ret)
  476. goto err_send;
  477. mutex_unlock(&file->mutex);
  478. return count;
  479. err_send:
  480. dequeue_send(file, packet);
  481. err_msg:
  482. ib_free_send_mad(packet->msg);
  483. err_ah:
  484. ib_destroy_ah(ah);
  485. err_up:
  486. mutex_unlock(&file->mutex);
  487. err:
  488. kfree(packet);
  489. return ret;
  490. }
  491. static unsigned int ib_umad_poll(struct file *filp, struct poll_table_struct *wait)
  492. {
  493. struct ib_umad_file *file = filp->private_data;
  494. /* we will always be able to post a MAD send */
  495. unsigned int mask = POLLOUT | POLLWRNORM;
  496. poll_wait(filp, &file->recv_wait, wait);
  497. if (!list_empty(&file->recv_list))
  498. mask |= POLLIN | POLLRDNORM;
  499. return mask;
  500. }
  501. static int ib_umad_reg_agent(struct ib_umad_file *file, void __user *arg,
  502. int compat_method_mask)
  503. {
  504. struct ib_user_mad_reg_req ureq;
  505. struct ib_mad_reg_req req;
  506. struct ib_mad_agent *agent = NULL;
  507. int agent_id;
  508. int ret;
  509. mutex_lock(&file->port->file_mutex);
  510. mutex_lock(&file->mutex);
  511. if (!file->port->ib_dev) {
  512. ret = -EPIPE;
  513. goto out;
  514. }
  515. if (copy_from_user(&ureq, arg, sizeof ureq)) {
  516. ret = -EFAULT;
  517. goto out;
  518. }
  519. if (ureq.qpn != 0 && ureq.qpn != 1) {
  520. ret = -EINVAL;
  521. goto out;
  522. }
  523. for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
  524. if (!__get_agent(file, agent_id))
  525. goto found;
  526. ret = -ENOMEM;
  527. goto out;
  528. found:
  529. if (ureq.mgmt_class) {
  530. req.mgmt_class = ureq.mgmt_class;
  531. req.mgmt_class_version = ureq.mgmt_class_version;
  532. memcpy(req.oui, ureq.oui, sizeof req.oui);
  533. if (compat_method_mask) {
  534. u32 *umm = (u32 *) ureq.method_mask;
  535. int i;
  536. for (i = 0; i < BITS_TO_LONGS(IB_MGMT_MAX_METHODS); ++i)
  537. req.method_mask[i] =
  538. umm[i * 2] | ((u64) umm[i * 2 + 1] << 32);
  539. } else
  540. memcpy(req.method_mask, ureq.method_mask,
  541. sizeof req.method_mask);
  542. }
  543. agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,
  544. ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI,
  545. ureq.mgmt_class ? &req : NULL,
  546. ureq.rmpp_version,
  547. send_handler, recv_handler, file);
  548. if (IS_ERR(agent)) {
  549. ret = PTR_ERR(agent);
  550. agent = NULL;
  551. goto out;
  552. }
  553. if (put_user(agent_id,
  554. (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {
  555. ret = -EFAULT;
  556. goto out;
  557. }
  558. if (!file->already_used) {
  559. file->already_used = 1;
  560. if (!file->use_pkey_index) {
  561. printk(KERN_WARNING "user_mad: process %s did not enable "
  562. "P_Key index support.\n", current->comm);
  563. printk(KERN_WARNING "user_mad: Documentation/infiniband/user_mad.txt "
  564. "has info on the new ABI.\n");
  565. }
  566. }
  567. file->agent[agent_id] = agent;
  568. ret = 0;
  569. out:
  570. mutex_unlock(&file->mutex);
  571. if (ret && agent)
  572. ib_unregister_mad_agent(agent);
  573. mutex_unlock(&file->port->file_mutex);
  574. return ret;
  575. }
  576. static int ib_umad_unreg_agent(struct ib_umad_file *file, u32 __user *arg)
  577. {
  578. struct ib_mad_agent *agent = NULL;
  579. u32 id;
  580. int ret = 0;
  581. if (get_user(id, arg))
  582. return -EFAULT;
  583. mutex_lock(&file->port->file_mutex);
  584. mutex_lock(&file->mutex);
  585. if (id >= IB_UMAD_MAX_AGENTS || !__get_agent(file, id)) {
  586. ret = -EINVAL;
  587. goto out;
  588. }
  589. agent = file->agent[id];
  590. file->agent[id] = NULL;
  591. out:
  592. mutex_unlock(&file->mutex);
  593. if (agent)
  594. ib_unregister_mad_agent(agent);
  595. mutex_unlock(&file->port->file_mutex);
  596. return ret;
  597. }
  598. static long ib_umad_enable_pkey(struct ib_umad_file *file)
  599. {
  600. int ret = 0;
  601. mutex_lock(&file->mutex);
  602. if (file->already_used)
  603. ret = -EINVAL;
  604. else
  605. file->use_pkey_index = 1;
  606. mutex_unlock(&file->mutex);
  607. return ret;
  608. }
  609. static long ib_umad_ioctl(struct file *filp, unsigned int cmd,
  610. unsigned long arg)
  611. {
  612. switch (cmd) {
  613. case IB_USER_MAD_REGISTER_AGENT:
  614. return ib_umad_reg_agent(filp->private_data, (void __user *) arg, 0);
  615. case IB_USER_MAD_UNREGISTER_AGENT:
  616. return ib_umad_unreg_agent(filp->private_data, (__u32 __user *) arg);
  617. case IB_USER_MAD_ENABLE_PKEY:
  618. return ib_umad_enable_pkey(filp->private_data);
  619. default:
  620. return -ENOIOCTLCMD;
  621. }
  622. }
  623. #ifdef CONFIG_COMPAT
  624. static long ib_umad_compat_ioctl(struct file *filp, unsigned int cmd,
  625. unsigned long arg)
  626. {
  627. switch (cmd) {
  628. case IB_USER_MAD_REGISTER_AGENT:
  629. return ib_umad_reg_agent(filp->private_data, compat_ptr(arg), 1);
  630. case IB_USER_MAD_UNREGISTER_AGENT:
  631. return ib_umad_unreg_agent(filp->private_data, compat_ptr(arg));
  632. case IB_USER_MAD_ENABLE_PKEY:
  633. return ib_umad_enable_pkey(filp->private_data);
  634. default:
  635. return -ENOIOCTLCMD;
  636. }
  637. }
  638. #endif
  639. /*
  640. * ib_umad_open() does not need the BKL:
  641. *
  642. * - the ib_umad_port structures are properly reference counted, and
  643. * everything else is purely local to the file being created, so
  644. * races against other open calls are not a problem;
  645. * - the ioctl method does not affect any global state outside of the
  646. * file structure being operated on;
  647. */
  648. static int ib_umad_open(struct inode *inode, struct file *filp)
  649. {
  650. struct ib_umad_port *port;
  651. struct ib_umad_file *file;
  652. int ret = -ENXIO;
  653. port = container_of(inode->i_cdev, struct ib_umad_port, cdev);
  654. mutex_lock(&port->file_mutex);
  655. if (!port->ib_dev)
  656. goto out;
  657. ret = -ENOMEM;
  658. file = kzalloc(sizeof *file, GFP_KERNEL);
  659. if (!file)
  660. goto out;
  661. mutex_init(&file->mutex);
  662. spin_lock_init(&file->send_lock);
  663. INIT_LIST_HEAD(&file->recv_list);
  664. INIT_LIST_HEAD(&file->send_list);
  665. init_waitqueue_head(&file->recv_wait);
  666. file->port = port;
  667. filp->private_data = file;
  668. list_add_tail(&file->port_list, &port->file_list);
  669. ret = nonseekable_open(inode, filp);
  670. if (ret) {
  671. list_del(&file->port_list);
  672. kfree(file);
  673. goto out;
  674. }
  675. kobject_get(&port->umad_dev->kobj);
  676. out:
  677. mutex_unlock(&port->file_mutex);
  678. return ret;
  679. }
  680. static int ib_umad_close(struct inode *inode, struct file *filp)
  681. {
  682. struct ib_umad_file *file = filp->private_data;
  683. struct ib_umad_device *dev = file->port->umad_dev;
  684. struct ib_umad_packet *packet, *tmp;
  685. int already_dead;
  686. int i;
  687. mutex_lock(&file->port->file_mutex);
  688. mutex_lock(&file->mutex);
  689. already_dead = file->agents_dead;
  690. file->agents_dead = 1;
  691. list_for_each_entry_safe(packet, tmp, &file->recv_list, list) {
  692. if (packet->recv_wc)
  693. ib_free_recv_mad(packet->recv_wc);
  694. kfree(packet);
  695. }
  696. list_del(&file->port_list);
  697. mutex_unlock(&file->mutex);
  698. if (!already_dead)
  699. for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
  700. if (file->agent[i])
  701. ib_unregister_mad_agent(file->agent[i]);
  702. mutex_unlock(&file->port->file_mutex);
  703. kfree(file);
  704. kobject_put(&dev->kobj);
  705. return 0;
  706. }
  707. static const struct file_operations umad_fops = {
  708. .owner = THIS_MODULE,
  709. .read = ib_umad_read,
  710. .write = ib_umad_write,
  711. .poll = ib_umad_poll,
  712. .unlocked_ioctl = ib_umad_ioctl,
  713. #ifdef CONFIG_COMPAT
  714. .compat_ioctl = ib_umad_compat_ioctl,
  715. #endif
  716. .open = ib_umad_open,
  717. .release = ib_umad_close,
  718. .llseek = no_llseek,
  719. };
  720. static int ib_umad_sm_open(struct inode *inode, struct file *filp)
  721. {
  722. struct ib_umad_port *port;
  723. struct ib_port_modify props = {
  724. .set_port_cap_mask = IB_PORT_SM
  725. };
  726. int ret;
  727. port = container_of(inode->i_cdev, struct ib_umad_port, sm_cdev);
  728. if (filp->f_flags & O_NONBLOCK) {
  729. if (down_trylock(&port->sm_sem)) {
  730. ret = -EAGAIN;
  731. goto fail;
  732. }
  733. } else {
  734. if (down_interruptible(&port->sm_sem)) {
  735. ret = -ERESTARTSYS;
  736. goto fail;
  737. }
  738. }
  739. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  740. if (ret)
  741. goto err_up_sem;
  742. filp->private_data = port;
  743. ret = nonseekable_open(inode, filp);
  744. if (ret)
  745. goto err_clr_sm_cap;
  746. kobject_get(&port->umad_dev->kobj);
  747. return 0;
  748. err_clr_sm_cap:
  749. swap(props.set_port_cap_mask, props.clr_port_cap_mask);
  750. ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  751. err_up_sem:
  752. up(&port->sm_sem);
  753. fail:
  754. return ret;
  755. }
  756. static int ib_umad_sm_close(struct inode *inode, struct file *filp)
  757. {
  758. struct ib_umad_port *port = filp->private_data;
  759. struct ib_port_modify props = {
  760. .clr_port_cap_mask = IB_PORT_SM
  761. };
  762. int ret = 0;
  763. mutex_lock(&port->file_mutex);
  764. if (port->ib_dev)
  765. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  766. mutex_unlock(&port->file_mutex);
  767. up(&port->sm_sem);
  768. kobject_put(&port->umad_dev->kobj);
  769. return ret;
  770. }
  771. static const struct file_operations umad_sm_fops = {
  772. .owner = THIS_MODULE,
  773. .open = ib_umad_sm_open,
  774. .release = ib_umad_sm_close,
  775. .llseek = no_llseek,
  776. };
  777. static struct ib_client umad_client = {
  778. .name = "umad",
  779. .add = ib_umad_add_one,
  780. .remove = ib_umad_remove_one
  781. };
  782. static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
  783. char *buf)
  784. {
  785. struct ib_umad_port *port = dev_get_drvdata(dev);
  786. if (!port)
  787. return -ENODEV;
  788. return sprintf(buf, "%s\n", port->ib_dev->name);
  789. }
  790. static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  791. static ssize_t show_port(struct device *dev, struct device_attribute *attr,
  792. char *buf)
  793. {
  794. struct ib_umad_port *port = dev_get_drvdata(dev);
  795. if (!port)
  796. return -ENODEV;
  797. return sprintf(buf, "%d\n", port->port_num);
  798. }
  799. static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
  800. static CLASS_ATTR_STRING(abi_version, S_IRUGO,
  801. __stringify(IB_USER_MAD_ABI_VERSION));
  802. static dev_t overflow_maj;
  803. static DECLARE_BITMAP(overflow_map, IB_UMAD_MAX_PORTS);
  804. static int find_overflow_devnum(void)
  805. {
  806. int ret;
  807. if (!overflow_maj) {
  808. ret = alloc_chrdev_region(&overflow_maj, 0, IB_UMAD_MAX_PORTS * 2,
  809. "infiniband_mad");
  810. if (ret) {
  811. printk(KERN_ERR "user_mad: couldn't register dynamic device number\n");
  812. return ret;
  813. }
  814. }
  815. ret = find_first_zero_bit(overflow_map, IB_UMAD_MAX_PORTS);
  816. if (ret >= IB_UMAD_MAX_PORTS)
  817. return -1;
  818. return ret;
  819. }
  820. static int ib_umad_init_port(struct ib_device *device, int port_num,
  821. struct ib_umad_device *umad_dev,
  822. struct ib_umad_port *port)
  823. {
  824. int devnum;
  825. dev_t base;
  826. spin_lock(&port_lock);
  827. devnum = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS);
  828. if (devnum >= IB_UMAD_MAX_PORTS) {
  829. spin_unlock(&port_lock);
  830. devnum = find_overflow_devnum();
  831. if (devnum < 0)
  832. return -1;
  833. spin_lock(&port_lock);
  834. port->dev_num = devnum + IB_UMAD_MAX_PORTS;
  835. base = devnum + overflow_maj;
  836. set_bit(devnum, overflow_map);
  837. } else {
  838. port->dev_num = devnum;
  839. base = devnum + base_dev;
  840. set_bit(devnum, dev_map);
  841. }
  842. spin_unlock(&port_lock);
  843. port->ib_dev = device;
  844. port->port_num = port_num;
  845. sema_init(&port->sm_sem, 1);
  846. mutex_init(&port->file_mutex);
  847. INIT_LIST_HEAD(&port->file_list);
  848. cdev_init(&port->cdev, &umad_fops);
  849. port->cdev.owner = THIS_MODULE;
  850. port->cdev.kobj.parent = &umad_dev->kobj;
  851. kobject_set_name(&port->cdev.kobj, "umad%d", port->dev_num);
  852. if (cdev_add(&port->cdev, base, 1))
  853. goto err_cdev;
  854. port->dev = device_create(umad_class, device->dma_device,
  855. port->cdev.dev, port,
  856. "umad%d", port->dev_num);
  857. if (IS_ERR(port->dev))
  858. goto err_cdev;
  859. if (device_create_file(port->dev, &dev_attr_ibdev))
  860. goto err_dev;
  861. if (device_create_file(port->dev, &dev_attr_port))
  862. goto err_dev;
  863. base += IB_UMAD_MAX_PORTS;
  864. cdev_init(&port->sm_cdev, &umad_sm_fops);
  865. port->sm_cdev.owner = THIS_MODULE;
  866. port->sm_cdev.kobj.parent = &umad_dev->kobj;
  867. kobject_set_name(&port->sm_cdev.kobj, "issm%d", port->dev_num);
  868. if (cdev_add(&port->sm_cdev, base, 1))
  869. goto err_sm_cdev;
  870. port->sm_dev = device_create(umad_class, device->dma_device,
  871. port->sm_cdev.dev, port,
  872. "issm%d", port->dev_num);
  873. if (IS_ERR(port->sm_dev))
  874. goto err_sm_cdev;
  875. if (device_create_file(port->sm_dev, &dev_attr_ibdev))
  876. goto err_sm_dev;
  877. if (device_create_file(port->sm_dev, &dev_attr_port))
  878. goto err_sm_dev;
  879. return 0;
  880. err_sm_dev:
  881. device_destroy(umad_class, port->sm_cdev.dev);
  882. err_sm_cdev:
  883. cdev_del(&port->sm_cdev);
  884. err_dev:
  885. device_destroy(umad_class, port->cdev.dev);
  886. err_cdev:
  887. cdev_del(&port->cdev);
  888. if (port->dev_num < IB_UMAD_MAX_PORTS)
  889. clear_bit(devnum, dev_map);
  890. else
  891. clear_bit(devnum, overflow_map);
  892. return -1;
  893. }
  894. static void ib_umad_kill_port(struct ib_umad_port *port)
  895. {
  896. struct ib_umad_file *file;
  897. int id;
  898. dev_set_drvdata(port->dev, NULL);
  899. dev_set_drvdata(port->sm_dev, NULL);
  900. device_destroy(umad_class, port->cdev.dev);
  901. device_destroy(umad_class, port->sm_cdev.dev);
  902. cdev_del(&port->cdev);
  903. cdev_del(&port->sm_cdev);
  904. mutex_lock(&port->file_mutex);
  905. port->ib_dev = NULL;
  906. list_for_each_entry(file, &port->file_list, port_list) {
  907. mutex_lock(&file->mutex);
  908. file->agents_dead = 1;
  909. mutex_unlock(&file->mutex);
  910. for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id)
  911. if (file->agent[id])
  912. ib_unregister_mad_agent(file->agent[id]);
  913. }
  914. mutex_unlock(&port->file_mutex);
  915. if (port->dev_num < IB_UMAD_MAX_PORTS)
  916. clear_bit(port->dev_num, dev_map);
  917. else
  918. clear_bit(port->dev_num - IB_UMAD_MAX_PORTS, overflow_map);
  919. }
  920. static void ib_umad_add_one(struct ib_device *device)
  921. {
  922. struct ib_umad_device *umad_dev;
  923. int s, e, i;
  924. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  925. return;
  926. if (device->node_type == RDMA_NODE_IB_SWITCH)
  927. s = e = 0;
  928. else {
  929. s = 1;
  930. e = device->phys_port_cnt;
  931. }
  932. umad_dev = kzalloc(sizeof *umad_dev +
  933. (e - s + 1) * sizeof (struct ib_umad_port),
  934. GFP_KERNEL);
  935. if (!umad_dev)
  936. return;
  937. kobject_init(&umad_dev->kobj, &ib_umad_dev_ktype);
  938. umad_dev->start_port = s;
  939. umad_dev->end_port = e;
  940. for (i = s; i <= e; ++i) {
  941. umad_dev->port[i - s].umad_dev = umad_dev;
  942. if (ib_umad_init_port(device, i, umad_dev,
  943. &umad_dev->port[i - s]))
  944. goto err;
  945. }
  946. ib_set_client_data(device, &umad_client, umad_dev);
  947. return;
  948. err:
  949. while (--i >= s)
  950. ib_umad_kill_port(&umad_dev->port[i - s]);
  951. kobject_put(&umad_dev->kobj);
  952. }
  953. static void ib_umad_remove_one(struct ib_device *device)
  954. {
  955. struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client);
  956. int i;
  957. if (!umad_dev)
  958. return;
  959. for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i)
  960. ib_umad_kill_port(&umad_dev->port[i]);
  961. kobject_put(&umad_dev->kobj);
  962. }
  963. static char *umad_devnode(struct device *dev, umode_t *mode)
  964. {
  965. return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
  966. }
  967. static int __init ib_umad_init(void)
  968. {
  969. int ret;
  970. ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2,
  971. "infiniband_mad");
  972. if (ret) {
  973. printk(KERN_ERR "user_mad: couldn't register device number\n");
  974. goto out;
  975. }
  976. umad_class = class_create(THIS_MODULE, "infiniband_mad");
  977. if (IS_ERR(umad_class)) {
  978. ret = PTR_ERR(umad_class);
  979. printk(KERN_ERR "user_mad: couldn't create class infiniband_mad\n");
  980. goto out_chrdev;
  981. }
  982. umad_class->devnode = umad_devnode;
  983. ret = class_create_file(umad_class, &class_attr_abi_version.attr);
  984. if (ret) {
  985. printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n");
  986. goto out_class;
  987. }
  988. ret = ib_register_client(&umad_client);
  989. if (ret) {
  990. printk(KERN_ERR "user_mad: couldn't register ib_umad client\n");
  991. goto out_class;
  992. }
  993. return 0;
  994. out_class:
  995. class_destroy(umad_class);
  996. out_chrdev:
  997. unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
  998. out:
  999. return ret;
  1000. }
  1001. static void __exit ib_umad_cleanup(void)
  1002. {
  1003. ib_unregister_client(&umad_client);
  1004. class_destroy(umad_class);
  1005. unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
  1006. if (overflow_maj)
  1007. unregister_chrdev_region(overflow_maj, IB_UMAD_MAX_PORTS * 2);
  1008. }
  1009. module_init(ib_umad_init);
  1010. module_exit(ib_umad_cleanup);