user_mad.c 30 KB

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