cxgb3_offload.c 37 KB

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