user_mad.c 34 KB

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