user_mad.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. *
  34. * $Id: user_mad.c 5596 2006-03-03 01:00:07Z sean.hefty $
  35. */
  36. #include <linux/module.h>
  37. #include <linux/init.h>
  38. #include <linux/device.h>
  39. #include <linux/err.h>
  40. #include <linux/fs.h>
  41. #include <linux/cdev.h>
  42. #include <linux/pci.h>
  43. #include <linux/dma-mapping.h>
  44. #include <linux/poll.h>
  45. #include <linux/rwsem.h>
  46. #include <linux/kref.h>
  47. #include <asm/uaccess.h>
  48. #include <asm/semaphore.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 *dev;
  83. struct class_device *class_dev;
  84. struct cdev *sm_dev;
  85. struct class_device *sm_class_dev;
  86. struct semaphore sm_sem;
  87. struct rw_semaphore 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 ib_umad_port *port;
  101. struct list_head recv_list;
  102. struct list_head port_list;
  103. spinlock_t recv_lock;
  104. wait_queue_head_t recv_wait;
  105. struct ib_mad_agent *agent[IB_UMAD_MAX_AGENTS];
  106. int agents_dead;
  107. };
  108. struct ib_umad_packet {
  109. struct ib_mad_send_buf *msg;
  110. struct ib_mad_recv_wc *recv_wc;
  111. struct list_head list;
  112. int length;
  113. struct ib_user_mad mad;
  114. };
  115. static struct class *umad_class;
  116. static const dev_t base_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE);
  117. static DEFINE_SPINLOCK(port_lock);
  118. static struct ib_umad_port *umad_port[IB_UMAD_MAX_PORTS];
  119. static DECLARE_BITMAP(dev_map, IB_UMAD_MAX_PORTS * 2);
  120. static void ib_umad_add_one(struct ib_device *device);
  121. static void ib_umad_remove_one(struct ib_device *device);
  122. static void ib_umad_release_dev(struct kref *ref)
  123. {
  124. struct ib_umad_device *dev =
  125. container_of(ref, struct ib_umad_device, ref);
  126. kfree(dev);
  127. }
  128. /* caller must hold port->mutex at least for reading */
  129. static struct ib_mad_agent *__get_agent(struct ib_umad_file *file, int id)
  130. {
  131. return file->agents_dead ? NULL : file->agent[id];
  132. }
  133. static int queue_packet(struct ib_umad_file *file,
  134. struct ib_mad_agent *agent,
  135. struct ib_umad_packet *packet)
  136. {
  137. int ret = 1;
  138. down_read(&file->port->mutex);
  139. for (packet->mad.hdr.id = 0;
  140. packet->mad.hdr.id < IB_UMAD_MAX_AGENTS;
  141. packet->mad.hdr.id++)
  142. if (agent == __get_agent(file, packet->mad.hdr.id)) {
  143. spin_lock_irq(&file->recv_lock);
  144. list_add_tail(&packet->list, &file->recv_list);
  145. spin_unlock_irq(&file->recv_lock);
  146. wake_up_interruptible(&file->recv_wait);
  147. ret = 0;
  148. break;
  149. }
  150. up_read(&file->port->mutex);
  151. return ret;
  152. }
  153. static int data_offset(u8 mgmt_class)
  154. {
  155. if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
  156. return IB_MGMT_SA_HDR;
  157. else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
  158. (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
  159. return IB_MGMT_VENDOR_HDR;
  160. else
  161. return IB_MGMT_RMPP_HDR;
  162. }
  163. static void send_handler(struct ib_mad_agent *agent,
  164. struct ib_mad_send_wc *send_wc)
  165. {
  166. struct ib_umad_file *file = agent->context;
  167. struct ib_umad_packet *packet = send_wc->send_buf->context[0];
  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 = sizeof (struct ib_user_mad) +
  192. mad_recv_wc->mad_len;
  193. packet->mad.hdr.qpn = cpu_to_be32(mad_recv_wc->wc->src_qp);
  194. packet->mad.hdr.lid = cpu_to_be16(mad_recv_wc->wc->slid);
  195. packet->mad.hdr.sl = mad_recv_wc->wc->sl;
  196. packet->mad.hdr.path_bits = mad_recv_wc->wc->dlid_path_bits;
  197. packet->mad.hdr.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH);
  198. if (packet->mad.hdr.grh_present) {
  199. /* XXX parse GRH */
  200. packet->mad.hdr.gid_index = 0;
  201. packet->mad.hdr.hop_limit = 0;
  202. packet->mad.hdr.traffic_class = 0;
  203. memset(packet->mad.hdr.gid, 0, 16);
  204. packet->mad.hdr.flow_label = 0;
  205. }
  206. if (queue_packet(file, agent, packet))
  207. goto err2;
  208. return;
  209. err2:
  210. kfree(packet);
  211. err1:
  212. ib_free_recv_mad(mad_recv_wc);
  213. }
  214. static ssize_t copy_recv_mad(char __user *buf, struct ib_umad_packet *packet,
  215. size_t count)
  216. {
  217. struct ib_mad_recv_buf *recv_buf;
  218. int left, seg_payload, offset, max_seg_payload;
  219. /* We need enough room to copy the first (or only) MAD segment. */
  220. recv_buf = &packet->recv_wc->recv_buf;
  221. if ((packet->length <= sizeof (*recv_buf->mad) &&
  222. count < sizeof (packet->mad) + packet->length) ||
  223. (packet->length > sizeof (*recv_buf->mad) &&
  224. count < sizeof (packet->mad) + sizeof (*recv_buf->mad)))
  225. return -EINVAL;
  226. if (copy_to_user(buf, &packet->mad, sizeof (packet->mad)))
  227. return -EFAULT;
  228. buf += sizeof (packet->mad);
  229. seg_payload = min_t(int, packet->length, sizeof (*recv_buf->mad));
  230. if (copy_to_user(buf, recv_buf->mad, seg_payload))
  231. return -EFAULT;
  232. if (seg_payload < packet->length) {
  233. /*
  234. * Multipacket RMPP MAD message. Copy remainder of message.
  235. * Note that last segment may have a shorter payload.
  236. */
  237. if (count < sizeof (packet->mad) + packet->length) {
  238. /*
  239. * The buffer is too small, return the first RMPP segment,
  240. * which includes the RMPP message length.
  241. */
  242. return -ENOSPC;
  243. }
  244. offset = data_offset(recv_buf->mad->mad_hdr.mgmt_class);
  245. max_seg_payload = sizeof (struct ib_mad) - offset;
  246. for (left = packet->length - seg_payload, buf += seg_payload;
  247. left; left -= seg_payload, buf += seg_payload) {
  248. recv_buf = container_of(recv_buf->list.next,
  249. struct ib_mad_recv_buf, list);
  250. seg_payload = min(left, max_seg_payload);
  251. if (copy_to_user(buf, ((void *) recv_buf->mad) + offset,
  252. seg_payload))
  253. return -EFAULT;
  254. }
  255. }
  256. return sizeof (packet->mad) + packet->length;
  257. }
  258. static ssize_t copy_send_mad(char __user *buf, struct ib_umad_packet *packet,
  259. size_t count)
  260. {
  261. ssize_t size = sizeof (packet->mad) + packet->length;
  262. if (count < size)
  263. return -EINVAL;
  264. if (copy_to_user(buf, &packet->mad, size))
  265. return -EFAULT;
  266. return size;
  267. }
  268. static ssize_t ib_umad_read(struct file *filp, char __user *buf,
  269. size_t count, loff_t *pos)
  270. {
  271. struct ib_umad_file *file = filp->private_data;
  272. struct ib_umad_packet *packet;
  273. ssize_t ret;
  274. if (count < sizeof (struct ib_user_mad))
  275. return -EINVAL;
  276. spin_lock_irq(&file->recv_lock);
  277. while (list_empty(&file->recv_list)) {
  278. spin_unlock_irq(&file->recv_lock);
  279. if (filp->f_flags & O_NONBLOCK)
  280. return -EAGAIN;
  281. if (wait_event_interruptible(file->recv_wait,
  282. !list_empty(&file->recv_list)))
  283. return -ERESTARTSYS;
  284. spin_lock_irq(&file->recv_lock);
  285. }
  286. packet = list_entry(file->recv_list.next, struct ib_umad_packet, list);
  287. list_del(&packet->list);
  288. spin_unlock_irq(&file->recv_lock);
  289. if (packet->recv_wc)
  290. ret = copy_recv_mad(buf, packet, count);
  291. else
  292. ret = copy_send_mad(buf, packet, count);
  293. if (ret < 0) {
  294. /* Requeue packet */
  295. spin_lock_irq(&file->recv_lock);
  296. list_add(&packet->list, &file->recv_list);
  297. spin_unlock_irq(&file->recv_lock);
  298. } else {
  299. if (packet->recv_wc)
  300. ib_free_recv_mad(packet->recv_wc);
  301. kfree(packet);
  302. }
  303. return ret;
  304. }
  305. static int copy_rmpp_mad(struct ib_mad_send_buf *msg, const char __user *buf)
  306. {
  307. int left, seg;
  308. /* Copy class specific header */
  309. if ((msg->hdr_len > IB_MGMT_RMPP_HDR) &&
  310. copy_from_user(msg->mad + IB_MGMT_RMPP_HDR, buf + IB_MGMT_RMPP_HDR,
  311. msg->hdr_len - IB_MGMT_RMPP_HDR))
  312. return -EFAULT;
  313. /* All headers are in place. Copy data segments. */
  314. for (seg = 1, left = msg->data_len, buf += msg->hdr_len; left > 0;
  315. seg++, left -= msg->seg_size, buf += msg->seg_size) {
  316. if (copy_from_user(ib_get_rmpp_segment(msg, seg), buf,
  317. min(left, msg->seg_size)))
  318. return -EFAULT;
  319. }
  320. return 0;
  321. }
  322. static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
  323. size_t count, loff_t *pos)
  324. {
  325. struct ib_umad_file *file = filp->private_data;
  326. struct ib_umad_packet *packet;
  327. struct ib_mad_agent *agent;
  328. struct ib_ah_attr ah_attr;
  329. struct ib_ah *ah;
  330. struct ib_rmpp_mad *rmpp_mad;
  331. u8 method;
  332. __be64 *tid;
  333. int ret, data_len, hdr_len, copy_offset, rmpp_active;
  334. if (count < sizeof (struct ib_user_mad) + IB_MGMT_RMPP_HDR)
  335. return -EINVAL;
  336. packet = kzalloc(sizeof *packet + IB_MGMT_RMPP_HDR, GFP_KERNEL);
  337. if (!packet)
  338. return -ENOMEM;
  339. if (copy_from_user(&packet->mad, buf,
  340. sizeof (struct ib_user_mad) + IB_MGMT_RMPP_HDR)) {
  341. ret = -EFAULT;
  342. goto err;
  343. }
  344. if (packet->mad.hdr.id < 0 ||
  345. packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) {
  346. ret = -EINVAL;
  347. goto err;
  348. }
  349. down_read(&file->port->mutex);
  350. agent = __get_agent(file, packet->mad.hdr.id);
  351. if (!agent) {
  352. ret = -EINVAL;
  353. goto err_up;
  354. }
  355. memset(&ah_attr, 0, sizeof ah_attr);
  356. ah_attr.dlid = be16_to_cpu(packet->mad.hdr.lid);
  357. ah_attr.sl = packet->mad.hdr.sl;
  358. ah_attr.src_path_bits = packet->mad.hdr.path_bits;
  359. ah_attr.port_num = file->port->port_num;
  360. if (packet->mad.hdr.grh_present) {
  361. ah_attr.ah_flags = IB_AH_GRH;
  362. memcpy(ah_attr.grh.dgid.raw, packet->mad.hdr.gid, 16);
  363. ah_attr.grh.flow_label = be32_to_cpu(packet->mad.hdr.flow_label);
  364. ah_attr.grh.hop_limit = packet->mad.hdr.hop_limit;
  365. ah_attr.grh.traffic_class = packet->mad.hdr.traffic_class;
  366. }
  367. ah = ib_create_ah(agent->qp->pd, &ah_attr);
  368. if (IS_ERR(ah)) {
  369. ret = PTR_ERR(ah);
  370. goto err_up;
  371. }
  372. rmpp_mad = (struct ib_rmpp_mad *) packet->mad.data;
  373. if (rmpp_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_ADM) {
  374. hdr_len = IB_MGMT_SA_HDR;
  375. copy_offset = IB_MGMT_RMPP_HDR;
  376. rmpp_active = ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
  377. IB_MGMT_RMPP_FLAG_ACTIVE;
  378. } else if (rmpp_mad->mad_hdr.mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START &&
  379. rmpp_mad->mad_hdr.mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END) {
  380. hdr_len = IB_MGMT_VENDOR_HDR;
  381. copy_offset = IB_MGMT_RMPP_HDR;
  382. rmpp_active = ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
  383. IB_MGMT_RMPP_FLAG_ACTIVE;
  384. } else {
  385. hdr_len = IB_MGMT_MAD_HDR;
  386. copy_offset = IB_MGMT_MAD_HDR;
  387. rmpp_active = 0;
  388. }
  389. data_len = count - sizeof (struct ib_user_mad) - hdr_len;
  390. packet->msg = ib_create_send_mad(agent,
  391. be32_to_cpu(packet->mad.hdr.qpn),
  392. 0, rmpp_active, hdr_len,
  393. data_len, GFP_KERNEL);
  394. if (IS_ERR(packet->msg)) {
  395. ret = PTR_ERR(packet->msg);
  396. goto err_ah;
  397. }
  398. packet->msg->ah = ah;
  399. packet->msg->timeout_ms = packet->mad.hdr.timeout_ms;
  400. packet->msg->retries = packet->mad.hdr.retries;
  401. packet->msg->context[0] = packet;
  402. /* Copy MAD header. Any RMPP header is already in place. */
  403. memcpy(packet->msg->mad, packet->mad.data, IB_MGMT_MAD_HDR);
  404. buf += sizeof (struct ib_user_mad);
  405. if (!rmpp_active) {
  406. if (copy_from_user(packet->msg->mad + copy_offset,
  407. buf + copy_offset,
  408. hdr_len + data_len - copy_offset)) {
  409. ret = -EFAULT;
  410. goto err_msg;
  411. }
  412. } else {
  413. ret = copy_rmpp_mad(packet->msg, buf);
  414. if (ret)
  415. goto err_msg;
  416. }
  417. /*
  418. * If userspace is generating a request that will generate a
  419. * response, we need to make sure the high-order part of the
  420. * transaction ID matches the agent being used to send the
  421. * MAD.
  422. */
  423. method = ((struct ib_mad_hdr *) packet->msg->mad)->method;
  424. if (!(method & IB_MGMT_METHOD_RESP) &&
  425. method != IB_MGMT_METHOD_TRAP_REPRESS &&
  426. method != IB_MGMT_METHOD_SEND) {
  427. tid = &((struct ib_mad_hdr *) packet->msg->mad)->tid;
  428. *tid = cpu_to_be64(((u64) agent->hi_tid) << 32 |
  429. (be64_to_cpup(tid) & 0xffffffff));
  430. }
  431. ret = ib_post_send_mad(packet->msg, NULL);
  432. if (ret)
  433. goto err_msg;
  434. up_read(&file->port->mutex);
  435. return count;
  436. err_msg:
  437. ib_free_send_mad(packet->msg);
  438. err_ah:
  439. ib_destroy_ah(ah);
  440. err_up:
  441. up_read(&file->port->mutex);
  442. err:
  443. kfree(packet);
  444. return ret;
  445. }
  446. static unsigned int ib_umad_poll(struct file *filp, struct poll_table_struct *wait)
  447. {
  448. struct ib_umad_file *file = filp->private_data;
  449. /* we will always be able to post a MAD send */
  450. unsigned int mask = POLLOUT | POLLWRNORM;
  451. poll_wait(filp, &file->recv_wait, wait);
  452. if (!list_empty(&file->recv_list))
  453. mask |= POLLIN | POLLRDNORM;
  454. return mask;
  455. }
  456. static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg)
  457. {
  458. struct ib_user_mad_reg_req ureq;
  459. struct ib_mad_reg_req req;
  460. struct ib_mad_agent *agent;
  461. int agent_id;
  462. int ret;
  463. down_write(&file->port->mutex);
  464. if (!file->port->ib_dev) {
  465. ret = -EPIPE;
  466. goto out;
  467. }
  468. if (copy_from_user(&ureq, (void __user *) arg, sizeof ureq)) {
  469. ret = -EFAULT;
  470. goto out;
  471. }
  472. if (ureq.qpn != 0 && ureq.qpn != 1) {
  473. ret = -EINVAL;
  474. goto out;
  475. }
  476. for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
  477. if (!__get_agent(file, agent_id))
  478. goto found;
  479. ret = -ENOMEM;
  480. goto out;
  481. found:
  482. if (ureq.mgmt_class) {
  483. req.mgmt_class = ureq.mgmt_class;
  484. req.mgmt_class_version = ureq.mgmt_class_version;
  485. memcpy(req.method_mask, ureq.method_mask, sizeof req.method_mask);
  486. memcpy(req.oui, ureq.oui, sizeof req.oui);
  487. }
  488. agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,
  489. ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI,
  490. ureq.mgmt_class ? &req : NULL,
  491. ureq.rmpp_version,
  492. send_handler, recv_handler, file);
  493. if (IS_ERR(agent)) {
  494. ret = PTR_ERR(agent);
  495. goto out;
  496. }
  497. if (put_user(agent_id,
  498. (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {
  499. ret = -EFAULT;
  500. ib_unregister_mad_agent(agent);
  501. goto out;
  502. }
  503. file->agent[agent_id] = agent;
  504. ret = 0;
  505. out:
  506. up_write(&file->port->mutex);
  507. return ret;
  508. }
  509. static int ib_umad_unreg_agent(struct ib_umad_file *file, unsigned long arg)
  510. {
  511. struct ib_mad_agent *agent = NULL;
  512. u32 id;
  513. int ret = 0;
  514. if (get_user(id, (u32 __user *) arg))
  515. return -EFAULT;
  516. down_write(&file->port->mutex);
  517. if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !__get_agent(file, id)) {
  518. ret = -EINVAL;
  519. goto out;
  520. }
  521. agent = file->agent[id];
  522. file->agent[id] = NULL;
  523. out:
  524. up_write(&file->port->mutex);
  525. if (agent)
  526. ib_unregister_mad_agent(agent);
  527. return ret;
  528. }
  529. static long ib_umad_ioctl(struct file *filp, unsigned int cmd,
  530. unsigned long arg)
  531. {
  532. switch (cmd) {
  533. case IB_USER_MAD_REGISTER_AGENT:
  534. return ib_umad_reg_agent(filp->private_data, arg);
  535. case IB_USER_MAD_UNREGISTER_AGENT:
  536. return ib_umad_unreg_agent(filp->private_data, arg);
  537. default:
  538. return -ENOIOCTLCMD;
  539. }
  540. }
  541. static int ib_umad_open(struct inode *inode, struct file *filp)
  542. {
  543. struct ib_umad_port *port;
  544. struct ib_umad_file *file;
  545. int ret = 0;
  546. spin_lock(&port_lock);
  547. port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE];
  548. if (port)
  549. kref_get(&port->umad_dev->ref);
  550. spin_unlock(&port_lock);
  551. if (!port)
  552. return -ENXIO;
  553. down_write(&port->mutex);
  554. if (!port->ib_dev) {
  555. ret = -ENXIO;
  556. goto out;
  557. }
  558. file = kzalloc(sizeof *file, GFP_KERNEL);
  559. if (!file) {
  560. kref_put(&port->umad_dev->ref, ib_umad_release_dev);
  561. ret = -ENOMEM;
  562. goto out;
  563. }
  564. spin_lock_init(&file->recv_lock);
  565. INIT_LIST_HEAD(&file->recv_list);
  566. init_waitqueue_head(&file->recv_wait);
  567. file->port = port;
  568. filp->private_data = file;
  569. list_add_tail(&file->port_list, &port->file_list);
  570. out:
  571. up_write(&port->mutex);
  572. return ret;
  573. }
  574. static int ib_umad_close(struct inode *inode, struct file *filp)
  575. {
  576. struct ib_umad_file *file = filp->private_data;
  577. struct ib_umad_device *dev = file->port->umad_dev;
  578. struct ib_umad_packet *packet, *tmp;
  579. int already_dead;
  580. int i;
  581. down_write(&file->port->mutex);
  582. already_dead = file->agents_dead;
  583. file->agents_dead = 1;
  584. list_for_each_entry_safe(packet, tmp, &file->recv_list, list) {
  585. if (packet->recv_wc)
  586. ib_free_recv_mad(packet->recv_wc);
  587. kfree(packet);
  588. }
  589. list_del(&file->port_list);
  590. downgrade_write(&file->port->mutex);
  591. if (!already_dead)
  592. for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
  593. if (file->agent[i])
  594. ib_unregister_mad_agent(file->agent[i]);
  595. up_read(&file->port->mutex);
  596. kfree(file);
  597. kref_put(&dev->ref, ib_umad_release_dev);
  598. return 0;
  599. }
  600. static struct file_operations umad_fops = {
  601. .owner = THIS_MODULE,
  602. .read = ib_umad_read,
  603. .write = ib_umad_write,
  604. .poll = ib_umad_poll,
  605. .unlocked_ioctl = ib_umad_ioctl,
  606. .compat_ioctl = ib_umad_ioctl,
  607. .open = ib_umad_open,
  608. .release = ib_umad_close
  609. };
  610. static int ib_umad_sm_open(struct inode *inode, struct file *filp)
  611. {
  612. struct ib_umad_port *port;
  613. struct ib_port_modify props = {
  614. .set_port_cap_mask = IB_PORT_SM
  615. };
  616. int ret;
  617. spin_lock(&port_lock);
  618. port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE - IB_UMAD_MAX_PORTS];
  619. if (port)
  620. kref_get(&port->umad_dev->ref);
  621. spin_unlock(&port_lock);
  622. if (!port)
  623. return -ENXIO;
  624. if (filp->f_flags & O_NONBLOCK) {
  625. if (down_trylock(&port->sm_sem)) {
  626. ret = -EAGAIN;
  627. goto fail;
  628. }
  629. } else {
  630. if (down_interruptible(&port->sm_sem)) {
  631. ret = -ERESTARTSYS;
  632. goto fail;
  633. }
  634. }
  635. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  636. if (ret) {
  637. up(&port->sm_sem);
  638. goto fail;
  639. }
  640. filp->private_data = port;
  641. return 0;
  642. fail:
  643. kref_put(&port->umad_dev->ref, ib_umad_release_dev);
  644. return ret;
  645. }
  646. static int ib_umad_sm_close(struct inode *inode, struct file *filp)
  647. {
  648. struct ib_umad_port *port = filp->private_data;
  649. struct ib_port_modify props = {
  650. .clr_port_cap_mask = IB_PORT_SM
  651. };
  652. int ret = 0;
  653. down_write(&port->mutex);
  654. if (port->ib_dev)
  655. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  656. up_write(&port->mutex);
  657. up(&port->sm_sem);
  658. kref_put(&port->umad_dev->ref, ib_umad_release_dev);
  659. return ret;
  660. }
  661. static struct file_operations umad_sm_fops = {
  662. .owner = THIS_MODULE,
  663. .open = ib_umad_sm_open,
  664. .release = ib_umad_sm_close
  665. };
  666. static struct ib_client umad_client = {
  667. .name = "umad",
  668. .add = ib_umad_add_one,
  669. .remove = ib_umad_remove_one
  670. };
  671. static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
  672. {
  673. struct ib_umad_port *port = class_get_devdata(class_dev);
  674. if (!port)
  675. return -ENODEV;
  676. return sprintf(buf, "%s\n", port->ib_dev->name);
  677. }
  678. static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  679. static ssize_t show_port(struct class_device *class_dev, char *buf)
  680. {
  681. struct ib_umad_port *port = class_get_devdata(class_dev);
  682. if (!port)
  683. return -ENODEV;
  684. return sprintf(buf, "%d\n", port->port_num);
  685. }
  686. static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
  687. static ssize_t show_abi_version(struct class *class, char *buf)
  688. {
  689. return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION);
  690. }
  691. static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  692. static int ib_umad_init_port(struct ib_device *device, int port_num,
  693. struct ib_umad_port *port)
  694. {
  695. spin_lock(&port_lock);
  696. port->dev_num = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS);
  697. if (port->dev_num >= IB_UMAD_MAX_PORTS) {
  698. spin_unlock(&port_lock);
  699. return -1;
  700. }
  701. set_bit(port->dev_num, dev_map);
  702. spin_unlock(&port_lock);
  703. port->ib_dev = device;
  704. port->port_num = port_num;
  705. init_MUTEX(&port->sm_sem);
  706. init_rwsem(&port->mutex);
  707. INIT_LIST_HEAD(&port->file_list);
  708. port->dev = cdev_alloc();
  709. if (!port->dev)
  710. return -1;
  711. port->dev->owner = THIS_MODULE;
  712. port->dev->ops = &umad_fops;
  713. kobject_set_name(&port->dev->kobj, "umad%d", port->dev_num);
  714. if (cdev_add(port->dev, base_dev + port->dev_num, 1))
  715. goto err_cdev;
  716. port->class_dev = class_device_create(umad_class, NULL, port->dev->dev,
  717. device->dma_device,
  718. "umad%d", port->dev_num);
  719. if (IS_ERR(port->class_dev))
  720. goto err_cdev;
  721. if (class_device_create_file(port->class_dev, &class_device_attr_ibdev))
  722. goto err_class;
  723. if (class_device_create_file(port->class_dev, &class_device_attr_port))
  724. goto err_class;
  725. port->sm_dev = cdev_alloc();
  726. if (!port->sm_dev)
  727. goto err_class;
  728. port->sm_dev->owner = THIS_MODULE;
  729. port->sm_dev->ops = &umad_sm_fops;
  730. kobject_set_name(&port->sm_dev->kobj, "issm%d", port->dev_num);
  731. if (cdev_add(port->sm_dev, base_dev + port->dev_num + IB_UMAD_MAX_PORTS, 1))
  732. goto err_sm_cdev;
  733. port->sm_class_dev = class_device_create(umad_class, NULL, port->sm_dev->dev,
  734. device->dma_device,
  735. "issm%d", port->dev_num);
  736. if (IS_ERR(port->sm_class_dev))
  737. goto err_sm_cdev;
  738. class_set_devdata(port->class_dev, port);
  739. class_set_devdata(port->sm_class_dev, port);
  740. if (class_device_create_file(port->sm_class_dev, &class_device_attr_ibdev))
  741. goto err_sm_class;
  742. if (class_device_create_file(port->sm_class_dev, &class_device_attr_port))
  743. goto err_sm_class;
  744. spin_lock(&port_lock);
  745. umad_port[port->dev_num] = port;
  746. spin_unlock(&port_lock);
  747. return 0;
  748. err_sm_class:
  749. class_device_destroy(umad_class, port->sm_dev->dev);
  750. err_sm_cdev:
  751. cdev_del(port->sm_dev);
  752. err_class:
  753. class_device_destroy(umad_class, port->dev->dev);
  754. err_cdev:
  755. cdev_del(port->dev);
  756. clear_bit(port->dev_num, dev_map);
  757. return -1;
  758. }
  759. static void ib_umad_kill_port(struct ib_umad_port *port)
  760. {
  761. struct ib_umad_file *file;
  762. int id;
  763. class_set_devdata(port->class_dev, NULL);
  764. class_set_devdata(port->sm_class_dev, NULL);
  765. class_device_destroy(umad_class, port->dev->dev);
  766. class_device_destroy(umad_class, port->sm_dev->dev);
  767. cdev_del(port->dev);
  768. cdev_del(port->sm_dev);
  769. spin_lock(&port_lock);
  770. umad_port[port->dev_num] = NULL;
  771. spin_unlock(&port_lock);
  772. down_write(&port->mutex);
  773. port->ib_dev = NULL;
  774. /*
  775. * Now go through the list of files attached to this port and
  776. * unregister all of their MAD agents. We need to hold
  777. * port->mutex while doing this to avoid racing with
  778. * ib_umad_close(), but we can't hold the mutex for writing
  779. * while calling ib_unregister_mad_agent(), since that might
  780. * deadlock by calling back into queue_packet(). So we
  781. * downgrade our lock to a read lock, and then drop and
  782. * reacquire the write lock for the next iteration.
  783. *
  784. * We do list_del_init() on the file's list_head so that the
  785. * list_del in ib_umad_close() is still OK, even after the
  786. * file is removed from the list.
  787. */
  788. while (!list_empty(&port->file_list)) {
  789. file = list_entry(port->file_list.next, struct ib_umad_file,
  790. port_list);
  791. file->agents_dead = 1;
  792. list_del_init(&file->port_list);
  793. downgrade_write(&port->mutex);
  794. for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id)
  795. if (file->agent[id])
  796. ib_unregister_mad_agent(file->agent[id]);
  797. up_read(&port->mutex);
  798. down_write(&port->mutex);
  799. }
  800. up_write(&port->mutex);
  801. clear_bit(port->dev_num, dev_map);
  802. }
  803. static void ib_umad_add_one(struct ib_device *device)
  804. {
  805. struct ib_umad_device *umad_dev;
  806. int s, e, i;
  807. if (device->node_type == IB_NODE_SWITCH)
  808. s = e = 0;
  809. else {
  810. s = 1;
  811. e = device->phys_port_cnt;
  812. }
  813. umad_dev = kzalloc(sizeof *umad_dev +
  814. (e - s + 1) * sizeof (struct ib_umad_port),
  815. GFP_KERNEL);
  816. if (!umad_dev)
  817. return;
  818. kref_init(&umad_dev->ref);
  819. umad_dev->start_port = s;
  820. umad_dev->end_port = e;
  821. for (i = s; i <= e; ++i) {
  822. umad_dev->port[i - s].umad_dev = umad_dev;
  823. if (ib_umad_init_port(device, i, &umad_dev->port[i - s]))
  824. goto err;
  825. }
  826. ib_set_client_data(device, &umad_client, umad_dev);
  827. return;
  828. err:
  829. while (--i >= s)
  830. ib_umad_kill_port(&umad_dev->port[i - s]);
  831. kref_put(&umad_dev->ref, ib_umad_release_dev);
  832. }
  833. static void ib_umad_remove_one(struct ib_device *device)
  834. {
  835. struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client);
  836. int i;
  837. if (!umad_dev)
  838. return;
  839. for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i)
  840. ib_umad_kill_port(&umad_dev->port[i]);
  841. kref_put(&umad_dev->ref, ib_umad_release_dev);
  842. }
  843. static int __init ib_umad_init(void)
  844. {
  845. int ret;
  846. ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2,
  847. "infiniband_mad");
  848. if (ret) {
  849. printk(KERN_ERR "user_mad: couldn't register device number\n");
  850. goto out;
  851. }
  852. umad_class = class_create(THIS_MODULE, "infiniband_mad");
  853. if (IS_ERR(umad_class)) {
  854. ret = PTR_ERR(umad_class);
  855. printk(KERN_ERR "user_mad: couldn't create class infiniband_mad\n");
  856. goto out_chrdev;
  857. }
  858. ret = class_create_file(umad_class, &class_attr_abi_version);
  859. if (ret) {
  860. printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n");
  861. goto out_class;
  862. }
  863. ret = ib_register_client(&umad_client);
  864. if (ret) {
  865. printk(KERN_ERR "user_mad: couldn't register ib_umad client\n");
  866. goto out_class;
  867. }
  868. return 0;
  869. out_class:
  870. class_destroy(umad_class);
  871. out_chrdev:
  872. unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
  873. out:
  874. return ret;
  875. }
  876. static void __exit ib_umad_cleanup(void)
  877. {
  878. ib_unregister_client(&umad_client);
  879. class_destroy(umad_class);
  880. unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
  881. }
  882. module_init(ib_umad_init);
  883. module_exit(ib_umad_cleanup);