cxgb3_offload.c 37 KB

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