cxgb3_offload.c 36 KB

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