cxgb3_offload.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. /*
  2. * Copyright (c) 2006-2008 Chelsio, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/list.h>
  33. #include <net/neighbour.h>
  34. #include <linux/notifier.h>
  35. #include <asm/atomic.h>
  36. #include <linux/proc_fs.h>
  37. #include <linux/if_vlan.h>
  38. #include <net/netevent.h>
  39. #include <linux/highmem.h>
  40. #include <linux/vmalloc.h>
  41. #include "common.h"
  42. #include "regs.h"
  43. #include "cxgb3_ioctl.h"
  44. #include "cxgb3_ctl_defs.h"
  45. #include "cxgb3_defs.h"
  46. #include "l2t.h"
  47. #include "firmware_exports.h"
  48. #include "cxgb3_offload.h"
  49. static LIST_HEAD(client_list);
  50. static LIST_HEAD(ofld_dev_list);
  51. static DEFINE_MUTEX(cxgb3_db_lock);
  52. static DEFINE_RWLOCK(adapter_list_lock);
  53. static LIST_HEAD(adapter_list);
  54. static const unsigned int MAX_ATIDS = 64 * 1024;
  55. static const unsigned int ATID_BASE = 0x10000;
  56. static inline int offload_activated(struct t3cdev *tdev)
  57. {
  58. const struct adapter *adapter = tdev2adap(tdev);
  59. return (test_bit(OFFLOAD_DEVMAP_BIT, &adapter->open_device_map));
  60. }
  61. /**
  62. * cxgb3_register_client - register an offload client
  63. * @client: the client
  64. *
  65. * Add the client to the client list,
  66. * and call backs the client for each activated offload device
  67. */
  68. void cxgb3_register_client(struct cxgb3_client *client)
  69. {
  70. struct t3cdev *tdev;
  71. mutex_lock(&cxgb3_db_lock);
  72. list_add_tail(&client->client_list, &client_list);
  73. if (client->add) {
  74. list_for_each_entry(tdev, &ofld_dev_list, ofld_dev_list) {
  75. if (offload_activated(tdev))
  76. client->add(tdev);
  77. }
  78. }
  79. mutex_unlock(&cxgb3_db_lock);
  80. }
  81. EXPORT_SYMBOL(cxgb3_register_client);
  82. /**
  83. * cxgb3_unregister_client - unregister an offload client
  84. * @client: the client
  85. *
  86. * Remove the client to the client list,
  87. * and call backs the client for each activated offload device.
  88. */
  89. void cxgb3_unregister_client(struct cxgb3_client *client)
  90. {
  91. struct t3cdev *tdev;
  92. mutex_lock(&cxgb3_db_lock);
  93. list_del(&client->client_list);
  94. if (client->remove) {
  95. list_for_each_entry(tdev, &ofld_dev_list, ofld_dev_list) {
  96. if (offload_activated(tdev))
  97. client->remove(tdev);
  98. }
  99. }
  100. mutex_unlock(&cxgb3_db_lock);
  101. }
  102. EXPORT_SYMBOL(cxgb3_unregister_client);
  103. /**
  104. * cxgb3_add_clients - activate registered clients for an offload device
  105. * @tdev: the offload device
  106. *
  107. * Call backs all registered clients once a offload device is activated
  108. */
  109. void cxgb3_add_clients(struct t3cdev *tdev)
  110. {
  111. struct cxgb3_client *client;
  112. mutex_lock(&cxgb3_db_lock);
  113. list_for_each_entry(client, &client_list, client_list) {
  114. if (client->add)
  115. client->add(tdev);
  116. }
  117. mutex_unlock(&cxgb3_db_lock);
  118. }
  119. /**
  120. * cxgb3_remove_clients - deactivates registered clients
  121. * for an offload device
  122. * @tdev: the offload device
  123. *
  124. * Call backs all registered clients once a offload device is deactivated
  125. */
  126. void cxgb3_remove_clients(struct t3cdev *tdev)
  127. {
  128. struct cxgb3_client *client;
  129. mutex_lock(&cxgb3_db_lock);
  130. list_for_each_entry(client, &client_list, client_list) {
  131. if (client->remove)
  132. client->remove(tdev);
  133. }
  134. mutex_unlock(&cxgb3_db_lock);
  135. }
  136. void cxgb3_event_notify(struct t3cdev *tdev, u32 event, u32 port)
  137. {
  138. struct cxgb3_client *client;
  139. mutex_lock(&cxgb3_db_lock);
  140. list_for_each_entry(client, &client_list, client_list) {
  141. if (client->event_handler)
  142. client->event_handler(tdev, event, port);
  143. }
  144. mutex_unlock(&cxgb3_db_lock);
  145. }
  146. static struct net_device *get_iff_from_mac(struct adapter *adapter,
  147. const unsigned char *mac,
  148. unsigned int vlan)
  149. {
  150. int i;
  151. for_each_port(adapter, i) {
  152. struct vlan_group *grp;
  153. struct net_device *dev = adapter->port[i];
  154. const struct port_info *p = netdev_priv(dev);
  155. if (!memcmp(dev->dev_addr, mac, ETH_ALEN)) {
  156. if (vlan && vlan != VLAN_VID_MASK) {
  157. grp = p->vlan_grp;
  158. dev = NULL;
  159. if (grp)
  160. dev = vlan_group_get_device(grp, vlan);
  161. } else
  162. while (dev->master)
  163. dev = dev->master;
  164. return dev;
  165. }
  166. }
  167. return NULL;
  168. }
  169. static int cxgb_ulp_iscsi_ctl(struct adapter *adapter, unsigned int req,
  170. void *data)
  171. {
  172. int i;
  173. int ret = 0;
  174. unsigned int val = 0;
  175. struct ulp_iscsi_info *uiip = data;
  176. switch (req) {
  177. case ULP_ISCSI_GET_PARAMS:
  178. uiip->pdev = adapter->pdev;
  179. uiip->llimit = t3_read_reg(adapter, A_ULPRX_ISCSI_LLIMIT);
  180. uiip->ulimit = t3_read_reg(adapter, A_ULPRX_ISCSI_ULIMIT);
  181. uiip->tagmask = t3_read_reg(adapter, A_ULPRX_ISCSI_TAGMASK);
  182. val = t3_read_reg(adapter, A_ULPRX_ISCSI_PSZ);
  183. for (i = 0; i < 4; i++, val >>= 8)
  184. uiip->pgsz_factor[i] = val & 0xFF;
  185. val = t3_read_reg(adapter, A_TP_PARA_REG7);
  186. uiip->max_txsz =
  187. uiip->max_rxsz = min((val >> S_PMMAXXFERLEN0)&M_PMMAXXFERLEN0,
  188. (val >> S_PMMAXXFERLEN1)&M_PMMAXXFERLEN1);
  189. /*
  190. * On tx, the iscsi pdu has to be <= tx page size and has to
  191. * fit into the Tx PM FIFO.
  192. */
  193. val = min(adapter->params.tp.tx_pg_size,
  194. t3_read_reg(adapter, A_PM1_TX_CFG) >> 17);
  195. uiip->max_txsz = min(val, uiip->max_txsz);
  196. /* set MaxRxData to 16224 */
  197. val = t3_read_reg(adapter, A_TP_PARA_REG2);
  198. if ((val >> S_MAXRXDATA) != 0x3f60) {
  199. val &= (M_RXCOALESCESIZE << S_RXCOALESCESIZE);
  200. val |= V_MAXRXDATA(0x3f60);
  201. printk(KERN_INFO
  202. "%s, iscsi set MaxRxData to 16224 (0x%x).\n",
  203. adapter->name, val);
  204. t3_write_reg(adapter, A_TP_PARA_REG2, val);
  205. }
  206. /*
  207. * on rx, the iscsi pdu has to be < rx page size and the
  208. * the max rx data length programmed in TP
  209. */
  210. val = min(adapter->params.tp.rx_pg_size,
  211. ((t3_read_reg(adapter, A_TP_PARA_REG2)) >>
  212. S_MAXRXDATA) & M_MAXRXDATA);
  213. uiip->max_rxsz = min(val, uiip->max_rxsz);
  214. break;
  215. case ULP_ISCSI_SET_PARAMS:
  216. t3_write_reg(adapter, A_ULPRX_ISCSI_TAGMASK, uiip->tagmask);
  217. /* program the ddp page sizes */
  218. for (i = 0; i < 4; i++)
  219. val |= (uiip->pgsz_factor[i] & 0xF) << (8 * i);
  220. if (val && (val != t3_read_reg(adapter, A_ULPRX_ISCSI_PSZ))) {
  221. printk(KERN_INFO
  222. "%s, setting iscsi pgsz 0x%x, %u,%u,%u,%u.\n",
  223. adapter->name, val, uiip->pgsz_factor[0],
  224. uiip->pgsz_factor[1], uiip->pgsz_factor[2],
  225. uiip->pgsz_factor[3]);
  226. t3_write_reg(adapter, A_ULPRX_ISCSI_PSZ, val);
  227. }
  228. break;
  229. default:
  230. ret = -EOPNOTSUPP;
  231. }
  232. return ret;
  233. }
  234. /* Response queue used for RDMA events. */
  235. #define ASYNC_NOTIF_RSPQ 0
  236. static int cxgb_rdma_ctl(struct adapter *adapter, unsigned int req, void *data)
  237. {
  238. int ret = 0;
  239. switch (req) {
  240. case RDMA_GET_PARAMS: {
  241. struct rdma_info *rdma = data;
  242. struct pci_dev *pdev = adapter->pdev;
  243. rdma->udbell_physbase = pci_resource_start(pdev, 2);
  244. rdma->udbell_len = pci_resource_len(pdev, 2);
  245. rdma->tpt_base =
  246. t3_read_reg(adapter, A_ULPTX_TPT_LLIMIT);
  247. rdma->tpt_top = t3_read_reg(adapter, A_ULPTX_TPT_ULIMIT);
  248. rdma->pbl_base =
  249. t3_read_reg(adapter, A_ULPTX_PBL_LLIMIT);
  250. rdma->pbl_top = t3_read_reg(adapter, A_ULPTX_PBL_ULIMIT);
  251. rdma->rqt_base = t3_read_reg(adapter, A_ULPRX_RQ_LLIMIT);
  252. rdma->rqt_top = t3_read_reg(adapter, A_ULPRX_RQ_ULIMIT);
  253. rdma->kdb_addr = adapter->regs + A_SG_KDOORBELL;
  254. rdma->pdev = pdev;
  255. break;
  256. }
  257. case RDMA_CQ_OP:{
  258. unsigned long flags;
  259. struct rdma_cq_op *rdma = data;
  260. /* may be called in any context */
  261. spin_lock_irqsave(&adapter->sge.reg_lock, flags);
  262. ret = t3_sge_cqcntxt_op(adapter, rdma->id, rdma->op,
  263. rdma->credits);
  264. spin_unlock_irqrestore(&adapter->sge.reg_lock, flags);
  265. break;
  266. }
  267. case RDMA_GET_MEM:{
  268. struct ch_mem_range *t = data;
  269. struct mc7 *mem;
  270. if ((t->addr & 7) || (t->len & 7))
  271. return -EINVAL;
  272. if (t->mem_id == MEM_CM)
  273. mem = &adapter->cm;
  274. else if (t->mem_id == MEM_PMRX)
  275. mem = &adapter->pmrx;
  276. else if (t->mem_id == MEM_PMTX)
  277. mem = &adapter->pmtx;
  278. else
  279. return -EINVAL;
  280. ret =
  281. t3_mc7_bd_read(mem, t->addr / 8, t->len / 8,
  282. (u64 *) t->buf);
  283. if (ret)
  284. return ret;
  285. break;
  286. }
  287. case RDMA_CQ_SETUP:{
  288. struct rdma_cq_setup *rdma = data;
  289. spin_lock_irq(&adapter->sge.reg_lock);
  290. ret =
  291. t3_sge_init_cqcntxt(adapter, rdma->id,
  292. rdma->base_addr, rdma->size,
  293. ASYNC_NOTIF_RSPQ,
  294. rdma->ovfl_mode, rdma->credits,
  295. rdma->credit_thres);
  296. spin_unlock_irq(&adapter->sge.reg_lock);
  297. break;
  298. }
  299. case RDMA_CQ_DISABLE:
  300. spin_lock_irq(&adapter->sge.reg_lock);
  301. ret = t3_sge_disable_cqcntxt(adapter, *(unsigned int *)data);
  302. spin_unlock_irq(&adapter->sge.reg_lock);
  303. break;
  304. case RDMA_CTRL_QP_SETUP:{
  305. struct rdma_ctrlqp_setup *rdma = data;
  306. spin_lock_irq(&adapter->sge.reg_lock);
  307. ret = t3_sge_init_ecntxt(adapter, FW_RI_SGEEC_START, 0,
  308. SGE_CNTXT_RDMA,
  309. ASYNC_NOTIF_RSPQ,
  310. rdma->base_addr, rdma->size,
  311. FW_RI_TID_START, 1, 0);
  312. spin_unlock_irq(&adapter->sge.reg_lock);
  313. break;
  314. }
  315. case RDMA_GET_MIB: {
  316. spin_lock(&adapter->stats_lock);
  317. t3_tp_get_mib_stats(adapter, (struct tp_mib_stats *)data);
  318. spin_unlock(&adapter->stats_lock);
  319. break;
  320. }
  321. default:
  322. ret = -EOPNOTSUPP;
  323. }
  324. return ret;
  325. }
  326. static int cxgb_offload_ctl(struct t3cdev *tdev, unsigned int req, void *data)
  327. {
  328. struct adapter *adapter = tdev2adap(tdev);
  329. struct tid_range *tid;
  330. struct mtutab *mtup;
  331. struct iff_mac *iffmacp;
  332. struct ddp_params *ddpp;
  333. struct adap_ports *ports;
  334. struct ofld_page_info *rx_page_info;
  335. struct tp_params *tp = &adapter->params.tp;
  336. int i;
  337. switch (req) {
  338. case GET_MAX_OUTSTANDING_WR:
  339. *(unsigned int *)data = FW_WR_NUM;
  340. break;
  341. case GET_WR_LEN:
  342. *(unsigned int *)data = WR_FLITS;
  343. break;
  344. case GET_TX_MAX_CHUNK:
  345. *(unsigned int *)data = 1 << 20; /* 1MB */
  346. break;
  347. case GET_TID_RANGE:
  348. tid = data;
  349. tid->num = t3_mc5_size(&adapter->mc5) -
  350. adapter->params.mc5.nroutes -
  351. adapter->params.mc5.nfilters - adapter->params.mc5.nservers;
  352. tid->base = 0;
  353. break;
  354. case GET_STID_RANGE:
  355. tid = data;
  356. tid->num = adapter->params.mc5.nservers;
  357. tid->base = t3_mc5_size(&adapter->mc5) - tid->num -
  358. adapter->params.mc5.nfilters - adapter->params.mc5.nroutes;
  359. break;
  360. case GET_L2T_CAPACITY:
  361. *(unsigned int *)data = 2048;
  362. break;
  363. case GET_MTUS:
  364. mtup = data;
  365. mtup->size = NMTUS;
  366. mtup->mtus = adapter->params.mtus;
  367. break;
  368. case GET_IFF_FROM_MAC:
  369. iffmacp = data;
  370. iffmacp->dev = get_iff_from_mac(adapter, iffmacp->mac_addr,
  371. iffmacp->vlan_tag &
  372. VLAN_VID_MASK);
  373. break;
  374. case GET_DDP_PARAMS:
  375. ddpp = data;
  376. ddpp->llimit = t3_read_reg(adapter, A_ULPRX_TDDP_LLIMIT);
  377. ddpp->ulimit = t3_read_reg(adapter, A_ULPRX_TDDP_ULIMIT);
  378. ddpp->tag_mask = t3_read_reg(adapter, A_ULPRX_TDDP_TAGMASK);
  379. break;
  380. case GET_PORTS:
  381. ports = data;
  382. ports->nports = adapter->params.nports;
  383. for_each_port(adapter, i)
  384. ports->lldevs[i] = adapter->port[i];
  385. break;
  386. case ULP_ISCSI_GET_PARAMS:
  387. case ULP_ISCSI_SET_PARAMS:
  388. if (!offload_running(adapter))
  389. return -EAGAIN;
  390. return cxgb_ulp_iscsi_ctl(adapter, req, data);
  391. case RDMA_GET_PARAMS:
  392. case RDMA_CQ_OP:
  393. case RDMA_CQ_SETUP:
  394. case RDMA_CQ_DISABLE:
  395. case RDMA_CTRL_QP_SETUP:
  396. case RDMA_GET_MEM:
  397. case RDMA_GET_MIB:
  398. if (!offload_running(adapter))
  399. return -EAGAIN;
  400. return cxgb_rdma_ctl(adapter, req, data);
  401. case GET_RX_PAGE_INFO:
  402. rx_page_info = data;
  403. rx_page_info->page_size = tp->rx_pg_size;
  404. rx_page_info->num = tp->rx_num_pgs;
  405. break;
  406. case GET_ISCSI_IPV4ADDR: {
  407. struct iscsi_ipv4addr *p = data;
  408. struct port_info *pi = netdev_priv(p->dev);
  409. p->ipv4addr = pi->iscsi_ipv4addr;
  410. break;
  411. }
  412. case GET_EMBEDDED_INFO: {
  413. struct ch_embedded_info *e = data;
  414. spin_lock(&adapter->stats_lock);
  415. t3_get_fw_version(adapter, &e->fw_vers);
  416. t3_get_tp_version(adapter, &e->tp_vers);
  417. spin_unlock(&adapter->stats_lock);
  418. break;
  419. }
  420. default:
  421. return -EOPNOTSUPP;
  422. }
  423. return 0;
  424. }
  425. /*
  426. * Dummy handler for Rx offload packets in case we get an offload packet before
  427. * proper processing is setup. This complains and drops the packet as it isn't
  428. * normal to get offload packets at this stage.
  429. */
  430. static int rx_offload_blackhole(struct t3cdev *dev, struct sk_buff **skbs,
  431. int n)
  432. {
  433. while (n--)
  434. dev_kfree_skb_any(skbs[n]);
  435. return 0;
  436. }
  437. static void dummy_neigh_update(struct t3cdev *dev, struct neighbour *neigh)
  438. {
  439. }
  440. void cxgb3_set_dummy_ops(struct t3cdev *dev)
  441. {
  442. dev->recv = rx_offload_blackhole;
  443. dev->neigh_update = dummy_neigh_update;
  444. }
  445. /*
  446. * Free an active-open TID.
  447. */
  448. void *cxgb3_free_atid(struct t3cdev *tdev, int atid)
  449. {
  450. struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
  451. union active_open_entry *p = atid2entry(t, atid);
  452. void *ctx = p->t3c_tid.ctx;
  453. spin_lock_bh(&t->atid_lock);
  454. p->next = t->afree;
  455. t->afree = p;
  456. t->atids_in_use--;
  457. spin_unlock_bh(&t->atid_lock);
  458. return ctx;
  459. }
  460. EXPORT_SYMBOL(cxgb3_free_atid);
  461. /*
  462. * Free a server TID and return it to the free pool.
  463. */
  464. void cxgb3_free_stid(struct t3cdev *tdev, int stid)
  465. {
  466. struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
  467. union listen_entry *p = stid2entry(t, stid);
  468. spin_lock_bh(&t->stid_lock);
  469. p->next = t->sfree;
  470. t->sfree = p;
  471. t->stids_in_use--;
  472. spin_unlock_bh(&t->stid_lock);
  473. }
  474. EXPORT_SYMBOL(cxgb3_free_stid);
  475. void cxgb3_insert_tid(struct t3cdev *tdev, struct cxgb3_client *client,
  476. void *ctx, unsigned int tid)
  477. {
  478. struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
  479. t->tid_tab[tid].client = client;
  480. t->tid_tab[tid].ctx = ctx;
  481. atomic_inc(&t->tids_in_use);
  482. }
  483. EXPORT_SYMBOL(cxgb3_insert_tid);
  484. /*
  485. * Populate a TID_RELEASE WR. The skb must be already propely sized.
  486. */
  487. static inline void mk_tid_release(struct sk_buff *skb, unsigned int tid)
  488. {
  489. struct cpl_tid_release *req;
  490. skb->priority = CPL_PRIORITY_SETUP;
  491. req = (struct cpl_tid_release *)__skb_put(skb, sizeof(*req));
  492. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  493. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
  494. }
  495. static void t3_process_tid_release_list(struct work_struct *work)
  496. {
  497. struct t3c_data *td = container_of(work, struct t3c_data,
  498. tid_release_task);
  499. struct sk_buff *skb;
  500. struct t3cdev *tdev = td->dev;
  501. spin_lock_bh(&td->tid_release_lock);
  502. while (td->tid_release_list) {
  503. struct t3c_tid_entry *p = td->tid_release_list;
  504. td->tid_release_list = (struct t3c_tid_entry *)p->ctx;
  505. spin_unlock_bh(&td->tid_release_lock);
  506. skb = alloc_skb(sizeof(struct cpl_tid_release),
  507. GFP_KERNEL);
  508. if (!skb)
  509. skb = td->nofail_skb;
  510. if (!skb) {
  511. spin_lock_bh(&td->tid_release_lock);
  512. p->ctx = (void *)td->tid_release_list;
  513. td->tid_release_list = (struct t3c_tid_entry *)p;
  514. break;
  515. }
  516. mk_tid_release(skb, p - td->tid_maps.tid_tab);
  517. cxgb3_ofld_send(tdev, skb);
  518. p->ctx = NULL;
  519. if (skb == td->nofail_skb)
  520. td->nofail_skb =
  521. alloc_skb(sizeof(struct cpl_tid_release),
  522. GFP_KERNEL);
  523. spin_lock_bh(&td->tid_release_lock);
  524. }
  525. td->release_list_incomplete = (td->tid_release_list == NULL) ? 0 : 1;
  526. spin_unlock_bh(&td->tid_release_lock);
  527. if (!td->nofail_skb)
  528. td->nofail_skb =
  529. alloc_skb(sizeof(struct cpl_tid_release),
  530. GFP_KERNEL);
  531. }
  532. /* use ctx as a next pointer in the tid release list */
  533. void cxgb3_queue_tid_release(struct t3cdev *tdev, unsigned int tid)
  534. {
  535. struct t3c_data *td = T3C_DATA(tdev);
  536. struct t3c_tid_entry *p = &td->tid_maps.tid_tab[tid];
  537. spin_lock_bh(&td->tid_release_lock);
  538. p->ctx = (void *)td->tid_release_list;
  539. p->client = NULL;
  540. td->tid_release_list = p;
  541. if (!p->ctx || td->release_list_incomplete)
  542. schedule_work(&td->tid_release_task);
  543. spin_unlock_bh(&td->tid_release_lock);
  544. }
  545. EXPORT_SYMBOL(cxgb3_queue_tid_release);
  546. /*
  547. * Remove a tid from the TID table. A client may defer processing its last
  548. * CPL message if it is locked at the time it arrives, and while the message
  549. * sits in the client's backlog the TID may be reused for another connection.
  550. * To handle this we atomically switch the TID association if it still points
  551. * to the original client context.
  552. */
  553. void cxgb3_remove_tid(struct t3cdev *tdev, void *ctx, unsigned int tid)
  554. {
  555. struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
  556. BUG_ON(tid >= t->ntids);
  557. if (tdev->type == T3A)
  558. (void)cmpxchg(&t->tid_tab[tid].ctx, ctx, NULL);
  559. else {
  560. struct sk_buff *skb;
  561. skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
  562. if (likely(skb)) {
  563. mk_tid_release(skb, tid);
  564. cxgb3_ofld_send(tdev, skb);
  565. t->tid_tab[tid].ctx = NULL;
  566. } else
  567. cxgb3_queue_tid_release(tdev, tid);
  568. }
  569. atomic_dec(&t->tids_in_use);
  570. }
  571. EXPORT_SYMBOL(cxgb3_remove_tid);
  572. int cxgb3_alloc_atid(struct t3cdev *tdev, struct cxgb3_client *client,
  573. void *ctx)
  574. {
  575. int atid = -1;
  576. struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
  577. spin_lock_bh(&t->atid_lock);
  578. if (t->afree &&
  579. t->atids_in_use + atomic_read(&t->tids_in_use) + MC5_MIN_TIDS <=
  580. t->ntids) {
  581. union active_open_entry *p = t->afree;
  582. atid = (p - t->atid_tab) + t->atid_base;
  583. t->afree = p->next;
  584. p->t3c_tid.ctx = ctx;
  585. p->t3c_tid.client = client;
  586. t->atids_in_use++;
  587. }
  588. spin_unlock_bh(&t->atid_lock);
  589. return atid;
  590. }
  591. EXPORT_SYMBOL(cxgb3_alloc_atid);
  592. int cxgb3_alloc_stid(struct t3cdev *tdev, struct cxgb3_client *client,
  593. void *ctx)
  594. {
  595. int stid = -1;
  596. struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
  597. spin_lock_bh(&t->stid_lock);
  598. if (t->sfree) {
  599. union listen_entry *p = t->sfree;
  600. stid = (p - t->stid_tab) + t->stid_base;
  601. t->sfree = p->next;
  602. p->t3c_tid.ctx = ctx;
  603. p->t3c_tid.client = client;
  604. t->stids_in_use++;
  605. }
  606. spin_unlock_bh(&t->stid_lock);
  607. return stid;
  608. }
  609. EXPORT_SYMBOL(cxgb3_alloc_stid);
  610. /* Get the t3cdev associated with a net_device */
  611. struct t3cdev *dev2t3cdev(struct net_device *dev)
  612. {
  613. const struct port_info *pi = netdev_priv(dev);
  614. return (struct t3cdev *)pi->adapter;
  615. }
  616. EXPORT_SYMBOL(dev2t3cdev);
  617. static int do_smt_write_rpl(struct t3cdev *dev, struct sk_buff *skb)
  618. {
  619. struct cpl_smt_write_rpl *rpl = cplhdr(skb);
  620. if (rpl->status != CPL_ERR_NONE)
  621. printk(KERN_ERR
  622. "Unexpected SMT_WRITE_RPL status %u for entry %u\n",
  623. rpl->status, GET_TID(rpl));
  624. return CPL_RET_BUF_DONE;
  625. }
  626. static int do_l2t_write_rpl(struct t3cdev *dev, struct sk_buff *skb)
  627. {
  628. struct cpl_l2t_write_rpl *rpl = cplhdr(skb);
  629. if (rpl->status != CPL_ERR_NONE)
  630. printk(KERN_ERR
  631. "Unexpected L2T_WRITE_RPL status %u for entry %u\n",
  632. rpl->status, GET_TID(rpl));
  633. return CPL_RET_BUF_DONE;
  634. }
  635. static int do_rte_write_rpl(struct t3cdev *dev, struct sk_buff *skb)
  636. {
  637. struct cpl_rte_write_rpl *rpl = cplhdr(skb);
  638. if (rpl->status != CPL_ERR_NONE)
  639. printk(KERN_ERR
  640. "Unexpected RTE_WRITE_RPL status %u for entry %u\n",
  641. rpl->status, GET_TID(rpl));
  642. return CPL_RET_BUF_DONE;
  643. }
  644. static int do_act_open_rpl(struct t3cdev *dev, struct sk_buff *skb)
  645. {
  646. struct cpl_act_open_rpl *rpl = cplhdr(skb);
  647. unsigned int atid = G_TID(ntohl(rpl->atid));
  648. struct t3c_tid_entry *t3c_tid;
  649. t3c_tid = lookup_atid(&(T3C_DATA(dev))->tid_maps, atid);
  650. if (t3c_tid && t3c_tid->ctx && t3c_tid->client &&
  651. t3c_tid->client->handlers &&
  652. t3c_tid->client->handlers[CPL_ACT_OPEN_RPL]) {
  653. return t3c_tid->client->handlers[CPL_ACT_OPEN_RPL] (dev, skb,
  654. t3c_tid->
  655. ctx);
  656. } else {
  657. printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
  658. dev->name, CPL_ACT_OPEN_RPL);
  659. return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
  660. }
  661. }
  662. static int do_stid_rpl(struct t3cdev *dev, struct sk_buff *skb)
  663. {
  664. union opcode_tid *p = cplhdr(skb);
  665. unsigned int stid = G_TID(ntohl(p->opcode_tid));
  666. struct t3c_tid_entry *t3c_tid;
  667. t3c_tid = lookup_stid(&(T3C_DATA(dev))->tid_maps, stid);
  668. if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
  669. t3c_tid->client->handlers[p->opcode]) {
  670. return t3c_tid->client->handlers[p->opcode] (dev, skb,
  671. t3c_tid->ctx);
  672. } else {
  673. printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
  674. dev->name, p->opcode);
  675. return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
  676. }
  677. }
  678. static int do_hwtid_rpl(struct t3cdev *dev, struct sk_buff *skb)
  679. {
  680. union opcode_tid *p = cplhdr(skb);
  681. unsigned int hwtid = G_TID(ntohl(p->opcode_tid));
  682. struct t3c_tid_entry *t3c_tid;
  683. t3c_tid = lookup_tid(&(T3C_DATA(dev))->tid_maps, hwtid);
  684. if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
  685. t3c_tid->client->handlers[p->opcode]) {
  686. return t3c_tid->client->handlers[p->opcode]
  687. (dev, skb, t3c_tid->ctx);
  688. } else {
  689. printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
  690. dev->name, p->opcode);
  691. return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
  692. }
  693. }
  694. static int do_cr(struct t3cdev *dev, struct sk_buff *skb)
  695. {
  696. struct cpl_pass_accept_req *req = cplhdr(skb);
  697. unsigned int stid = G_PASS_OPEN_TID(ntohl(req->tos_tid));
  698. struct tid_info *t = &(T3C_DATA(dev))->tid_maps;
  699. struct t3c_tid_entry *t3c_tid;
  700. unsigned int tid = GET_TID(req);
  701. if (unlikely(tid >= t->ntids)) {
  702. printk("%s: passive open TID %u too large\n",
  703. dev->name, tid);
  704. t3_fatal_err(tdev2adap(dev));
  705. return CPL_RET_BUF_DONE;
  706. }
  707. t3c_tid = lookup_stid(t, stid);
  708. if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
  709. t3c_tid->client->handlers[CPL_PASS_ACCEPT_REQ]) {
  710. return t3c_tid->client->handlers[CPL_PASS_ACCEPT_REQ]
  711. (dev, skb, t3c_tid->ctx);
  712. } else {
  713. printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
  714. dev->name, CPL_PASS_ACCEPT_REQ);
  715. return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
  716. }
  717. }
  718. /*
  719. * Returns an sk_buff for a reply CPL message of size len. If the input
  720. * sk_buff has no other users it is trimmed and reused, otherwise a new buffer
  721. * is allocated. The input skb must be of size at least len. Note that this
  722. * operation does not destroy the original skb data even if it decides to reuse
  723. * the buffer.
  724. */
  725. static struct sk_buff *cxgb3_get_cpl_reply_skb(struct sk_buff *skb, size_t len,
  726. gfp_t gfp)
  727. {
  728. if (likely(!skb_cloned(skb))) {
  729. BUG_ON(skb->len < len);
  730. __skb_trim(skb, len);
  731. skb_get(skb);
  732. } else {
  733. skb = alloc_skb(len, gfp);
  734. if (skb)
  735. __skb_put(skb, len);
  736. }
  737. return skb;
  738. }
  739. static int do_abort_req_rss(struct t3cdev *dev, struct sk_buff *skb)
  740. {
  741. union opcode_tid *p = cplhdr(skb);
  742. unsigned int hwtid = G_TID(ntohl(p->opcode_tid));
  743. struct t3c_tid_entry *t3c_tid;
  744. t3c_tid = lookup_tid(&(T3C_DATA(dev))->tid_maps, hwtid);
  745. if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
  746. t3c_tid->client->handlers[p->opcode]) {
  747. return t3c_tid->client->handlers[p->opcode]
  748. (dev, skb, t3c_tid->ctx);
  749. } else {
  750. struct cpl_abort_req_rss *req = cplhdr(skb);
  751. struct cpl_abort_rpl *rpl;
  752. struct sk_buff *reply_skb;
  753. unsigned int tid = GET_TID(req);
  754. u8 cmd = req->status;
  755. if (req->status == CPL_ERR_RTX_NEG_ADVICE ||
  756. req->status == CPL_ERR_PERSIST_NEG_ADVICE)
  757. goto out;
  758. reply_skb = cxgb3_get_cpl_reply_skb(skb,
  759. sizeof(struct
  760. cpl_abort_rpl),
  761. GFP_ATOMIC);
  762. if (!reply_skb) {
  763. printk("do_abort_req_rss: couldn't get skb!\n");
  764. goto out;
  765. }
  766. reply_skb->priority = CPL_PRIORITY_DATA;
  767. __skb_put(reply_skb, sizeof(struct cpl_abort_rpl));
  768. rpl = cplhdr(reply_skb);
  769. rpl->wr.wr_hi =
  770. htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_RPL));
  771. rpl->wr.wr_lo = htonl(V_WR_TID(tid));
  772. OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_ABORT_RPL, tid));
  773. rpl->cmd = cmd;
  774. cxgb3_ofld_send(dev, reply_skb);
  775. out:
  776. return CPL_RET_BUF_DONE;
  777. }
  778. }
  779. static int do_act_establish(struct t3cdev *dev, struct sk_buff *skb)
  780. {
  781. struct cpl_act_establish *req = cplhdr(skb);
  782. unsigned int atid = G_PASS_OPEN_TID(ntohl(req->tos_tid));
  783. struct tid_info *t = &(T3C_DATA(dev))->tid_maps;
  784. struct t3c_tid_entry *t3c_tid;
  785. unsigned int tid = GET_TID(req);
  786. if (unlikely(tid >= t->ntids)) {
  787. printk("%s: active establish TID %u too large\n",
  788. dev->name, tid);
  789. t3_fatal_err(tdev2adap(dev));
  790. return CPL_RET_BUF_DONE;
  791. }
  792. t3c_tid = lookup_atid(t, atid);
  793. if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
  794. t3c_tid->client->handlers[CPL_ACT_ESTABLISH]) {
  795. return t3c_tid->client->handlers[CPL_ACT_ESTABLISH]
  796. (dev, skb, t3c_tid->ctx);
  797. } else {
  798. printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
  799. dev->name, CPL_ACT_ESTABLISH);
  800. return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
  801. }
  802. }
  803. static int do_trace(struct t3cdev *dev, struct sk_buff *skb)
  804. {
  805. struct cpl_trace_pkt *p = cplhdr(skb);
  806. skb->protocol = htons(0xffff);
  807. skb->dev = dev->lldev;
  808. skb_pull(skb, sizeof(*p));
  809. skb_reset_mac_header(skb);
  810. netif_receive_skb(skb);
  811. return 0;
  812. }
  813. /*
  814. * That skb would better have come from process_responses() where we abuse
  815. * ->priority and ->csum to carry our data. NB: if we get to per-arch
  816. * ->csum, the things might get really interesting here.
  817. */
  818. static inline u32 get_hwtid(struct sk_buff *skb)
  819. {
  820. return ntohl((__force __be32)skb->priority) >> 8 & 0xfffff;
  821. }
  822. static inline u32 get_opcode(struct sk_buff *skb)
  823. {
  824. return G_OPCODE(ntohl((__force __be32)skb->csum));
  825. }
  826. static int do_term(struct t3cdev *dev, struct sk_buff *skb)
  827. {
  828. unsigned int hwtid = get_hwtid(skb);
  829. unsigned int opcode = get_opcode(skb);
  830. struct t3c_tid_entry *t3c_tid;
  831. t3c_tid = lookup_tid(&(T3C_DATA(dev))->tid_maps, hwtid);
  832. if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
  833. t3c_tid->client->handlers[opcode]) {
  834. return t3c_tid->client->handlers[opcode] (dev, skb,
  835. t3c_tid->ctx);
  836. } else {
  837. printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
  838. dev->name, opcode);
  839. return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
  840. }
  841. }
  842. static int nb_callback(struct notifier_block *self, unsigned long event,
  843. void *ctx)
  844. {
  845. switch (event) {
  846. case (NETEVENT_NEIGH_UPDATE):{
  847. cxgb_neigh_update((struct neighbour *)ctx);
  848. break;
  849. }
  850. case (NETEVENT_PMTU_UPDATE):
  851. break;
  852. case (NETEVENT_REDIRECT):{
  853. struct netevent_redirect *nr = ctx;
  854. cxgb_redirect(nr->old, nr->new);
  855. cxgb_neigh_update(nr->new->neighbour);
  856. break;
  857. }
  858. default:
  859. break;
  860. }
  861. return 0;
  862. }
  863. static struct notifier_block nb = {
  864. .notifier_call = nb_callback
  865. };
  866. /*
  867. * Process a received packet with an unknown/unexpected CPL opcode.
  868. */
  869. static int do_bad_cpl(struct t3cdev *dev, struct sk_buff *skb)
  870. {
  871. printk(KERN_ERR "%s: received bad CPL command 0x%x\n", dev->name,
  872. *skb->data);
  873. return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
  874. }
  875. /*
  876. * Handlers for each CPL opcode
  877. */
  878. static cpl_handler_func cpl_handlers[NUM_CPL_CMDS];
  879. /*
  880. * Add a new handler to the CPL dispatch table. A NULL handler may be supplied
  881. * to unregister an existing handler.
  882. */
  883. void t3_register_cpl_handler(unsigned int opcode, cpl_handler_func h)
  884. {
  885. if (opcode < NUM_CPL_CMDS)
  886. cpl_handlers[opcode] = h ? h : do_bad_cpl;
  887. else
  888. printk(KERN_ERR "T3C: handler registration for "
  889. "opcode %x failed\n", opcode);
  890. }
  891. EXPORT_SYMBOL(t3_register_cpl_handler);
  892. /*
  893. * T3CDEV's receive method.
  894. */
  895. int process_rx(struct t3cdev *dev, struct sk_buff **skbs, int n)
  896. {
  897. while (n--) {
  898. struct sk_buff *skb = *skbs++;
  899. unsigned int opcode = get_opcode(skb);
  900. int ret = cpl_handlers[opcode] (dev, skb);
  901. #if VALIDATE_TID
  902. if (ret & CPL_RET_UNKNOWN_TID) {
  903. union opcode_tid *p = cplhdr(skb);
  904. printk(KERN_ERR "%s: CPL message (opcode %u) had "
  905. "unknown TID %u\n", dev->name, opcode,
  906. G_TID(ntohl(p->opcode_tid)));
  907. }
  908. #endif
  909. if (ret & CPL_RET_BUF_DONE)
  910. kfree_skb(skb);
  911. }
  912. return 0;
  913. }
  914. /*
  915. * Sends an sk_buff to a T3C driver after dealing with any active network taps.
  916. */
  917. int cxgb3_ofld_send(struct t3cdev *dev, struct sk_buff *skb)
  918. {
  919. int r;
  920. local_bh_disable();
  921. r = dev->send(dev, skb);
  922. local_bh_enable();
  923. return r;
  924. }
  925. EXPORT_SYMBOL(cxgb3_ofld_send);
  926. static int is_offloading(struct net_device *dev)
  927. {
  928. struct adapter *adapter;
  929. int i;
  930. read_lock_bh(&adapter_list_lock);
  931. list_for_each_entry(adapter, &adapter_list, adapter_list) {
  932. for_each_port(adapter, i) {
  933. if (dev == adapter->port[i]) {
  934. read_unlock_bh(&adapter_list_lock);
  935. return 1;
  936. }
  937. }
  938. }
  939. read_unlock_bh(&adapter_list_lock);
  940. return 0;
  941. }
  942. void cxgb_neigh_update(struct neighbour *neigh)
  943. {
  944. struct net_device *dev = neigh->dev;
  945. if (dev && (is_offloading(dev))) {
  946. struct t3cdev *tdev = dev2t3cdev(dev);
  947. BUG_ON(!tdev);
  948. t3_l2t_update(tdev, neigh);
  949. }
  950. }
  951. static void set_l2t_ix(struct t3cdev *tdev, u32 tid, struct l2t_entry *e)
  952. {
  953. struct sk_buff *skb;
  954. struct cpl_set_tcb_field *req;
  955. skb = alloc_skb(sizeof(*req), GFP_ATOMIC);
  956. if (!skb) {
  957. printk(KERN_ERR "%s: cannot allocate skb!\n", __func__);
  958. return;
  959. }
  960. skb->priority = CPL_PRIORITY_CONTROL;
  961. req = (struct cpl_set_tcb_field *)skb_put(skb, sizeof(*req));
  962. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  963. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, tid));
  964. req->reply = 0;
  965. req->cpu_idx = 0;
  966. req->word = htons(W_TCB_L2T_IX);
  967. req->mask = cpu_to_be64(V_TCB_L2T_IX(M_TCB_L2T_IX));
  968. req->val = cpu_to_be64(V_TCB_L2T_IX(e->idx));
  969. tdev->send(tdev, skb);
  970. }
  971. void cxgb_redirect(struct dst_entry *old, struct dst_entry *new)
  972. {
  973. struct net_device *olddev, *newdev;
  974. struct tid_info *ti;
  975. struct t3cdev *tdev;
  976. u32 tid;
  977. int update_tcb;
  978. struct l2t_entry *e;
  979. struct t3c_tid_entry *te;
  980. olddev = old->neighbour->dev;
  981. newdev = new->neighbour->dev;
  982. if (!is_offloading(olddev))
  983. return;
  984. if (!is_offloading(newdev)) {
  985. printk(KERN_WARNING "%s: Redirect to non-offload "
  986. "device ignored.\n", __func__);
  987. return;
  988. }
  989. tdev = dev2t3cdev(olddev);
  990. BUG_ON(!tdev);
  991. if (tdev != dev2t3cdev(newdev)) {
  992. printk(KERN_WARNING "%s: Redirect to different "
  993. "offload device ignored.\n", __func__);
  994. return;
  995. }
  996. /* Add new L2T entry */
  997. e = t3_l2t_get(tdev, new->neighbour, newdev);
  998. if (!e) {
  999. printk(KERN_ERR "%s: couldn't allocate new l2t entry!\n",
  1000. __func__);
  1001. return;
  1002. }
  1003. /* Walk tid table and notify clients of dst change. */
  1004. ti = &(T3C_DATA(tdev))->tid_maps;
  1005. for (tid = 0; tid < ti->ntids; tid++) {
  1006. te = lookup_tid(ti, tid);
  1007. BUG_ON(!te);
  1008. if (te && te->ctx && te->client && te->client->redirect) {
  1009. update_tcb = te->client->redirect(te->ctx, old, new, e);
  1010. if (update_tcb) {
  1011. l2t_hold(L2DATA(tdev), e);
  1012. set_l2t_ix(tdev, tid, e);
  1013. }
  1014. }
  1015. }
  1016. l2t_release(L2DATA(tdev), e);
  1017. }
  1018. /*
  1019. * Allocate a chunk of memory using kmalloc or, if that fails, vmalloc.
  1020. * The allocated memory is cleared.
  1021. */
  1022. void *cxgb_alloc_mem(unsigned long size)
  1023. {
  1024. void *p = kmalloc(size, GFP_KERNEL);
  1025. if (!p)
  1026. p = vmalloc(size);
  1027. if (p)
  1028. memset(p, 0, size);
  1029. return p;
  1030. }
  1031. /*
  1032. * Free memory allocated through t3_alloc_mem().
  1033. */
  1034. void cxgb_free_mem(void *addr)
  1035. {
  1036. if (is_vmalloc_addr(addr))
  1037. vfree(addr);
  1038. else
  1039. kfree(addr);
  1040. }
  1041. /*
  1042. * Allocate and initialize the TID tables. Returns 0 on success.
  1043. */
  1044. static int init_tid_tabs(struct tid_info *t, unsigned int ntids,
  1045. unsigned int natids, unsigned int nstids,
  1046. unsigned int atid_base, unsigned int stid_base)
  1047. {
  1048. unsigned long size = ntids * sizeof(*t->tid_tab) +
  1049. natids * sizeof(*t->atid_tab) + nstids * sizeof(*t->stid_tab);
  1050. t->tid_tab = cxgb_alloc_mem(size);
  1051. if (!t->tid_tab)
  1052. return -ENOMEM;
  1053. t->stid_tab = (union listen_entry *)&t->tid_tab[ntids];
  1054. t->atid_tab = (union active_open_entry *)&t->stid_tab[nstids];
  1055. t->ntids = ntids;
  1056. t->nstids = nstids;
  1057. t->stid_base = stid_base;
  1058. t->sfree = NULL;
  1059. t->natids = natids;
  1060. t->atid_base = atid_base;
  1061. t->afree = NULL;
  1062. t->stids_in_use = t->atids_in_use = 0;
  1063. atomic_set(&t->tids_in_use, 0);
  1064. spin_lock_init(&t->stid_lock);
  1065. spin_lock_init(&t->atid_lock);
  1066. /*
  1067. * Setup the free lists for stid_tab and atid_tab.
  1068. */
  1069. if (nstids) {
  1070. while (--nstids)
  1071. t->stid_tab[nstids - 1].next = &t->stid_tab[nstids];
  1072. t->sfree = t->stid_tab;
  1073. }
  1074. if (natids) {
  1075. while (--natids)
  1076. t->atid_tab[natids - 1].next = &t->atid_tab[natids];
  1077. t->afree = t->atid_tab;
  1078. }
  1079. return 0;
  1080. }
  1081. static void free_tid_maps(struct tid_info *t)
  1082. {
  1083. cxgb_free_mem(t->tid_tab);
  1084. }
  1085. static inline void add_adapter(struct adapter *adap)
  1086. {
  1087. write_lock_bh(&adapter_list_lock);
  1088. list_add_tail(&adap->adapter_list, &adapter_list);
  1089. write_unlock_bh(&adapter_list_lock);
  1090. }
  1091. static inline void remove_adapter(struct adapter *adap)
  1092. {
  1093. write_lock_bh(&adapter_list_lock);
  1094. list_del(&adap->adapter_list);
  1095. write_unlock_bh(&adapter_list_lock);
  1096. }
  1097. int cxgb3_offload_activate(struct adapter *adapter)
  1098. {
  1099. struct t3cdev *dev = &adapter->tdev;
  1100. int natids, err;
  1101. struct t3c_data *t;
  1102. struct tid_range stid_range, tid_range;
  1103. struct mtutab mtutab;
  1104. unsigned int l2t_capacity;
  1105. t = kcalloc(1, sizeof(*t), GFP_KERNEL);
  1106. if (!t)
  1107. return -ENOMEM;
  1108. err = -EOPNOTSUPP;
  1109. if (dev->ctl(dev, GET_TX_MAX_CHUNK, &t->tx_max_chunk) < 0 ||
  1110. dev->ctl(dev, GET_MAX_OUTSTANDING_WR, &t->max_wrs) < 0 ||
  1111. dev->ctl(dev, GET_L2T_CAPACITY, &l2t_capacity) < 0 ||
  1112. dev->ctl(dev, GET_MTUS, &mtutab) < 0 ||
  1113. dev->ctl(dev, GET_TID_RANGE, &tid_range) < 0 ||
  1114. dev->ctl(dev, GET_STID_RANGE, &stid_range) < 0)
  1115. goto out_free;
  1116. err = -ENOMEM;
  1117. L2DATA(dev) = t3_init_l2t(l2t_capacity);
  1118. if (!L2DATA(dev))
  1119. goto out_free;
  1120. natids = min(tid_range.num / 2, MAX_ATIDS);
  1121. err = init_tid_tabs(&t->tid_maps, tid_range.num, natids,
  1122. stid_range.num, ATID_BASE, stid_range.base);
  1123. if (err)
  1124. goto out_free_l2t;
  1125. t->mtus = mtutab.mtus;
  1126. t->nmtus = mtutab.size;
  1127. INIT_WORK(&t->tid_release_task, t3_process_tid_release_list);
  1128. spin_lock_init(&t->tid_release_lock);
  1129. INIT_LIST_HEAD(&t->list_node);
  1130. t->dev = dev;
  1131. T3C_DATA(dev) = t;
  1132. dev->recv = process_rx;
  1133. dev->neigh_update = t3_l2t_update;
  1134. /* Register netevent handler once */
  1135. if (list_empty(&adapter_list))
  1136. register_netevent_notifier(&nb);
  1137. t->nofail_skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_KERNEL);
  1138. t->release_list_incomplete = 0;
  1139. add_adapter(adapter);
  1140. return 0;
  1141. out_free_l2t:
  1142. t3_free_l2t(L2DATA(dev));
  1143. L2DATA(dev) = NULL;
  1144. out_free:
  1145. kfree(t);
  1146. return err;
  1147. }
  1148. void cxgb3_offload_deactivate(struct adapter *adapter)
  1149. {
  1150. struct t3cdev *tdev = &adapter->tdev;
  1151. struct t3c_data *t = T3C_DATA(tdev);
  1152. remove_adapter(adapter);
  1153. if (list_empty(&adapter_list))
  1154. unregister_netevent_notifier(&nb);
  1155. free_tid_maps(&t->tid_maps);
  1156. T3C_DATA(tdev) = NULL;
  1157. t3_free_l2t(L2DATA(tdev));
  1158. L2DATA(tdev) = NULL;
  1159. if (t->nofail_skb)
  1160. kfree_skb(t->nofail_skb);
  1161. kfree(t);
  1162. }
  1163. static inline void register_tdev(struct t3cdev *tdev)
  1164. {
  1165. static int unit;
  1166. mutex_lock(&cxgb3_db_lock);
  1167. snprintf(tdev->name, sizeof(tdev->name), "ofld_dev%d", unit++);
  1168. list_add_tail(&tdev->ofld_dev_list, &ofld_dev_list);
  1169. mutex_unlock(&cxgb3_db_lock);
  1170. }
  1171. static inline void unregister_tdev(struct t3cdev *tdev)
  1172. {
  1173. mutex_lock(&cxgb3_db_lock);
  1174. list_del(&tdev->ofld_dev_list);
  1175. mutex_unlock(&cxgb3_db_lock);
  1176. }
  1177. static inline int adap2type(struct adapter *adapter)
  1178. {
  1179. int type = 0;
  1180. switch (adapter->params.rev) {
  1181. case T3_REV_A:
  1182. type = T3A;
  1183. break;
  1184. case T3_REV_B:
  1185. case T3_REV_B2:
  1186. type = T3B;
  1187. break;
  1188. case T3_REV_C:
  1189. type = T3C;
  1190. break;
  1191. }
  1192. return type;
  1193. }
  1194. void __devinit cxgb3_adapter_ofld(struct adapter *adapter)
  1195. {
  1196. struct t3cdev *tdev = &adapter->tdev;
  1197. INIT_LIST_HEAD(&tdev->ofld_dev_list);
  1198. cxgb3_set_dummy_ops(tdev);
  1199. tdev->send = t3_offload_tx;
  1200. tdev->ctl = cxgb_offload_ctl;
  1201. tdev->type = adap2type(adapter);
  1202. register_tdev(tdev);
  1203. }
  1204. void __devexit cxgb3_adapter_unofld(struct adapter *adapter)
  1205. {
  1206. struct t3cdev *tdev = &adapter->tdev;
  1207. tdev->recv = NULL;
  1208. tdev->neigh_update = NULL;
  1209. unregister_tdev(tdev);
  1210. }
  1211. void __init cxgb3_offload_init(void)
  1212. {
  1213. int i;
  1214. for (i = 0; i < NUM_CPL_CMDS; ++i)
  1215. cpl_handlers[i] = do_bad_cpl;
  1216. t3_register_cpl_handler(CPL_SMT_WRITE_RPL, do_smt_write_rpl);
  1217. t3_register_cpl_handler(CPL_L2T_WRITE_RPL, do_l2t_write_rpl);
  1218. t3_register_cpl_handler(CPL_RTE_WRITE_RPL, do_rte_write_rpl);
  1219. t3_register_cpl_handler(CPL_PASS_OPEN_RPL, do_stid_rpl);
  1220. t3_register_cpl_handler(CPL_CLOSE_LISTSRV_RPL, do_stid_rpl);
  1221. t3_register_cpl_handler(CPL_PASS_ACCEPT_REQ, do_cr);
  1222. t3_register_cpl_handler(CPL_PASS_ESTABLISH, do_hwtid_rpl);
  1223. t3_register_cpl_handler(CPL_ABORT_RPL_RSS, do_hwtid_rpl);
  1224. t3_register_cpl_handler(CPL_ABORT_RPL, do_hwtid_rpl);
  1225. t3_register_cpl_handler(CPL_RX_URG_NOTIFY, do_hwtid_rpl);
  1226. t3_register_cpl_handler(CPL_RX_DATA, do_hwtid_rpl);
  1227. t3_register_cpl_handler(CPL_TX_DATA_ACK, do_hwtid_rpl);
  1228. t3_register_cpl_handler(CPL_TX_DMA_ACK, do_hwtid_rpl);
  1229. t3_register_cpl_handler(CPL_ACT_OPEN_RPL, do_act_open_rpl);
  1230. t3_register_cpl_handler(CPL_PEER_CLOSE, do_hwtid_rpl);
  1231. t3_register_cpl_handler(CPL_CLOSE_CON_RPL, do_hwtid_rpl);
  1232. t3_register_cpl_handler(CPL_ABORT_REQ_RSS, do_abort_req_rss);
  1233. t3_register_cpl_handler(CPL_ACT_ESTABLISH, do_act_establish);
  1234. t3_register_cpl_handler(CPL_SET_TCB_RPL, do_hwtid_rpl);
  1235. t3_register_cpl_handler(CPL_GET_TCB_RPL, do_hwtid_rpl);
  1236. t3_register_cpl_handler(CPL_RDMA_TERMINATE, do_term);
  1237. t3_register_cpl_handler(CPL_RDMA_EC_STATUS, do_hwtid_rpl);
  1238. t3_register_cpl_handler(CPL_TRACE_PKT, do_trace);
  1239. t3_register_cpl_handler(CPL_RX_DATA_DDP, do_hwtid_rpl);
  1240. t3_register_cpl_handler(CPL_RX_DDP_COMPLETE, do_hwtid_rpl);
  1241. t3_register_cpl_handler(CPL_ISCSI_HDR, do_hwtid_rpl);
  1242. }