user_mad.c 34 KB

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