user_mad.c 34 KB

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