cnic.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858
  1. /* cnic.c: Broadcom CNIC core network driver.
  2. *
  3. * Copyright (c) 2006-2009 Broadcom Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation.
  8. *
  9. * Original skeleton written by: John(Zongxi) Chen (zongxi@broadcom.com)
  10. * Modified and maintained by: Michael Chan <mchan@broadcom.com>
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/list.h>
  16. #include <linux/slab.h>
  17. #include <linux/pci.h>
  18. #include <linux/init.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/uio_driver.h>
  21. #include <linux/in.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/delay.h>
  24. #include <linux/ethtool.h>
  25. #include <linux/if_vlan.h>
  26. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  27. #define BCM_VLAN 1
  28. #endif
  29. #include <net/ip.h>
  30. #include <net/tcp.h>
  31. #include <net/route.h>
  32. #include <net/ipv6.h>
  33. #include <net/ip6_route.h>
  34. #include <scsi/iscsi_if.h>
  35. #include "cnic_if.h"
  36. #include "bnx2.h"
  37. #include "bnx2x_reg.h"
  38. #include "bnx2x_fw_defs.h"
  39. #include "bnx2x_hsi.h"
  40. #include "../scsi/bnx2i/57xx_iscsi_constants.h"
  41. #include "../scsi/bnx2i/57xx_iscsi_hsi.h"
  42. #include "cnic.h"
  43. #include "cnic_defs.h"
  44. #define DRV_MODULE_NAME "cnic"
  45. #define PFX DRV_MODULE_NAME ": "
  46. static char version[] __devinitdata =
  47. "Broadcom NetXtreme II CNIC Driver " DRV_MODULE_NAME " v" CNIC_MODULE_VERSION " (" CNIC_MODULE_RELDATE ")\n";
  48. MODULE_AUTHOR("Michael Chan <mchan@broadcom.com> and John(Zongxi) "
  49. "Chen (zongxi@broadcom.com");
  50. MODULE_DESCRIPTION("Broadcom NetXtreme II CNIC Driver");
  51. MODULE_LICENSE("GPL");
  52. MODULE_VERSION(CNIC_MODULE_VERSION);
  53. static LIST_HEAD(cnic_dev_list);
  54. static DEFINE_RWLOCK(cnic_dev_lock);
  55. static DEFINE_MUTEX(cnic_lock);
  56. static struct cnic_ulp_ops *cnic_ulp_tbl[MAX_CNIC_ULP_TYPE];
  57. static int cnic_service_bnx2(void *, void *);
  58. static int cnic_ctl(void *, struct cnic_ctl_info *);
  59. static struct cnic_ops cnic_bnx2_ops = {
  60. .cnic_owner = THIS_MODULE,
  61. .cnic_handler = cnic_service_bnx2,
  62. .cnic_ctl = cnic_ctl,
  63. };
  64. static void cnic_shutdown_rings(struct cnic_dev *);
  65. static void cnic_init_rings(struct cnic_dev *);
  66. static int cnic_cm_set_pg(struct cnic_sock *);
  67. static int cnic_uio_open(struct uio_info *uinfo, struct inode *inode)
  68. {
  69. struct cnic_dev *dev = uinfo->priv;
  70. struct cnic_local *cp = dev->cnic_priv;
  71. if (!capable(CAP_NET_ADMIN))
  72. return -EPERM;
  73. if (cp->uio_dev != -1)
  74. return -EBUSY;
  75. rtnl_lock();
  76. if (!test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
  77. rtnl_unlock();
  78. return -ENODEV;
  79. }
  80. cp->uio_dev = iminor(inode);
  81. cnic_init_rings(dev);
  82. rtnl_unlock();
  83. return 0;
  84. }
  85. static int cnic_uio_close(struct uio_info *uinfo, struct inode *inode)
  86. {
  87. struct cnic_dev *dev = uinfo->priv;
  88. struct cnic_local *cp = dev->cnic_priv;
  89. cnic_shutdown_rings(dev);
  90. cp->uio_dev = -1;
  91. return 0;
  92. }
  93. static inline void cnic_hold(struct cnic_dev *dev)
  94. {
  95. atomic_inc(&dev->ref_count);
  96. }
  97. static inline void cnic_put(struct cnic_dev *dev)
  98. {
  99. atomic_dec(&dev->ref_count);
  100. }
  101. static inline void csk_hold(struct cnic_sock *csk)
  102. {
  103. atomic_inc(&csk->ref_count);
  104. }
  105. static inline void csk_put(struct cnic_sock *csk)
  106. {
  107. atomic_dec(&csk->ref_count);
  108. }
  109. static struct cnic_dev *cnic_from_netdev(struct net_device *netdev)
  110. {
  111. struct cnic_dev *cdev;
  112. read_lock(&cnic_dev_lock);
  113. list_for_each_entry(cdev, &cnic_dev_list, list) {
  114. if (netdev == cdev->netdev) {
  115. cnic_hold(cdev);
  116. read_unlock(&cnic_dev_lock);
  117. return cdev;
  118. }
  119. }
  120. read_unlock(&cnic_dev_lock);
  121. return NULL;
  122. }
  123. static inline void ulp_get(struct cnic_ulp_ops *ulp_ops)
  124. {
  125. atomic_inc(&ulp_ops->ref_count);
  126. }
  127. static inline void ulp_put(struct cnic_ulp_ops *ulp_ops)
  128. {
  129. atomic_dec(&ulp_ops->ref_count);
  130. }
  131. static void cnic_ctx_wr(struct cnic_dev *dev, u32 cid_addr, u32 off, u32 val)
  132. {
  133. struct cnic_local *cp = dev->cnic_priv;
  134. struct cnic_eth_dev *ethdev = cp->ethdev;
  135. struct drv_ctl_info info;
  136. struct drv_ctl_io *io = &info.data.io;
  137. info.cmd = DRV_CTL_CTX_WR_CMD;
  138. io->cid_addr = cid_addr;
  139. io->offset = off;
  140. io->data = val;
  141. ethdev->drv_ctl(dev->netdev, &info);
  142. }
  143. static void cnic_reg_wr_ind(struct cnic_dev *dev, u32 off, u32 val)
  144. {
  145. struct cnic_local *cp = dev->cnic_priv;
  146. struct cnic_eth_dev *ethdev = cp->ethdev;
  147. struct drv_ctl_info info;
  148. struct drv_ctl_io *io = &info.data.io;
  149. info.cmd = DRV_CTL_IO_WR_CMD;
  150. io->offset = off;
  151. io->data = val;
  152. ethdev->drv_ctl(dev->netdev, &info);
  153. }
  154. static u32 cnic_reg_rd_ind(struct cnic_dev *dev, u32 off)
  155. {
  156. struct cnic_local *cp = dev->cnic_priv;
  157. struct cnic_eth_dev *ethdev = cp->ethdev;
  158. struct drv_ctl_info info;
  159. struct drv_ctl_io *io = &info.data.io;
  160. info.cmd = DRV_CTL_IO_RD_CMD;
  161. io->offset = off;
  162. ethdev->drv_ctl(dev->netdev, &info);
  163. return io->data;
  164. }
  165. static int cnic_in_use(struct cnic_sock *csk)
  166. {
  167. return test_bit(SK_F_INUSE, &csk->flags);
  168. }
  169. static void cnic_kwq_completion(struct cnic_dev *dev, u32 count)
  170. {
  171. struct cnic_local *cp = dev->cnic_priv;
  172. struct cnic_eth_dev *ethdev = cp->ethdev;
  173. struct drv_ctl_info info;
  174. info.cmd = DRV_CTL_COMPLETION_CMD;
  175. info.data.comp.comp_count = count;
  176. ethdev->drv_ctl(dev->netdev, &info);
  177. }
  178. static int cnic_send_nlmsg(struct cnic_local *cp, u32 type,
  179. struct cnic_sock *csk)
  180. {
  181. struct iscsi_path path_req;
  182. char *buf = NULL;
  183. u16 len = 0;
  184. u32 msg_type = ISCSI_KEVENT_IF_DOWN;
  185. struct cnic_ulp_ops *ulp_ops;
  186. if (cp->uio_dev == -1)
  187. return -ENODEV;
  188. if (csk) {
  189. len = sizeof(path_req);
  190. buf = (char *) &path_req;
  191. memset(&path_req, 0, len);
  192. msg_type = ISCSI_KEVENT_PATH_REQ;
  193. path_req.handle = (u64) csk->l5_cid;
  194. if (test_bit(SK_F_IPV6, &csk->flags)) {
  195. memcpy(&path_req.dst.v6_addr, &csk->dst_ip[0],
  196. sizeof(struct in6_addr));
  197. path_req.ip_addr_len = 16;
  198. } else {
  199. memcpy(&path_req.dst.v4_addr, &csk->dst_ip[0],
  200. sizeof(struct in_addr));
  201. path_req.ip_addr_len = 4;
  202. }
  203. path_req.vlan_id = csk->vlan_id;
  204. path_req.pmtu = csk->mtu;
  205. }
  206. rcu_read_lock();
  207. ulp_ops = rcu_dereference(cnic_ulp_tbl[CNIC_ULP_ISCSI]);
  208. if (ulp_ops)
  209. ulp_ops->iscsi_nl_send_msg(cp->dev, msg_type, buf, len);
  210. rcu_read_unlock();
  211. return 0;
  212. }
  213. static int cnic_iscsi_nl_msg_recv(struct cnic_dev *dev, u32 msg_type,
  214. char *buf, u16 len)
  215. {
  216. int rc = -EINVAL;
  217. switch (msg_type) {
  218. case ISCSI_UEVENT_PATH_UPDATE: {
  219. struct cnic_local *cp;
  220. u32 l5_cid;
  221. struct cnic_sock *csk;
  222. struct iscsi_path *path_resp;
  223. if (len < sizeof(*path_resp))
  224. break;
  225. path_resp = (struct iscsi_path *) buf;
  226. cp = dev->cnic_priv;
  227. l5_cid = (u32) path_resp->handle;
  228. if (l5_cid >= MAX_CM_SK_TBL_SZ)
  229. break;
  230. csk = &cp->csk_tbl[l5_cid];
  231. csk_hold(csk);
  232. if (cnic_in_use(csk)) {
  233. memcpy(csk->ha, path_resp->mac_addr, 6);
  234. if (test_bit(SK_F_IPV6, &csk->flags))
  235. memcpy(&csk->src_ip[0], &path_resp->src.v6_addr,
  236. sizeof(struct in6_addr));
  237. else
  238. memcpy(&csk->src_ip[0], &path_resp->src.v4_addr,
  239. sizeof(struct in_addr));
  240. if (is_valid_ether_addr(csk->ha))
  241. cnic_cm_set_pg(csk);
  242. }
  243. csk_put(csk);
  244. rc = 0;
  245. }
  246. }
  247. return rc;
  248. }
  249. static int cnic_offld_prep(struct cnic_sock *csk)
  250. {
  251. if (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
  252. return 0;
  253. if (!test_bit(SK_F_CONNECT_START, &csk->flags)) {
  254. clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
  255. return 0;
  256. }
  257. return 1;
  258. }
  259. static int cnic_close_prep(struct cnic_sock *csk)
  260. {
  261. clear_bit(SK_F_CONNECT_START, &csk->flags);
  262. smp_mb__after_clear_bit();
  263. if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
  264. while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
  265. msleep(1);
  266. return 1;
  267. }
  268. return 0;
  269. }
  270. static int cnic_abort_prep(struct cnic_sock *csk)
  271. {
  272. clear_bit(SK_F_CONNECT_START, &csk->flags);
  273. smp_mb__after_clear_bit();
  274. while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
  275. msleep(1);
  276. if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
  277. csk->state = L4_KCQE_OPCODE_VALUE_RESET_COMP;
  278. return 1;
  279. }
  280. return 0;
  281. }
  282. static void cnic_uio_stop(void)
  283. {
  284. struct cnic_dev *dev;
  285. read_lock(&cnic_dev_lock);
  286. list_for_each_entry(dev, &cnic_dev_list, list) {
  287. struct cnic_local *cp = dev->cnic_priv;
  288. if (cp->cnic_uinfo)
  289. cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
  290. }
  291. read_unlock(&cnic_dev_lock);
  292. }
  293. int cnic_register_driver(int ulp_type, struct cnic_ulp_ops *ulp_ops)
  294. {
  295. struct cnic_dev *dev;
  296. if (ulp_type >= MAX_CNIC_ULP_TYPE) {
  297. printk(KERN_ERR PFX "cnic_register_driver: Bad type %d\n",
  298. ulp_type);
  299. return -EINVAL;
  300. }
  301. mutex_lock(&cnic_lock);
  302. if (cnic_ulp_tbl[ulp_type]) {
  303. printk(KERN_ERR PFX "cnic_register_driver: Type %d has already "
  304. "been registered\n", ulp_type);
  305. mutex_unlock(&cnic_lock);
  306. return -EBUSY;
  307. }
  308. read_lock(&cnic_dev_lock);
  309. list_for_each_entry(dev, &cnic_dev_list, list) {
  310. struct cnic_local *cp = dev->cnic_priv;
  311. clear_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]);
  312. }
  313. read_unlock(&cnic_dev_lock);
  314. atomic_set(&ulp_ops->ref_count, 0);
  315. rcu_assign_pointer(cnic_ulp_tbl[ulp_type], ulp_ops);
  316. mutex_unlock(&cnic_lock);
  317. /* Prevent race conditions with netdev_event */
  318. rtnl_lock();
  319. read_lock(&cnic_dev_lock);
  320. list_for_each_entry(dev, &cnic_dev_list, list) {
  321. struct cnic_local *cp = dev->cnic_priv;
  322. if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]))
  323. ulp_ops->cnic_init(dev);
  324. }
  325. read_unlock(&cnic_dev_lock);
  326. rtnl_unlock();
  327. return 0;
  328. }
  329. int cnic_unregister_driver(int ulp_type)
  330. {
  331. struct cnic_dev *dev;
  332. struct cnic_ulp_ops *ulp_ops;
  333. int i = 0;
  334. if (ulp_type >= MAX_CNIC_ULP_TYPE) {
  335. printk(KERN_ERR PFX "cnic_unregister_driver: Bad type %d\n",
  336. ulp_type);
  337. return -EINVAL;
  338. }
  339. mutex_lock(&cnic_lock);
  340. ulp_ops = cnic_ulp_tbl[ulp_type];
  341. if (!ulp_ops) {
  342. printk(KERN_ERR PFX "cnic_unregister_driver: Type %d has not "
  343. "been registered\n", ulp_type);
  344. goto out_unlock;
  345. }
  346. read_lock(&cnic_dev_lock);
  347. list_for_each_entry(dev, &cnic_dev_list, list) {
  348. struct cnic_local *cp = dev->cnic_priv;
  349. if (rcu_dereference(cp->ulp_ops[ulp_type])) {
  350. printk(KERN_ERR PFX "cnic_unregister_driver: Type %d "
  351. "still has devices registered\n", ulp_type);
  352. read_unlock(&cnic_dev_lock);
  353. goto out_unlock;
  354. }
  355. }
  356. read_unlock(&cnic_dev_lock);
  357. if (ulp_type == CNIC_ULP_ISCSI)
  358. cnic_uio_stop();
  359. rcu_assign_pointer(cnic_ulp_tbl[ulp_type], NULL);
  360. mutex_unlock(&cnic_lock);
  361. synchronize_rcu();
  362. while ((atomic_read(&ulp_ops->ref_count) != 0) && (i < 20)) {
  363. msleep(100);
  364. i++;
  365. }
  366. if (atomic_read(&ulp_ops->ref_count) != 0)
  367. printk(KERN_WARNING PFX "%s: Failed waiting for ref count to go"
  368. " to zero.\n", dev->netdev->name);
  369. return 0;
  370. out_unlock:
  371. mutex_unlock(&cnic_lock);
  372. return -EINVAL;
  373. }
  374. static int cnic_start_hw(struct cnic_dev *);
  375. static void cnic_stop_hw(struct cnic_dev *);
  376. static int cnic_register_device(struct cnic_dev *dev, int ulp_type,
  377. void *ulp_ctx)
  378. {
  379. struct cnic_local *cp = dev->cnic_priv;
  380. struct cnic_ulp_ops *ulp_ops;
  381. if (ulp_type >= MAX_CNIC_ULP_TYPE) {
  382. printk(KERN_ERR PFX "cnic_register_device: Bad type %d\n",
  383. ulp_type);
  384. return -EINVAL;
  385. }
  386. mutex_lock(&cnic_lock);
  387. if (cnic_ulp_tbl[ulp_type] == NULL) {
  388. printk(KERN_ERR PFX "cnic_register_device: Driver with type %d "
  389. "has not been registered\n", ulp_type);
  390. mutex_unlock(&cnic_lock);
  391. return -EAGAIN;
  392. }
  393. if (rcu_dereference(cp->ulp_ops[ulp_type])) {
  394. printk(KERN_ERR PFX "cnic_register_device: Type %d has already "
  395. "been registered to this device\n", ulp_type);
  396. mutex_unlock(&cnic_lock);
  397. return -EBUSY;
  398. }
  399. clear_bit(ULP_F_START, &cp->ulp_flags[ulp_type]);
  400. cp->ulp_handle[ulp_type] = ulp_ctx;
  401. ulp_ops = cnic_ulp_tbl[ulp_type];
  402. rcu_assign_pointer(cp->ulp_ops[ulp_type], ulp_ops);
  403. cnic_hold(dev);
  404. if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
  405. if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[ulp_type]))
  406. ulp_ops->cnic_start(cp->ulp_handle[ulp_type]);
  407. mutex_unlock(&cnic_lock);
  408. return 0;
  409. }
  410. EXPORT_SYMBOL(cnic_register_driver);
  411. static int cnic_unregister_device(struct cnic_dev *dev, int ulp_type)
  412. {
  413. struct cnic_local *cp = dev->cnic_priv;
  414. int i = 0;
  415. if (ulp_type >= MAX_CNIC_ULP_TYPE) {
  416. printk(KERN_ERR PFX "cnic_unregister_device: Bad type %d\n",
  417. ulp_type);
  418. return -EINVAL;
  419. }
  420. mutex_lock(&cnic_lock);
  421. if (rcu_dereference(cp->ulp_ops[ulp_type])) {
  422. rcu_assign_pointer(cp->ulp_ops[ulp_type], NULL);
  423. cnic_put(dev);
  424. } else {
  425. printk(KERN_ERR PFX "cnic_unregister_device: device not "
  426. "registered to this ulp type %d\n", ulp_type);
  427. mutex_unlock(&cnic_lock);
  428. return -EINVAL;
  429. }
  430. mutex_unlock(&cnic_lock);
  431. synchronize_rcu();
  432. while (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]) &&
  433. i < 20) {
  434. msleep(100);
  435. i++;
  436. }
  437. if (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]))
  438. printk(KERN_WARNING PFX "%s: Failed waiting for ULP up call"
  439. " to complete.\n", dev->netdev->name);
  440. return 0;
  441. }
  442. EXPORT_SYMBOL(cnic_unregister_driver);
  443. static int cnic_init_id_tbl(struct cnic_id_tbl *id_tbl, u32 size, u32 start_id)
  444. {
  445. id_tbl->start = start_id;
  446. id_tbl->max = size;
  447. id_tbl->next = 0;
  448. spin_lock_init(&id_tbl->lock);
  449. id_tbl->table = kzalloc(DIV_ROUND_UP(size, 32) * 4, GFP_KERNEL);
  450. if (!id_tbl->table)
  451. return -ENOMEM;
  452. return 0;
  453. }
  454. static void cnic_free_id_tbl(struct cnic_id_tbl *id_tbl)
  455. {
  456. kfree(id_tbl->table);
  457. id_tbl->table = NULL;
  458. }
  459. static int cnic_alloc_id(struct cnic_id_tbl *id_tbl, u32 id)
  460. {
  461. int ret = -1;
  462. id -= id_tbl->start;
  463. if (id >= id_tbl->max)
  464. return ret;
  465. spin_lock(&id_tbl->lock);
  466. if (!test_bit(id, id_tbl->table)) {
  467. set_bit(id, id_tbl->table);
  468. ret = 0;
  469. }
  470. spin_unlock(&id_tbl->lock);
  471. return ret;
  472. }
  473. /* Returns -1 if not successful */
  474. static u32 cnic_alloc_new_id(struct cnic_id_tbl *id_tbl)
  475. {
  476. u32 id;
  477. spin_lock(&id_tbl->lock);
  478. id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
  479. if (id >= id_tbl->max) {
  480. id = -1;
  481. if (id_tbl->next != 0) {
  482. id = find_first_zero_bit(id_tbl->table, id_tbl->next);
  483. if (id >= id_tbl->next)
  484. id = -1;
  485. }
  486. }
  487. if (id < id_tbl->max) {
  488. set_bit(id, id_tbl->table);
  489. id_tbl->next = (id + 1) & (id_tbl->max - 1);
  490. id += id_tbl->start;
  491. }
  492. spin_unlock(&id_tbl->lock);
  493. return id;
  494. }
  495. static void cnic_free_id(struct cnic_id_tbl *id_tbl, u32 id)
  496. {
  497. if (id == -1)
  498. return;
  499. id -= id_tbl->start;
  500. if (id >= id_tbl->max)
  501. return;
  502. clear_bit(id, id_tbl->table);
  503. }
  504. static void cnic_free_dma(struct cnic_dev *dev, struct cnic_dma *dma)
  505. {
  506. int i;
  507. if (!dma->pg_arr)
  508. return;
  509. for (i = 0; i < dma->num_pages; i++) {
  510. if (dma->pg_arr[i]) {
  511. pci_free_consistent(dev->pcidev, BCM_PAGE_SIZE,
  512. dma->pg_arr[i], dma->pg_map_arr[i]);
  513. dma->pg_arr[i] = NULL;
  514. }
  515. }
  516. if (dma->pgtbl) {
  517. pci_free_consistent(dev->pcidev, dma->pgtbl_size,
  518. dma->pgtbl, dma->pgtbl_map);
  519. dma->pgtbl = NULL;
  520. }
  521. kfree(dma->pg_arr);
  522. dma->pg_arr = NULL;
  523. dma->num_pages = 0;
  524. }
  525. static void cnic_setup_page_tbl(struct cnic_dev *dev, struct cnic_dma *dma)
  526. {
  527. int i;
  528. u32 *page_table = dma->pgtbl;
  529. for (i = 0; i < dma->num_pages; i++) {
  530. /* Each entry needs to be in big endian format. */
  531. *page_table = (u32) ((u64) dma->pg_map_arr[i] >> 32);
  532. page_table++;
  533. *page_table = (u32) dma->pg_map_arr[i];
  534. page_table++;
  535. }
  536. }
  537. static int cnic_alloc_dma(struct cnic_dev *dev, struct cnic_dma *dma,
  538. int pages, int use_pg_tbl)
  539. {
  540. int i, size;
  541. struct cnic_local *cp = dev->cnic_priv;
  542. size = pages * (sizeof(void *) + sizeof(dma_addr_t));
  543. dma->pg_arr = kzalloc(size, GFP_ATOMIC);
  544. if (dma->pg_arr == NULL)
  545. return -ENOMEM;
  546. dma->pg_map_arr = (dma_addr_t *) (dma->pg_arr + pages);
  547. dma->num_pages = pages;
  548. for (i = 0; i < pages; i++) {
  549. dma->pg_arr[i] = pci_alloc_consistent(dev->pcidev,
  550. BCM_PAGE_SIZE,
  551. &dma->pg_map_arr[i]);
  552. if (dma->pg_arr[i] == NULL)
  553. goto error;
  554. }
  555. if (!use_pg_tbl)
  556. return 0;
  557. dma->pgtbl_size = ((pages * 8) + BCM_PAGE_SIZE - 1) &
  558. ~(BCM_PAGE_SIZE - 1);
  559. dma->pgtbl = pci_alloc_consistent(dev->pcidev, dma->pgtbl_size,
  560. &dma->pgtbl_map);
  561. if (dma->pgtbl == NULL)
  562. goto error;
  563. cp->setup_pgtbl(dev, dma);
  564. return 0;
  565. error:
  566. cnic_free_dma(dev, dma);
  567. return -ENOMEM;
  568. }
  569. static void cnic_free_context(struct cnic_dev *dev)
  570. {
  571. struct cnic_local *cp = dev->cnic_priv;
  572. int i;
  573. for (i = 0; i < cp->ctx_blks; i++) {
  574. if (cp->ctx_arr[i].ctx) {
  575. pci_free_consistent(dev->pcidev, cp->ctx_blk_size,
  576. cp->ctx_arr[i].ctx,
  577. cp->ctx_arr[i].mapping);
  578. cp->ctx_arr[i].ctx = NULL;
  579. }
  580. }
  581. }
  582. static void cnic_free_resc(struct cnic_dev *dev)
  583. {
  584. struct cnic_local *cp = dev->cnic_priv;
  585. int i = 0;
  586. if (cp->cnic_uinfo) {
  587. while (cp->uio_dev != -1 && i < 15) {
  588. msleep(100);
  589. i++;
  590. }
  591. uio_unregister_device(cp->cnic_uinfo);
  592. kfree(cp->cnic_uinfo);
  593. cp->cnic_uinfo = NULL;
  594. }
  595. if (cp->l2_buf) {
  596. pci_free_consistent(dev->pcidev, cp->l2_buf_size,
  597. cp->l2_buf, cp->l2_buf_map);
  598. cp->l2_buf = NULL;
  599. }
  600. if (cp->l2_ring) {
  601. pci_free_consistent(dev->pcidev, cp->l2_ring_size,
  602. cp->l2_ring, cp->l2_ring_map);
  603. cp->l2_ring = NULL;
  604. }
  605. cnic_free_context(dev);
  606. kfree(cp->ctx_arr);
  607. cp->ctx_arr = NULL;
  608. cp->ctx_blks = 0;
  609. cnic_free_dma(dev, &cp->gbl_buf_info);
  610. cnic_free_dma(dev, &cp->conn_buf_info);
  611. cnic_free_dma(dev, &cp->kwq_info);
  612. cnic_free_dma(dev, &cp->kcq_info);
  613. kfree(cp->iscsi_tbl);
  614. cp->iscsi_tbl = NULL;
  615. kfree(cp->ctx_tbl);
  616. cp->ctx_tbl = NULL;
  617. cnic_free_id_tbl(&cp->cid_tbl);
  618. }
  619. static int cnic_alloc_context(struct cnic_dev *dev)
  620. {
  621. struct cnic_local *cp = dev->cnic_priv;
  622. if (CHIP_NUM(cp) == CHIP_NUM_5709) {
  623. int i, k, arr_size;
  624. cp->ctx_blk_size = BCM_PAGE_SIZE;
  625. cp->cids_per_blk = BCM_PAGE_SIZE / 128;
  626. arr_size = BNX2_MAX_CID / cp->cids_per_blk *
  627. sizeof(struct cnic_ctx);
  628. cp->ctx_arr = kzalloc(arr_size, GFP_KERNEL);
  629. if (cp->ctx_arr == NULL)
  630. return -ENOMEM;
  631. k = 0;
  632. for (i = 0; i < 2; i++) {
  633. u32 j, reg, off, lo, hi;
  634. if (i == 0)
  635. off = BNX2_PG_CTX_MAP;
  636. else
  637. off = BNX2_ISCSI_CTX_MAP;
  638. reg = cnic_reg_rd_ind(dev, off);
  639. lo = reg >> 16;
  640. hi = reg & 0xffff;
  641. for (j = lo; j < hi; j += cp->cids_per_blk, k++)
  642. cp->ctx_arr[k].cid = j;
  643. }
  644. cp->ctx_blks = k;
  645. if (cp->ctx_blks >= (BNX2_MAX_CID / cp->cids_per_blk)) {
  646. cp->ctx_blks = 0;
  647. return -ENOMEM;
  648. }
  649. for (i = 0; i < cp->ctx_blks; i++) {
  650. cp->ctx_arr[i].ctx =
  651. pci_alloc_consistent(dev->pcidev, BCM_PAGE_SIZE,
  652. &cp->ctx_arr[i].mapping);
  653. if (cp->ctx_arr[i].ctx == NULL)
  654. return -ENOMEM;
  655. }
  656. }
  657. return 0;
  658. }
  659. static int cnic_alloc_l2_rings(struct cnic_dev *dev, int pages)
  660. {
  661. struct cnic_local *cp = dev->cnic_priv;
  662. cp->l2_ring_size = pages * BCM_PAGE_SIZE;
  663. cp->l2_ring = pci_alloc_consistent(dev->pcidev, cp->l2_ring_size,
  664. &cp->l2_ring_map);
  665. if (!cp->l2_ring)
  666. return -ENOMEM;
  667. cp->l2_buf_size = (cp->l2_rx_ring_size + 1) * cp->l2_single_buf_size;
  668. cp->l2_buf_size = PAGE_ALIGN(cp->l2_buf_size);
  669. cp->l2_buf = pci_alloc_consistent(dev->pcidev, cp->l2_buf_size,
  670. &cp->l2_buf_map);
  671. if (!cp->l2_buf)
  672. return -ENOMEM;
  673. return 0;
  674. }
  675. static int cnic_alloc_uio(struct cnic_dev *dev) {
  676. struct cnic_local *cp = dev->cnic_priv;
  677. struct uio_info *uinfo;
  678. int ret;
  679. uinfo = kzalloc(sizeof(*uinfo), GFP_ATOMIC);
  680. if (!uinfo)
  681. return -ENOMEM;
  682. uinfo->mem[0].addr = dev->netdev->base_addr;
  683. uinfo->mem[0].internal_addr = dev->regview;
  684. uinfo->mem[0].size = dev->netdev->mem_end - dev->netdev->mem_start;
  685. uinfo->mem[0].memtype = UIO_MEM_PHYS;
  686. if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
  687. uinfo->mem[1].addr = (unsigned long) cp->status_blk & PAGE_MASK;
  688. if (cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
  689. uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE * 9;
  690. else
  691. uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE;
  692. uinfo->name = "bnx2_cnic";
  693. }
  694. uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
  695. uinfo->mem[2].addr = (unsigned long) cp->l2_ring;
  696. uinfo->mem[2].size = cp->l2_ring_size;
  697. uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
  698. uinfo->mem[3].addr = (unsigned long) cp->l2_buf;
  699. uinfo->mem[3].size = cp->l2_buf_size;
  700. uinfo->mem[3].memtype = UIO_MEM_LOGICAL;
  701. uinfo->version = CNIC_MODULE_VERSION;
  702. uinfo->irq = UIO_IRQ_CUSTOM;
  703. uinfo->open = cnic_uio_open;
  704. uinfo->release = cnic_uio_close;
  705. uinfo->priv = dev;
  706. ret = uio_register_device(&dev->pcidev->dev, uinfo);
  707. if (ret) {
  708. kfree(uinfo);
  709. return ret;
  710. }
  711. cp->cnic_uinfo = uinfo;
  712. return 0;
  713. }
  714. static int cnic_alloc_bnx2_resc(struct cnic_dev *dev)
  715. {
  716. struct cnic_local *cp = dev->cnic_priv;
  717. int ret;
  718. ret = cnic_alloc_dma(dev, &cp->kwq_info, KWQ_PAGE_CNT, 1);
  719. if (ret)
  720. goto error;
  721. cp->kwq = (struct kwqe **) cp->kwq_info.pg_arr;
  722. ret = cnic_alloc_dma(dev, &cp->kcq_info, KCQ_PAGE_CNT, 1);
  723. if (ret)
  724. goto error;
  725. cp->kcq = (struct kcqe **) cp->kcq_info.pg_arr;
  726. ret = cnic_alloc_context(dev);
  727. if (ret)
  728. goto error;
  729. ret = cnic_alloc_l2_rings(dev, 2);
  730. if (ret)
  731. goto error;
  732. ret = cnic_alloc_uio(dev);
  733. if (ret)
  734. goto error;
  735. return 0;
  736. error:
  737. cnic_free_resc(dev);
  738. return ret;
  739. }
  740. static inline u32 cnic_kwq_avail(struct cnic_local *cp)
  741. {
  742. return cp->max_kwq_idx -
  743. ((cp->kwq_prod_idx - cp->kwq_con_idx) & cp->max_kwq_idx);
  744. }
  745. static int cnic_submit_bnx2_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
  746. u32 num_wqes)
  747. {
  748. struct cnic_local *cp = dev->cnic_priv;
  749. struct kwqe *prod_qe;
  750. u16 prod, sw_prod, i;
  751. if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
  752. return -EAGAIN; /* bnx2 is down */
  753. spin_lock_bh(&cp->cnic_ulp_lock);
  754. if (num_wqes > cnic_kwq_avail(cp) &&
  755. !(cp->cnic_local_flags & CNIC_LCL_FL_KWQ_INIT)) {
  756. spin_unlock_bh(&cp->cnic_ulp_lock);
  757. return -EAGAIN;
  758. }
  759. cp->cnic_local_flags &= ~CNIC_LCL_FL_KWQ_INIT;
  760. prod = cp->kwq_prod_idx;
  761. sw_prod = prod & MAX_KWQ_IDX;
  762. for (i = 0; i < num_wqes; i++) {
  763. prod_qe = &cp->kwq[KWQ_PG(sw_prod)][KWQ_IDX(sw_prod)];
  764. memcpy(prod_qe, wqes[i], sizeof(struct kwqe));
  765. prod++;
  766. sw_prod = prod & MAX_KWQ_IDX;
  767. }
  768. cp->kwq_prod_idx = prod;
  769. CNIC_WR16(dev, cp->kwq_io_addr, cp->kwq_prod_idx);
  770. spin_unlock_bh(&cp->cnic_ulp_lock);
  771. return 0;
  772. }
  773. static void service_kcqes(struct cnic_dev *dev, int num_cqes)
  774. {
  775. struct cnic_local *cp = dev->cnic_priv;
  776. int i, j;
  777. i = 0;
  778. j = 1;
  779. while (num_cqes) {
  780. struct cnic_ulp_ops *ulp_ops;
  781. int ulp_type;
  782. u32 kcqe_op_flag = cp->completed_kcq[i]->kcqe_op_flag;
  783. u32 kcqe_layer = kcqe_op_flag & KCQE_FLAGS_LAYER_MASK;
  784. if (unlikely(kcqe_op_flag & KCQE_RAMROD_COMPLETION))
  785. cnic_kwq_completion(dev, 1);
  786. while (j < num_cqes) {
  787. u32 next_op = cp->completed_kcq[i + j]->kcqe_op_flag;
  788. if ((next_op & KCQE_FLAGS_LAYER_MASK) != kcqe_layer)
  789. break;
  790. if (unlikely(next_op & KCQE_RAMROD_COMPLETION))
  791. cnic_kwq_completion(dev, 1);
  792. j++;
  793. }
  794. if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_RDMA)
  795. ulp_type = CNIC_ULP_RDMA;
  796. else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_ISCSI)
  797. ulp_type = CNIC_ULP_ISCSI;
  798. else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L4)
  799. ulp_type = CNIC_ULP_L4;
  800. else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L2)
  801. goto end;
  802. else {
  803. printk(KERN_ERR PFX "%s: Unknown type of KCQE(0x%x)\n",
  804. dev->netdev->name, kcqe_op_flag);
  805. goto end;
  806. }
  807. rcu_read_lock();
  808. ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
  809. if (likely(ulp_ops)) {
  810. ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
  811. cp->completed_kcq + i, j);
  812. }
  813. rcu_read_unlock();
  814. end:
  815. num_cqes -= j;
  816. i += j;
  817. j = 1;
  818. }
  819. return;
  820. }
  821. static u16 cnic_bnx2_next_idx(u16 idx)
  822. {
  823. return idx + 1;
  824. }
  825. static u16 cnic_bnx2_hw_idx(u16 idx)
  826. {
  827. return idx;
  828. }
  829. static int cnic_get_kcqes(struct cnic_dev *dev, u16 hw_prod, u16 *sw_prod)
  830. {
  831. struct cnic_local *cp = dev->cnic_priv;
  832. u16 i, ri, last;
  833. struct kcqe *kcqe;
  834. int kcqe_cnt = 0, last_cnt = 0;
  835. i = ri = last = *sw_prod;
  836. ri &= MAX_KCQ_IDX;
  837. while ((i != hw_prod) && (kcqe_cnt < MAX_COMPLETED_KCQE)) {
  838. kcqe = &cp->kcq[KCQ_PG(ri)][KCQ_IDX(ri)];
  839. cp->completed_kcq[kcqe_cnt++] = kcqe;
  840. i = cp->next_idx(i);
  841. ri = i & MAX_KCQ_IDX;
  842. if (likely(!(kcqe->kcqe_op_flag & KCQE_FLAGS_NEXT))) {
  843. last_cnt = kcqe_cnt;
  844. last = i;
  845. }
  846. }
  847. *sw_prod = last;
  848. return last_cnt;
  849. }
  850. static void cnic_chk_pkt_rings(struct cnic_local *cp)
  851. {
  852. u16 rx_cons = *cp->rx_cons_ptr;
  853. u16 tx_cons = *cp->tx_cons_ptr;
  854. if (cp->tx_cons != tx_cons || cp->rx_cons != rx_cons) {
  855. cp->tx_cons = tx_cons;
  856. cp->rx_cons = rx_cons;
  857. uio_event_notify(cp->cnic_uinfo);
  858. }
  859. }
  860. static int cnic_service_bnx2(void *data, void *status_blk)
  861. {
  862. struct cnic_dev *dev = data;
  863. struct status_block *sblk = status_blk;
  864. struct cnic_local *cp = dev->cnic_priv;
  865. u32 status_idx = sblk->status_idx;
  866. u16 hw_prod, sw_prod;
  867. int kcqe_cnt;
  868. if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags)))
  869. return status_idx;
  870. cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
  871. hw_prod = sblk->status_completion_producer_index;
  872. sw_prod = cp->kcq_prod_idx;
  873. while (sw_prod != hw_prod) {
  874. kcqe_cnt = cnic_get_kcqes(dev, hw_prod, &sw_prod);
  875. if (kcqe_cnt == 0)
  876. goto done;
  877. service_kcqes(dev, kcqe_cnt);
  878. /* Tell compiler that status_blk fields can change. */
  879. barrier();
  880. if (status_idx != sblk->status_idx) {
  881. status_idx = sblk->status_idx;
  882. cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
  883. hw_prod = sblk->status_completion_producer_index;
  884. } else
  885. break;
  886. }
  887. done:
  888. CNIC_WR16(dev, cp->kcq_io_addr, sw_prod);
  889. cp->kcq_prod_idx = sw_prod;
  890. cnic_chk_pkt_rings(cp);
  891. return status_idx;
  892. }
  893. static void cnic_service_bnx2_msix(unsigned long data)
  894. {
  895. struct cnic_dev *dev = (struct cnic_dev *) data;
  896. struct cnic_local *cp = dev->cnic_priv;
  897. struct status_block_msix *status_blk = cp->bnx2_status_blk;
  898. u32 status_idx = status_blk->status_idx;
  899. u16 hw_prod, sw_prod;
  900. int kcqe_cnt;
  901. cp->kwq_con_idx = status_blk->status_cmd_consumer_index;
  902. hw_prod = status_blk->status_completion_producer_index;
  903. sw_prod = cp->kcq_prod_idx;
  904. while (sw_prod != hw_prod) {
  905. kcqe_cnt = cnic_get_kcqes(dev, hw_prod, &sw_prod);
  906. if (kcqe_cnt == 0)
  907. goto done;
  908. service_kcqes(dev, kcqe_cnt);
  909. /* Tell compiler that status_blk fields can change. */
  910. barrier();
  911. if (status_idx != status_blk->status_idx) {
  912. status_idx = status_blk->status_idx;
  913. cp->kwq_con_idx = status_blk->status_cmd_consumer_index;
  914. hw_prod = status_blk->status_completion_producer_index;
  915. } else
  916. break;
  917. }
  918. done:
  919. CNIC_WR16(dev, cp->kcq_io_addr, sw_prod);
  920. cp->kcq_prod_idx = sw_prod;
  921. cnic_chk_pkt_rings(cp);
  922. cp->last_status_idx = status_idx;
  923. CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
  924. BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
  925. }
  926. static irqreturn_t cnic_irq(int irq, void *dev_instance)
  927. {
  928. struct cnic_dev *dev = dev_instance;
  929. struct cnic_local *cp = dev->cnic_priv;
  930. u16 prod = cp->kcq_prod_idx & MAX_KCQ_IDX;
  931. if (cp->ack_int)
  932. cp->ack_int(dev);
  933. prefetch(cp->status_blk);
  934. prefetch(&cp->kcq[KCQ_PG(prod)][KCQ_IDX(prod)]);
  935. if (likely(test_bit(CNIC_F_CNIC_UP, &dev->flags)))
  936. tasklet_schedule(&cp->cnic_irq_task);
  937. return IRQ_HANDLED;
  938. }
  939. static void cnic_ulp_stop(struct cnic_dev *dev)
  940. {
  941. struct cnic_local *cp = dev->cnic_priv;
  942. int if_type;
  943. if (cp->cnic_uinfo)
  944. cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
  945. for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
  946. struct cnic_ulp_ops *ulp_ops;
  947. mutex_lock(&cnic_lock);
  948. ulp_ops = cp->ulp_ops[if_type];
  949. if (!ulp_ops) {
  950. mutex_unlock(&cnic_lock);
  951. continue;
  952. }
  953. set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
  954. mutex_unlock(&cnic_lock);
  955. if (test_and_clear_bit(ULP_F_START, &cp->ulp_flags[if_type]))
  956. ulp_ops->cnic_stop(cp->ulp_handle[if_type]);
  957. clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
  958. }
  959. }
  960. static void cnic_ulp_start(struct cnic_dev *dev)
  961. {
  962. struct cnic_local *cp = dev->cnic_priv;
  963. int if_type;
  964. for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
  965. struct cnic_ulp_ops *ulp_ops;
  966. mutex_lock(&cnic_lock);
  967. ulp_ops = cp->ulp_ops[if_type];
  968. if (!ulp_ops || !ulp_ops->cnic_start) {
  969. mutex_unlock(&cnic_lock);
  970. continue;
  971. }
  972. set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
  973. mutex_unlock(&cnic_lock);
  974. if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[if_type]))
  975. ulp_ops->cnic_start(cp->ulp_handle[if_type]);
  976. clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
  977. }
  978. }
  979. static int cnic_ctl(void *data, struct cnic_ctl_info *info)
  980. {
  981. struct cnic_dev *dev = data;
  982. switch (info->cmd) {
  983. case CNIC_CTL_STOP_CMD:
  984. cnic_hold(dev);
  985. cnic_ulp_stop(dev);
  986. cnic_stop_hw(dev);
  987. cnic_put(dev);
  988. break;
  989. case CNIC_CTL_START_CMD:
  990. cnic_hold(dev);
  991. if (!cnic_start_hw(dev))
  992. cnic_ulp_start(dev);
  993. cnic_put(dev);
  994. break;
  995. default:
  996. return -EINVAL;
  997. }
  998. return 0;
  999. }
  1000. static void cnic_ulp_init(struct cnic_dev *dev)
  1001. {
  1002. int i;
  1003. struct cnic_local *cp = dev->cnic_priv;
  1004. for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
  1005. struct cnic_ulp_ops *ulp_ops;
  1006. mutex_lock(&cnic_lock);
  1007. ulp_ops = cnic_ulp_tbl[i];
  1008. if (!ulp_ops || !ulp_ops->cnic_init) {
  1009. mutex_unlock(&cnic_lock);
  1010. continue;
  1011. }
  1012. ulp_get(ulp_ops);
  1013. mutex_unlock(&cnic_lock);
  1014. if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[i]))
  1015. ulp_ops->cnic_init(dev);
  1016. ulp_put(ulp_ops);
  1017. }
  1018. }
  1019. static void cnic_ulp_exit(struct cnic_dev *dev)
  1020. {
  1021. int i;
  1022. struct cnic_local *cp = dev->cnic_priv;
  1023. for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
  1024. struct cnic_ulp_ops *ulp_ops;
  1025. mutex_lock(&cnic_lock);
  1026. ulp_ops = cnic_ulp_tbl[i];
  1027. if (!ulp_ops || !ulp_ops->cnic_exit) {
  1028. mutex_unlock(&cnic_lock);
  1029. continue;
  1030. }
  1031. ulp_get(ulp_ops);
  1032. mutex_unlock(&cnic_lock);
  1033. if (test_and_clear_bit(ULP_F_INIT, &cp->ulp_flags[i]))
  1034. ulp_ops->cnic_exit(dev);
  1035. ulp_put(ulp_ops);
  1036. }
  1037. }
  1038. static int cnic_cm_offload_pg(struct cnic_sock *csk)
  1039. {
  1040. struct cnic_dev *dev = csk->dev;
  1041. struct l4_kwq_offload_pg *l4kwqe;
  1042. struct kwqe *wqes[1];
  1043. l4kwqe = (struct l4_kwq_offload_pg *) &csk->kwqe1;
  1044. memset(l4kwqe, 0, sizeof(*l4kwqe));
  1045. wqes[0] = (struct kwqe *) l4kwqe;
  1046. l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_OFFLOAD_PG;
  1047. l4kwqe->flags =
  1048. L4_LAYER_CODE << L4_KWQ_OFFLOAD_PG_LAYER_CODE_SHIFT;
  1049. l4kwqe->l2hdr_nbytes = ETH_HLEN;
  1050. l4kwqe->da0 = csk->ha[0];
  1051. l4kwqe->da1 = csk->ha[1];
  1052. l4kwqe->da2 = csk->ha[2];
  1053. l4kwqe->da3 = csk->ha[3];
  1054. l4kwqe->da4 = csk->ha[4];
  1055. l4kwqe->da5 = csk->ha[5];
  1056. l4kwqe->sa0 = dev->mac_addr[0];
  1057. l4kwqe->sa1 = dev->mac_addr[1];
  1058. l4kwqe->sa2 = dev->mac_addr[2];
  1059. l4kwqe->sa3 = dev->mac_addr[3];
  1060. l4kwqe->sa4 = dev->mac_addr[4];
  1061. l4kwqe->sa5 = dev->mac_addr[5];
  1062. l4kwqe->etype = ETH_P_IP;
  1063. l4kwqe->ipid_count = DEF_IPID_COUNT;
  1064. l4kwqe->host_opaque = csk->l5_cid;
  1065. if (csk->vlan_id) {
  1066. l4kwqe->pg_flags |= L4_KWQ_OFFLOAD_PG_VLAN_TAGGING;
  1067. l4kwqe->vlan_tag = csk->vlan_id;
  1068. l4kwqe->l2hdr_nbytes += 4;
  1069. }
  1070. return dev->submit_kwqes(dev, wqes, 1);
  1071. }
  1072. static int cnic_cm_update_pg(struct cnic_sock *csk)
  1073. {
  1074. struct cnic_dev *dev = csk->dev;
  1075. struct l4_kwq_update_pg *l4kwqe;
  1076. struct kwqe *wqes[1];
  1077. l4kwqe = (struct l4_kwq_update_pg *) &csk->kwqe1;
  1078. memset(l4kwqe, 0, sizeof(*l4kwqe));
  1079. wqes[0] = (struct kwqe *) l4kwqe;
  1080. l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPDATE_PG;
  1081. l4kwqe->flags =
  1082. L4_LAYER_CODE << L4_KWQ_UPDATE_PG_LAYER_CODE_SHIFT;
  1083. l4kwqe->pg_cid = csk->pg_cid;
  1084. l4kwqe->da0 = csk->ha[0];
  1085. l4kwqe->da1 = csk->ha[1];
  1086. l4kwqe->da2 = csk->ha[2];
  1087. l4kwqe->da3 = csk->ha[3];
  1088. l4kwqe->da4 = csk->ha[4];
  1089. l4kwqe->da5 = csk->ha[5];
  1090. l4kwqe->pg_host_opaque = csk->l5_cid;
  1091. l4kwqe->pg_valids = L4_KWQ_UPDATE_PG_VALIDS_DA;
  1092. return dev->submit_kwqes(dev, wqes, 1);
  1093. }
  1094. static int cnic_cm_upload_pg(struct cnic_sock *csk)
  1095. {
  1096. struct cnic_dev *dev = csk->dev;
  1097. struct l4_kwq_upload *l4kwqe;
  1098. struct kwqe *wqes[1];
  1099. l4kwqe = (struct l4_kwq_upload *) &csk->kwqe1;
  1100. memset(l4kwqe, 0, sizeof(*l4kwqe));
  1101. wqes[0] = (struct kwqe *) l4kwqe;
  1102. l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPLOAD_PG;
  1103. l4kwqe->flags =
  1104. L4_LAYER_CODE << L4_KWQ_UPLOAD_LAYER_CODE_SHIFT;
  1105. l4kwqe->cid = csk->pg_cid;
  1106. return dev->submit_kwqes(dev, wqes, 1);
  1107. }
  1108. static int cnic_cm_conn_req(struct cnic_sock *csk)
  1109. {
  1110. struct cnic_dev *dev = csk->dev;
  1111. struct l4_kwq_connect_req1 *l4kwqe1;
  1112. struct l4_kwq_connect_req2 *l4kwqe2;
  1113. struct l4_kwq_connect_req3 *l4kwqe3;
  1114. struct kwqe *wqes[3];
  1115. u8 tcp_flags = 0;
  1116. int num_wqes = 2;
  1117. l4kwqe1 = (struct l4_kwq_connect_req1 *) &csk->kwqe1;
  1118. l4kwqe2 = (struct l4_kwq_connect_req2 *) &csk->kwqe2;
  1119. l4kwqe3 = (struct l4_kwq_connect_req3 *) &csk->kwqe3;
  1120. memset(l4kwqe1, 0, sizeof(*l4kwqe1));
  1121. memset(l4kwqe2, 0, sizeof(*l4kwqe2));
  1122. memset(l4kwqe3, 0, sizeof(*l4kwqe3));
  1123. l4kwqe3->op_code = L4_KWQE_OPCODE_VALUE_CONNECT3;
  1124. l4kwqe3->flags =
  1125. L4_LAYER_CODE << L4_KWQ_CONNECT_REQ3_LAYER_CODE_SHIFT;
  1126. l4kwqe3->ka_timeout = csk->ka_timeout;
  1127. l4kwqe3->ka_interval = csk->ka_interval;
  1128. l4kwqe3->ka_max_probe_count = csk->ka_max_probe_count;
  1129. l4kwqe3->tos = csk->tos;
  1130. l4kwqe3->ttl = csk->ttl;
  1131. l4kwqe3->snd_seq_scale = csk->snd_seq_scale;
  1132. l4kwqe3->pmtu = csk->mtu;
  1133. l4kwqe3->rcv_buf = csk->rcv_buf;
  1134. l4kwqe3->snd_buf = csk->snd_buf;
  1135. l4kwqe3->seed = csk->seed;
  1136. wqes[0] = (struct kwqe *) l4kwqe1;
  1137. if (test_bit(SK_F_IPV6, &csk->flags)) {
  1138. wqes[1] = (struct kwqe *) l4kwqe2;
  1139. wqes[2] = (struct kwqe *) l4kwqe3;
  1140. num_wqes = 3;
  1141. l4kwqe1->conn_flags = L4_KWQ_CONNECT_REQ1_IP_V6;
  1142. l4kwqe2->op_code = L4_KWQE_OPCODE_VALUE_CONNECT2;
  1143. l4kwqe2->flags =
  1144. L4_KWQ_CONNECT_REQ2_LINKED_WITH_NEXT |
  1145. L4_LAYER_CODE << L4_KWQ_CONNECT_REQ2_LAYER_CODE_SHIFT;
  1146. l4kwqe2->src_ip_v6_2 = be32_to_cpu(csk->src_ip[1]);
  1147. l4kwqe2->src_ip_v6_3 = be32_to_cpu(csk->src_ip[2]);
  1148. l4kwqe2->src_ip_v6_4 = be32_to_cpu(csk->src_ip[3]);
  1149. l4kwqe2->dst_ip_v6_2 = be32_to_cpu(csk->dst_ip[1]);
  1150. l4kwqe2->dst_ip_v6_3 = be32_to_cpu(csk->dst_ip[2]);
  1151. l4kwqe2->dst_ip_v6_4 = be32_to_cpu(csk->dst_ip[3]);
  1152. l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct ipv6hdr) -
  1153. sizeof(struct tcphdr);
  1154. } else {
  1155. wqes[1] = (struct kwqe *) l4kwqe3;
  1156. l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct iphdr) -
  1157. sizeof(struct tcphdr);
  1158. }
  1159. l4kwqe1->op_code = L4_KWQE_OPCODE_VALUE_CONNECT1;
  1160. l4kwqe1->flags =
  1161. (L4_LAYER_CODE << L4_KWQ_CONNECT_REQ1_LAYER_CODE_SHIFT) |
  1162. L4_KWQ_CONNECT_REQ3_LINKED_WITH_NEXT;
  1163. l4kwqe1->cid = csk->cid;
  1164. l4kwqe1->pg_cid = csk->pg_cid;
  1165. l4kwqe1->src_ip = be32_to_cpu(csk->src_ip[0]);
  1166. l4kwqe1->dst_ip = be32_to_cpu(csk->dst_ip[0]);
  1167. l4kwqe1->src_port = be16_to_cpu(csk->src_port);
  1168. l4kwqe1->dst_port = be16_to_cpu(csk->dst_port);
  1169. if (csk->tcp_flags & SK_TCP_NO_DELAY_ACK)
  1170. tcp_flags |= L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK;
  1171. if (csk->tcp_flags & SK_TCP_KEEP_ALIVE)
  1172. tcp_flags |= L4_KWQ_CONNECT_REQ1_KEEP_ALIVE;
  1173. if (csk->tcp_flags & SK_TCP_NAGLE)
  1174. tcp_flags |= L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE;
  1175. if (csk->tcp_flags & SK_TCP_TIMESTAMP)
  1176. tcp_flags |= L4_KWQ_CONNECT_REQ1_TIME_STAMP;
  1177. if (csk->tcp_flags & SK_TCP_SACK)
  1178. tcp_flags |= L4_KWQ_CONNECT_REQ1_SACK;
  1179. if (csk->tcp_flags & SK_TCP_SEG_SCALING)
  1180. tcp_flags |= L4_KWQ_CONNECT_REQ1_SEG_SCALING;
  1181. l4kwqe1->tcp_flags = tcp_flags;
  1182. return dev->submit_kwqes(dev, wqes, num_wqes);
  1183. }
  1184. static int cnic_cm_close_req(struct cnic_sock *csk)
  1185. {
  1186. struct cnic_dev *dev = csk->dev;
  1187. struct l4_kwq_close_req *l4kwqe;
  1188. struct kwqe *wqes[1];
  1189. l4kwqe = (struct l4_kwq_close_req *) &csk->kwqe2;
  1190. memset(l4kwqe, 0, sizeof(*l4kwqe));
  1191. wqes[0] = (struct kwqe *) l4kwqe;
  1192. l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_CLOSE;
  1193. l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_CLOSE_REQ_LAYER_CODE_SHIFT;
  1194. l4kwqe->cid = csk->cid;
  1195. return dev->submit_kwqes(dev, wqes, 1);
  1196. }
  1197. static int cnic_cm_abort_req(struct cnic_sock *csk)
  1198. {
  1199. struct cnic_dev *dev = csk->dev;
  1200. struct l4_kwq_reset_req *l4kwqe;
  1201. struct kwqe *wqes[1];
  1202. l4kwqe = (struct l4_kwq_reset_req *) &csk->kwqe2;
  1203. memset(l4kwqe, 0, sizeof(*l4kwqe));
  1204. wqes[0] = (struct kwqe *) l4kwqe;
  1205. l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_RESET;
  1206. l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_RESET_REQ_LAYER_CODE_SHIFT;
  1207. l4kwqe->cid = csk->cid;
  1208. return dev->submit_kwqes(dev, wqes, 1);
  1209. }
  1210. static int cnic_cm_create(struct cnic_dev *dev, int ulp_type, u32 cid,
  1211. u32 l5_cid, struct cnic_sock **csk, void *context)
  1212. {
  1213. struct cnic_local *cp = dev->cnic_priv;
  1214. struct cnic_sock *csk1;
  1215. if (l5_cid >= MAX_CM_SK_TBL_SZ)
  1216. return -EINVAL;
  1217. csk1 = &cp->csk_tbl[l5_cid];
  1218. if (atomic_read(&csk1->ref_count))
  1219. return -EAGAIN;
  1220. if (test_and_set_bit(SK_F_INUSE, &csk1->flags))
  1221. return -EBUSY;
  1222. csk1->dev = dev;
  1223. csk1->cid = cid;
  1224. csk1->l5_cid = l5_cid;
  1225. csk1->ulp_type = ulp_type;
  1226. csk1->context = context;
  1227. csk1->ka_timeout = DEF_KA_TIMEOUT;
  1228. csk1->ka_interval = DEF_KA_INTERVAL;
  1229. csk1->ka_max_probe_count = DEF_KA_MAX_PROBE_COUNT;
  1230. csk1->tos = DEF_TOS;
  1231. csk1->ttl = DEF_TTL;
  1232. csk1->snd_seq_scale = DEF_SND_SEQ_SCALE;
  1233. csk1->rcv_buf = DEF_RCV_BUF;
  1234. csk1->snd_buf = DEF_SND_BUF;
  1235. csk1->seed = DEF_SEED;
  1236. *csk = csk1;
  1237. return 0;
  1238. }
  1239. static void cnic_cm_cleanup(struct cnic_sock *csk)
  1240. {
  1241. if (csk->src_port) {
  1242. struct cnic_dev *dev = csk->dev;
  1243. struct cnic_local *cp = dev->cnic_priv;
  1244. cnic_free_id(&cp->csk_port_tbl, csk->src_port);
  1245. csk->src_port = 0;
  1246. }
  1247. }
  1248. static void cnic_close_conn(struct cnic_sock *csk)
  1249. {
  1250. if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags)) {
  1251. cnic_cm_upload_pg(csk);
  1252. clear_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
  1253. }
  1254. cnic_cm_cleanup(csk);
  1255. }
  1256. static int cnic_cm_destroy(struct cnic_sock *csk)
  1257. {
  1258. if (!cnic_in_use(csk))
  1259. return -EINVAL;
  1260. csk_hold(csk);
  1261. clear_bit(SK_F_INUSE, &csk->flags);
  1262. smp_mb__after_clear_bit();
  1263. while (atomic_read(&csk->ref_count) != 1)
  1264. msleep(1);
  1265. cnic_cm_cleanup(csk);
  1266. csk->flags = 0;
  1267. csk_put(csk);
  1268. return 0;
  1269. }
  1270. static inline u16 cnic_get_vlan(struct net_device *dev,
  1271. struct net_device **vlan_dev)
  1272. {
  1273. if (dev->priv_flags & IFF_802_1Q_VLAN) {
  1274. *vlan_dev = vlan_dev_real_dev(dev);
  1275. return vlan_dev_vlan_id(dev);
  1276. }
  1277. *vlan_dev = dev;
  1278. return 0;
  1279. }
  1280. static int cnic_get_v4_route(struct sockaddr_in *dst_addr,
  1281. struct dst_entry **dst)
  1282. {
  1283. #if defined(CONFIG_INET)
  1284. struct flowi fl;
  1285. int err;
  1286. struct rtable *rt;
  1287. memset(&fl, 0, sizeof(fl));
  1288. fl.nl_u.ip4_u.daddr = dst_addr->sin_addr.s_addr;
  1289. err = ip_route_output_key(&init_net, &rt, &fl);
  1290. if (!err)
  1291. *dst = &rt->u.dst;
  1292. return err;
  1293. #else
  1294. return -ENETUNREACH;
  1295. #endif
  1296. }
  1297. static int cnic_get_v6_route(struct sockaddr_in6 *dst_addr,
  1298. struct dst_entry **dst)
  1299. {
  1300. #if defined(CONFIG_IPV6) || (defined(CONFIG_IPV6_MODULE) && defined(MODULE))
  1301. struct flowi fl;
  1302. memset(&fl, 0, sizeof(fl));
  1303. ipv6_addr_copy(&fl.fl6_dst, &dst_addr->sin6_addr);
  1304. if (ipv6_addr_type(&fl.fl6_dst) & IPV6_ADDR_LINKLOCAL)
  1305. fl.oif = dst_addr->sin6_scope_id;
  1306. *dst = ip6_route_output(&init_net, NULL, &fl);
  1307. if (*dst)
  1308. return 0;
  1309. #endif
  1310. return -ENETUNREACH;
  1311. }
  1312. static struct cnic_dev *cnic_cm_select_dev(struct sockaddr_in *dst_addr,
  1313. int ulp_type)
  1314. {
  1315. struct cnic_dev *dev = NULL;
  1316. struct dst_entry *dst;
  1317. struct net_device *netdev = NULL;
  1318. int err = -ENETUNREACH;
  1319. if (dst_addr->sin_family == AF_INET)
  1320. err = cnic_get_v4_route(dst_addr, &dst);
  1321. else if (dst_addr->sin_family == AF_INET6) {
  1322. struct sockaddr_in6 *dst_addr6 =
  1323. (struct sockaddr_in6 *) dst_addr;
  1324. err = cnic_get_v6_route(dst_addr6, &dst);
  1325. } else
  1326. return NULL;
  1327. if (err)
  1328. return NULL;
  1329. if (!dst->dev)
  1330. goto done;
  1331. cnic_get_vlan(dst->dev, &netdev);
  1332. dev = cnic_from_netdev(netdev);
  1333. done:
  1334. dst_release(dst);
  1335. if (dev)
  1336. cnic_put(dev);
  1337. return dev;
  1338. }
  1339. static int cnic_resolve_addr(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
  1340. {
  1341. struct cnic_dev *dev = csk->dev;
  1342. struct cnic_local *cp = dev->cnic_priv;
  1343. return cnic_send_nlmsg(cp, ISCSI_KEVENT_PATH_REQ, csk);
  1344. }
  1345. static int cnic_get_route(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
  1346. {
  1347. struct cnic_dev *dev = csk->dev;
  1348. struct cnic_local *cp = dev->cnic_priv;
  1349. int is_v6, err, rc = -ENETUNREACH;
  1350. struct dst_entry *dst;
  1351. struct net_device *realdev;
  1352. u32 local_port;
  1353. if (saddr->local.v6.sin6_family == AF_INET6 &&
  1354. saddr->remote.v6.sin6_family == AF_INET6)
  1355. is_v6 = 1;
  1356. else if (saddr->local.v4.sin_family == AF_INET &&
  1357. saddr->remote.v4.sin_family == AF_INET)
  1358. is_v6 = 0;
  1359. else
  1360. return -EINVAL;
  1361. clear_bit(SK_F_IPV6, &csk->flags);
  1362. if (is_v6) {
  1363. #if defined(CONFIG_IPV6) || (defined(CONFIG_IPV6_MODULE) && defined(MODULE))
  1364. set_bit(SK_F_IPV6, &csk->flags);
  1365. err = cnic_get_v6_route(&saddr->remote.v6, &dst);
  1366. if (err)
  1367. return err;
  1368. if (!dst || dst->error || !dst->dev)
  1369. goto err_out;
  1370. memcpy(&csk->dst_ip[0], &saddr->remote.v6.sin6_addr,
  1371. sizeof(struct in6_addr));
  1372. csk->dst_port = saddr->remote.v6.sin6_port;
  1373. local_port = saddr->local.v6.sin6_port;
  1374. #else
  1375. return rc;
  1376. #endif
  1377. } else {
  1378. err = cnic_get_v4_route(&saddr->remote.v4, &dst);
  1379. if (err)
  1380. return err;
  1381. if (!dst || dst->error || !dst->dev)
  1382. goto err_out;
  1383. csk->dst_ip[0] = saddr->remote.v4.sin_addr.s_addr;
  1384. csk->dst_port = saddr->remote.v4.sin_port;
  1385. local_port = saddr->local.v4.sin_port;
  1386. }
  1387. csk->vlan_id = cnic_get_vlan(dst->dev, &realdev);
  1388. if (realdev != dev->netdev)
  1389. goto err_out;
  1390. if (local_port >= CNIC_LOCAL_PORT_MIN &&
  1391. local_port < CNIC_LOCAL_PORT_MAX) {
  1392. if (cnic_alloc_id(&cp->csk_port_tbl, local_port))
  1393. local_port = 0;
  1394. } else
  1395. local_port = 0;
  1396. if (!local_port) {
  1397. local_port = cnic_alloc_new_id(&cp->csk_port_tbl);
  1398. if (local_port == -1) {
  1399. rc = -ENOMEM;
  1400. goto err_out;
  1401. }
  1402. }
  1403. csk->src_port = local_port;
  1404. csk->mtu = dst_mtu(dst);
  1405. rc = 0;
  1406. err_out:
  1407. dst_release(dst);
  1408. return rc;
  1409. }
  1410. static void cnic_init_csk_state(struct cnic_sock *csk)
  1411. {
  1412. csk->state = 0;
  1413. clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
  1414. clear_bit(SK_F_CLOSING, &csk->flags);
  1415. }
  1416. static int cnic_cm_connect(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
  1417. {
  1418. int err = 0;
  1419. if (!cnic_in_use(csk))
  1420. return -EINVAL;
  1421. if (test_and_set_bit(SK_F_CONNECT_START, &csk->flags))
  1422. return -EINVAL;
  1423. cnic_init_csk_state(csk);
  1424. err = cnic_get_route(csk, saddr);
  1425. if (err)
  1426. goto err_out;
  1427. err = cnic_resolve_addr(csk, saddr);
  1428. if (!err)
  1429. return 0;
  1430. err_out:
  1431. clear_bit(SK_F_CONNECT_START, &csk->flags);
  1432. return err;
  1433. }
  1434. static int cnic_cm_abort(struct cnic_sock *csk)
  1435. {
  1436. struct cnic_local *cp = csk->dev->cnic_priv;
  1437. u32 opcode;
  1438. if (!cnic_in_use(csk))
  1439. return -EINVAL;
  1440. if (cnic_abort_prep(csk))
  1441. return cnic_cm_abort_req(csk);
  1442. /* Getting here means that we haven't started connect, or
  1443. * connect was not successful.
  1444. */
  1445. csk->state = L4_KCQE_OPCODE_VALUE_RESET_COMP;
  1446. if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
  1447. opcode = csk->state;
  1448. else
  1449. opcode = L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD;
  1450. cp->close_conn(csk, opcode);
  1451. return 0;
  1452. }
  1453. static int cnic_cm_close(struct cnic_sock *csk)
  1454. {
  1455. if (!cnic_in_use(csk))
  1456. return -EINVAL;
  1457. if (cnic_close_prep(csk)) {
  1458. csk->state = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
  1459. return cnic_cm_close_req(csk);
  1460. }
  1461. return 0;
  1462. }
  1463. static void cnic_cm_upcall(struct cnic_local *cp, struct cnic_sock *csk,
  1464. u8 opcode)
  1465. {
  1466. struct cnic_ulp_ops *ulp_ops;
  1467. int ulp_type = csk->ulp_type;
  1468. rcu_read_lock();
  1469. ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
  1470. if (ulp_ops) {
  1471. if (opcode == L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE)
  1472. ulp_ops->cm_connect_complete(csk);
  1473. else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
  1474. ulp_ops->cm_close_complete(csk);
  1475. else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED)
  1476. ulp_ops->cm_remote_abort(csk);
  1477. else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_COMP)
  1478. ulp_ops->cm_abort_complete(csk);
  1479. else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED)
  1480. ulp_ops->cm_remote_close(csk);
  1481. }
  1482. rcu_read_unlock();
  1483. }
  1484. static int cnic_cm_set_pg(struct cnic_sock *csk)
  1485. {
  1486. if (cnic_offld_prep(csk)) {
  1487. if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
  1488. cnic_cm_update_pg(csk);
  1489. else
  1490. cnic_cm_offload_pg(csk);
  1491. }
  1492. return 0;
  1493. }
  1494. static void cnic_cm_process_offld_pg(struct cnic_dev *dev, struct l4_kcq *kcqe)
  1495. {
  1496. struct cnic_local *cp = dev->cnic_priv;
  1497. u32 l5_cid = kcqe->pg_host_opaque;
  1498. u8 opcode = kcqe->op_code;
  1499. struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
  1500. csk_hold(csk);
  1501. if (!cnic_in_use(csk))
  1502. goto done;
  1503. if (opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
  1504. clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
  1505. goto done;
  1506. }
  1507. csk->pg_cid = kcqe->pg_cid;
  1508. set_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
  1509. cnic_cm_conn_req(csk);
  1510. done:
  1511. csk_put(csk);
  1512. }
  1513. static void cnic_cm_process_kcqe(struct cnic_dev *dev, struct kcqe *kcqe)
  1514. {
  1515. struct cnic_local *cp = dev->cnic_priv;
  1516. struct l4_kcq *l4kcqe = (struct l4_kcq *) kcqe;
  1517. u8 opcode = l4kcqe->op_code;
  1518. u32 l5_cid;
  1519. struct cnic_sock *csk;
  1520. if (opcode == L4_KCQE_OPCODE_VALUE_OFFLOAD_PG ||
  1521. opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
  1522. cnic_cm_process_offld_pg(dev, l4kcqe);
  1523. return;
  1524. }
  1525. l5_cid = l4kcqe->conn_id;
  1526. if (opcode & 0x80)
  1527. l5_cid = l4kcqe->cid;
  1528. if (l5_cid >= MAX_CM_SK_TBL_SZ)
  1529. return;
  1530. csk = &cp->csk_tbl[l5_cid];
  1531. csk_hold(csk);
  1532. if (!cnic_in_use(csk)) {
  1533. csk_put(csk);
  1534. return;
  1535. }
  1536. switch (opcode) {
  1537. case L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE:
  1538. if (l4kcqe->status == 0)
  1539. set_bit(SK_F_OFFLD_COMPLETE, &csk->flags);
  1540. smp_mb__before_clear_bit();
  1541. clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
  1542. cnic_cm_upcall(cp, csk, opcode);
  1543. break;
  1544. case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
  1545. if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags))
  1546. csk->state = opcode;
  1547. /* fall through */
  1548. case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
  1549. case L4_KCQE_OPCODE_VALUE_RESET_COMP:
  1550. cp->close_conn(csk, opcode);
  1551. break;
  1552. case L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED:
  1553. cnic_cm_upcall(cp, csk, opcode);
  1554. break;
  1555. }
  1556. csk_put(csk);
  1557. }
  1558. static void cnic_cm_indicate_kcqe(void *data, struct kcqe *kcqe[], u32 num)
  1559. {
  1560. struct cnic_dev *dev = data;
  1561. int i;
  1562. for (i = 0; i < num; i++)
  1563. cnic_cm_process_kcqe(dev, kcqe[i]);
  1564. }
  1565. static struct cnic_ulp_ops cm_ulp_ops = {
  1566. .indicate_kcqes = cnic_cm_indicate_kcqe,
  1567. };
  1568. static void cnic_cm_free_mem(struct cnic_dev *dev)
  1569. {
  1570. struct cnic_local *cp = dev->cnic_priv;
  1571. kfree(cp->csk_tbl);
  1572. cp->csk_tbl = NULL;
  1573. cnic_free_id_tbl(&cp->csk_port_tbl);
  1574. }
  1575. static int cnic_cm_alloc_mem(struct cnic_dev *dev)
  1576. {
  1577. struct cnic_local *cp = dev->cnic_priv;
  1578. cp->csk_tbl = kzalloc(sizeof(struct cnic_sock) * MAX_CM_SK_TBL_SZ,
  1579. GFP_KERNEL);
  1580. if (!cp->csk_tbl)
  1581. return -ENOMEM;
  1582. if (cnic_init_id_tbl(&cp->csk_port_tbl, CNIC_LOCAL_PORT_RANGE,
  1583. CNIC_LOCAL_PORT_MIN)) {
  1584. cnic_cm_free_mem(dev);
  1585. return -ENOMEM;
  1586. }
  1587. return 0;
  1588. }
  1589. static int cnic_ready_to_close(struct cnic_sock *csk, u32 opcode)
  1590. {
  1591. if ((opcode == csk->state) ||
  1592. (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED &&
  1593. csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)) {
  1594. if (!test_and_set_bit(SK_F_CLOSING, &csk->flags))
  1595. return 1;
  1596. }
  1597. return 0;
  1598. }
  1599. static void cnic_close_bnx2_conn(struct cnic_sock *csk, u32 opcode)
  1600. {
  1601. struct cnic_dev *dev = csk->dev;
  1602. struct cnic_local *cp = dev->cnic_priv;
  1603. clear_bit(SK_F_CONNECT_START, &csk->flags);
  1604. if (cnic_ready_to_close(csk, opcode)) {
  1605. cnic_close_conn(csk);
  1606. cnic_cm_upcall(cp, csk, opcode);
  1607. }
  1608. }
  1609. static void cnic_cm_stop_bnx2_hw(struct cnic_dev *dev)
  1610. {
  1611. }
  1612. static int cnic_cm_init_bnx2_hw(struct cnic_dev *dev)
  1613. {
  1614. u32 seed;
  1615. get_random_bytes(&seed, 4);
  1616. cnic_ctx_wr(dev, 45, 0, seed);
  1617. return 0;
  1618. }
  1619. static int cnic_cm_open(struct cnic_dev *dev)
  1620. {
  1621. struct cnic_local *cp = dev->cnic_priv;
  1622. int err;
  1623. err = cnic_cm_alloc_mem(dev);
  1624. if (err)
  1625. return err;
  1626. err = cp->start_cm(dev);
  1627. if (err)
  1628. goto err_out;
  1629. dev->cm_create = cnic_cm_create;
  1630. dev->cm_destroy = cnic_cm_destroy;
  1631. dev->cm_connect = cnic_cm_connect;
  1632. dev->cm_abort = cnic_cm_abort;
  1633. dev->cm_close = cnic_cm_close;
  1634. dev->cm_select_dev = cnic_cm_select_dev;
  1635. cp->ulp_handle[CNIC_ULP_L4] = dev;
  1636. rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], &cm_ulp_ops);
  1637. return 0;
  1638. err_out:
  1639. cnic_cm_free_mem(dev);
  1640. return err;
  1641. }
  1642. static int cnic_cm_shutdown(struct cnic_dev *dev)
  1643. {
  1644. struct cnic_local *cp = dev->cnic_priv;
  1645. int i;
  1646. cp->stop_cm(dev);
  1647. if (!cp->csk_tbl)
  1648. return 0;
  1649. for (i = 0; i < MAX_CM_SK_TBL_SZ; i++) {
  1650. struct cnic_sock *csk = &cp->csk_tbl[i];
  1651. clear_bit(SK_F_INUSE, &csk->flags);
  1652. cnic_cm_cleanup(csk);
  1653. }
  1654. cnic_cm_free_mem(dev);
  1655. return 0;
  1656. }
  1657. static void cnic_init_context(struct cnic_dev *dev, u32 cid)
  1658. {
  1659. struct cnic_local *cp = dev->cnic_priv;
  1660. u32 cid_addr;
  1661. int i;
  1662. if (CHIP_NUM(cp) == CHIP_NUM_5709)
  1663. return;
  1664. cid_addr = GET_CID_ADDR(cid);
  1665. for (i = 0; i < CTX_SIZE; i += 4)
  1666. cnic_ctx_wr(dev, cid_addr, i, 0);
  1667. }
  1668. static int cnic_setup_5709_context(struct cnic_dev *dev, int valid)
  1669. {
  1670. struct cnic_local *cp = dev->cnic_priv;
  1671. int ret = 0, i;
  1672. u32 valid_bit = valid ? BNX2_CTX_HOST_PAGE_TBL_DATA0_VALID : 0;
  1673. if (CHIP_NUM(cp) != CHIP_NUM_5709)
  1674. return 0;
  1675. for (i = 0; i < cp->ctx_blks; i++) {
  1676. int j;
  1677. u32 idx = cp->ctx_arr[i].cid / cp->cids_per_blk;
  1678. u32 val;
  1679. memset(cp->ctx_arr[i].ctx, 0, BCM_PAGE_SIZE);
  1680. CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA0,
  1681. (cp->ctx_arr[i].mapping & 0xffffffff) | valid_bit);
  1682. CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA1,
  1683. (u64) cp->ctx_arr[i].mapping >> 32);
  1684. CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL, idx |
  1685. BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ);
  1686. for (j = 0; j < 10; j++) {
  1687. val = CNIC_RD(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL);
  1688. if (!(val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ))
  1689. break;
  1690. udelay(5);
  1691. }
  1692. if (val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ) {
  1693. ret = -EBUSY;
  1694. break;
  1695. }
  1696. }
  1697. return ret;
  1698. }
  1699. static void cnic_free_irq(struct cnic_dev *dev)
  1700. {
  1701. struct cnic_local *cp = dev->cnic_priv;
  1702. struct cnic_eth_dev *ethdev = cp->ethdev;
  1703. if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
  1704. cp->disable_int_sync(dev);
  1705. tasklet_disable(&cp->cnic_irq_task);
  1706. free_irq(ethdev->irq_arr[0].vector, dev);
  1707. }
  1708. }
  1709. static int cnic_init_bnx2_irq(struct cnic_dev *dev)
  1710. {
  1711. struct cnic_local *cp = dev->cnic_priv;
  1712. struct cnic_eth_dev *ethdev = cp->ethdev;
  1713. if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
  1714. int err, i = 0;
  1715. int sblk_num = cp->status_blk_num;
  1716. u32 base = ((sblk_num - 1) * BNX2_HC_SB_CONFIG_SIZE) +
  1717. BNX2_HC_SB_CONFIG_1;
  1718. CNIC_WR(dev, base, BNX2_HC_SB_CONFIG_1_ONE_SHOT);
  1719. CNIC_WR(dev, base + BNX2_HC_COMP_PROD_TRIP_OFF, (2 << 16) | 8);
  1720. CNIC_WR(dev, base + BNX2_HC_COM_TICKS_OFF, (64 << 16) | 220);
  1721. CNIC_WR(dev, base + BNX2_HC_CMD_TICKS_OFF, (64 << 16) | 220);
  1722. cp->bnx2_status_blk = cp->status_blk;
  1723. cp->last_status_idx = cp->bnx2_status_blk->status_idx;
  1724. tasklet_init(&cp->cnic_irq_task, &cnic_service_bnx2_msix,
  1725. (unsigned long) dev);
  1726. err = request_irq(ethdev->irq_arr[0].vector, cnic_irq, 0,
  1727. "cnic", dev);
  1728. if (err) {
  1729. tasklet_disable(&cp->cnic_irq_task);
  1730. return err;
  1731. }
  1732. while (cp->bnx2_status_blk->status_completion_producer_index &&
  1733. i < 10) {
  1734. CNIC_WR(dev, BNX2_HC_COALESCE_NOW,
  1735. 1 << (11 + sblk_num));
  1736. udelay(10);
  1737. i++;
  1738. barrier();
  1739. }
  1740. if (cp->bnx2_status_blk->status_completion_producer_index) {
  1741. cnic_free_irq(dev);
  1742. goto failed;
  1743. }
  1744. } else {
  1745. struct status_block *sblk = cp->status_blk;
  1746. u32 hc_cmd = CNIC_RD(dev, BNX2_HC_COMMAND);
  1747. int i = 0;
  1748. while (sblk->status_completion_producer_index && i < 10) {
  1749. CNIC_WR(dev, BNX2_HC_COMMAND,
  1750. hc_cmd | BNX2_HC_COMMAND_COAL_NOW_WO_INT);
  1751. udelay(10);
  1752. i++;
  1753. barrier();
  1754. }
  1755. if (sblk->status_completion_producer_index)
  1756. goto failed;
  1757. }
  1758. return 0;
  1759. failed:
  1760. printk(KERN_ERR PFX "%s: " "KCQ index not resetting to 0.\n",
  1761. dev->netdev->name);
  1762. return -EBUSY;
  1763. }
  1764. static void cnic_enable_bnx2_int(struct cnic_dev *dev)
  1765. {
  1766. struct cnic_local *cp = dev->cnic_priv;
  1767. struct cnic_eth_dev *ethdev = cp->ethdev;
  1768. if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
  1769. return;
  1770. CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
  1771. BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
  1772. }
  1773. static void cnic_disable_bnx2_int_sync(struct cnic_dev *dev)
  1774. {
  1775. struct cnic_local *cp = dev->cnic_priv;
  1776. struct cnic_eth_dev *ethdev = cp->ethdev;
  1777. if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
  1778. return;
  1779. CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
  1780. BNX2_PCICFG_INT_ACK_CMD_MASK_INT);
  1781. CNIC_RD(dev, BNX2_PCICFG_INT_ACK_CMD);
  1782. synchronize_irq(ethdev->irq_arr[0].vector);
  1783. }
  1784. static void cnic_init_bnx2_tx_ring(struct cnic_dev *dev)
  1785. {
  1786. struct cnic_local *cp = dev->cnic_priv;
  1787. struct cnic_eth_dev *ethdev = cp->ethdev;
  1788. u32 cid_addr, tx_cid, sb_id;
  1789. u32 val, offset0, offset1, offset2, offset3;
  1790. int i;
  1791. struct tx_bd *txbd;
  1792. dma_addr_t buf_map;
  1793. struct status_block *s_blk = cp->status_blk;
  1794. sb_id = cp->status_blk_num;
  1795. tx_cid = 20;
  1796. cnic_init_context(dev, tx_cid);
  1797. cnic_init_context(dev, tx_cid + 1);
  1798. cp->tx_cons_ptr = &s_blk->status_tx_quick_consumer_index2;
  1799. if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
  1800. struct status_block_msix *sblk = cp->status_blk;
  1801. tx_cid = TX_TSS_CID + sb_id - 1;
  1802. cnic_init_context(dev, tx_cid);
  1803. CNIC_WR(dev, BNX2_TSCH_TSS_CFG, (sb_id << 24) |
  1804. (TX_TSS_CID << 7));
  1805. cp->tx_cons_ptr = &sblk->status_tx_quick_consumer_index;
  1806. }
  1807. cp->tx_cons = *cp->tx_cons_ptr;
  1808. cid_addr = GET_CID_ADDR(tx_cid);
  1809. if (CHIP_NUM(cp) == CHIP_NUM_5709) {
  1810. u32 cid_addr2 = GET_CID_ADDR(tx_cid + 4) + 0x40;
  1811. for (i = 0; i < PHY_CTX_SIZE; i += 4)
  1812. cnic_ctx_wr(dev, cid_addr2, i, 0);
  1813. offset0 = BNX2_L2CTX_TYPE_XI;
  1814. offset1 = BNX2_L2CTX_CMD_TYPE_XI;
  1815. offset2 = BNX2_L2CTX_TBDR_BHADDR_HI_XI;
  1816. offset3 = BNX2_L2CTX_TBDR_BHADDR_LO_XI;
  1817. } else {
  1818. offset0 = BNX2_L2CTX_TYPE;
  1819. offset1 = BNX2_L2CTX_CMD_TYPE;
  1820. offset2 = BNX2_L2CTX_TBDR_BHADDR_HI;
  1821. offset3 = BNX2_L2CTX_TBDR_BHADDR_LO;
  1822. }
  1823. val = BNX2_L2CTX_TYPE_TYPE_L2 | BNX2_L2CTX_TYPE_SIZE_L2;
  1824. cnic_ctx_wr(dev, cid_addr, offset0, val);
  1825. val = BNX2_L2CTX_CMD_TYPE_TYPE_L2 | (8 << 16);
  1826. cnic_ctx_wr(dev, cid_addr, offset1, val);
  1827. txbd = (struct tx_bd *) cp->l2_ring;
  1828. buf_map = cp->l2_buf_map;
  1829. for (i = 0; i < MAX_TX_DESC_CNT; i++, txbd++) {
  1830. txbd->tx_bd_haddr_hi = (u64) buf_map >> 32;
  1831. txbd->tx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
  1832. }
  1833. val = (u64) cp->l2_ring_map >> 32;
  1834. cnic_ctx_wr(dev, cid_addr, offset2, val);
  1835. txbd->tx_bd_haddr_hi = val;
  1836. val = (u64) cp->l2_ring_map & 0xffffffff;
  1837. cnic_ctx_wr(dev, cid_addr, offset3, val);
  1838. txbd->tx_bd_haddr_lo = val;
  1839. }
  1840. static void cnic_init_bnx2_rx_ring(struct cnic_dev *dev)
  1841. {
  1842. struct cnic_local *cp = dev->cnic_priv;
  1843. struct cnic_eth_dev *ethdev = cp->ethdev;
  1844. u32 cid_addr, sb_id, val, coal_reg, coal_val;
  1845. int i;
  1846. struct rx_bd *rxbd;
  1847. struct status_block *s_blk = cp->status_blk;
  1848. sb_id = cp->status_blk_num;
  1849. cnic_init_context(dev, 2);
  1850. cp->rx_cons_ptr = &s_blk->status_rx_quick_consumer_index2;
  1851. coal_reg = BNX2_HC_COMMAND;
  1852. coal_val = CNIC_RD(dev, coal_reg);
  1853. if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
  1854. struct status_block_msix *sblk = cp->status_blk;
  1855. cp->rx_cons_ptr = &sblk->status_rx_quick_consumer_index;
  1856. coal_reg = BNX2_HC_COALESCE_NOW;
  1857. coal_val = 1 << (11 + sb_id);
  1858. }
  1859. i = 0;
  1860. while (!(*cp->rx_cons_ptr != 0) && i < 10) {
  1861. CNIC_WR(dev, coal_reg, coal_val);
  1862. udelay(10);
  1863. i++;
  1864. barrier();
  1865. }
  1866. cp->rx_cons = *cp->rx_cons_ptr;
  1867. cid_addr = GET_CID_ADDR(2);
  1868. val = BNX2_L2CTX_CTX_TYPE_CTX_BD_CHN_TYPE_VALUE |
  1869. BNX2_L2CTX_CTX_TYPE_SIZE_L2 | (0x02 << 8);
  1870. cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_CTX_TYPE, val);
  1871. if (sb_id == 0)
  1872. val = 2 << BNX2_L2CTX_STATUSB_NUM_SHIFT;
  1873. else
  1874. val = BNX2_L2CTX_STATUSB_NUM(sb_id);
  1875. cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_HOST_BDIDX, val);
  1876. rxbd = (struct rx_bd *) (cp->l2_ring + BCM_PAGE_SIZE);
  1877. for (i = 0; i < MAX_RX_DESC_CNT; i++, rxbd++) {
  1878. dma_addr_t buf_map;
  1879. int n = (i % cp->l2_rx_ring_size) + 1;
  1880. buf_map = cp->l2_buf_map + (n * cp->l2_single_buf_size);
  1881. rxbd->rx_bd_len = cp->l2_single_buf_size;
  1882. rxbd->rx_bd_flags = RX_BD_FLAGS_START | RX_BD_FLAGS_END;
  1883. rxbd->rx_bd_haddr_hi = (u64) buf_map >> 32;
  1884. rxbd->rx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
  1885. }
  1886. val = (u64) (cp->l2_ring_map + BCM_PAGE_SIZE) >> 32;
  1887. cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_HI, val);
  1888. rxbd->rx_bd_haddr_hi = val;
  1889. val = (u64) (cp->l2_ring_map + BCM_PAGE_SIZE) & 0xffffffff;
  1890. cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_LO, val);
  1891. rxbd->rx_bd_haddr_lo = val;
  1892. val = cnic_reg_rd_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD);
  1893. cnic_reg_wr_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD, val | (1 << 2));
  1894. }
  1895. static void cnic_shutdown_bnx2_rx_ring(struct cnic_dev *dev)
  1896. {
  1897. struct kwqe *wqes[1], l2kwqe;
  1898. memset(&l2kwqe, 0, sizeof(l2kwqe));
  1899. wqes[0] = &l2kwqe;
  1900. l2kwqe.kwqe_op_flag = (L2_LAYER_CODE << KWQE_FLAGS_LAYER_SHIFT) |
  1901. (L2_KWQE_OPCODE_VALUE_FLUSH <<
  1902. KWQE_OPCODE_SHIFT) | 2;
  1903. dev->submit_kwqes(dev, wqes, 1);
  1904. }
  1905. static void cnic_set_bnx2_mac(struct cnic_dev *dev)
  1906. {
  1907. struct cnic_local *cp = dev->cnic_priv;
  1908. u32 val;
  1909. val = cp->func << 2;
  1910. cp->shmem_base = cnic_reg_rd_ind(dev, BNX2_SHM_HDR_ADDR_0 + val);
  1911. val = cnic_reg_rd_ind(dev, cp->shmem_base +
  1912. BNX2_PORT_HW_CFG_ISCSI_MAC_UPPER);
  1913. dev->mac_addr[0] = (u8) (val >> 8);
  1914. dev->mac_addr[1] = (u8) val;
  1915. CNIC_WR(dev, BNX2_EMAC_MAC_MATCH4, val);
  1916. val = cnic_reg_rd_ind(dev, cp->shmem_base +
  1917. BNX2_PORT_HW_CFG_ISCSI_MAC_LOWER);
  1918. dev->mac_addr[2] = (u8) (val >> 24);
  1919. dev->mac_addr[3] = (u8) (val >> 16);
  1920. dev->mac_addr[4] = (u8) (val >> 8);
  1921. dev->mac_addr[5] = (u8) val;
  1922. CNIC_WR(dev, BNX2_EMAC_MAC_MATCH5, val);
  1923. val = 4 | BNX2_RPM_SORT_USER2_BC_EN;
  1924. if (CHIP_NUM(cp) != CHIP_NUM_5709)
  1925. val |= BNX2_RPM_SORT_USER2_PROM_VLAN;
  1926. CNIC_WR(dev, BNX2_RPM_SORT_USER2, 0x0);
  1927. CNIC_WR(dev, BNX2_RPM_SORT_USER2, val);
  1928. CNIC_WR(dev, BNX2_RPM_SORT_USER2, val | BNX2_RPM_SORT_USER2_ENA);
  1929. }
  1930. static int cnic_start_bnx2_hw(struct cnic_dev *dev)
  1931. {
  1932. struct cnic_local *cp = dev->cnic_priv;
  1933. struct cnic_eth_dev *ethdev = cp->ethdev;
  1934. struct status_block *sblk = cp->status_blk;
  1935. u32 val;
  1936. int err;
  1937. cnic_set_bnx2_mac(dev);
  1938. val = CNIC_RD(dev, BNX2_MQ_CONFIG);
  1939. val &= ~BNX2_MQ_CONFIG_KNL_BYP_BLK_SIZE;
  1940. if (BCM_PAGE_BITS > 12)
  1941. val |= (12 - 8) << 4;
  1942. else
  1943. val |= (BCM_PAGE_BITS - 8) << 4;
  1944. CNIC_WR(dev, BNX2_MQ_CONFIG, val);
  1945. CNIC_WR(dev, BNX2_HC_COMP_PROD_TRIP, (2 << 16) | 8);
  1946. CNIC_WR(dev, BNX2_HC_COM_TICKS, (64 << 16) | 220);
  1947. CNIC_WR(dev, BNX2_HC_CMD_TICKS, (64 << 16) | 220);
  1948. err = cnic_setup_5709_context(dev, 1);
  1949. if (err)
  1950. return err;
  1951. cnic_init_context(dev, KWQ_CID);
  1952. cnic_init_context(dev, KCQ_CID);
  1953. cp->kwq_cid_addr = GET_CID_ADDR(KWQ_CID);
  1954. cp->kwq_io_addr = MB_GET_CID_ADDR(KWQ_CID) + L5_KRNLQ_HOST_QIDX;
  1955. cp->max_kwq_idx = MAX_KWQ_IDX;
  1956. cp->kwq_prod_idx = 0;
  1957. cp->kwq_con_idx = 0;
  1958. cp->cnic_local_flags |= CNIC_LCL_FL_KWQ_INIT;
  1959. if (CHIP_NUM(cp) == CHIP_NUM_5706 || CHIP_NUM(cp) == CHIP_NUM_5708)
  1960. cp->kwq_con_idx_ptr = &sblk->status_rx_quick_consumer_index15;
  1961. else
  1962. cp->kwq_con_idx_ptr = &sblk->status_cmd_consumer_index;
  1963. /* Initialize the kernel work queue context. */
  1964. val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
  1965. (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
  1966. cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_TYPE, val);
  1967. val = (BCM_PAGE_SIZE / sizeof(struct kwqe) - 1) << 16;
  1968. cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
  1969. val = ((BCM_PAGE_SIZE / sizeof(struct kwqe)) << 16) | KWQ_PAGE_CNT;
  1970. cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
  1971. val = (u32) ((u64) cp->kwq_info.pgtbl_map >> 32);
  1972. cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
  1973. val = (u32) cp->kwq_info.pgtbl_map;
  1974. cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
  1975. cp->kcq_cid_addr = GET_CID_ADDR(KCQ_CID);
  1976. cp->kcq_io_addr = MB_GET_CID_ADDR(KCQ_CID) + L5_KRNLQ_HOST_QIDX;
  1977. cp->kcq_prod_idx = 0;
  1978. /* Initialize the kernel complete queue context. */
  1979. val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
  1980. (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
  1981. cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_TYPE, val);
  1982. val = (BCM_PAGE_SIZE / sizeof(struct kcqe) - 1) << 16;
  1983. cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
  1984. val = ((BCM_PAGE_SIZE / sizeof(struct kcqe)) << 16) | KCQ_PAGE_CNT;
  1985. cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
  1986. val = (u32) ((u64) cp->kcq_info.pgtbl_map >> 32);
  1987. cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
  1988. val = (u32) cp->kcq_info.pgtbl_map;
  1989. cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
  1990. cp->int_num = 0;
  1991. if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
  1992. u32 sb_id = cp->status_blk_num;
  1993. u32 sb = BNX2_L2CTX_STATUSB_NUM(sb_id);
  1994. cp->int_num = sb_id << BNX2_PCICFG_INT_ACK_CMD_INT_NUM_SHIFT;
  1995. cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
  1996. cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
  1997. }
  1998. /* Enable Commnad Scheduler notification when we write to the
  1999. * host producer index of the kernel contexts. */
  2000. CNIC_WR(dev, BNX2_MQ_KNL_CMD_MASK1, 2);
  2001. /* Enable Command Scheduler notification when we write to either
  2002. * the Send Queue or Receive Queue producer indexes of the kernel
  2003. * bypass contexts. */
  2004. CNIC_WR(dev, BNX2_MQ_KNL_BYP_CMD_MASK1, 7);
  2005. CNIC_WR(dev, BNX2_MQ_KNL_BYP_WRITE_MASK1, 7);
  2006. /* Notify COM when the driver post an application buffer. */
  2007. CNIC_WR(dev, BNX2_MQ_KNL_RX_V2P_MASK2, 0x2000);
  2008. /* Set the CP and COM doorbells. These two processors polls the
  2009. * doorbell for a non zero value before running. This must be done
  2010. * after setting up the kernel queue contexts. */
  2011. cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 1);
  2012. cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 1);
  2013. cnic_init_bnx2_tx_ring(dev);
  2014. cnic_init_bnx2_rx_ring(dev);
  2015. err = cnic_init_bnx2_irq(dev);
  2016. if (err) {
  2017. printk(KERN_ERR PFX "%s: cnic_init_irq failed\n",
  2018. dev->netdev->name);
  2019. cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
  2020. cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
  2021. return err;
  2022. }
  2023. return 0;
  2024. }
  2025. static void cnic_init_rings(struct cnic_dev *dev)
  2026. {
  2027. if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
  2028. cnic_init_bnx2_tx_ring(dev);
  2029. cnic_init_bnx2_rx_ring(dev);
  2030. }
  2031. }
  2032. static void cnic_shutdown_rings(struct cnic_dev *dev)
  2033. {
  2034. if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
  2035. cnic_shutdown_bnx2_rx_ring(dev);
  2036. }
  2037. }
  2038. static int cnic_register_netdev(struct cnic_dev *dev)
  2039. {
  2040. struct cnic_local *cp = dev->cnic_priv;
  2041. struct cnic_eth_dev *ethdev = cp->ethdev;
  2042. int err;
  2043. if (!ethdev)
  2044. return -ENODEV;
  2045. if (ethdev->drv_state & CNIC_DRV_STATE_REGD)
  2046. return 0;
  2047. err = ethdev->drv_register_cnic(dev->netdev, cp->cnic_ops, dev);
  2048. if (err)
  2049. printk(KERN_ERR PFX "%s: register_cnic failed\n",
  2050. dev->netdev->name);
  2051. return err;
  2052. }
  2053. static void cnic_unregister_netdev(struct cnic_dev *dev)
  2054. {
  2055. struct cnic_local *cp = dev->cnic_priv;
  2056. struct cnic_eth_dev *ethdev = cp->ethdev;
  2057. if (!ethdev)
  2058. return;
  2059. ethdev->drv_unregister_cnic(dev->netdev);
  2060. }
  2061. static int cnic_start_hw(struct cnic_dev *dev)
  2062. {
  2063. struct cnic_local *cp = dev->cnic_priv;
  2064. struct cnic_eth_dev *ethdev = cp->ethdev;
  2065. int err;
  2066. if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
  2067. return -EALREADY;
  2068. dev->regview = ethdev->io_base;
  2069. cp->chip_id = ethdev->chip_id;
  2070. pci_dev_get(dev->pcidev);
  2071. cp->func = PCI_FUNC(dev->pcidev->devfn);
  2072. cp->status_blk = ethdev->irq_arr[0].status_blk;
  2073. cp->status_blk_num = ethdev->irq_arr[0].status_blk_num;
  2074. err = cp->alloc_resc(dev);
  2075. if (err) {
  2076. printk(KERN_ERR PFX "%s: allocate resource failure\n",
  2077. dev->netdev->name);
  2078. goto err1;
  2079. }
  2080. err = cp->start_hw(dev);
  2081. if (err)
  2082. goto err1;
  2083. err = cnic_cm_open(dev);
  2084. if (err)
  2085. goto err1;
  2086. set_bit(CNIC_F_CNIC_UP, &dev->flags);
  2087. cp->enable_int(dev);
  2088. return 0;
  2089. err1:
  2090. cp->free_resc(dev);
  2091. pci_dev_put(dev->pcidev);
  2092. return err;
  2093. }
  2094. static void cnic_stop_bnx2_hw(struct cnic_dev *dev)
  2095. {
  2096. cnic_disable_bnx2_int_sync(dev);
  2097. cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
  2098. cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
  2099. cnic_init_context(dev, KWQ_CID);
  2100. cnic_init_context(dev, KCQ_CID);
  2101. cnic_setup_5709_context(dev, 0);
  2102. cnic_free_irq(dev);
  2103. cnic_free_resc(dev);
  2104. }
  2105. static void cnic_stop_hw(struct cnic_dev *dev)
  2106. {
  2107. if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
  2108. struct cnic_local *cp = dev->cnic_priv;
  2109. clear_bit(CNIC_F_CNIC_UP, &dev->flags);
  2110. rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], NULL);
  2111. synchronize_rcu();
  2112. cnic_cm_shutdown(dev);
  2113. cp->stop_hw(dev);
  2114. pci_dev_put(dev->pcidev);
  2115. }
  2116. }
  2117. static void cnic_free_dev(struct cnic_dev *dev)
  2118. {
  2119. int i = 0;
  2120. while ((atomic_read(&dev->ref_count) != 0) && i < 10) {
  2121. msleep(100);
  2122. i++;
  2123. }
  2124. if (atomic_read(&dev->ref_count) != 0)
  2125. printk(KERN_ERR PFX "%s: Failed waiting for ref count to go"
  2126. " to zero.\n", dev->netdev->name);
  2127. printk(KERN_INFO PFX "Removed CNIC device: %s\n", dev->netdev->name);
  2128. dev_put(dev->netdev);
  2129. kfree(dev);
  2130. }
  2131. static struct cnic_dev *cnic_alloc_dev(struct net_device *dev,
  2132. struct pci_dev *pdev)
  2133. {
  2134. struct cnic_dev *cdev;
  2135. struct cnic_local *cp;
  2136. int alloc_size;
  2137. alloc_size = sizeof(struct cnic_dev) + sizeof(struct cnic_local);
  2138. cdev = kzalloc(alloc_size , GFP_KERNEL);
  2139. if (cdev == NULL) {
  2140. printk(KERN_ERR PFX "%s: allocate dev struct failure\n",
  2141. dev->name);
  2142. return NULL;
  2143. }
  2144. cdev->netdev = dev;
  2145. cdev->cnic_priv = (char *)cdev + sizeof(struct cnic_dev);
  2146. cdev->register_device = cnic_register_device;
  2147. cdev->unregister_device = cnic_unregister_device;
  2148. cdev->iscsi_nl_msg_recv = cnic_iscsi_nl_msg_recv;
  2149. cp = cdev->cnic_priv;
  2150. cp->dev = cdev;
  2151. cp->uio_dev = -1;
  2152. cp->l2_single_buf_size = 0x400;
  2153. cp->l2_rx_ring_size = 3;
  2154. spin_lock_init(&cp->cnic_ulp_lock);
  2155. printk(KERN_INFO PFX "Added CNIC device: %s\n", dev->name);
  2156. return cdev;
  2157. }
  2158. static struct cnic_dev *init_bnx2_cnic(struct net_device *dev)
  2159. {
  2160. struct pci_dev *pdev;
  2161. struct cnic_dev *cdev;
  2162. struct cnic_local *cp;
  2163. struct cnic_eth_dev *ethdev = NULL;
  2164. struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
  2165. probe = symbol_get(bnx2_cnic_probe);
  2166. if (probe) {
  2167. ethdev = (*probe)(dev);
  2168. symbol_put(bnx2_cnic_probe);
  2169. }
  2170. if (!ethdev)
  2171. return NULL;
  2172. pdev = ethdev->pdev;
  2173. if (!pdev)
  2174. return NULL;
  2175. dev_hold(dev);
  2176. pci_dev_get(pdev);
  2177. if (pdev->device == PCI_DEVICE_ID_NX2_5709 ||
  2178. pdev->device == PCI_DEVICE_ID_NX2_5709S) {
  2179. u8 rev;
  2180. pci_read_config_byte(pdev, PCI_REVISION_ID, &rev);
  2181. if (rev < 0x10) {
  2182. pci_dev_put(pdev);
  2183. goto cnic_err;
  2184. }
  2185. }
  2186. pci_dev_put(pdev);
  2187. cdev = cnic_alloc_dev(dev, pdev);
  2188. if (cdev == NULL)
  2189. goto cnic_err;
  2190. set_bit(CNIC_F_BNX2_CLASS, &cdev->flags);
  2191. cdev->submit_kwqes = cnic_submit_bnx2_kwqes;
  2192. cp = cdev->cnic_priv;
  2193. cp->ethdev = ethdev;
  2194. cdev->pcidev = pdev;
  2195. cp->cnic_ops = &cnic_bnx2_ops;
  2196. cp->start_hw = cnic_start_bnx2_hw;
  2197. cp->stop_hw = cnic_stop_bnx2_hw;
  2198. cp->setup_pgtbl = cnic_setup_page_tbl;
  2199. cp->alloc_resc = cnic_alloc_bnx2_resc;
  2200. cp->free_resc = cnic_free_resc;
  2201. cp->start_cm = cnic_cm_init_bnx2_hw;
  2202. cp->stop_cm = cnic_cm_stop_bnx2_hw;
  2203. cp->enable_int = cnic_enable_bnx2_int;
  2204. cp->disable_int_sync = cnic_disable_bnx2_int_sync;
  2205. cp->close_conn = cnic_close_bnx2_conn;
  2206. cp->next_idx = cnic_bnx2_next_idx;
  2207. cp->hw_idx = cnic_bnx2_hw_idx;
  2208. return cdev;
  2209. cnic_err:
  2210. dev_put(dev);
  2211. return NULL;
  2212. }
  2213. static struct cnic_dev *is_cnic_dev(struct net_device *dev)
  2214. {
  2215. struct ethtool_drvinfo drvinfo;
  2216. struct cnic_dev *cdev = NULL;
  2217. if (dev->ethtool_ops && dev->ethtool_ops->get_drvinfo) {
  2218. memset(&drvinfo, 0, sizeof(drvinfo));
  2219. dev->ethtool_ops->get_drvinfo(dev, &drvinfo);
  2220. if (!strcmp(drvinfo.driver, "bnx2"))
  2221. cdev = init_bnx2_cnic(dev);
  2222. if (cdev) {
  2223. write_lock(&cnic_dev_lock);
  2224. list_add(&cdev->list, &cnic_dev_list);
  2225. write_unlock(&cnic_dev_lock);
  2226. }
  2227. }
  2228. return cdev;
  2229. }
  2230. /**
  2231. * netdev event handler
  2232. */
  2233. static int cnic_netdev_event(struct notifier_block *this, unsigned long event,
  2234. void *ptr)
  2235. {
  2236. struct net_device *netdev = ptr;
  2237. struct cnic_dev *dev;
  2238. int if_type;
  2239. int new_dev = 0;
  2240. dev = cnic_from_netdev(netdev);
  2241. if (!dev && (event == NETDEV_REGISTER || event == NETDEV_UP)) {
  2242. /* Check for the hot-plug device */
  2243. dev = is_cnic_dev(netdev);
  2244. if (dev) {
  2245. new_dev = 1;
  2246. cnic_hold(dev);
  2247. }
  2248. }
  2249. if (dev) {
  2250. struct cnic_local *cp = dev->cnic_priv;
  2251. if (new_dev)
  2252. cnic_ulp_init(dev);
  2253. else if (event == NETDEV_UNREGISTER)
  2254. cnic_ulp_exit(dev);
  2255. if (event == NETDEV_UP) {
  2256. if (cnic_register_netdev(dev) != 0) {
  2257. cnic_put(dev);
  2258. goto done;
  2259. }
  2260. if (!cnic_start_hw(dev))
  2261. cnic_ulp_start(dev);
  2262. }
  2263. rcu_read_lock();
  2264. for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
  2265. struct cnic_ulp_ops *ulp_ops;
  2266. void *ctx;
  2267. ulp_ops = rcu_dereference(cp->ulp_ops[if_type]);
  2268. if (!ulp_ops || !ulp_ops->indicate_netevent)
  2269. continue;
  2270. ctx = cp->ulp_handle[if_type];
  2271. ulp_ops->indicate_netevent(ctx, event);
  2272. }
  2273. rcu_read_unlock();
  2274. if (event == NETDEV_GOING_DOWN) {
  2275. cnic_ulp_stop(dev);
  2276. cnic_stop_hw(dev);
  2277. cnic_unregister_netdev(dev);
  2278. } else if (event == NETDEV_UNREGISTER) {
  2279. write_lock(&cnic_dev_lock);
  2280. list_del_init(&dev->list);
  2281. write_unlock(&cnic_dev_lock);
  2282. cnic_put(dev);
  2283. cnic_free_dev(dev);
  2284. goto done;
  2285. }
  2286. cnic_put(dev);
  2287. }
  2288. done:
  2289. return NOTIFY_DONE;
  2290. }
  2291. static struct notifier_block cnic_netdev_notifier = {
  2292. .notifier_call = cnic_netdev_event
  2293. };
  2294. static void cnic_release(void)
  2295. {
  2296. struct cnic_dev *dev;
  2297. while (!list_empty(&cnic_dev_list)) {
  2298. dev = list_entry(cnic_dev_list.next, struct cnic_dev, list);
  2299. if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
  2300. cnic_ulp_stop(dev);
  2301. cnic_stop_hw(dev);
  2302. }
  2303. cnic_ulp_exit(dev);
  2304. cnic_unregister_netdev(dev);
  2305. list_del_init(&dev->list);
  2306. cnic_free_dev(dev);
  2307. }
  2308. }
  2309. static int __init cnic_init(void)
  2310. {
  2311. int rc = 0;
  2312. printk(KERN_INFO "%s", version);
  2313. rc = register_netdevice_notifier(&cnic_netdev_notifier);
  2314. if (rc) {
  2315. cnic_release();
  2316. return rc;
  2317. }
  2318. return 0;
  2319. }
  2320. static void __exit cnic_exit(void)
  2321. {
  2322. unregister_netdevice_notifier(&cnic_netdev_notifier);
  2323. cnic_release();
  2324. return;
  2325. }
  2326. module_init(cnic_init);
  2327. module_exit(cnic_exit);