cxgb3_offload.c 35 KB

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