ipath_mad.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  1. /*
  2. * Copyright (c) 2006 QLogic, Inc. All rights reserved.
  3. * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <rdma/ib_smi.h>
  34. #include "ipath_kernel.h"
  35. #include "ipath_verbs.h"
  36. #include "ipath_common.h"
  37. #define IB_SMP_UNSUP_VERSION __constant_htons(0x0004)
  38. #define IB_SMP_UNSUP_METHOD __constant_htons(0x0008)
  39. #define IB_SMP_UNSUP_METH_ATTR __constant_htons(0x000C)
  40. #define IB_SMP_INVALID_FIELD __constant_htons(0x001C)
  41. static int reply(struct ib_smp *smp)
  42. {
  43. /*
  44. * The verbs framework will handle the directed/LID route
  45. * packet changes.
  46. */
  47. smp->method = IB_MGMT_METHOD_GET_RESP;
  48. if (smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
  49. smp->status |= IB_SMP_DIRECTION;
  50. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
  51. }
  52. static int recv_subn_get_nodedescription(struct ib_smp *smp,
  53. struct ib_device *ibdev)
  54. {
  55. if (smp->attr_mod)
  56. smp->status |= IB_SMP_INVALID_FIELD;
  57. strncpy(smp->data, ibdev->node_desc, sizeof(smp->data));
  58. return reply(smp);
  59. }
  60. struct nodeinfo {
  61. u8 base_version;
  62. u8 class_version;
  63. u8 node_type;
  64. u8 num_ports;
  65. __be64 sys_guid;
  66. __be64 node_guid;
  67. __be64 port_guid;
  68. __be16 partition_cap;
  69. __be16 device_id;
  70. __be32 revision;
  71. u8 local_port_num;
  72. u8 vendor_id[3];
  73. } __attribute__ ((packed));
  74. static int recv_subn_get_nodeinfo(struct ib_smp *smp,
  75. struct ib_device *ibdev, u8 port)
  76. {
  77. struct nodeinfo *nip = (struct nodeinfo *)&smp->data;
  78. struct ipath_devdata *dd = to_idev(ibdev)->dd;
  79. u32 vendor, majrev, minrev;
  80. /* GUID 0 is illegal */
  81. if (smp->attr_mod || (dd->ipath_guid == 0))
  82. smp->status |= IB_SMP_INVALID_FIELD;
  83. nip->base_version = 1;
  84. nip->class_version = 1;
  85. nip->node_type = 1; /* channel adapter */
  86. /*
  87. * XXX The num_ports value will need a layer function to get
  88. * the value if we ever have more than one IB port on a chip.
  89. * We will also need to get the GUID for the port.
  90. */
  91. nip->num_ports = ibdev->phys_port_cnt;
  92. /* This is already in network order */
  93. nip->sys_guid = to_idev(ibdev)->sys_image_guid;
  94. nip->node_guid = dd->ipath_guid;
  95. nip->port_guid = nip->sys_guid;
  96. nip->partition_cap = cpu_to_be16(ipath_get_npkeys(dd));
  97. nip->device_id = cpu_to_be16(dd->ipath_deviceid);
  98. majrev = dd->ipath_majrev;
  99. minrev = dd->ipath_minrev;
  100. nip->revision = cpu_to_be32((majrev << 16) | minrev);
  101. nip->local_port_num = port;
  102. vendor = dd->ipath_vendorid;
  103. nip->vendor_id[0] = 0;
  104. nip->vendor_id[1] = vendor >> 8;
  105. nip->vendor_id[2] = vendor;
  106. return reply(smp);
  107. }
  108. static int recv_subn_get_guidinfo(struct ib_smp *smp,
  109. struct ib_device *ibdev)
  110. {
  111. u32 startgx = 8 * be32_to_cpu(smp->attr_mod);
  112. __be64 *p = (__be64 *) smp->data;
  113. /* 32 blocks of 8 64-bit GUIDs per block */
  114. memset(smp->data, 0, sizeof(smp->data));
  115. /*
  116. * We only support one GUID for now. If this changes, the
  117. * portinfo.guid_cap field needs to be updated too.
  118. */
  119. if (startgx == 0) {
  120. __be64 g = to_idev(ibdev)->dd->ipath_guid;
  121. if (g == 0)
  122. /* GUID 0 is illegal */
  123. smp->status |= IB_SMP_INVALID_FIELD;
  124. else
  125. /* The first is a copy of the read-only HW GUID. */
  126. *p = g;
  127. } else
  128. smp->status |= IB_SMP_INVALID_FIELD;
  129. return reply(smp);
  130. }
  131. static int get_overrunthreshold(struct ipath_devdata *dd)
  132. {
  133. return (dd->ipath_ibcctrl >>
  134. INFINIPATH_IBCC_OVERRUNTHRESHOLD_SHIFT) &
  135. INFINIPATH_IBCC_OVERRUNTHRESHOLD_MASK;
  136. }
  137. /**
  138. * set_overrunthreshold - set the overrun threshold
  139. * @dd: the infinipath device
  140. * @n: the new threshold
  141. *
  142. * Note that this will only take effect when the link state changes.
  143. */
  144. static int set_overrunthreshold(struct ipath_devdata *dd, unsigned n)
  145. {
  146. unsigned v;
  147. v = (dd->ipath_ibcctrl >> INFINIPATH_IBCC_OVERRUNTHRESHOLD_SHIFT) &
  148. INFINIPATH_IBCC_OVERRUNTHRESHOLD_MASK;
  149. if (v != n) {
  150. dd->ipath_ibcctrl &=
  151. ~(INFINIPATH_IBCC_OVERRUNTHRESHOLD_MASK <<
  152. INFINIPATH_IBCC_OVERRUNTHRESHOLD_SHIFT);
  153. dd->ipath_ibcctrl |=
  154. (u64) n << INFINIPATH_IBCC_OVERRUNTHRESHOLD_SHIFT;
  155. ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
  156. dd->ipath_ibcctrl);
  157. }
  158. return 0;
  159. }
  160. static int get_phyerrthreshold(struct ipath_devdata *dd)
  161. {
  162. return (dd->ipath_ibcctrl >>
  163. INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT) &
  164. INFINIPATH_IBCC_PHYERRTHRESHOLD_MASK;
  165. }
  166. /**
  167. * set_phyerrthreshold - set the physical error threshold
  168. * @dd: the infinipath device
  169. * @n: the new threshold
  170. *
  171. * Note that this will only take effect when the link state changes.
  172. */
  173. static int set_phyerrthreshold(struct ipath_devdata *dd, unsigned n)
  174. {
  175. unsigned v;
  176. v = (dd->ipath_ibcctrl >> INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT) &
  177. INFINIPATH_IBCC_PHYERRTHRESHOLD_MASK;
  178. if (v != n) {
  179. dd->ipath_ibcctrl &=
  180. ~(INFINIPATH_IBCC_PHYERRTHRESHOLD_MASK <<
  181. INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT);
  182. dd->ipath_ibcctrl |=
  183. (u64) n << INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT;
  184. ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
  185. dd->ipath_ibcctrl);
  186. }
  187. return 0;
  188. }
  189. /**
  190. * get_linkdowndefaultstate - get the default linkdown state
  191. * @dd: the infinipath device
  192. *
  193. * Returns zero if the default is POLL, 1 if the default is SLEEP.
  194. */
  195. static int get_linkdowndefaultstate(struct ipath_devdata *dd)
  196. {
  197. return !!(dd->ipath_ibcctrl & INFINIPATH_IBCC_LINKDOWNDEFAULTSTATE);
  198. }
  199. static int recv_subn_get_portinfo(struct ib_smp *smp,
  200. struct ib_device *ibdev, u8 port)
  201. {
  202. struct ipath_ibdev *dev;
  203. struct ib_port_info *pip = (struct ib_port_info *)smp->data;
  204. u16 lid;
  205. u8 ibcstat;
  206. u8 mtu;
  207. int ret;
  208. if (be32_to_cpu(smp->attr_mod) > ibdev->phys_port_cnt) {
  209. smp->status |= IB_SMP_INVALID_FIELD;
  210. ret = reply(smp);
  211. goto bail;
  212. }
  213. dev = to_idev(ibdev);
  214. /* Clear all fields. Only set the non-zero fields. */
  215. memset(smp->data, 0, sizeof(smp->data));
  216. /* Only return the mkey if the protection field allows it. */
  217. if (smp->method == IB_MGMT_METHOD_SET || dev->mkey == smp->mkey ||
  218. (dev->mkeyprot_resv_lmc >> 6) == 0)
  219. pip->mkey = dev->mkey;
  220. pip->gid_prefix = dev->gid_prefix;
  221. lid = dev->dd->ipath_lid;
  222. pip->lid = lid ? cpu_to_be16(lid) : IB_LID_PERMISSIVE;
  223. pip->sm_lid = cpu_to_be16(dev->sm_lid);
  224. pip->cap_mask = cpu_to_be32(dev->port_cap_flags);
  225. /* pip->diag_code; */
  226. pip->mkey_lease_period = cpu_to_be16(dev->mkey_lease_period);
  227. pip->local_port_num = port;
  228. pip->link_width_enabled = dev->link_width_enabled;
  229. pip->link_width_supported = 3; /* 1x or 4x */
  230. pip->link_width_active = 2; /* 4x */
  231. pip->linkspeed_portstate = 0x10; /* 2.5Gbps */
  232. ibcstat = dev->dd->ipath_lastibcstat;
  233. pip->linkspeed_portstate |= ((ibcstat >> 4) & 0x3) + 1;
  234. pip->portphysstate_linkdown =
  235. (ipath_cvt_physportstate[ibcstat & 0xf] << 4) |
  236. (get_linkdowndefaultstate(dev->dd) ? 1 : 2);
  237. pip->mkeyprot_resv_lmc = dev->mkeyprot_resv_lmc;
  238. pip->linkspeedactive_enabled = 0x11; /* 2.5Gbps, 2.5Gbps */
  239. switch (dev->dd->ipath_ibmtu) {
  240. case 4096:
  241. mtu = IB_MTU_4096;
  242. break;
  243. case 2048:
  244. mtu = IB_MTU_2048;
  245. break;
  246. case 1024:
  247. mtu = IB_MTU_1024;
  248. break;
  249. case 512:
  250. mtu = IB_MTU_512;
  251. break;
  252. case 256:
  253. mtu = IB_MTU_256;
  254. break;
  255. default: /* oops, something is wrong */
  256. mtu = IB_MTU_2048;
  257. break;
  258. }
  259. pip->neighbormtu_mastersmsl = (mtu << 4) | dev->sm_sl;
  260. pip->vlcap_inittype = 0x10; /* VLCap = VL0, InitType = 0 */
  261. pip->vl_high_limit = dev->vl_high_limit;
  262. /* pip->vl_arb_high_cap; // only one VL */
  263. /* pip->vl_arb_low_cap; // only one VL */
  264. /* InitTypeReply = 0 */
  265. pip->inittypereply_mtucap = IB_MTU_4096;
  266. // HCAs ignore VLStallCount and HOQLife
  267. /* pip->vlstallcnt_hoqlife; */
  268. pip->operationalvl_pei_peo_fpi_fpo = 0x10; /* OVLs = 1 */
  269. pip->mkey_violations = cpu_to_be16(dev->mkey_violations);
  270. /* P_KeyViolations are counted by hardware. */
  271. pip->pkey_violations =
  272. cpu_to_be16((ipath_get_cr_errpkey(dev->dd) -
  273. dev->z_pkey_violations) & 0xFFFF);
  274. pip->qkey_violations = cpu_to_be16(dev->qkey_violations);
  275. /* Only the hardware GUID is supported for now */
  276. pip->guid_cap = 1;
  277. pip->clientrereg_resv_subnetto = dev->subnet_timeout;
  278. /* 32.768 usec. response time (guessing) */
  279. pip->resv_resptimevalue = 3;
  280. pip->localphyerrors_overrunerrors =
  281. (get_phyerrthreshold(dev->dd) << 4) |
  282. get_overrunthreshold(dev->dd);
  283. /* pip->max_credit_hint; */
  284. /* pip->link_roundtrip_latency[3]; */
  285. ret = reply(smp);
  286. bail:
  287. return ret;
  288. }
  289. /**
  290. * get_pkeys - return the PKEY table for port 0
  291. * @dd: the infinipath device
  292. * @pkeys: the pkey table is placed here
  293. */
  294. static int get_pkeys(struct ipath_devdata *dd, u16 * pkeys)
  295. {
  296. struct ipath_portdata *pd = dd->ipath_pd[0];
  297. memcpy(pkeys, pd->port_pkeys, sizeof(pd->port_pkeys));
  298. return 0;
  299. }
  300. static int recv_subn_get_pkeytable(struct ib_smp *smp,
  301. struct ib_device *ibdev)
  302. {
  303. u32 startpx = 32 * (be32_to_cpu(smp->attr_mod) & 0xffff);
  304. u16 *p = (u16 *) smp->data;
  305. __be16 *q = (__be16 *) smp->data;
  306. /* 64 blocks of 32 16-bit P_Key entries */
  307. memset(smp->data, 0, sizeof(smp->data));
  308. if (startpx == 0) {
  309. struct ipath_ibdev *dev = to_idev(ibdev);
  310. unsigned i, n = ipath_get_npkeys(dev->dd);
  311. get_pkeys(dev->dd, p);
  312. for (i = 0; i < n; i++)
  313. q[i] = cpu_to_be16(p[i]);
  314. } else
  315. smp->status |= IB_SMP_INVALID_FIELD;
  316. return reply(smp);
  317. }
  318. static int recv_subn_set_guidinfo(struct ib_smp *smp,
  319. struct ib_device *ibdev)
  320. {
  321. /* The only GUID we support is the first read-only entry. */
  322. return recv_subn_get_guidinfo(smp, ibdev);
  323. }
  324. /**
  325. * set_linkdowndefaultstate - set the default linkdown state
  326. * @dd: the infinipath device
  327. * @sleep: the new state
  328. *
  329. * Note that this will only take effect when the link state changes.
  330. */
  331. static int set_linkdowndefaultstate(struct ipath_devdata *dd, int sleep)
  332. {
  333. if (sleep)
  334. dd->ipath_ibcctrl |= INFINIPATH_IBCC_LINKDOWNDEFAULTSTATE;
  335. else
  336. dd->ipath_ibcctrl &= ~INFINIPATH_IBCC_LINKDOWNDEFAULTSTATE;
  337. ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
  338. dd->ipath_ibcctrl);
  339. return 0;
  340. }
  341. /**
  342. * recv_subn_set_portinfo - set port information
  343. * @smp: the incoming SM packet
  344. * @ibdev: the infiniband device
  345. * @port: the port on the device
  346. *
  347. * Set Portinfo (see ch. 14.2.5.6).
  348. */
  349. static int recv_subn_set_portinfo(struct ib_smp *smp,
  350. struct ib_device *ibdev, u8 port)
  351. {
  352. struct ib_port_info *pip = (struct ib_port_info *)smp->data;
  353. struct ib_event event;
  354. struct ipath_ibdev *dev;
  355. u32 flags;
  356. char clientrereg = 0;
  357. u16 lid, smlid;
  358. u8 lwe;
  359. u8 lse;
  360. u8 state;
  361. u16 lstate;
  362. u32 mtu;
  363. int ret, ore;
  364. if (be32_to_cpu(smp->attr_mod) > ibdev->phys_port_cnt)
  365. goto err;
  366. dev = to_idev(ibdev);
  367. event.device = ibdev;
  368. event.element.port_num = port;
  369. dev->mkey = pip->mkey;
  370. dev->gid_prefix = pip->gid_prefix;
  371. dev->mkey_lease_period = be16_to_cpu(pip->mkey_lease_period);
  372. lid = be16_to_cpu(pip->lid);
  373. if (lid != dev->dd->ipath_lid) {
  374. /* Must be a valid unicast LID address. */
  375. if (lid == 0 || lid >= IPATH_MULTICAST_LID_BASE)
  376. goto err;
  377. ipath_set_lid(dev->dd, lid, pip->mkeyprot_resv_lmc & 7);
  378. event.event = IB_EVENT_LID_CHANGE;
  379. ib_dispatch_event(&event);
  380. }
  381. smlid = be16_to_cpu(pip->sm_lid);
  382. if (smlid != dev->sm_lid) {
  383. /* Must be a valid unicast LID address. */
  384. if (smlid == 0 || smlid >= IPATH_MULTICAST_LID_BASE)
  385. goto err;
  386. dev->sm_lid = smlid;
  387. event.event = IB_EVENT_SM_CHANGE;
  388. ib_dispatch_event(&event);
  389. }
  390. /* Only 4x supported but allow 1x or 4x to be set (see 14.2.6.6). */
  391. lwe = pip->link_width_enabled;
  392. if ((lwe >= 4 && lwe <= 8) || (lwe >= 0xC && lwe <= 0xFE))
  393. goto err;
  394. if (lwe == 0xFF)
  395. dev->link_width_enabled = 3; /* 1x or 4x */
  396. else if (lwe)
  397. dev->link_width_enabled = lwe;
  398. /* Only 2.5 Gbs supported. */
  399. lse = pip->linkspeedactive_enabled & 0xF;
  400. if (lse >= 2 && lse <= 0xE)
  401. goto err;
  402. /* Set link down default state. */
  403. switch (pip->portphysstate_linkdown & 0xF) {
  404. case 0: /* NOP */
  405. break;
  406. case 1: /* SLEEP */
  407. if (set_linkdowndefaultstate(dev->dd, 1))
  408. goto err;
  409. break;
  410. case 2: /* POLL */
  411. if (set_linkdowndefaultstate(dev->dd, 0))
  412. goto err;
  413. break;
  414. default:
  415. goto err;
  416. }
  417. dev->mkeyprot_resv_lmc = pip->mkeyprot_resv_lmc;
  418. dev->vl_high_limit = pip->vl_high_limit;
  419. switch ((pip->neighbormtu_mastersmsl >> 4) & 0xF) {
  420. case IB_MTU_256:
  421. mtu = 256;
  422. break;
  423. case IB_MTU_512:
  424. mtu = 512;
  425. break;
  426. case IB_MTU_1024:
  427. mtu = 1024;
  428. break;
  429. case IB_MTU_2048:
  430. mtu = 2048;
  431. break;
  432. case IB_MTU_4096:
  433. mtu = 4096;
  434. break;
  435. default:
  436. /* XXX We have already partially updated our state! */
  437. goto err;
  438. }
  439. ipath_set_mtu(dev->dd, mtu);
  440. dev->sm_sl = pip->neighbormtu_mastersmsl & 0xF;
  441. /* We only support VL0 */
  442. if (((pip->operationalvl_pei_peo_fpi_fpo >> 4) & 0xF) > 1)
  443. goto err;
  444. if (pip->mkey_violations == 0)
  445. dev->mkey_violations = 0;
  446. /*
  447. * Hardware counter can't be reset so snapshot and subtract
  448. * later.
  449. */
  450. if (pip->pkey_violations == 0)
  451. dev->z_pkey_violations = ipath_get_cr_errpkey(dev->dd);
  452. if (pip->qkey_violations == 0)
  453. dev->qkey_violations = 0;
  454. ore = pip->localphyerrors_overrunerrors;
  455. if (set_phyerrthreshold(dev->dd, (ore >> 4) & 0xF))
  456. goto err;
  457. if (set_overrunthreshold(dev->dd, (ore & 0xF)))
  458. goto err;
  459. dev->subnet_timeout = pip->clientrereg_resv_subnetto & 0x1F;
  460. if (pip->clientrereg_resv_subnetto & 0x80) {
  461. clientrereg = 1;
  462. event.event = IB_EVENT_CLIENT_REREGISTER;
  463. ib_dispatch_event(&event);
  464. }
  465. /*
  466. * Do the port state change now that the other link parameters
  467. * have been set.
  468. * Changing the port physical state only makes sense if the link
  469. * is down or is being set to down.
  470. */
  471. state = pip->linkspeed_portstate & 0xF;
  472. flags = dev->dd->ipath_flags;
  473. lstate = (pip->portphysstate_linkdown >> 4) & 0xF;
  474. if (lstate && !(state == IB_PORT_DOWN || state == IB_PORT_NOP))
  475. goto err;
  476. /*
  477. * Only state changes of DOWN, ARM, and ACTIVE are valid
  478. * and must be in the correct state to take effect (see 7.2.6).
  479. */
  480. switch (state) {
  481. case IB_PORT_NOP:
  482. if (lstate == 0)
  483. break;
  484. /* FALLTHROUGH */
  485. case IB_PORT_DOWN:
  486. if (lstate == 0)
  487. if (get_linkdowndefaultstate(dev->dd))
  488. lstate = IPATH_IB_LINKDOWN_SLEEP;
  489. else
  490. lstate = IPATH_IB_LINKDOWN;
  491. else if (lstate == 1)
  492. lstate = IPATH_IB_LINKDOWN_SLEEP;
  493. else if (lstate == 2)
  494. lstate = IPATH_IB_LINKDOWN;
  495. else if (lstate == 3)
  496. lstate = IPATH_IB_LINKDOWN_DISABLE;
  497. else
  498. goto err;
  499. ipath_set_linkstate(dev->dd, lstate);
  500. if (flags & IPATH_LINKACTIVE) {
  501. event.event = IB_EVENT_PORT_ERR;
  502. ib_dispatch_event(&event);
  503. }
  504. break;
  505. case IB_PORT_ARMED:
  506. if (!(flags & (IPATH_LINKINIT | IPATH_LINKACTIVE)))
  507. break;
  508. ipath_set_linkstate(dev->dd, IPATH_IB_LINKARM);
  509. if (flags & IPATH_LINKACTIVE) {
  510. event.event = IB_EVENT_PORT_ERR;
  511. ib_dispatch_event(&event);
  512. }
  513. break;
  514. case IB_PORT_ACTIVE:
  515. if (!(flags & IPATH_LINKARMED))
  516. break;
  517. ipath_set_linkstate(dev->dd, IPATH_IB_LINKACTIVE);
  518. event.event = IB_EVENT_PORT_ACTIVE;
  519. ib_dispatch_event(&event);
  520. break;
  521. default:
  522. /* XXX We have already partially updated our state! */
  523. goto err;
  524. }
  525. ret = recv_subn_get_portinfo(smp, ibdev, port);
  526. if (clientrereg)
  527. pip->clientrereg_resv_subnetto |= 0x80;
  528. goto done;
  529. err:
  530. smp->status |= IB_SMP_INVALID_FIELD;
  531. ret = recv_subn_get_portinfo(smp, ibdev, port);
  532. done:
  533. return ret;
  534. }
  535. /**
  536. * rm_pkey - decrecment the reference count for the given PKEY
  537. * @dd: the infinipath device
  538. * @key: the PKEY index
  539. *
  540. * Return true if this was the last reference and the hardware table entry
  541. * needs to be changed.
  542. */
  543. static int rm_pkey(struct ipath_devdata *dd, u16 key)
  544. {
  545. int i;
  546. int ret;
  547. for (i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
  548. if (dd->ipath_pkeys[i] != key)
  549. continue;
  550. if (atomic_dec_and_test(&dd->ipath_pkeyrefs[i])) {
  551. dd->ipath_pkeys[i] = 0;
  552. ret = 1;
  553. goto bail;
  554. }
  555. break;
  556. }
  557. ret = 0;
  558. bail:
  559. return ret;
  560. }
  561. /**
  562. * add_pkey - add the given PKEY to the hardware table
  563. * @dd: the infinipath device
  564. * @key: the PKEY
  565. *
  566. * Return an error code if unable to add the entry, zero if no change,
  567. * or 1 if the hardware PKEY register needs to be updated.
  568. */
  569. static int add_pkey(struct ipath_devdata *dd, u16 key)
  570. {
  571. int i;
  572. u16 lkey = key & 0x7FFF;
  573. int any = 0;
  574. int ret;
  575. if (lkey == 0x7FFF) {
  576. ret = 0;
  577. goto bail;
  578. }
  579. /* Look for an empty slot or a matching PKEY. */
  580. for (i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
  581. if (!dd->ipath_pkeys[i]) {
  582. any++;
  583. continue;
  584. }
  585. /* If it matches exactly, try to increment the ref count */
  586. if (dd->ipath_pkeys[i] == key) {
  587. if (atomic_inc_return(&dd->ipath_pkeyrefs[i]) > 1) {
  588. ret = 0;
  589. goto bail;
  590. }
  591. /* Lost the race. Look for an empty slot below. */
  592. atomic_dec(&dd->ipath_pkeyrefs[i]);
  593. any++;
  594. }
  595. /*
  596. * It makes no sense to have both the limited and unlimited
  597. * PKEY set at the same time since the unlimited one will
  598. * disable the limited one.
  599. */
  600. if ((dd->ipath_pkeys[i] & 0x7FFF) == lkey) {
  601. ret = -EEXIST;
  602. goto bail;
  603. }
  604. }
  605. if (!any) {
  606. ret = -EBUSY;
  607. goto bail;
  608. }
  609. for (i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
  610. if (!dd->ipath_pkeys[i] &&
  611. atomic_inc_return(&dd->ipath_pkeyrefs[i]) == 1) {
  612. /* for ipathstats, etc. */
  613. ipath_stats.sps_pkeys[i] = lkey;
  614. dd->ipath_pkeys[i] = key;
  615. ret = 1;
  616. goto bail;
  617. }
  618. }
  619. ret = -EBUSY;
  620. bail:
  621. return ret;
  622. }
  623. /**
  624. * set_pkeys - set the PKEY table for port 0
  625. * @dd: the infinipath device
  626. * @pkeys: the PKEY table
  627. */
  628. static int set_pkeys(struct ipath_devdata *dd, u16 *pkeys)
  629. {
  630. struct ipath_portdata *pd;
  631. int i;
  632. int changed = 0;
  633. pd = dd->ipath_pd[0];
  634. for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
  635. u16 key = pkeys[i];
  636. u16 okey = pd->port_pkeys[i];
  637. if (key == okey)
  638. continue;
  639. /*
  640. * The value of this PKEY table entry is changing.
  641. * Remove the old entry in the hardware's array of PKEYs.
  642. */
  643. if (okey & 0x7FFF)
  644. changed |= rm_pkey(dd, okey);
  645. if (key & 0x7FFF) {
  646. int ret = add_pkey(dd, key);
  647. if (ret < 0)
  648. key = 0;
  649. else
  650. changed |= ret;
  651. }
  652. pd->port_pkeys[i] = key;
  653. }
  654. if (changed) {
  655. u64 pkey;
  656. pkey = (u64) dd->ipath_pkeys[0] |
  657. ((u64) dd->ipath_pkeys[1] << 16) |
  658. ((u64) dd->ipath_pkeys[2] << 32) |
  659. ((u64) dd->ipath_pkeys[3] << 48);
  660. ipath_cdbg(VERBOSE, "p0 new pkey reg %llx\n",
  661. (unsigned long long) pkey);
  662. ipath_write_kreg(dd, dd->ipath_kregs->kr_partitionkey,
  663. pkey);
  664. }
  665. return 0;
  666. }
  667. static int recv_subn_set_pkeytable(struct ib_smp *smp,
  668. struct ib_device *ibdev)
  669. {
  670. u32 startpx = 32 * (be32_to_cpu(smp->attr_mod) & 0xffff);
  671. __be16 *p = (__be16 *) smp->data;
  672. u16 *q = (u16 *) smp->data;
  673. struct ipath_ibdev *dev = to_idev(ibdev);
  674. unsigned i, n = ipath_get_npkeys(dev->dd);
  675. for (i = 0; i < n; i++)
  676. q[i] = be16_to_cpu(p[i]);
  677. if (startpx != 0 || set_pkeys(dev->dd, q) != 0)
  678. smp->status |= IB_SMP_INVALID_FIELD;
  679. return recv_subn_get_pkeytable(smp, ibdev);
  680. }
  681. #define IB_PMA_CLASS_PORT_INFO __constant_htons(0x0001)
  682. #define IB_PMA_PORT_SAMPLES_CONTROL __constant_htons(0x0010)
  683. #define IB_PMA_PORT_SAMPLES_RESULT __constant_htons(0x0011)
  684. #define IB_PMA_PORT_COUNTERS __constant_htons(0x0012)
  685. #define IB_PMA_PORT_COUNTERS_EXT __constant_htons(0x001D)
  686. #define IB_PMA_PORT_SAMPLES_RESULT_EXT __constant_htons(0x001E)
  687. struct ib_perf {
  688. u8 base_version;
  689. u8 mgmt_class;
  690. u8 class_version;
  691. u8 method;
  692. __be16 status;
  693. __be16 unused;
  694. __be64 tid;
  695. __be16 attr_id;
  696. __be16 resv;
  697. __be32 attr_mod;
  698. u8 reserved[40];
  699. u8 data[192];
  700. } __attribute__ ((packed));
  701. struct ib_pma_classportinfo {
  702. u8 base_version;
  703. u8 class_version;
  704. __be16 cap_mask;
  705. u8 reserved[3];
  706. u8 resp_time_value; /* only lower 5 bits */
  707. union ib_gid redirect_gid;
  708. __be32 redirect_tc_sl_fl; /* 8, 4, 20 bits respectively */
  709. __be16 redirect_lid;
  710. __be16 redirect_pkey;
  711. __be32 redirect_qp; /* only lower 24 bits */
  712. __be32 redirect_qkey;
  713. union ib_gid trap_gid;
  714. __be32 trap_tc_sl_fl; /* 8, 4, 20 bits respectively */
  715. __be16 trap_lid;
  716. __be16 trap_pkey;
  717. __be32 trap_hl_qp; /* 8, 24 bits respectively */
  718. __be32 trap_qkey;
  719. } __attribute__ ((packed));
  720. struct ib_pma_portsamplescontrol {
  721. u8 opcode;
  722. u8 port_select;
  723. u8 tick;
  724. u8 counter_width; /* only lower 3 bits */
  725. __be32 counter_mask0_9; /* 2, 10 * 3, bits */
  726. __be16 counter_mask10_14; /* 1, 5 * 3, bits */
  727. u8 sample_mechanisms;
  728. u8 sample_status; /* only lower 2 bits */
  729. __be64 option_mask;
  730. __be64 vendor_mask;
  731. __be32 sample_start;
  732. __be32 sample_interval;
  733. __be16 tag;
  734. __be16 counter_select[15];
  735. } __attribute__ ((packed));
  736. struct ib_pma_portsamplesresult {
  737. __be16 tag;
  738. __be16 sample_status; /* only lower 2 bits */
  739. __be32 counter[15];
  740. } __attribute__ ((packed));
  741. struct ib_pma_portsamplesresult_ext {
  742. __be16 tag;
  743. __be16 sample_status; /* only lower 2 bits */
  744. __be32 extended_width; /* only upper 2 bits */
  745. __be64 counter[15];
  746. } __attribute__ ((packed));
  747. struct ib_pma_portcounters {
  748. u8 reserved;
  749. u8 port_select;
  750. __be16 counter_select;
  751. __be16 symbol_error_counter;
  752. u8 link_error_recovery_counter;
  753. u8 link_downed_counter;
  754. __be16 port_rcv_errors;
  755. __be16 port_rcv_remphys_errors;
  756. __be16 port_rcv_switch_relay_errors;
  757. __be16 port_xmit_discards;
  758. u8 port_xmit_constraint_errors;
  759. u8 port_rcv_constraint_errors;
  760. u8 reserved1;
  761. u8 lli_ebor_errors; /* 4, 4, bits */
  762. __be16 reserved2;
  763. __be16 vl15_dropped;
  764. __be32 port_xmit_data;
  765. __be32 port_rcv_data;
  766. __be32 port_xmit_packets;
  767. __be32 port_rcv_packets;
  768. } __attribute__ ((packed));
  769. #define IB_PMA_SEL_SYMBOL_ERROR __constant_htons(0x0001)
  770. #define IB_PMA_SEL_LINK_ERROR_RECOVERY __constant_htons(0x0002)
  771. #define IB_PMA_SEL_LINK_DOWNED __constant_htons(0x0004)
  772. #define IB_PMA_SEL_PORT_RCV_ERRORS __constant_htons(0x0008)
  773. #define IB_PMA_SEL_PORT_RCV_REMPHYS_ERRORS __constant_htons(0x0010)
  774. #define IB_PMA_SEL_PORT_XMIT_DISCARDS __constant_htons(0x0040)
  775. #define IB_PMA_SEL_LOCAL_LINK_INTEGRITY_ERRORS __constant_htons(0x0200)
  776. #define IB_PMA_SEL_EXCESSIVE_BUFFER_OVERRUNS __constant_htons(0x0400)
  777. #define IB_PMA_SEL_PORT_VL15_DROPPED __constant_htons(0x0800)
  778. #define IB_PMA_SEL_PORT_XMIT_DATA __constant_htons(0x1000)
  779. #define IB_PMA_SEL_PORT_RCV_DATA __constant_htons(0x2000)
  780. #define IB_PMA_SEL_PORT_XMIT_PACKETS __constant_htons(0x4000)
  781. #define IB_PMA_SEL_PORT_RCV_PACKETS __constant_htons(0x8000)
  782. struct ib_pma_portcounters_ext {
  783. u8 reserved;
  784. u8 port_select;
  785. __be16 counter_select;
  786. __be32 reserved1;
  787. __be64 port_xmit_data;
  788. __be64 port_rcv_data;
  789. __be64 port_xmit_packets;
  790. __be64 port_rcv_packets;
  791. __be64 port_unicast_xmit_packets;
  792. __be64 port_unicast_rcv_packets;
  793. __be64 port_multicast_xmit_packets;
  794. __be64 port_multicast_rcv_packets;
  795. } __attribute__ ((packed));
  796. #define IB_PMA_SELX_PORT_XMIT_DATA __constant_htons(0x0001)
  797. #define IB_PMA_SELX_PORT_RCV_DATA __constant_htons(0x0002)
  798. #define IB_PMA_SELX_PORT_XMIT_PACKETS __constant_htons(0x0004)
  799. #define IB_PMA_SELX_PORT_RCV_PACKETS __constant_htons(0x0008)
  800. #define IB_PMA_SELX_PORT_UNI_XMIT_PACKETS __constant_htons(0x0010)
  801. #define IB_PMA_SELX_PORT_UNI_RCV_PACKETS __constant_htons(0x0020)
  802. #define IB_PMA_SELX_PORT_MULTI_XMIT_PACKETS __constant_htons(0x0040)
  803. #define IB_PMA_SELX_PORT_MULTI_RCV_PACKETS __constant_htons(0x0080)
  804. static int recv_pma_get_classportinfo(struct ib_perf *pmp)
  805. {
  806. struct ib_pma_classportinfo *p =
  807. (struct ib_pma_classportinfo *)pmp->data;
  808. memset(pmp->data, 0, sizeof(pmp->data));
  809. if (pmp->attr_mod != 0)
  810. pmp->status |= IB_SMP_INVALID_FIELD;
  811. /* Indicate AllPortSelect is valid (only one port anyway) */
  812. p->cap_mask = __constant_cpu_to_be16(1 << 8);
  813. p->base_version = 1;
  814. p->class_version = 1;
  815. /*
  816. * Expected response time is 4.096 usec. * 2^18 == 1.073741824
  817. * sec.
  818. */
  819. p->resp_time_value = 18;
  820. return reply((struct ib_smp *) pmp);
  821. }
  822. /*
  823. * The PortSamplesControl.CounterMasks field is an array of 3 bit fields
  824. * which specify the N'th counter's capabilities. See ch. 16.1.3.2.
  825. * We support 5 counters which only count the mandatory quantities.
  826. */
  827. #define COUNTER_MASK(q, n) (q << ((9 - n) * 3))
  828. #define COUNTER_MASK0_9 \
  829. __constant_cpu_to_be32(COUNTER_MASK(1, 0) | \
  830. COUNTER_MASK(1, 1) | \
  831. COUNTER_MASK(1, 2) | \
  832. COUNTER_MASK(1, 3) | \
  833. COUNTER_MASK(1, 4))
  834. static int recv_pma_get_portsamplescontrol(struct ib_perf *pmp,
  835. struct ib_device *ibdev, u8 port)
  836. {
  837. struct ib_pma_portsamplescontrol *p =
  838. (struct ib_pma_portsamplescontrol *)pmp->data;
  839. struct ipath_ibdev *dev = to_idev(ibdev);
  840. unsigned long flags;
  841. u8 port_select = p->port_select;
  842. memset(pmp->data, 0, sizeof(pmp->data));
  843. p->port_select = port_select;
  844. if (pmp->attr_mod != 0 ||
  845. (port_select != port && port_select != 0xFF))
  846. pmp->status |= IB_SMP_INVALID_FIELD;
  847. /*
  848. * Ticks are 10x the link transfer period which for 2.5Gbs is 4
  849. * nsec. 0 == 4 nsec., 1 == 8 nsec., ..., 255 == 1020 nsec. Sample
  850. * intervals are counted in ticks. Since we use Linux timers, that
  851. * count in jiffies, we can't sample for less than 1000 ticks if HZ
  852. * == 1000 (4000 ticks if HZ is 250).
  853. */
  854. /* XXX This is WRONG. */
  855. p->tick = 250; /* 1 usec. */
  856. p->counter_width = 4; /* 32 bit counters */
  857. p->counter_mask0_9 = COUNTER_MASK0_9;
  858. spin_lock_irqsave(&dev->pending_lock, flags);
  859. p->sample_status = dev->pma_sample_status;
  860. p->sample_start = cpu_to_be32(dev->pma_sample_start);
  861. p->sample_interval = cpu_to_be32(dev->pma_sample_interval);
  862. p->tag = cpu_to_be16(dev->pma_tag);
  863. p->counter_select[0] = dev->pma_counter_select[0];
  864. p->counter_select[1] = dev->pma_counter_select[1];
  865. p->counter_select[2] = dev->pma_counter_select[2];
  866. p->counter_select[3] = dev->pma_counter_select[3];
  867. p->counter_select[4] = dev->pma_counter_select[4];
  868. spin_unlock_irqrestore(&dev->pending_lock, flags);
  869. return reply((struct ib_smp *) pmp);
  870. }
  871. static int recv_pma_set_portsamplescontrol(struct ib_perf *pmp,
  872. struct ib_device *ibdev, u8 port)
  873. {
  874. struct ib_pma_portsamplescontrol *p =
  875. (struct ib_pma_portsamplescontrol *)pmp->data;
  876. struct ipath_ibdev *dev = to_idev(ibdev);
  877. unsigned long flags;
  878. u32 start;
  879. int ret;
  880. if (pmp->attr_mod != 0 ||
  881. (p->port_select != port && p->port_select != 0xFF)) {
  882. pmp->status |= IB_SMP_INVALID_FIELD;
  883. ret = reply((struct ib_smp *) pmp);
  884. goto bail;
  885. }
  886. start = be32_to_cpu(p->sample_start);
  887. if (start != 0) {
  888. spin_lock_irqsave(&dev->pending_lock, flags);
  889. if (dev->pma_sample_status == IB_PMA_SAMPLE_STATUS_DONE) {
  890. dev->pma_sample_status =
  891. IB_PMA_SAMPLE_STATUS_STARTED;
  892. dev->pma_sample_start = start;
  893. dev->pma_sample_interval =
  894. be32_to_cpu(p->sample_interval);
  895. dev->pma_tag = be16_to_cpu(p->tag);
  896. if (p->counter_select[0])
  897. dev->pma_counter_select[0] =
  898. p->counter_select[0];
  899. if (p->counter_select[1])
  900. dev->pma_counter_select[1] =
  901. p->counter_select[1];
  902. if (p->counter_select[2])
  903. dev->pma_counter_select[2] =
  904. p->counter_select[2];
  905. if (p->counter_select[3])
  906. dev->pma_counter_select[3] =
  907. p->counter_select[3];
  908. if (p->counter_select[4])
  909. dev->pma_counter_select[4] =
  910. p->counter_select[4];
  911. }
  912. spin_unlock_irqrestore(&dev->pending_lock, flags);
  913. }
  914. ret = recv_pma_get_portsamplescontrol(pmp, ibdev, port);
  915. bail:
  916. return ret;
  917. }
  918. static u64 get_counter(struct ipath_ibdev *dev, __be16 sel)
  919. {
  920. u64 ret;
  921. switch (sel) {
  922. case IB_PMA_PORT_XMIT_DATA:
  923. ret = dev->ipath_sword;
  924. break;
  925. case IB_PMA_PORT_RCV_DATA:
  926. ret = dev->ipath_rword;
  927. break;
  928. case IB_PMA_PORT_XMIT_PKTS:
  929. ret = dev->ipath_spkts;
  930. break;
  931. case IB_PMA_PORT_RCV_PKTS:
  932. ret = dev->ipath_rpkts;
  933. break;
  934. case IB_PMA_PORT_XMIT_WAIT:
  935. ret = dev->ipath_xmit_wait;
  936. break;
  937. default:
  938. ret = 0;
  939. }
  940. return ret;
  941. }
  942. static int recv_pma_get_portsamplesresult(struct ib_perf *pmp,
  943. struct ib_device *ibdev)
  944. {
  945. struct ib_pma_portsamplesresult *p =
  946. (struct ib_pma_portsamplesresult *)pmp->data;
  947. struct ipath_ibdev *dev = to_idev(ibdev);
  948. int i;
  949. memset(pmp->data, 0, sizeof(pmp->data));
  950. p->tag = cpu_to_be16(dev->pma_tag);
  951. p->sample_status = cpu_to_be16(dev->pma_sample_status);
  952. for (i = 0; i < ARRAY_SIZE(dev->pma_counter_select); i++)
  953. p->counter[i] = cpu_to_be32(
  954. get_counter(dev, dev->pma_counter_select[i]));
  955. return reply((struct ib_smp *) pmp);
  956. }
  957. static int recv_pma_get_portsamplesresult_ext(struct ib_perf *pmp,
  958. struct ib_device *ibdev)
  959. {
  960. struct ib_pma_portsamplesresult_ext *p =
  961. (struct ib_pma_portsamplesresult_ext *)pmp->data;
  962. struct ipath_ibdev *dev = to_idev(ibdev);
  963. int i;
  964. memset(pmp->data, 0, sizeof(pmp->data));
  965. p->tag = cpu_to_be16(dev->pma_tag);
  966. p->sample_status = cpu_to_be16(dev->pma_sample_status);
  967. /* 64 bits */
  968. p->extended_width = __constant_cpu_to_be32(0x80000000);
  969. for (i = 0; i < ARRAY_SIZE(dev->pma_counter_select); i++)
  970. p->counter[i] = cpu_to_be64(
  971. get_counter(dev, dev->pma_counter_select[i]));
  972. return reply((struct ib_smp *) pmp);
  973. }
  974. static int recv_pma_get_portcounters(struct ib_perf *pmp,
  975. struct ib_device *ibdev, u8 port)
  976. {
  977. struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
  978. pmp->data;
  979. struct ipath_ibdev *dev = to_idev(ibdev);
  980. struct ipath_verbs_counters cntrs;
  981. u8 port_select = p->port_select;
  982. ipath_get_counters(dev->dd, &cntrs);
  983. /* Adjust counters for any resets done. */
  984. cntrs.symbol_error_counter -= dev->z_symbol_error_counter;
  985. cntrs.link_error_recovery_counter -=
  986. dev->z_link_error_recovery_counter;
  987. cntrs.link_downed_counter -= dev->z_link_downed_counter;
  988. cntrs.port_rcv_errors += dev->rcv_errors;
  989. cntrs.port_rcv_errors -= dev->z_port_rcv_errors;
  990. cntrs.port_rcv_remphys_errors -= dev->z_port_rcv_remphys_errors;
  991. cntrs.port_xmit_discards -= dev->z_port_xmit_discards;
  992. cntrs.port_xmit_data -= dev->z_port_xmit_data;
  993. cntrs.port_rcv_data -= dev->z_port_rcv_data;
  994. cntrs.port_xmit_packets -= dev->z_port_xmit_packets;
  995. cntrs.port_rcv_packets -= dev->z_port_rcv_packets;
  996. cntrs.local_link_integrity_errors -=
  997. dev->z_local_link_integrity_errors;
  998. cntrs.excessive_buffer_overrun_errors -=
  999. dev->z_excessive_buffer_overrun_errors;
  1000. memset(pmp->data, 0, sizeof(pmp->data));
  1001. p->port_select = port_select;
  1002. if (pmp->attr_mod != 0 ||
  1003. (port_select != port && port_select != 0xFF))
  1004. pmp->status |= IB_SMP_INVALID_FIELD;
  1005. if (cntrs.symbol_error_counter > 0xFFFFUL)
  1006. p->symbol_error_counter = __constant_cpu_to_be16(0xFFFF);
  1007. else
  1008. p->symbol_error_counter =
  1009. cpu_to_be16((u16)cntrs.symbol_error_counter);
  1010. if (cntrs.link_error_recovery_counter > 0xFFUL)
  1011. p->link_error_recovery_counter = 0xFF;
  1012. else
  1013. p->link_error_recovery_counter =
  1014. (u8)cntrs.link_error_recovery_counter;
  1015. if (cntrs.link_downed_counter > 0xFFUL)
  1016. p->link_downed_counter = 0xFF;
  1017. else
  1018. p->link_downed_counter = (u8)cntrs.link_downed_counter;
  1019. if (cntrs.port_rcv_errors > 0xFFFFUL)
  1020. p->port_rcv_errors = __constant_cpu_to_be16(0xFFFF);
  1021. else
  1022. p->port_rcv_errors =
  1023. cpu_to_be16((u16) cntrs.port_rcv_errors);
  1024. if (cntrs.port_rcv_remphys_errors > 0xFFFFUL)
  1025. p->port_rcv_remphys_errors = __constant_cpu_to_be16(0xFFFF);
  1026. else
  1027. p->port_rcv_remphys_errors =
  1028. cpu_to_be16((u16)cntrs.port_rcv_remphys_errors);
  1029. if (cntrs.port_xmit_discards > 0xFFFFUL)
  1030. p->port_xmit_discards = __constant_cpu_to_be16(0xFFFF);
  1031. else
  1032. p->port_xmit_discards =
  1033. cpu_to_be16((u16)cntrs.port_xmit_discards);
  1034. if (cntrs.local_link_integrity_errors > 0xFUL)
  1035. cntrs.local_link_integrity_errors = 0xFUL;
  1036. if (cntrs.excessive_buffer_overrun_errors > 0xFUL)
  1037. cntrs.excessive_buffer_overrun_errors = 0xFUL;
  1038. p->lli_ebor_errors = (cntrs.local_link_integrity_errors << 4) |
  1039. cntrs.excessive_buffer_overrun_errors;
  1040. if (dev->n_vl15_dropped > 0xFFFFUL)
  1041. p->vl15_dropped = __constant_cpu_to_be16(0xFFFF);
  1042. else
  1043. p->vl15_dropped = cpu_to_be16((u16)dev->n_vl15_dropped);
  1044. if (cntrs.port_xmit_data > 0xFFFFFFFFUL)
  1045. p->port_xmit_data = __constant_cpu_to_be32(0xFFFFFFFF);
  1046. else
  1047. p->port_xmit_data = cpu_to_be32((u32)cntrs.port_xmit_data);
  1048. if (cntrs.port_rcv_data > 0xFFFFFFFFUL)
  1049. p->port_rcv_data = __constant_cpu_to_be32(0xFFFFFFFF);
  1050. else
  1051. p->port_rcv_data = cpu_to_be32((u32)cntrs.port_rcv_data);
  1052. if (cntrs.port_xmit_packets > 0xFFFFFFFFUL)
  1053. p->port_xmit_packets = __constant_cpu_to_be32(0xFFFFFFFF);
  1054. else
  1055. p->port_xmit_packets =
  1056. cpu_to_be32((u32)cntrs.port_xmit_packets);
  1057. if (cntrs.port_rcv_packets > 0xFFFFFFFFUL)
  1058. p->port_rcv_packets = __constant_cpu_to_be32(0xFFFFFFFF);
  1059. else
  1060. p->port_rcv_packets =
  1061. cpu_to_be32((u32) cntrs.port_rcv_packets);
  1062. return reply((struct ib_smp *) pmp);
  1063. }
  1064. static int recv_pma_get_portcounters_ext(struct ib_perf *pmp,
  1065. struct ib_device *ibdev, u8 port)
  1066. {
  1067. struct ib_pma_portcounters_ext *p =
  1068. (struct ib_pma_portcounters_ext *)pmp->data;
  1069. struct ipath_ibdev *dev = to_idev(ibdev);
  1070. u64 swords, rwords, spkts, rpkts, xwait;
  1071. u8 port_select = p->port_select;
  1072. ipath_snapshot_counters(dev->dd, &swords, &rwords, &spkts,
  1073. &rpkts, &xwait);
  1074. /* Adjust counters for any resets done. */
  1075. swords -= dev->z_port_xmit_data;
  1076. rwords -= dev->z_port_rcv_data;
  1077. spkts -= dev->z_port_xmit_packets;
  1078. rpkts -= dev->z_port_rcv_packets;
  1079. memset(pmp->data, 0, sizeof(pmp->data));
  1080. p->port_select = port_select;
  1081. if (pmp->attr_mod != 0 ||
  1082. (port_select != port && port_select != 0xFF))
  1083. pmp->status |= IB_SMP_INVALID_FIELD;
  1084. p->port_xmit_data = cpu_to_be64(swords);
  1085. p->port_rcv_data = cpu_to_be64(rwords);
  1086. p->port_xmit_packets = cpu_to_be64(spkts);
  1087. p->port_rcv_packets = cpu_to_be64(rpkts);
  1088. p->port_unicast_xmit_packets = cpu_to_be64(dev->n_unicast_xmit);
  1089. p->port_unicast_rcv_packets = cpu_to_be64(dev->n_unicast_rcv);
  1090. p->port_multicast_xmit_packets = cpu_to_be64(dev->n_multicast_xmit);
  1091. p->port_multicast_rcv_packets = cpu_to_be64(dev->n_multicast_rcv);
  1092. return reply((struct ib_smp *) pmp);
  1093. }
  1094. static int recv_pma_set_portcounters(struct ib_perf *pmp,
  1095. struct ib_device *ibdev, u8 port)
  1096. {
  1097. struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
  1098. pmp->data;
  1099. struct ipath_ibdev *dev = to_idev(ibdev);
  1100. struct ipath_verbs_counters cntrs;
  1101. /*
  1102. * Since the HW doesn't support clearing counters, we save the
  1103. * current count and subtract it from future responses.
  1104. */
  1105. ipath_get_counters(dev->dd, &cntrs);
  1106. if (p->counter_select & IB_PMA_SEL_SYMBOL_ERROR)
  1107. dev->z_symbol_error_counter = cntrs.symbol_error_counter;
  1108. if (p->counter_select & IB_PMA_SEL_LINK_ERROR_RECOVERY)
  1109. dev->z_link_error_recovery_counter =
  1110. cntrs.link_error_recovery_counter;
  1111. if (p->counter_select & IB_PMA_SEL_LINK_DOWNED)
  1112. dev->z_link_downed_counter = cntrs.link_downed_counter;
  1113. if (p->counter_select & IB_PMA_SEL_PORT_RCV_ERRORS)
  1114. dev->z_port_rcv_errors =
  1115. cntrs.port_rcv_errors + dev->rcv_errors;
  1116. if (p->counter_select & IB_PMA_SEL_PORT_RCV_REMPHYS_ERRORS)
  1117. dev->z_port_rcv_remphys_errors =
  1118. cntrs.port_rcv_remphys_errors;
  1119. if (p->counter_select & IB_PMA_SEL_PORT_XMIT_DISCARDS)
  1120. dev->z_port_xmit_discards = cntrs.port_xmit_discards;
  1121. if (p->counter_select & IB_PMA_SEL_LOCAL_LINK_INTEGRITY_ERRORS)
  1122. dev->z_local_link_integrity_errors =
  1123. cntrs.local_link_integrity_errors;
  1124. if (p->counter_select & IB_PMA_SEL_EXCESSIVE_BUFFER_OVERRUNS)
  1125. dev->z_excessive_buffer_overrun_errors =
  1126. cntrs.excessive_buffer_overrun_errors;
  1127. if (p->counter_select & IB_PMA_SEL_PORT_VL15_DROPPED)
  1128. dev->n_vl15_dropped = 0;
  1129. if (p->counter_select & IB_PMA_SEL_PORT_XMIT_DATA)
  1130. dev->z_port_xmit_data = cntrs.port_xmit_data;
  1131. if (p->counter_select & IB_PMA_SEL_PORT_RCV_DATA)
  1132. dev->z_port_rcv_data = cntrs.port_rcv_data;
  1133. if (p->counter_select & IB_PMA_SEL_PORT_XMIT_PACKETS)
  1134. dev->z_port_xmit_packets = cntrs.port_xmit_packets;
  1135. if (p->counter_select & IB_PMA_SEL_PORT_RCV_PACKETS)
  1136. dev->z_port_rcv_packets = cntrs.port_rcv_packets;
  1137. return recv_pma_get_portcounters(pmp, ibdev, port);
  1138. }
  1139. static int recv_pma_set_portcounters_ext(struct ib_perf *pmp,
  1140. struct ib_device *ibdev, u8 port)
  1141. {
  1142. struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
  1143. pmp->data;
  1144. struct ipath_ibdev *dev = to_idev(ibdev);
  1145. u64 swords, rwords, spkts, rpkts, xwait;
  1146. ipath_snapshot_counters(dev->dd, &swords, &rwords, &spkts,
  1147. &rpkts, &xwait);
  1148. if (p->counter_select & IB_PMA_SELX_PORT_XMIT_DATA)
  1149. dev->z_port_xmit_data = swords;
  1150. if (p->counter_select & IB_PMA_SELX_PORT_RCV_DATA)
  1151. dev->z_port_rcv_data = rwords;
  1152. if (p->counter_select & IB_PMA_SELX_PORT_XMIT_PACKETS)
  1153. dev->z_port_xmit_packets = spkts;
  1154. if (p->counter_select & IB_PMA_SELX_PORT_RCV_PACKETS)
  1155. dev->z_port_rcv_packets = rpkts;
  1156. if (p->counter_select & IB_PMA_SELX_PORT_UNI_XMIT_PACKETS)
  1157. dev->n_unicast_xmit = 0;
  1158. if (p->counter_select & IB_PMA_SELX_PORT_UNI_RCV_PACKETS)
  1159. dev->n_unicast_rcv = 0;
  1160. if (p->counter_select & IB_PMA_SELX_PORT_MULTI_XMIT_PACKETS)
  1161. dev->n_multicast_xmit = 0;
  1162. if (p->counter_select & IB_PMA_SELX_PORT_MULTI_RCV_PACKETS)
  1163. dev->n_multicast_rcv = 0;
  1164. return recv_pma_get_portcounters_ext(pmp, ibdev, port);
  1165. }
  1166. static int process_subn(struct ib_device *ibdev, int mad_flags,
  1167. u8 port_num, struct ib_mad *in_mad,
  1168. struct ib_mad *out_mad)
  1169. {
  1170. struct ib_smp *smp = (struct ib_smp *)out_mad;
  1171. struct ipath_ibdev *dev = to_idev(ibdev);
  1172. int ret;
  1173. *out_mad = *in_mad;
  1174. if (smp->class_version != 1) {
  1175. smp->status |= IB_SMP_UNSUP_VERSION;
  1176. ret = reply(smp);
  1177. goto bail;
  1178. }
  1179. /* Is the mkey in the process of expiring? */
  1180. if (dev->mkey_lease_timeout && jiffies >= dev->mkey_lease_timeout) {
  1181. /* Clear timeout and mkey protection field. */
  1182. dev->mkey_lease_timeout = 0;
  1183. dev->mkeyprot_resv_lmc &= 0x3F;
  1184. }
  1185. /*
  1186. * M_Key checking depends on
  1187. * Portinfo:M_Key_protect_bits
  1188. */
  1189. if ((mad_flags & IB_MAD_IGNORE_MKEY) == 0 && dev->mkey != 0 &&
  1190. dev->mkey != smp->mkey &&
  1191. (smp->method == IB_MGMT_METHOD_SET ||
  1192. (smp->method == IB_MGMT_METHOD_GET &&
  1193. (dev->mkeyprot_resv_lmc >> 7) != 0))) {
  1194. if (dev->mkey_violations != 0xFFFF)
  1195. ++dev->mkey_violations;
  1196. if (dev->mkey_lease_timeout ||
  1197. dev->mkey_lease_period == 0) {
  1198. ret = IB_MAD_RESULT_SUCCESS |
  1199. IB_MAD_RESULT_CONSUMED;
  1200. goto bail;
  1201. }
  1202. dev->mkey_lease_timeout = jiffies +
  1203. dev->mkey_lease_period * HZ;
  1204. /* Future: Generate a trap notice. */
  1205. ret = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
  1206. goto bail;
  1207. } else if (dev->mkey_lease_timeout)
  1208. dev->mkey_lease_timeout = 0;
  1209. switch (smp->method) {
  1210. case IB_MGMT_METHOD_GET:
  1211. switch (smp->attr_id) {
  1212. case IB_SMP_ATTR_NODE_DESC:
  1213. ret = recv_subn_get_nodedescription(smp, ibdev);
  1214. goto bail;
  1215. case IB_SMP_ATTR_NODE_INFO:
  1216. ret = recv_subn_get_nodeinfo(smp, ibdev, port_num);
  1217. goto bail;
  1218. case IB_SMP_ATTR_GUID_INFO:
  1219. ret = recv_subn_get_guidinfo(smp, ibdev);
  1220. goto bail;
  1221. case IB_SMP_ATTR_PORT_INFO:
  1222. ret = recv_subn_get_portinfo(smp, ibdev, port_num);
  1223. goto bail;
  1224. case IB_SMP_ATTR_PKEY_TABLE:
  1225. ret = recv_subn_get_pkeytable(smp, ibdev);
  1226. goto bail;
  1227. case IB_SMP_ATTR_SM_INFO:
  1228. if (dev->port_cap_flags & IB_PORT_SM_DISABLED) {
  1229. ret = IB_MAD_RESULT_SUCCESS |
  1230. IB_MAD_RESULT_CONSUMED;
  1231. goto bail;
  1232. }
  1233. if (dev->port_cap_flags & IB_PORT_SM) {
  1234. ret = IB_MAD_RESULT_SUCCESS;
  1235. goto bail;
  1236. }
  1237. /* FALLTHROUGH */
  1238. default:
  1239. smp->status |= IB_SMP_UNSUP_METH_ATTR;
  1240. ret = reply(smp);
  1241. goto bail;
  1242. }
  1243. case IB_MGMT_METHOD_SET:
  1244. switch (smp->attr_id) {
  1245. case IB_SMP_ATTR_GUID_INFO:
  1246. ret = recv_subn_set_guidinfo(smp, ibdev);
  1247. goto bail;
  1248. case IB_SMP_ATTR_PORT_INFO:
  1249. ret = recv_subn_set_portinfo(smp, ibdev, port_num);
  1250. goto bail;
  1251. case IB_SMP_ATTR_PKEY_TABLE:
  1252. ret = recv_subn_set_pkeytable(smp, ibdev);
  1253. goto bail;
  1254. case IB_SMP_ATTR_SM_INFO:
  1255. if (dev->port_cap_flags & IB_PORT_SM_DISABLED) {
  1256. ret = IB_MAD_RESULT_SUCCESS |
  1257. IB_MAD_RESULT_CONSUMED;
  1258. goto bail;
  1259. }
  1260. if (dev->port_cap_flags & IB_PORT_SM) {
  1261. ret = IB_MAD_RESULT_SUCCESS;
  1262. goto bail;
  1263. }
  1264. /* FALLTHROUGH */
  1265. default:
  1266. smp->status |= IB_SMP_UNSUP_METH_ATTR;
  1267. ret = reply(smp);
  1268. goto bail;
  1269. }
  1270. case IB_MGMT_METHOD_GET_RESP:
  1271. /*
  1272. * The ib_mad module will call us to process responses
  1273. * before checking for other consumers.
  1274. * Just tell the caller to process it normally.
  1275. */
  1276. ret = IB_MAD_RESULT_FAILURE;
  1277. goto bail;
  1278. default:
  1279. smp->status |= IB_SMP_UNSUP_METHOD;
  1280. ret = reply(smp);
  1281. }
  1282. bail:
  1283. return ret;
  1284. }
  1285. static int process_perf(struct ib_device *ibdev, u8 port_num,
  1286. struct ib_mad *in_mad,
  1287. struct ib_mad *out_mad)
  1288. {
  1289. struct ib_perf *pmp = (struct ib_perf *)out_mad;
  1290. int ret;
  1291. *out_mad = *in_mad;
  1292. if (pmp->class_version != 1) {
  1293. pmp->status |= IB_SMP_UNSUP_VERSION;
  1294. ret = reply((struct ib_smp *) pmp);
  1295. goto bail;
  1296. }
  1297. switch (pmp->method) {
  1298. case IB_MGMT_METHOD_GET:
  1299. switch (pmp->attr_id) {
  1300. case IB_PMA_CLASS_PORT_INFO:
  1301. ret = recv_pma_get_classportinfo(pmp);
  1302. goto bail;
  1303. case IB_PMA_PORT_SAMPLES_CONTROL:
  1304. ret = recv_pma_get_portsamplescontrol(pmp, ibdev,
  1305. port_num);
  1306. goto bail;
  1307. case IB_PMA_PORT_SAMPLES_RESULT:
  1308. ret = recv_pma_get_portsamplesresult(pmp, ibdev);
  1309. goto bail;
  1310. case IB_PMA_PORT_SAMPLES_RESULT_EXT:
  1311. ret = recv_pma_get_portsamplesresult_ext(pmp,
  1312. ibdev);
  1313. goto bail;
  1314. case IB_PMA_PORT_COUNTERS:
  1315. ret = recv_pma_get_portcounters(pmp, ibdev,
  1316. port_num);
  1317. goto bail;
  1318. case IB_PMA_PORT_COUNTERS_EXT:
  1319. ret = recv_pma_get_portcounters_ext(pmp, ibdev,
  1320. port_num);
  1321. goto bail;
  1322. default:
  1323. pmp->status |= IB_SMP_UNSUP_METH_ATTR;
  1324. ret = reply((struct ib_smp *) pmp);
  1325. goto bail;
  1326. }
  1327. case IB_MGMT_METHOD_SET:
  1328. switch (pmp->attr_id) {
  1329. case IB_PMA_PORT_SAMPLES_CONTROL:
  1330. ret = recv_pma_set_portsamplescontrol(pmp, ibdev,
  1331. port_num);
  1332. goto bail;
  1333. case IB_PMA_PORT_COUNTERS:
  1334. ret = recv_pma_set_portcounters(pmp, ibdev,
  1335. port_num);
  1336. goto bail;
  1337. case IB_PMA_PORT_COUNTERS_EXT:
  1338. ret = recv_pma_set_portcounters_ext(pmp, ibdev,
  1339. port_num);
  1340. goto bail;
  1341. default:
  1342. pmp->status |= IB_SMP_UNSUP_METH_ATTR;
  1343. ret = reply((struct ib_smp *) pmp);
  1344. goto bail;
  1345. }
  1346. case IB_MGMT_METHOD_GET_RESP:
  1347. /*
  1348. * The ib_mad module will call us to process responses
  1349. * before checking for other consumers.
  1350. * Just tell the caller to process it normally.
  1351. */
  1352. ret = IB_MAD_RESULT_FAILURE;
  1353. goto bail;
  1354. default:
  1355. pmp->status |= IB_SMP_UNSUP_METHOD;
  1356. ret = reply((struct ib_smp *) pmp);
  1357. }
  1358. bail:
  1359. return ret;
  1360. }
  1361. /**
  1362. * ipath_process_mad - process an incoming MAD packet
  1363. * @ibdev: the infiniband device this packet came in on
  1364. * @mad_flags: MAD flags
  1365. * @port_num: the port number this packet came in on
  1366. * @in_wc: the work completion entry for this packet
  1367. * @in_grh: the global route header for this packet
  1368. * @in_mad: the incoming MAD
  1369. * @out_mad: any outgoing MAD reply
  1370. *
  1371. * Returns IB_MAD_RESULT_SUCCESS if this is a MAD that we are not
  1372. * interested in processing.
  1373. *
  1374. * Note that the verbs framework has already done the MAD sanity checks,
  1375. * and hop count/pointer updating for IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
  1376. * MADs.
  1377. *
  1378. * This is called by the ib_mad module.
  1379. */
  1380. int ipath_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
  1381. struct ib_wc *in_wc, struct ib_grh *in_grh,
  1382. struct ib_mad *in_mad, struct ib_mad *out_mad)
  1383. {
  1384. int ret;
  1385. switch (in_mad->mad_hdr.mgmt_class) {
  1386. case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
  1387. case IB_MGMT_CLASS_SUBN_LID_ROUTED:
  1388. ret = process_subn(ibdev, mad_flags, port_num,
  1389. in_mad, out_mad);
  1390. goto bail;
  1391. case IB_MGMT_CLASS_PERF_MGMT:
  1392. ret = process_perf(ibdev, port_num, in_mad, out_mad);
  1393. goto bail;
  1394. default:
  1395. ret = IB_MAD_RESULT_SUCCESS;
  1396. }
  1397. bail:
  1398. return ret;
  1399. }