user_mad.c 36 KB

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