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