cxgb3_offload.c 34 KB

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