cxgb3_offload.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  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 <linux/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 net_device *dev = adapter->port[i];
  156. if (!memcmp(dev->dev_addr, mac, ETH_ALEN)) {
  157. if (vlan && vlan != VLAN_VID_MASK) {
  158. rcu_read_lock();
  159. dev = __vlan_find_dev_deep(dev, vlan);
  160. rcu_read_unlock();
  161. } else if (netif_is_bond_slave(dev)) {
  162. while (dev->master)
  163. dev = dev->master;
  164. }
  165. return dev;
  166. }
  167. }
  168. return NULL;
  169. }
  170. static int cxgb_ulp_iscsi_ctl(struct adapter *adapter, unsigned int req,
  171. void *data)
  172. {
  173. int i;
  174. int ret = 0;
  175. unsigned int val = 0;
  176. struct ulp_iscsi_info *uiip = data;
  177. switch (req) {
  178. case ULP_ISCSI_GET_PARAMS:
  179. uiip->pdev = adapter->pdev;
  180. uiip->llimit = t3_read_reg(adapter, A_ULPRX_ISCSI_LLIMIT);
  181. uiip->ulimit = t3_read_reg(adapter, A_ULPRX_ISCSI_ULIMIT);
  182. uiip->tagmask = t3_read_reg(adapter, A_ULPRX_ISCSI_TAGMASK);
  183. val = t3_read_reg(adapter, A_ULPRX_ISCSI_PSZ);
  184. for (i = 0; i < 4; i++, val >>= 8)
  185. uiip->pgsz_factor[i] = val & 0xFF;
  186. val = t3_read_reg(adapter, A_TP_PARA_REG7);
  187. uiip->max_txsz =
  188. uiip->max_rxsz = min((val >> S_PMMAXXFERLEN0)&M_PMMAXXFERLEN0,
  189. (val >> S_PMMAXXFERLEN1)&M_PMMAXXFERLEN1);
  190. /*
  191. * On tx, the iscsi pdu has to be <= tx page size and has to
  192. * fit into the Tx PM FIFO.
  193. */
  194. val = min(adapter->params.tp.tx_pg_size,
  195. t3_read_reg(adapter, A_PM1_TX_CFG) >> 17);
  196. uiip->max_txsz = min(val, uiip->max_txsz);
  197. /* set MaxRxData to 16224 */
  198. val = t3_read_reg(adapter, A_TP_PARA_REG2);
  199. if ((val >> S_MAXRXDATA) != 0x3f60) {
  200. val &= (M_RXCOALESCESIZE << S_RXCOALESCESIZE);
  201. val |= V_MAXRXDATA(0x3f60);
  202. printk(KERN_INFO
  203. "%s, iscsi set MaxRxData to 16224 (0x%x).\n",
  204. adapter->name, val);
  205. t3_write_reg(adapter, A_TP_PARA_REG2, val);
  206. }
  207. /*
  208. * on rx, the iscsi pdu has to be < rx page size and the
  209. * the max rx data length programmed in TP
  210. */
  211. val = min(adapter->params.tp.rx_pg_size,
  212. ((t3_read_reg(adapter, A_TP_PARA_REG2)) >>
  213. S_MAXRXDATA) & M_MAXRXDATA);
  214. uiip->max_rxsz = min(val, uiip->max_rxsz);
  215. break;
  216. case ULP_ISCSI_SET_PARAMS:
  217. t3_write_reg(adapter, A_ULPRX_ISCSI_TAGMASK, uiip->tagmask);
  218. /* program the ddp page sizes */
  219. for (i = 0; i < 4; i++)
  220. val |= (uiip->pgsz_factor[i] & 0xF) << (8 * i);
  221. if (val && (val != t3_read_reg(adapter, A_ULPRX_ISCSI_PSZ))) {
  222. printk(KERN_INFO
  223. "%s, setting iscsi pgsz 0x%x, %u,%u,%u,%u.\n",
  224. adapter->name, val, uiip->pgsz_factor[0],
  225. uiip->pgsz_factor[1], uiip->pgsz_factor[2],
  226. uiip->pgsz_factor[3]);
  227. t3_write_reg(adapter, A_ULPRX_ISCSI_PSZ, val);
  228. }
  229. break;
  230. default:
  231. ret = -EOPNOTSUPP;
  232. }
  233. return ret;
  234. }
  235. /* Response queue used for RDMA events. */
  236. #define ASYNC_NOTIF_RSPQ 0
  237. static int cxgb_rdma_ctl(struct adapter *adapter, unsigned int req, void *data)
  238. {
  239. int ret = 0;
  240. switch (req) {
  241. case RDMA_GET_PARAMS: {
  242. struct rdma_info *rdma = data;
  243. struct pci_dev *pdev = adapter->pdev;
  244. rdma->udbell_physbase = pci_resource_start(pdev, 2);
  245. rdma->udbell_len = pci_resource_len(pdev, 2);
  246. rdma->tpt_base =
  247. t3_read_reg(adapter, A_ULPTX_TPT_LLIMIT);
  248. rdma->tpt_top = t3_read_reg(adapter, A_ULPTX_TPT_ULIMIT);
  249. rdma->pbl_base =
  250. t3_read_reg(adapter, A_ULPTX_PBL_LLIMIT);
  251. rdma->pbl_top = t3_read_reg(adapter, A_ULPTX_PBL_ULIMIT);
  252. rdma->rqt_base = t3_read_reg(adapter, A_ULPRX_RQ_LLIMIT);
  253. rdma->rqt_top = t3_read_reg(adapter, A_ULPRX_RQ_ULIMIT);
  254. rdma->kdb_addr = adapter->regs + A_SG_KDOORBELL;
  255. rdma->pdev = pdev;
  256. break;
  257. }
  258. case RDMA_CQ_OP:{
  259. unsigned long flags;
  260. struct rdma_cq_op *rdma = data;
  261. /* may be called in any context */
  262. spin_lock_irqsave(&adapter->sge.reg_lock, flags);
  263. ret = t3_sge_cqcntxt_op(adapter, rdma->id, rdma->op,
  264. rdma->credits);
  265. spin_unlock_irqrestore(&adapter->sge.reg_lock, flags);
  266. break;
  267. }
  268. case RDMA_GET_MEM:{
  269. struct ch_mem_range *t = data;
  270. struct mc7 *mem;
  271. if ((t->addr & 7) || (t->len & 7))
  272. return -EINVAL;
  273. if (t->mem_id == MEM_CM)
  274. mem = &adapter->cm;
  275. else if (t->mem_id == MEM_PMRX)
  276. mem = &adapter->pmrx;
  277. else if (t->mem_id == MEM_PMTX)
  278. mem = &adapter->pmtx;
  279. else
  280. return -EINVAL;
  281. ret =
  282. t3_mc7_bd_read(mem, t->addr / 8, t->len / 8,
  283. (u64 *) t->buf);
  284. if (ret)
  285. return ret;
  286. break;
  287. }
  288. case RDMA_CQ_SETUP:{
  289. struct rdma_cq_setup *rdma = data;
  290. spin_lock_irq(&adapter->sge.reg_lock);
  291. ret =
  292. t3_sge_init_cqcntxt(adapter, rdma->id,
  293. rdma->base_addr, rdma->size,
  294. ASYNC_NOTIF_RSPQ,
  295. rdma->ovfl_mode, rdma->credits,
  296. rdma->credit_thres);
  297. spin_unlock_irq(&adapter->sge.reg_lock);
  298. break;
  299. }
  300. case RDMA_CQ_DISABLE:
  301. spin_lock_irq(&adapter->sge.reg_lock);
  302. ret = t3_sge_disable_cqcntxt(adapter, *(unsigned int *)data);
  303. spin_unlock_irq(&adapter->sge.reg_lock);
  304. break;
  305. case RDMA_CTRL_QP_SETUP:{
  306. struct rdma_ctrlqp_setup *rdma = data;
  307. spin_lock_irq(&adapter->sge.reg_lock);
  308. ret = t3_sge_init_ecntxt(adapter, FW_RI_SGEEC_START, 0,
  309. SGE_CNTXT_RDMA,
  310. ASYNC_NOTIF_RSPQ,
  311. rdma->base_addr, rdma->size,
  312. FW_RI_TID_START, 1, 0);
  313. spin_unlock_irq(&adapter->sge.reg_lock);
  314. break;
  315. }
  316. case RDMA_GET_MIB: {
  317. spin_lock(&adapter->stats_lock);
  318. t3_tp_get_mib_stats(adapter, (struct tp_mib_stats *)data);
  319. spin_unlock(&adapter->stats_lock);
  320. break;
  321. }
  322. default:
  323. ret = -EOPNOTSUPP;
  324. }
  325. return ret;
  326. }
  327. static int cxgb_offload_ctl(struct t3cdev *tdev, unsigned int req, void *data)
  328. {
  329. struct adapter *adapter = tdev2adap(tdev);
  330. struct tid_range *tid;
  331. struct mtutab *mtup;
  332. struct iff_mac *iffmacp;
  333. struct ddp_params *ddpp;
  334. struct adap_ports *ports;
  335. struct ofld_page_info *rx_page_info;
  336. struct tp_params *tp = &adapter->params.tp;
  337. int i;
  338. switch (req) {
  339. case GET_MAX_OUTSTANDING_WR:
  340. *(unsigned int *)data = FW_WR_NUM;
  341. break;
  342. case GET_WR_LEN:
  343. *(unsigned int *)data = WR_FLITS;
  344. break;
  345. case GET_TX_MAX_CHUNK:
  346. *(unsigned int *)data = 1 << 20; /* 1MB */
  347. break;
  348. case GET_TID_RANGE:
  349. tid = data;
  350. tid->num = t3_mc5_size(&adapter->mc5) -
  351. adapter->params.mc5.nroutes -
  352. adapter->params.mc5.nfilters - adapter->params.mc5.nservers;
  353. tid->base = 0;
  354. break;
  355. case GET_STID_RANGE:
  356. tid = data;
  357. tid->num = adapter->params.mc5.nservers;
  358. tid->base = t3_mc5_size(&adapter->mc5) - tid->num -
  359. adapter->params.mc5.nfilters - adapter->params.mc5.nroutes;
  360. break;
  361. case GET_L2T_CAPACITY:
  362. *(unsigned int *)data = 2048;
  363. break;
  364. case GET_MTUS:
  365. mtup = data;
  366. mtup->size = NMTUS;
  367. mtup->mtus = adapter->params.mtus;
  368. break;
  369. case GET_IFF_FROM_MAC:
  370. iffmacp = data;
  371. iffmacp->dev = get_iff_from_mac(adapter, iffmacp->mac_addr,
  372. iffmacp->vlan_tag &
  373. VLAN_VID_MASK);
  374. break;
  375. case GET_DDP_PARAMS:
  376. ddpp = data;
  377. ddpp->llimit = t3_read_reg(adapter, A_ULPRX_TDDP_LLIMIT);
  378. ddpp->ulimit = t3_read_reg(adapter, A_ULPRX_TDDP_ULIMIT);
  379. ddpp->tag_mask = t3_read_reg(adapter, A_ULPRX_TDDP_TAGMASK);
  380. break;
  381. case GET_PORTS:
  382. ports = data;
  383. ports->nports = adapter->params.nports;
  384. for_each_port(adapter, i)
  385. ports->lldevs[i] = adapter->port[i];
  386. break;
  387. case ULP_ISCSI_GET_PARAMS:
  388. case ULP_ISCSI_SET_PARAMS:
  389. if (!offload_running(adapter))
  390. return -EAGAIN;
  391. return cxgb_ulp_iscsi_ctl(adapter, req, data);
  392. case RDMA_GET_PARAMS:
  393. case RDMA_CQ_OP:
  394. case RDMA_CQ_SETUP:
  395. case RDMA_CQ_DISABLE:
  396. case RDMA_CTRL_QP_SETUP:
  397. case RDMA_GET_MEM:
  398. case RDMA_GET_MIB:
  399. if (!offload_running(adapter))
  400. return -EAGAIN;
  401. return cxgb_rdma_ctl(adapter, req, data);
  402. case GET_RX_PAGE_INFO:
  403. rx_page_info = data;
  404. rx_page_info->page_size = tp->rx_pg_size;
  405. rx_page_info->num = tp->rx_num_pgs;
  406. break;
  407. case GET_ISCSI_IPV4ADDR: {
  408. struct iscsi_ipv4addr *p = data;
  409. struct port_info *pi = netdev_priv(p->dev);
  410. p->ipv4addr = pi->iscsi_ipv4addr;
  411. break;
  412. }
  413. case GET_EMBEDDED_INFO: {
  414. struct ch_embedded_info *e = data;
  415. spin_lock(&adapter->stats_lock);
  416. t3_get_fw_version(adapter, &e->fw_vers);
  417. t3_get_tp_version(adapter, &e->tp_vers);
  418. spin_unlock(&adapter->stats_lock);
  419. break;
  420. }
  421. default:
  422. return -EOPNOTSUPP;
  423. }
  424. return 0;
  425. }
  426. /*
  427. * Dummy handler for Rx offload packets in case we get an offload packet before
  428. * proper processing is setup. This complains and drops the packet as it isn't
  429. * normal to get offload packets at this stage.
  430. */
  431. static int rx_offload_blackhole(struct t3cdev *dev, struct sk_buff **skbs,
  432. int n)
  433. {
  434. while (n--)
  435. dev_kfree_skb_any(skbs[n]);
  436. return 0;
  437. }
  438. static void dummy_neigh_update(struct t3cdev *dev, struct neighbour *neigh)
  439. {
  440. }
  441. void cxgb3_set_dummy_ops(struct t3cdev *dev)
  442. {
  443. dev->recv = rx_offload_blackhole;
  444. dev->neigh_update = dummy_neigh_update;
  445. }
  446. /*
  447. * Free an active-open TID.
  448. */
  449. void *cxgb3_free_atid(struct t3cdev *tdev, int atid)
  450. {
  451. struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
  452. union active_open_entry *p = atid2entry(t, atid);
  453. void *ctx = p->t3c_tid.ctx;
  454. spin_lock_bh(&t->atid_lock);
  455. p->next = t->afree;
  456. t->afree = p;
  457. t->atids_in_use--;
  458. spin_unlock_bh(&t->atid_lock);
  459. return ctx;
  460. }
  461. EXPORT_SYMBOL(cxgb3_free_atid);
  462. /*
  463. * Free a server TID and return it to the free pool.
  464. */
  465. void cxgb3_free_stid(struct t3cdev *tdev, int stid)
  466. {
  467. struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
  468. union listen_entry *p = stid2entry(t, stid);
  469. spin_lock_bh(&t->stid_lock);
  470. p->next = t->sfree;
  471. t->sfree = p;
  472. t->stids_in_use--;
  473. spin_unlock_bh(&t->stid_lock);
  474. }
  475. EXPORT_SYMBOL(cxgb3_free_stid);
  476. void cxgb3_insert_tid(struct t3cdev *tdev, struct cxgb3_client *client,
  477. void *ctx, unsigned int tid)
  478. {
  479. struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
  480. t->tid_tab[tid].client = client;
  481. t->tid_tab[tid].ctx = ctx;
  482. atomic_inc(&t->tids_in_use);
  483. }
  484. EXPORT_SYMBOL(cxgb3_insert_tid);
  485. /*
  486. * Populate a TID_RELEASE WR. The skb must be already propely sized.
  487. */
  488. static inline void mk_tid_release(struct sk_buff *skb, unsigned int tid)
  489. {
  490. struct cpl_tid_release *req;
  491. skb->priority = CPL_PRIORITY_SETUP;
  492. req = (struct cpl_tid_release *)__skb_put(skb, sizeof(*req));
  493. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  494. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
  495. }
  496. static void t3_process_tid_release_list(struct work_struct *work)
  497. {
  498. struct t3c_data *td = container_of(work, struct t3c_data,
  499. tid_release_task);
  500. struct sk_buff *skb;
  501. struct t3cdev *tdev = td->dev;
  502. spin_lock_bh(&td->tid_release_lock);
  503. while (td->tid_release_list) {
  504. struct t3c_tid_entry *p = td->tid_release_list;
  505. td->tid_release_list = p->ctx;
  506. spin_unlock_bh(&td->tid_release_lock);
  507. skb = alloc_skb(sizeof(struct cpl_tid_release),
  508. GFP_KERNEL);
  509. if (!skb)
  510. skb = td->nofail_skb;
  511. if (!skb) {
  512. spin_lock_bh(&td->tid_release_lock);
  513. p->ctx = (void *)td->tid_release_list;
  514. td->tid_release_list = (struct t3c_tid_entry *)p;
  515. break;
  516. }
  517. mk_tid_release(skb, p - td->tid_maps.tid_tab);
  518. cxgb3_ofld_send(tdev, skb);
  519. p->ctx = NULL;
  520. if (skb == td->nofail_skb)
  521. td->nofail_skb =
  522. alloc_skb(sizeof(struct cpl_tid_release),
  523. GFP_KERNEL);
  524. spin_lock_bh(&td->tid_release_lock);
  525. }
  526. td->release_list_incomplete = (td->tid_release_list == NULL) ? 0 : 1;
  527. spin_unlock_bh(&td->tid_release_lock);
  528. if (!td->nofail_skb)
  529. td->nofail_skb =
  530. alloc_skb(sizeof(struct cpl_tid_release),
  531. GFP_KERNEL);
  532. }
  533. /* use ctx as a next pointer in the tid release list */
  534. void cxgb3_queue_tid_release(struct t3cdev *tdev, unsigned int tid)
  535. {
  536. struct t3c_data *td = T3C_DATA(tdev);
  537. struct t3c_tid_entry *p = &td->tid_maps.tid_tab[tid];
  538. spin_lock_bh(&td->tid_release_lock);
  539. p->ctx = (void *)td->tid_release_list;
  540. p->client = NULL;
  541. td->tid_release_list = p;
  542. if (!p->ctx || td->release_list_incomplete)
  543. schedule_work(&td->tid_release_task);
  544. spin_unlock_bh(&td->tid_release_lock);
  545. }
  546. EXPORT_SYMBOL(cxgb3_queue_tid_release);
  547. /*
  548. * Remove a tid from the TID table. A client may defer processing its last
  549. * CPL message if it is locked at the time it arrives, and while the message
  550. * sits in the client's backlog the TID may be reused for another connection.
  551. * To handle this we atomically switch the TID association if it still points
  552. * to the original client context.
  553. */
  554. void cxgb3_remove_tid(struct t3cdev *tdev, void *ctx, unsigned int tid)
  555. {
  556. struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
  557. BUG_ON(tid >= t->ntids);
  558. if (tdev->type == T3A)
  559. (void)cmpxchg(&t->tid_tab[tid].ctx, ctx, NULL);
  560. else {
  561. struct sk_buff *skb;
  562. skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
  563. if (likely(skb)) {
  564. mk_tid_release(skb, tid);
  565. cxgb3_ofld_send(tdev, skb);
  566. t->tid_tab[tid].ctx = NULL;
  567. } else
  568. cxgb3_queue_tid_release(tdev, tid);
  569. }
  570. atomic_dec(&t->tids_in_use);
  571. }
  572. EXPORT_SYMBOL(cxgb3_remove_tid);
  573. int cxgb3_alloc_atid(struct t3cdev *tdev, struct cxgb3_client *client,
  574. void *ctx)
  575. {
  576. int atid = -1;
  577. struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
  578. spin_lock_bh(&t->atid_lock);
  579. if (t->afree &&
  580. t->atids_in_use + atomic_read(&t->tids_in_use) + MC5_MIN_TIDS <=
  581. t->ntids) {
  582. union active_open_entry *p = t->afree;
  583. atid = (p - t->atid_tab) + t->atid_base;
  584. t->afree = p->next;
  585. p->t3c_tid.ctx = ctx;
  586. p->t3c_tid.client = client;
  587. t->atids_in_use++;
  588. }
  589. spin_unlock_bh(&t->atid_lock);
  590. return atid;
  591. }
  592. EXPORT_SYMBOL(cxgb3_alloc_atid);
  593. int cxgb3_alloc_stid(struct t3cdev *tdev, struct cxgb3_client *client,
  594. void *ctx)
  595. {
  596. int stid = -1;
  597. struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
  598. spin_lock_bh(&t->stid_lock);
  599. if (t->sfree) {
  600. union listen_entry *p = t->sfree;
  601. stid = (p - t->stid_tab) + t->stid_base;
  602. t->sfree = p->next;
  603. p->t3c_tid.ctx = ctx;
  604. p->t3c_tid.client = client;
  605. t->stids_in_use++;
  606. }
  607. spin_unlock_bh(&t->stid_lock);
  608. return stid;
  609. }
  610. EXPORT_SYMBOL(cxgb3_alloc_stid);
  611. /* Get the t3cdev associated with a net_device */
  612. struct t3cdev *dev2t3cdev(struct net_device *dev)
  613. {
  614. const struct port_info *pi = netdev_priv(dev);
  615. return (struct t3cdev *)pi->adapter;
  616. }
  617. EXPORT_SYMBOL(dev2t3cdev);
  618. static int do_smt_write_rpl(struct t3cdev *dev, struct sk_buff *skb)
  619. {
  620. struct cpl_smt_write_rpl *rpl = cplhdr(skb);
  621. if (rpl->status != CPL_ERR_NONE)
  622. printk(KERN_ERR
  623. "Unexpected SMT_WRITE_RPL status %u for entry %u\n",
  624. rpl->status, GET_TID(rpl));
  625. return CPL_RET_BUF_DONE;
  626. }
  627. static int do_l2t_write_rpl(struct t3cdev *dev, struct sk_buff *skb)
  628. {
  629. struct cpl_l2t_write_rpl *rpl = cplhdr(skb);
  630. if (rpl->status != CPL_ERR_NONE)
  631. printk(KERN_ERR
  632. "Unexpected L2T_WRITE_RPL status %u for entry %u\n",
  633. rpl->status, GET_TID(rpl));
  634. return CPL_RET_BUF_DONE;
  635. }
  636. static int do_rte_write_rpl(struct t3cdev *dev, struct sk_buff *skb)
  637. {
  638. struct cpl_rte_write_rpl *rpl = cplhdr(skb);
  639. if (rpl->status != CPL_ERR_NONE)
  640. printk(KERN_ERR
  641. "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. printk(KERN_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. printk(KERN_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. printk(KERN_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. printk(KERN_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. printk(KERN_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. printk(KERN_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);
  854. cxgb_neigh_update(dst_get_neighbour(nr->new));
  855. break;
  856. }
  857. default:
  858. break;
  859. }
  860. return 0;
  861. }
  862. static struct notifier_block nb = {
  863. .notifier_call = nb_callback
  864. };
  865. /*
  866. * Process a received packet with an unknown/unexpected CPL opcode.
  867. */
  868. static int do_bad_cpl(struct t3cdev *dev, struct sk_buff *skb)
  869. {
  870. printk(KERN_ERR "%s: received bad CPL command 0x%x\n", dev->name,
  871. *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. printk(KERN_ERR "T3C: handler registration for "
  888. "opcode %x failed\n", 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. printk(KERN_ERR "%s: CPL message (opcode %u) had "
  904. "unknown TID %u\n", dev->name, opcode,
  905. G_TID(ntohl(p->opcode_tid)));
  906. }
  907. #endif
  908. if (ret & CPL_RET_BUF_DONE)
  909. kfree_skb(skb);
  910. }
  911. return 0;
  912. }
  913. /*
  914. * Sends an sk_buff to a T3C driver after dealing with any active network taps.
  915. */
  916. int cxgb3_ofld_send(struct t3cdev *dev, struct sk_buff *skb)
  917. {
  918. int r;
  919. local_bh_disable();
  920. r = dev->send(dev, skb);
  921. local_bh_enable();
  922. return r;
  923. }
  924. EXPORT_SYMBOL(cxgb3_ofld_send);
  925. static int is_offloading(struct net_device *dev)
  926. {
  927. struct adapter *adapter;
  928. int i;
  929. read_lock_bh(&adapter_list_lock);
  930. list_for_each_entry(adapter, &adapter_list, adapter_list) {
  931. for_each_port(adapter, i) {
  932. if (dev == adapter->port[i]) {
  933. read_unlock_bh(&adapter_list_lock);
  934. return 1;
  935. }
  936. }
  937. }
  938. read_unlock_bh(&adapter_list_lock);
  939. return 0;
  940. }
  941. static void cxgb_neigh_update(struct neighbour *neigh)
  942. {
  943. struct net_device *dev = neigh->dev;
  944. if (dev && (is_offloading(dev))) {
  945. struct t3cdev *tdev = dev2t3cdev(dev);
  946. BUG_ON(!tdev);
  947. t3_l2t_update(tdev, neigh);
  948. }
  949. }
  950. static void set_l2t_ix(struct t3cdev *tdev, u32 tid, struct l2t_entry *e)
  951. {
  952. struct sk_buff *skb;
  953. struct cpl_set_tcb_field *req;
  954. skb = alloc_skb(sizeof(*req), GFP_ATOMIC);
  955. if (!skb) {
  956. printk(KERN_ERR "%s: cannot allocate skb!\n", __func__);
  957. return;
  958. }
  959. skb->priority = CPL_PRIORITY_CONTROL;
  960. req = (struct cpl_set_tcb_field *)skb_put(skb, sizeof(*req));
  961. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  962. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, tid));
  963. req->reply = 0;
  964. req->cpu_idx = 0;
  965. req->word = htons(W_TCB_L2T_IX);
  966. req->mask = cpu_to_be64(V_TCB_L2T_IX(M_TCB_L2T_IX));
  967. req->val = cpu_to_be64(V_TCB_L2T_IX(e->idx));
  968. tdev->send(tdev, skb);
  969. }
  970. static void cxgb_redirect(struct dst_entry *old, struct dst_entry *new)
  971. {
  972. struct net_device *olddev, *newdev;
  973. struct tid_info *ti;
  974. struct t3cdev *tdev;
  975. u32 tid;
  976. int update_tcb;
  977. struct l2t_entry *e;
  978. struct t3c_tid_entry *te;
  979. olddev = dst_get_neighbour(old)->dev;
  980. newdev = dst_get_neighbour(new)->dev;
  981. if (!is_offloading(olddev))
  982. return;
  983. if (!is_offloading(newdev)) {
  984. printk(KERN_WARNING "%s: Redirect to non-offload "
  985. "device ignored.\n", __func__);
  986. return;
  987. }
  988. tdev = dev2t3cdev(olddev);
  989. BUG_ON(!tdev);
  990. if (tdev != dev2t3cdev(newdev)) {
  991. printk(KERN_WARNING "%s: Redirect to different "
  992. "offload device ignored.\n", __func__);
  993. return;
  994. }
  995. /* Add new L2T entry */
  996. e = t3_l2t_get(tdev, dst_get_neighbour(new), newdev);
  997. if (!e) {
  998. printk(KERN_ERR "%s: couldn't allocate new l2t entry!\n",
  999. __func__);
  1000. return;
  1001. }
  1002. /* Walk tid table and notify clients of dst change. */
  1003. ti = &(T3C_DATA(tdev))->tid_maps;
  1004. for (tid = 0; tid < ti->ntids; tid++) {
  1005. te = lookup_tid(ti, tid);
  1006. BUG_ON(!te);
  1007. if (te && te->ctx && te->client && te->client->redirect) {
  1008. update_tcb = te->client->redirect(te->ctx, old, new, e);
  1009. if (update_tcb) {
  1010. l2t_hold(L2DATA(tdev), e);
  1011. set_l2t_ix(tdev, tid, e);
  1012. }
  1013. }
  1014. }
  1015. l2t_release(L2DATA(tdev), e);
  1016. }
  1017. /*
  1018. * Allocate a chunk of memory using kmalloc or, if that fails, vmalloc.
  1019. * The allocated memory is cleared.
  1020. */
  1021. void *cxgb_alloc_mem(unsigned long size)
  1022. {
  1023. void *p = kzalloc(size, GFP_KERNEL);
  1024. if (!p)
  1025. p = vzalloc(size);
  1026. return p;
  1027. }
  1028. /*
  1029. * Free memory allocated through t3_alloc_mem().
  1030. */
  1031. void cxgb_free_mem(void *addr)
  1032. {
  1033. if (is_vmalloc_addr(addr))
  1034. vfree(addr);
  1035. else
  1036. kfree(addr);
  1037. }
  1038. /*
  1039. * Allocate and initialize the TID tables. Returns 0 on success.
  1040. */
  1041. static int init_tid_tabs(struct tid_info *t, unsigned int ntids,
  1042. unsigned int natids, unsigned int nstids,
  1043. unsigned int atid_base, unsigned int stid_base)
  1044. {
  1045. unsigned long size = ntids * sizeof(*t->tid_tab) +
  1046. natids * sizeof(*t->atid_tab) + nstids * sizeof(*t->stid_tab);
  1047. t->tid_tab = cxgb_alloc_mem(size);
  1048. if (!t->tid_tab)
  1049. return -ENOMEM;
  1050. t->stid_tab = (union listen_entry *)&t->tid_tab[ntids];
  1051. t->atid_tab = (union active_open_entry *)&t->stid_tab[nstids];
  1052. t->ntids = ntids;
  1053. t->nstids = nstids;
  1054. t->stid_base = stid_base;
  1055. t->sfree = NULL;
  1056. t->natids = natids;
  1057. t->atid_base = atid_base;
  1058. t->afree = NULL;
  1059. t->stids_in_use = t->atids_in_use = 0;
  1060. atomic_set(&t->tids_in_use, 0);
  1061. spin_lock_init(&t->stid_lock);
  1062. spin_lock_init(&t->atid_lock);
  1063. /*
  1064. * Setup the free lists for stid_tab and atid_tab.
  1065. */
  1066. if (nstids) {
  1067. while (--nstids)
  1068. t->stid_tab[nstids - 1].next = &t->stid_tab[nstids];
  1069. t->sfree = t->stid_tab;
  1070. }
  1071. if (natids) {
  1072. while (--natids)
  1073. t->atid_tab[natids - 1].next = &t->atid_tab[natids];
  1074. t->afree = t->atid_tab;
  1075. }
  1076. return 0;
  1077. }
  1078. static void free_tid_maps(struct tid_info *t)
  1079. {
  1080. cxgb_free_mem(t->tid_tab);
  1081. }
  1082. static inline void add_adapter(struct adapter *adap)
  1083. {
  1084. write_lock_bh(&adapter_list_lock);
  1085. list_add_tail(&adap->adapter_list, &adapter_list);
  1086. write_unlock_bh(&adapter_list_lock);
  1087. }
  1088. static inline void remove_adapter(struct adapter *adap)
  1089. {
  1090. write_lock_bh(&adapter_list_lock);
  1091. list_del(&adap->adapter_list);
  1092. write_unlock_bh(&adapter_list_lock);
  1093. }
  1094. int cxgb3_offload_activate(struct adapter *adapter)
  1095. {
  1096. struct t3cdev *dev = &adapter->tdev;
  1097. int natids, err;
  1098. struct t3c_data *t;
  1099. struct tid_range stid_range, tid_range;
  1100. struct mtutab mtutab;
  1101. unsigned int l2t_capacity;
  1102. t = kzalloc(sizeof(*t), GFP_KERNEL);
  1103. if (!t)
  1104. return -ENOMEM;
  1105. err = -EOPNOTSUPP;
  1106. if (dev->ctl(dev, GET_TX_MAX_CHUNK, &t->tx_max_chunk) < 0 ||
  1107. dev->ctl(dev, GET_MAX_OUTSTANDING_WR, &t->max_wrs) < 0 ||
  1108. dev->ctl(dev, GET_L2T_CAPACITY, &l2t_capacity) < 0 ||
  1109. dev->ctl(dev, GET_MTUS, &mtutab) < 0 ||
  1110. dev->ctl(dev, GET_TID_RANGE, &tid_range) < 0 ||
  1111. dev->ctl(dev, GET_STID_RANGE, &stid_range) < 0)
  1112. goto out_free;
  1113. err = -ENOMEM;
  1114. L2DATA(dev) = t3_init_l2t(l2t_capacity);
  1115. if (!L2DATA(dev))
  1116. goto out_free;
  1117. natids = min(tid_range.num / 2, MAX_ATIDS);
  1118. err = init_tid_tabs(&t->tid_maps, tid_range.num, natids,
  1119. stid_range.num, ATID_BASE, stid_range.base);
  1120. if (err)
  1121. goto out_free_l2t;
  1122. t->mtus = mtutab.mtus;
  1123. t->nmtus = mtutab.size;
  1124. INIT_WORK(&t->tid_release_task, t3_process_tid_release_list);
  1125. spin_lock_init(&t->tid_release_lock);
  1126. INIT_LIST_HEAD(&t->list_node);
  1127. t->dev = dev;
  1128. T3C_DATA(dev) = t;
  1129. dev->recv = process_rx;
  1130. dev->neigh_update = t3_l2t_update;
  1131. /* Register netevent handler once */
  1132. if (list_empty(&adapter_list))
  1133. register_netevent_notifier(&nb);
  1134. t->nofail_skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_KERNEL);
  1135. t->release_list_incomplete = 0;
  1136. add_adapter(adapter);
  1137. return 0;
  1138. out_free_l2t:
  1139. t3_free_l2t(L2DATA(dev));
  1140. L2DATA(dev) = NULL;
  1141. out_free:
  1142. kfree(t);
  1143. return err;
  1144. }
  1145. void cxgb3_offload_deactivate(struct adapter *adapter)
  1146. {
  1147. struct t3cdev *tdev = &adapter->tdev;
  1148. struct t3c_data *t = T3C_DATA(tdev);
  1149. remove_adapter(adapter);
  1150. if (list_empty(&adapter_list))
  1151. unregister_netevent_notifier(&nb);
  1152. free_tid_maps(&t->tid_maps);
  1153. T3C_DATA(tdev) = NULL;
  1154. t3_free_l2t(L2DATA(tdev));
  1155. L2DATA(tdev) = NULL;
  1156. if (t->nofail_skb)
  1157. kfree_skb(t->nofail_skb);
  1158. kfree(t);
  1159. }
  1160. static inline void register_tdev(struct t3cdev *tdev)
  1161. {
  1162. static int unit;
  1163. mutex_lock(&cxgb3_db_lock);
  1164. snprintf(tdev->name, sizeof(tdev->name), "ofld_dev%d", unit++);
  1165. list_add_tail(&tdev->ofld_dev_list, &ofld_dev_list);
  1166. mutex_unlock(&cxgb3_db_lock);
  1167. }
  1168. static inline void unregister_tdev(struct t3cdev *tdev)
  1169. {
  1170. mutex_lock(&cxgb3_db_lock);
  1171. list_del(&tdev->ofld_dev_list);
  1172. mutex_unlock(&cxgb3_db_lock);
  1173. }
  1174. static inline int adap2type(struct adapter *adapter)
  1175. {
  1176. int type = 0;
  1177. switch (adapter->params.rev) {
  1178. case T3_REV_A:
  1179. type = T3A;
  1180. break;
  1181. case T3_REV_B:
  1182. case T3_REV_B2:
  1183. type = T3B;
  1184. break;
  1185. case T3_REV_C:
  1186. type = T3C;
  1187. break;
  1188. }
  1189. return type;
  1190. }
  1191. void __devinit cxgb3_adapter_ofld(struct adapter *adapter)
  1192. {
  1193. struct t3cdev *tdev = &adapter->tdev;
  1194. INIT_LIST_HEAD(&tdev->ofld_dev_list);
  1195. cxgb3_set_dummy_ops(tdev);
  1196. tdev->send = t3_offload_tx;
  1197. tdev->ctl = cxgb_offload_ctl;
  1198. tdev->type = adap2type(adapter);
  1199. register_tdev(tdev);
  1200. }
  1201. void __devexit cxgb3_adapter_unofld(struct adapter *adapter)
  1202. {
  1203. struct t3cdev *tdev = &adapter->tdev;
  1204. tdev->recv = NULL;
  1205. tdev->neigh_update = NULL;
  1206. unregister_tdev(tdev);
  1207. }
  1208. void __init cxgb3_offload_init(void)
  1209. {
  1210. int i;
  1211. for (i = 0; i < NUM_CPL_CMDS; ++i)
  1212. cpl_handlers[i] = do_bad_cpl;
  1213. t3_register_cpl_handler(CPL_SMT_WRITE_RPL, do_smt_write_rpl);
  1214. t3_register_cpl_handler(CPL_L2T_WRITE_RPL, do_l2t_write_rpl);
  1215. t3_register_cpl_handler(CPL_RTE_WRITE_RPL, do_rte_write_rpl);
  1216. t3_register_cpl_handler(CPL_PASS_OPEN_RPL, do_stid_rpl);
  1217. t3_register_cpl_handler(CPL_CLOSE_LISTSRV_RPL, do_stid_rpl);
  1218. t3_register_cpl_handler(CPL_PASS_ACCEPT_REQ, do_cr);
  1219. t3_register_cpl_handler(CPL_PASS_ESTABLISH, do_hwtid_rpl);
  1220. t3_register_cpl_handler(CPL_ABORT_RPL_RSS, do_hwtid_rpl);
  1221. t3_register_cpl_handler(CPL_ABORT_RPL, do_hwtid_rpl);
  1222. t3_register_cpl_handler(CPL_RX_URG_NOTIFY, do_hwtid_rpl);
  1223. t3_register_cpl_handler(CPL_RX_DATA, do_hwtid_rpl);
  1224. t3_register_cpl_handler(CPL_TX_DATA_ACK, do_hwtid_rpl);
  1225. t3_register_cpl_handler(CPL_TX_DMA_ACK, do_hwtid_rpl);
  1226. t3_register_cpl_handler(CPL_ACT_OPEN_RPL, do_act_open_rpl);
  1227. t3_register_cpl_handler(CPL_PEER_CLOSE, do_hwtid_rpl);
  1228. t3_register_cpl_handler(CPL_CLOSE_CON_RPL, do_hwtid_rpl);
  1229. t3_register_cpl_handler(CPL_ABORT_REQ_RSS, do_abort_req_rss);
  1230. t3_register_cpl_handler(CPL_ACT_ESTABLISH, do_act_establish);
  1231. t3_register_cpl_handler(CPL_SET_TCB_RPL, do_hwtid_rpl);
  1232. t3_register_cpl_handler(CPL_GET_TCB_RPL, do_hwtid_rpl);
  1233. t3_register_cpl_handler(CPL_RDMA_TERMINATE, do_term);
  1234. t3_register_cpl_handler(CPL_RDMA_EC_STATUS, do_hwtid_rpl);
  1235. t3_register_cpl_handler(CPL_TRACE_PKT, do_trace);
  1236. t3_register_cpl_handler(CPL_RX_DATA_DDP, do_hwtid_rpl);
  1237. t3_register_cpl_handler(CPL_RX_DDP_COMPLETE, do_hwtid_rpl);
  1238. t3_register_cpl_handler(CPL_ISCSI_HDR, do_hwtid_rpl);
  1239. }