user_mad.c 27 KB

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