cxgb3_offload.c 37 KB

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