qla3xxx.c 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972
  1. /*
  2. * QLogic QLA3xxx NIC HBA Driver
  3. * Copyright (c) 2003-2006 QLogic Corporation
  4. *
  5. * See LICENSE.qla3xxx for copyright and licensing details.
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/types.h>
  11. #include <linux/module.h>
  12. #include <linux/list.h>
  13. #include <linux/pci.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/sched.h>
  16. #include <linux/slab.h>
  17. #include <linux/dmapool.h>
  18. #include <linux/mempool.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/kthread.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/errno.h>
  23. #include <linux/ioport.h>
  24. #include <linux/ip.h>
  25. #include <linux/in.h>
  26. #include <linux/if_arp.h>
  27. #include <linux/if_ether.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/etherdevice.h>
  30. #include <linux/ethtool.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/rtnetlink.h>
  33. #include <linux/if_vlan.h>
  34. #include <linux/delay.h>
  35. #include <linux/mm.h>
  36. #include <linux/prefetch.h>
  37. #include "qla3xxx.h"
  38. #define DRV_NAME "qla3xxx"
  39. #define DRV_STRING "QLogic ISP3XXX Network Driver"
  40. #define DRV_VERSION "v2.03.00-k5"
  41. static const char ql3xxx_driver_name[] = DRV_NAME;
  42. static const char ql3xxx_driver_version[] = DRV_VERSION;
  43. #define TIMED_OUT_MSG \
  44. "Timed out waiting for management port to get free before issuing command\n"
  45. MODULE_AUTHOR("QLogic Corporation");
  46. MODULE_DESCRIPTION("QLogic ISP3XXX Network Driver " DRV_VERSION " ");
  47. MODULE_LICENSE("GPL");
  48. MODULE_VERSION(DRV_VERSION);
  49. static const u32 default_msg
  50. = NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK
  51. | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN;
  52. static int debug = -1; /* defaults above */
  53. module_param(debug, int, 0);
  54. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  55. static int msi;
  56. module_param(msi, int, 0);
  57. MODULE_PARM_DESC(msi, "Turn on Message Signaled Interrupts.");
  58. static DEFINE_PCI_DEVICE_TABLE(ql3xxx_pci_tbl) = {
  59. {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, QL3022_DEVICE_ID)},
  60. {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, QL3032_DEVICE_ID)},
  61. /* required last entry */
  62. {0,}
  63. };
  64. MODULE_DEVICE_TABLE(pci, ql3xxx_pci_tbl);
  65. /*
  66. * These are the known PHY's which are used
  67. */
  68. enum PHY_DEVICE_TYPE {
  69. PHY_TYPE_UNKNOWN = 0,
  70. PHY_VITESSE_VSC8211,
  71. PHY_AGERE_ET1011C,
  72. MAX_PHY_DEV_TYPES
  73. };
  74. struct PHY_DEVICE_INFO {
  75. const enum PHY_DEVICE_TYPE phyDevice;
  76. const u32 phyIdOUI;
  77. const u16 phyIdModel;
  78. const char *name;
  79. };
  80. static const struct PHY_DEVICE_INFO PHY_DEVICES[] = {
  81. {PHY_TYPE_UNKNOWN, 0x000000, 0x0, "PHY_TYPE_UNKNOWN"},
  82. {PHY_VITESSE_VSC8211, 0x0003f1, 0xb, "PHY_VITESSE_VSC8211"},
  83. {PHY_AGERE_ET1011C, 0x00a0bc, 0x1, "PHY_AGERE_ET1011C"},
  84. };
  85. /*
  86. * Caller must take hw_lock.
  87. */
  88. static int ql_sem_spinlock(struct ql3_adapter *qdev,
  89. u32 sem_mask, u32 sem_bits)
  90. {
  91. struct ql3xxx_port_registers __iomem *port_regs =
  92. qdev->mem_map_registers;
  93. u32 value;
  94. unsigned int seconds = 3;
  95. do {
  96. writel((sem_mask | sem_bits),
  97. &port_regs->CommonRegs.semaphoreReg);
  98. value = readl(&port_regs->CommonRegs.semaphoreReg);
  99. if ((value & (sem_mask >> 16)) == sem_bits)
  100. return 0;
  101. ssleep(1);
  102. } while (--seconds);
  103. return -1;
  104. }
  105. static void ql_sem_unlock(struct ql3_adapter *qdev, u32 sem_mask)
  106. {
  107. struct ql3xxx_port_registers __iomem *port_regs =
  108. qdev->mem_map_registers;
  109. writel(sem_mask, &port_regs->CommonRegs.semaphoreReg);
  110. readl(&port_regs->CommonRegs.semaphoreReg);
  111. }
  112. static int ql_sem_lock(struct ql3_adapter *qdev, u32 sem_mask, u32 sem_bits)
  113. {
  114. struct ql3xxx_port_registers __iomem *port_regs =
  115. qdev->mem_map_registers;
  116. u32 value;
  117. writel((sem_mask | sem_bits), &port_regs->CommonRegs.semaphoreReg);
  118. value = readl(&port_regs->CommonRegs.semaphoreReg);
  119. return ((value & (sem_mask >> 16)) == sem_bits);
  120. }
  121. /*
  122. * Caller holds hw_lock.
  123. */
  124. static int ql_wait_for_drvr_lock(struct ql3_adapter *qdev)
  125. {
  126. int i = 0;
  127. while (i < 10) {
  128. if (i)
  129. ssleep(1);
  130. if (ql_sem_lock(qdev,
  131. QL_DRVR_SEM_MASK,
  132. (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index)
  133. * 2) << 1)) {
  134. netdev_printk(KERN_DEBUG, qdev->ndev,
  135. "driver lock acquired\n");
  136. return 1;
  137. }
  138. }
  139. netdev_err(qdev->ndev, "Timed out waiting for driver lock...\n");
  140. return 0;
  141. }
  142. static void ql_set_register_page(struct ql3_adapter *qdev, u32 page)
  143. {
  144. struct ql3xxx_port_registers __iomem *port_regs =
  145. qdev->mem_map_registers;
  146. writel(((ISP_CONTROL_NP_MASK << 16) | page),
  147. &port_regs->CommonRegs.ispControlStatus);
  148. readl(&port_regs->CommonRegs.ispControlStatus);
  149. qdev->current_page = page;
  150. }
  151. static u32 ql_read_common_reg_l(struct ql3_adapter *qdev, u32 __iomem *reg)
  152. {
  153. u32 value;
  154. unsigned long hw_flags;
  155. spin_lock_irqsave(&qdev->hw_lock, hw_flags);
  156. value = readl(reg);
  157. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  158. return value;
  159. }
  160. static u32 ql_read_common_reg(struct ql3_adapter *qdev, u32 __iomem *reg)
  161. {
  162. return readl(reg);
  163. }
  164. static u32 ql_read_page0_reg_l(struct ql3_adapter *qdev, u32 __iomem *reg)
  165. {
  166. u32 value;
  167. unsigned long hw_flags;
  168. spin_lock_irqsave(&qdev->hw_lock, hw_flags);
  169. if (qdev->current_page != 0)
  170. ql_set_register_page(qdev, 0);
  171. value = readl(reg);
  172. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  173. return value;
  174. }
  175. static u32 ql_read_page0_reg(struct ql3_adapter *qdev, u32 __iomem *reg)
  176. {
  177. if (qdev->current_page != 0)
  178. ql_set_register_page(qdev, 0);
  179. return readl(reg);
  180. }
  181. static void ql_write_common_reg_l(struct ql3_adapter *qdev,
  182. u32 __iomem *reg, u32 value)
  183. {
  184. unsigned long hw_flags;
  185. spin_lock_irqsave(&qdev->hw_lock, hw_flags);
  186. writel(value, reg);
  187. readl(reg);
  188. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  189. }
  190. static void ql_write_common_reg(struct ql3_adapter *qdev,
  191. u32 __iomem *reg, u32 value)
  192. {
  193. writel(value, reg);
  194. readl(reg);
  195. }
  196. static void ql_write_nvram_reg(struct ql3_adapter *qdev,
  197. u32 __iomem *reg, u32 value)
  198. {
  199. writel(value, reg);
  200. readl(reg);
  201. udelay(1);
  202. }
  203. static void ql_write_page0_reg(struct ql3_adapter *qdev,
  204. u32 __iomem *reg, u32 value)
  205. {
  206. if (qdev->current_page != 0)
  207. ql_set_register_page(qdev, 0);
  208. writel(value, reg);
  209. readl(reg);
  210. }
  211. /*
  212. * Caller holds hw_lock. Only called during init.
  213. */
  214. static void ql_write_page1_reg(struct ql3_adapter *qdev,
  215. u32 __iomem *reg, u32 value)
  216. {
  217. if (qdev->current_page != 1)
  218. ql_set_register_page(qdev, 1);
  219. writel(value, reg);
  220. readl(reg);
  221. }
  222. /*
  223. * Caller holds hw_lock. Only called during init.
  224. */
  225. static void ql_write_page2_reg(struct ql3_adapter *qdev,
  226. u32 __iomem *reg, u32 value)
  227. {
  228. if (qdev->current_page != 2)
  229. ql_set_register_page(qdev, 2);
  230. writel(value, reg);
  231. readl(reg);
  232. }
  233. static void ql_disable_interrupts(struct ql3_adapter *qdev)
  234. {
  235. struct ql3xxx_port_registers __iomem *port_regs =
  236. qdev->mem_map_registers;
  237. ql_write_common_reg_l(qdev, &port_regs->CommonRegs.ispInterruptMaskReg,
  238. (ISP_IMR_ENABLE_INT << 16));
  239. }
  240. static void ql_enable_interrupts(struct ql3_adapter *qdev)
  241. {
  242. struct ql3xxx_port_registers __iomem *port_regs =
  243. qdev->mem_map_registers;
  244. ql_write_common_reg_l(qdev, &port_regs->CommonRegs.ispInterruptMaskReg,
  245. ((0xff << 16) | ISP_IMR_ENABLE_INT));
  246. }
  247. static void ql_release_to_lrg_buf_free_list(struct ql3_adapter *qdev,
  248. struct ql_rcv_buf_cb *lrg_buf_cb)
  249. {
  250. dma_addr_t map;
  251. int err;
  252. lrg_buf_cb->next = NULL;
  253. if (qdev->lrg_buf_free_tail == NULL) { /* The list is empty */
  254. qdev->lrg_buf_free_head = qdev->lrg_buf_free_tail = lrg_buf_cb;
  255. } else {
  256. qdev->lrg_buf_free_tail->next = lrg_buf_cb;
  257. qdev->lrg_buf_free_tail = lrg_buf_cb;
  258. }
  259. if (!lrg_buf_cb->skb) {
  260. lrg_buf_cb->skb = netdev_alloc_skb(qdev->ndev,
  261. qdev->lrg_buffer_len);
  262. if (unlikely(!lrg_buf_cb->skb)) {
  263. netdev_err(qdev->ndev, "failed netdev_alloc_skb()\n");
  264. qdev->lrg_buf_skb_check++;
  265. } else {
  266. /*
  267. * We save some space to copy the ethhdr from first
  268. * buffer
  269. */
  270. skb_reserve(lrg_buf_cb->skb, QL_HEADER_SPACE);
  271. map = pci_map_single(qdev->pdev,
  272. lrg_buf_cb->skb->data,
  273. qdev->lrg_buffer_len -
  274. QL_HEADER_SPACE,
  275. PCI_DMA_FROMDEVICE);
  276. err = pci_dma_mapping_error(qdev->pdev, map);
  277. if (err) {
  278. netdev_err(qdev->ndev,
  279. "PCI mapping failed with error: %d\n",
  280. err);
  281. dev_kfree_skb(lrg_buf_cb->skb);
  282. lrg_buf_cb->skb = NULL;
  283. qdev->lrg_buf_skb_check++;
  284. return;
  285. }
  286. lrg_buf_cb->buf_phy_addr_low =
  287. cpu_to_le32(LS_64BITS(map));
  288. lrg_buf_cb->buf_phy_addr_high =
  289. cpu_to_le32(MS_64BITS(map));
  290. dma_unmap_addr_set(lrg_buf_cb, mapaddr, map);
  291. dma_unmap_len_set(lrg_buf_cb, maplen,
  292. qdev->lrg_buffer_len -
  293. QL_HEADER_SPACE);
  294. }
  295. }
  296. qdev->lrg_buf_free_count++;
  297. }
  298. static struct ql_rcv_buf_cb *ql_get_from_lrg_buf_free_list(struct ql3_adapter
  299. *qdev)
  300. {
  301. struct ql_rcv_buf_cb *lrg_buf_cb = qdev->lrg_buf_free_head;
  302. if (lrg_buf_cb != NULL) {
  303. qdev->lrg_buf_free_head = lrg_buf_cb->next;
  304. if (qdev->lrg_buf_free_head == NULL)
  305. qdev->lrg_buf_free_tail = NULL;
  306. qdev->lrg_buf_free_count--;
  307. }
  308. return lrg_buf_cb;
  309. }
  310. static u32 addrBits = EEPROM_NO_ADDR_BITS;
  311. static u32 dataBits = EEPROM_NO_DATA_BITS;
  312. static void fm93c56a_deselect(struct ql3_adapter *qdev);
  313. static void eeprom_readword(struct ql3_adapter *qdev, u32 eepromAddr,
  314. unsigned short *value);
  315. /*
  316. * Caller holds hw_lock.
  317. */
  318. static void fm93c56a_select(struct ql3_adapter *qdev)
  319. {
  320. struct ql3xxx_port_registers __iomem *port_regs =
  321. qdev->mem_map_registers;
  322. __iomem u32 *spir = &port_regs->CommonRegs.serialPortInterfaceReg;
  323. qdev->eeprom_cmd_data = AUBURN_EEPROM_CS_1;
  324. ql_write_nvram_reg(qdev, spir, ISP_NVRAM_MASK | qdev->eeprom_cmd_data);
  325. ql_write_nvram_reg(qdev, spir,
  326. ((ISP_NVRAM_MASK << 16) | qdev->eeprom_cmd_data));
  327. }
  328. /*
  329. * Caller holds hw_lock.
  330. */
  331. static void fm93c56a_cmd(struct ql3_adapter *qdev, u32 cmd, u32 eepromAddr)
  332. {
  333. int i;
  334. u32 mask;
  335. u32 dataBit;
  336. u32 previousBit;
  337. struct ql3xxx_port_registers __iomem *port_regs =
  338. qdev->mem_map_registers;
  339. __iomem u32 *spir = &port_regs->CommonRegs.serialPortInterfaceReg;
  340. /* Clock in a zero, then do the start bit */
  341. ql_write_nvram_reg(qdev, spir,
  342. (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
  343. AUBURN_EEPROM_DO_1));
  344. ql_write_nvram_reg(qdev, spir,
  345. (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
  346. AUBURN_EEPROM_DO_1 | AUBURN_EEPROM_CLK_RISE));
  347. ql_write_nvram_reg(qdev, spir,
  348. (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
  349. AUBURN_EEPROM_DO_1 | AUBURN_EEPROM_CLK_FALL));
  350. mask = 1 << (FM93C56A_CMD_BITS - 1);
  351. /* Force the previous data bit to be different */
  352. previousBit = 0xffff;
  353. for (i = 0; i < FM93C56A_CMD_BITS; i++) {
  354. dataBit = (cmd & mask)
  355. ? AUBURN_EEPROM_DO_1
  356. : AUBURN_EEPROM_DO_0;
  357. if (previousBit != dataBit) {
  358. /* If the bit changed, change the DO state to match */
  359. ql_write_nvram_reg(qdev, spir,
  360. (ISP_NVRAM_MASK |
  361. qdev->eeprom_cmd_data | dataBit));
  362. previousBit = dataBit;
  363. }
  364. ql_write_nvram_reg(qdev, spir,
  365. (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
  366. dataBit | AUBURN_EEPROM_CLK_RISE));
  367. ql_write_nvram_reg(qdev, spir,
  368. (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
  369. dataBit | AUBURN_EEPROM_CLK_FALL));
  370. cmd = cmd << 1;
  371. }
  372. mask = 1 << (addrBits - 1);
  373. /* Force the previous data bit to be different */
  374. previousBit = 0xffff;
  375. for (i = 0; i < addrBits; i++) {
  376. dataBit = (eepromAddr & mask) ? AUBURN_EEPROM_DO_1
  377. : AUBURN_EEPROM_DO_0;
  378. if (previousBit != dataBit) {
  379. /*
  380. * If the bit changed, then change the DO state to
  381. * match
  382. */
  383. ql_write_nvram_reg(qdev, spir,
  384. (ISP_NVRAM_MASK |
  385. qdev->eeprom_cmd_data | dataBit));
  386. previousBit = dataBit;
  387. }
  388. ql_write_nvram_reg(qdev, spir,
  389. (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
  390. dataBit | AUBURN_EEPROM_CLK_RISE));
  391. ql_write_nvram_reg(qdev, spir,
  392. (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
  393. dataBit | AUBURN_EEPROM_CLK_FALL));
  394. eepromAddr = eepromAddr << 1;
  395. }
  396. }
  397. /*
  398. * Caller holds hw_lock.
  399. */
  400. static void fm93c56a_deselect(struct ql3_adapter *qdev)
  401. {
  402. struct ql3xxx_port_registers __iomem *port_regs =
  403. qdev->mem_map_registers;
  404. __iomem u32 *spir = &port_regs->CommonRegs.serialPortInterfaceReg;
  405. qdev->eeprom_cmd_data = AUBURN_EEPROM_CS_0;
  406. ql_write_nvram_reg(qdev, spir, ISP_NVRAM_MASK | qdev->eeprom_cmd_data);
  407. }
  408. /*
  409. * Caller holds hw_lock.
  410. */
  411. static void fm93c56a_datain(struct ql3_adapter *qdev, unsigned short *value)
  412. {
  413. int i;
  414. u32 data = 0;
  415. u32 dataBit;
  416. struct ql3xxx_port_registers __iomem *port_regs =
  417. qdev->mem_map_registers;
  418. __iomem u32 *spir = &port_regs->CommonRegs.serialPortInterfaceReg;
  419. /* Read the data bits */
  420. /* The first bit is a dummy. Clock right over it. */
  421. for (i = 0; i < dataBits; i++) {
  422. ql_write_nvram_reg(qdev, spir,
  423. ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
  424. AUBURN_EEPROM_CLK_RISE);
  425. ql_write_nvram_reg(qdev, spir,
  426. ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
  427. AUBURN_EEPROM_CLK_FALL);
  428. dataBit = (ql_read_common_reg(qdev, spir) &
  429. AUBURN_EEPROM_DI_1) ? 1 : 0;
  430. data = (data << 1) | dataBit;
  431. }
  432. *value = (u16)data;
  433. }
  434. /*
  435. * Caller holds hw_lock.
  436. */
  437. static void eeprom_readword(struct ql3_adapter *qdev,
  438. u32 eepromAddr, unsigned short *value)
  439. {
  440. fm93c56a_select(qdev);
  441. fm93c56a_cmd(qdev, (int)FM93C56A_READ, eepromAddr);
  442. fm93c56a_datain(qdev, value);
  443. fm93c56a_deselect(qdev);
  444. }
  445. static void ql_set_mac_addr(struct net_device *ndev, u16 *addr)
  446. {
  447. __le16 *p = (__le16 *)ndev->dev_addr;
  448. p[0] = cpu_to_le16(addr[0]);
  449. p[1] = cpu_to_le16(addr[1]);
  450. p[2] = cpu_to_le16(addr[2]);
  451. }
  452. static int ql_get_nvram_params(struct ql3_adapter *qdev)
  453. {
  454. u16 *pEEPROMData;
  455. u16 checksum = 0;
  456. u32 index;
  457. unsigned long hw_flags;
  458. spin_lock_irqsave(&qdev->hw_lock, hw_flags);
  459. pEEPROMData = (u16 *)&qdev->nvram_data;
  460. qdev->eeprom_cmd_data = 0;
  461. if (ql_sem_spinlock(qdev, QL_NVRAM_SEM_MASK,
  462. (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
  463. 2) << 10)) {
  464. pr_err("%s: Failed ql_sem_spinlock()\n", __func__);
  465. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  466. return -1;
  467. }
  468. for (index = 0; index < EEPROM_SIZE; index++) {
  469. eeprom_readword(qdev, index, pEEPROMData);
  470. checksum += *pEEPROMData;
  471. pEEPROMData++;
  472. }
  473. ql_sem_unlock(qdev, QL_NVRAM_SEM_MASK);
  474. if (checksum != 0) {
  475. netdev_err(qdev->ndev, "checksum should be zero, is %x!!\n",
  476. checksum);
  477. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  478. return -1;
  479. }
  480. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  481. return checksum;
  482. }
  483. static const u32 PHYAddr[2] = {
  484. PORT0_PHY_ADDRESS, PORT1_PHY_ADDRESS
  485. };
  486. static int ql_wait_for_mii_ready(struct ql3_adapter *qdev)
  487. {
  488. struct ql3xxx_port_registers __iomem *port_regs =
  489. qdev->mem_map_registers;
  490. u32 temp;
  491. int count = 1000;
  492. while (count) {
  493. temp = ql_read_page0_reg(qdev, &port_regs->macMIIStatusReg);
  494. if (!(temp & MAC_MII_STATUS_BSY))
  495. return 0;
  496. udelay(10);
  497. count--;
  498. }
  499. return -1;
  500. }
  501. static void ql_mii_enable_scan_mode(struct ql3_adapter *qdev)
  502. {
  503. struct ql3xxx_port_registers __iomem *port_regs =
  504. qdev->mem_map_registers;
  505. u32 scanControl;
  506. if (qdev->numPorts > 1) {
  507. /* Auto scan will cycle through multiple ports */
  508. scanControl = MAC_MII_CONTROL_AS | MAC_MII_CONTROL_SC;
  509. } else {
  510. scanControl = MAC_MII_CONTROL_SC;
  511. }
  512. /*
  513. * Scan register 1 of PHY/PETBI,
  514. * Set up to scan both devices
  515. * The autoscan starts from the first register, completes
  516. * the last one before rolling over to the first
  517. */
  518. ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
  519. PHYAddr[0] | MII_SCAN_REGISTER);
  520. ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
  521. (scanControl) |
  522. ((MAC_MII_CONTROL_SC | MAC_MII_CONTROL_AS) << 16));
  523. }
  524. static u8 ql_mii_disable_scan_mode(struct ql3_adapter *qdev)
  525. {
  526. u8 ret;
  527. struct ql3xxx_port_registers __iomem *port_regs =
  528. qdev->mem_map_registers;
  529. /* See if scan mode is enabled before we turn it off */
  530. if (ql_read_page0_reg(qdev, &port_regs->macMIIMgmtControlReg) &
  531. (MAC_MII_CONTROL_AS | MAC_MII_CONTROL_SC)) {
  532. /* Scan is enabled */
  533. ret = 1;
  534. } else {
  535. /* Scan is disabled */
  536. ret = 0;
  537. }
  538. /*
  539. * When disabling scan mode you must first change the MII register
  540. * address
  541. */
  542. ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
  543. PHYAddr[0] | MII_SCAN_REGISTER);
  544. ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
  545. ((MAC_MII_CONTROL_SC | MAC_MII_CONTROL_AS |
  546. MAC_MII_CONTROL_RC) << 16));
  547. return ret;
  548. }
  549. static int ql_mii_write_reg_ex(struct ql3_adapter *qdev,
  550. u16 regAddr, u16 value, u32 phyAddr)
  551. {
  552. struct ql3xxx_port_registers __iomem *port_regs =
  553. qdev->mem_map_registers;
  554. u8 scanWasEnabled;
  555. scanWasEnabled = ql_mii_disable_scan_mode(qdev);
  556. if (ql_wait_for_mii_ready(qdev)) {
  557. netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
  558. return -1;
  559. }
  560. ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
  561. phyAddr | regAddr);
  562. ql_write_page0_reg(qdev, &port_regs->macMIIMgmtDataReg, value);
  563. /* Wait for write to complete 9/10/04 SJP */
  564. if (ql_wait_for_mii_ready(qdev)) {
  565. netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
  566. return -1;
  567. }
  568. if (scanWasEnabled)
  569. ql_mii_enable_scan_mode(qdev);
  570. return 0;
  571. }
  572. static int ql_mii_read_reg_ex(struct ql3_adapter *qdev, u16 regAddr,
  573. u16 *value, u32 phyAddr)
  574. {
  575. struct ql3xxx_port_registers __iomem *port_regs =
  576. qdev->mem_map_registers;
  577. u8 scanWasEnabled;
  578. u32 temp;
  579. scanWasEnabled = ql_mii_disable_scan_mode(qdev);
  580. if (ql_wait_for_mii_ready(qdev)) {
  581. netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
  582. return -1;
  583. }
  584. ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
  585. phyAddr | regAddr);
  586. ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
  587. (MAC_MII_CONTROL_RC << 16));
  588. ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
  589. (MAC_MII_CONTROL_RC << 16) | MAC_MII_CONTROL_RC);
  590. /* Wait for the read to complete */
  591. if (ql_wait_for_mii_ready(qdev)) {
  592. netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
  593. return -1;
  594. }
  595. temp = ql_read_page0_reg(qdev, &port_regs->macMIIMgmtDataReg);
  596. *value = (u16) temp;
  597. if (scanWasEnabled)
  598. ql_mii_enable_scan_mode(qdev);
  599. return 0;
  600. }
  601. static int ql_mii_write_reg(struct ql3_adapter *qdev, u16 regAddr, u16 value)
  602. {
  603. struct ql3xxx_port_registers __iomem *port_regs =
  604. qdev->mem_map_registers;
  605. ql_mii_disable_scan_mode(qdev);
  606. if (ql_wait_for_mii_ready(qdev)) {
  607. netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
  608. return -1;
  609. }
  610. ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
  611. qdev->PHYAddr | regAddr);
  612. ql_write_page0_reg(qdev, &port_regs->macMIIMgmtDataReg, value);
  613. /* Wait for write to complete. */
  614. if (ql_wait_for_mii_ready(qdev)) {
  615. netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
  616. return -1;
  617. }
  618. ql_mii_enable_scan_mode(qdev);
  619. return 0;
  620. }
  621. static int ql_mii_read_reg(struct ql3_adapter *qdev, u16 regAddr, u16 *value)
  622. {
  623. u32 temp;
  624. struct ql3xxx_port_registers __iomem *port_regs =
  625. qdev->mem_map_registers;
  626. ql_mii_disable_scan_mode(qdev);
  627. if (ql_wait_for_mii_ready(qdev)) {
  628. netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
  629. return -1;
  630. }
  631. ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
  632. qdev->PHYAddr | regAddr);
  633. ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
  634. (MAC_MII_CONTROL_RC << 16));
  635. ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
  636. (MAC_MII_CONTROL_RC << 16) | MAC_MII_CONTROL_RC);
  637. /* Wait for the read to complete */
  638. if (ql_wait_for_mii_ready(qdev)) {
  639. netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
  640. return -1;
  641. }
  642. temp = ql_read_page0_reg(qdev, &port_regs->macMIIMgmtDataReg);
  643. *value = (u16) temp;
  644. ql_mii_enable_scan_mode(qdev);
  645. return 0;
  646. }
  647. static void ql_petbi_reset(struct ql3_adapter *qdev)
  648. {
  649. ql_mii_write_reg(qdev, PETBI_CONTROL_REG, PETBI_CTRL_SOFT_RESET);
  650. }
  651. static void ql_petbi_start_neg(struct ql3_adapter *qdev)
  652. {
  653. u16 reg;
  654. /* Enable Auto-negotiation sense */
  655. ql_mii_read_reg(qdev, PETBI_TBI_CTRL, &reg);
  656. reg |= PETBI_TBI_AUTO_SENSE;
  657. ql_mii_write_reg(qdev, PETBI_TBI_CTRL, reg);
  658. ql_mii_write_reg(qdev, PETBI_NEG_ADVER,
  659. PETBI_NEG_PAUSE | PETBI_NEG_DUPLEX);
  660. ql_mii_write_reg(qdev, PETBI_CONTROL_REG,
  661. PETBI_CTRL_AUTO_NEG | PETBI_CTRL_RESTART_NEG |
  662. PETBI_CTRL_FULL_DUPLEX | PETBI_CTRL_SPEED_1000);
  663. }
  664. static void ql_petbi_reset_ex(struct ql3_adapter *qdev)
  665. {
  666. ql_mii_write_reg_ex(qdev, PETBI_CONTROL_REG, PETBI_CTRL_SOFT_RESET,
  667. PHYAddr[qdev->mac_index]);
  668. }
  669. static void ql_petbi_start_neg_ex(struct ql3_adapter *qdev)
  670. {
  671. u16 reg;
  672. /* Enable Auto-negotiation sense */
  673. ql_mii_read_reg_ex(qdev, PETBI_TBI_CTRL, &reg,
  674. PHYAddr[qdev->mac_index]);
  675. reg |= PETBI_TBI_AUTO_SENSE;
  676. ql_mii_write_reg_ex(qdev, PETBI_TBI_CTRL, reg,
  677. PHYAddr[qdev->mac_index]);
  678. ql_mii_write_reg_ex(qdev, PETBI_NEG_ADVER,
  679. PETBI_NEG_PAUSE | PETBI_NEG_DUPLEX,
  680. PHYAddr[qdev->mac_index]);
  681. ql_mii_write_reg_ex(qdev, PETBI_CONTROL_REG,
  682. PETBI_CTRL_AUTO_NEG | PETBI_CTRL_RESTART_NEG |
  683. PETBI_CTRL_FULL_DUPLEX | PETBI_CTRL_SPEED_1000,
  684. PHYAddr[qdev->mac_index]);
  685. }
  686. static void ql_petbi_init(struct ql3_adapter *qdev)
  687. {
  688. ql_petbi_reset(qdev);
  689. ql_petbi_start_neg(qdev);
  690. }
  691. static void ql_petbi_init_ex(struct ql3_adapter *qdev)
  692. {
  693. ql_petbi_reset_ex(qdev);
  694. ql_petbi_start_neg_ex(qdev);
  695. }
  696. static int ql_is_petbi_neg_pause(struct ql3_adapter *qdev)
  697. {
  698. u16 reg;
  699. if (ql_mii_read_reg(qdev, PETBI_NEG_PARTNER, &reg) < 0)
  700. return 0;
  701. return (reg & PETBI_NEG_PAUSE_MASK) == PETBI_NEG_PAUSE;
  702. }
  703. static void phyAgereSpecificInit(struct ql3_adapter *qdev, u32 miiAddr)
  704. {
  705. netdev_info(qdev->ndev, "enabling Agere specific PHY\n");
  706. /* power down device bit 11 = 1 */
  707. ql_mii_write_reg_ex(qdev, 0x00, 0x1940, miiAddr);
  708. /* enable diagnostic mode bit 2 = 1 */
  709. ql_mii_write_reg_ex(qdev, 0x12, 0x840e, miiAddr);
  710. /* 1000MB amplitude adjust (see Agere errata) */
  711. ql_mii_write_reg_ex(qdev, 0x10, 0x8805, miiAddr);
  712. /* 1000MB amplitude adjust (see Agere errata) */
  713. ql_mii_write_reg_ex(qdev, 0x11, 0xf03e, miiAddr);
  714. /* 100MB amplitude adjust (see Agere errata) */
  715. ql_mii_write_reg_ex(qdev, 0x10, 0x8806, miiAddr);
  716. /* 100MB amplitude adjust (see Agere errata) */
  717. ql_mii_write_reg_ex(qdev, 0x11, 0x003e, miiAddr);
  718. /* 10MB amplitude adjust (see Agere errata) */
  719. ql_mii_write_reg_ex(qdev, 0x10, 0x8807, miiAddr);
  720. /* 10MB amplitude adjust (see Agere errata) */
  721. ql_mii_write_reg_ex(qdev, 0x11, 0x1f00, miiAddr);
  722. /* point to hidden reg 0x2806 */
  723. ql_mii_write_reg_ex(qdev, 0x10, 0x2806, miiAddr);
  724. /* Write new PHYAD w/bit 5 set */
  725. ql_mii_write_reg_ex(qdev, 0x11,
  726. 0x0020 | (PHYAddr[qdev->mac_index] >> 8), miiAddr);
  727. /*
  728. * Disable diagnostic mode bit 2 = 0
  729. * Power up device bit 11 = 0
  730. * Link up (on) and activity (blink)
  731. */
  732. ql_mii_write_reg(qdev, 0x12, 0x840a);
  733. ql_mii_write_reg(qdev, 0x00, 0x1140);
  734. ql_mii_write_reg(qdev, 0x1c, 0xfaf0);
  735. }
  736. static enum PHY_DEVICE_TYPE getPhyType(struct ql3_adapter *qdev,
  737. u16 phyIdReg0, u16 phyIdReg1)
  738. {
  739. enum PHY_DEVICE_TYPE result = PHY_TYPE_UNKNOWN;
  740. u32 oui;
  741. u16 model;
  742. int i;
  743. if (phyIdReg0 == 0xffff)
  744. return result;
  745. if (phyIdReg1 == 0xffff)
  746. return result;
  747. /* oui is split between two registers */
  748. oui = (phyIdReg0 << 6) | ((phyIdReg1 & PHY_OUI_1_MASK) >> 10);
  749. model = (phyIdReg1 & PHY_MODEL_MASK) >> 4;
  750. /* Scan table for this PHY */
  751. for (i = 0; i < MAX_PHY_DEV_TYPES; i++) {
  752. if ((oui == PHY_DEVICES[i].phyIdOUI) &&
  753. (model == PHY_DEVICES[i].phyIdModel)) {
  754. netdev_info(qdev->ndev, "Phy: %s\n",
  755. PHY_DEVICES[i].name);
  756. result = PHY_DEVICES[i].phyDevice;
  757. break;
  758. }
  759. }
  760. return result;
  761. }
  762. static int ql_phy_get_speed(struct ql3_adapter *qdev)
  763. {
  764. u16 reg;
  765. switch (qdev->phyType) {
  766. case PHY_AGERE_ET1011C: {
  767. if (ql_mii_read_reg(qdev, 0x1A, &reg) < 0)
  768. return 0;
  769. reg = (reg >> 8) & 3;
  770. break;
  771. }
  772. default:
  773. if (ql_mii_read_reg(qdev, AUX_CONTROL_STATUS, &reg) < 0)
  774. return 0;
  775. reg = (((reg & 0x18) >> 3) & 3);
  776. }
  777. switch (reg) {
  778. case 2:
  779. return SPEED_1000;
  780. case 1:
  781. return SPEED_100;
  782. case 0:
  783. return SPEED_10;
  784. default:
  785. return -1;
  786. }
  787. }
  788. static int ql_is_full_dup(struct ql3_adapter *qdev)
  789. {
  790. u16 reg;
  791. switch (qdev->phyType) {
  792. case PHY_AGERE_ET1011C: {
  793. if (ql_mii_read_reg(qdev, 0x1A, &reg))
  794. return 0;
  795. return ((reg & 0x0080) && (reg & 0x1000)) != 0;
  796. }
  797. case PHY_VITESSE_VSC8211:
  798. default: {
  799. if (ql_mii_read_reg(qdev, AUX_CONTROL_STATUS, &reg) < 0)
  800. return 0;
  801. return (reg & PHY_AUX_DUPLEX_STAT) != 0;
  802. }
  803. }
  804. }
  805. static int ql_is_phy_neg_pause(struct ql3_adapter *qdev)
  806. {
  807. u16 reg;
  808. if (ql_mii_read_reg(qdev, PHY_NEG_PARTNER, &reg) < 0)
  809. return 0;
  810. return (reg & PHY_NEG_PAUSE) != 0;
  811. }
  812. static int PHY_Setup(struct ql3_adapter *qdev)
  813. {
  814. u16 reg1;
  815. u16 reg2;
  816. bool agereAddrChangeNeeded = false;
  817. u32 miiAddr = 0;
  818. int err;
  819. /* Determine the PHY we are using by reading the ID's */
  820. err = ql_mii_read_reg(qdev, PHY_ID_0_REG, &reg1);
  821. if (err != 0) {
  822. netdev_err(qdev->ndev, "Could not read from reg PHY_ID_0_REG\n");
  823. return err;
  824. }
  825. err = ql_mii_read_reg(qdev, PHY_ID_1_REG, &reg2);
  826. if (err != 0) {
  827. netdev_err(qdev->ndev, "Could not read from reg PHY_ID_1_REG\n");
  828. return err;
  829. }
  830. /* Check if we have a Agere PHY */
  831. if ((reg1 == 0xffff) || (reg2 == 0xffff)) {
  832. /* Determine which MII address we should be using
  833. determined by the index of the card */
  834. if (qdev->mac_index == 0)
  835. miiAddr = MII_AGERE_ADDR_1;
  836. else
  837. miiAddr = MII_AGERE_ADDR_2;
  838. err = ql_mii_read_reg_ex(qdev, PHY_ID_0_REG, &reg1, miiAddr);
  839. if (err != 0) {
  840. netdev_err(qdev->ndev,
  841. "Could not read from reg PHY_ID_0_REG after Agere detected\n");
  842. return err;
  843. }
  844. err = ql_mii_read_reg_ex(qdev, PHY_ID_1_REG, &reg2, miiAddr);
  845. if (err != 0) {
  846. netdev_err(qdev->ndev, "Could not read from reg PHY_ID_1_REG after Agere detected\n");
  847. return err;
  848. }
  849. /* We need to remember to initialize the Agere PHY */
  850. agereAddrChangeNeeded = true;
  851. }
  852. /* Determine the particular PHY we have on board to apply
  853. PHY specific initializations */
  854. qdev->phyType = getPhyType(qdev, reg1, reg2);
  855. if ((qdev->phyType == PHY_AGERE_ET1011C) && agereAddrChangeNeeded) {
  856. /* need this here so address gets changed */
  857. phyAgereSpecificInit(qdev, miiAddr);
  858. } else if (qdev->phyType == PHY_TYPE_UNKNOWN) {
  859. netdev_err(qdev->ndev, "PHY is unknown\n");
  860. return -EIO;
  861. }
  862. return 0;
  863. }
  864. /*
  865. * Caller holds hw_lock.
  866. */
  867. static void ql_mac_enable(struct ql3_adapter *qdev, u32 enable)
  868. {
  869. struct ql3xxx_port_registers __iomem *port_regs =
  870. qdev->mem_map_registers;
  871. u32 value;
  872. if (enable)
  873. value = (MAC_CONFIG_REG_PE | (MAC_CONFIG_REG_PE << 16));
  874. else
  875. value = (MAC_CONFIG_REG_PE << 16);
  876. if (qdev->mac_index)
  877. ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
  878. else
  879. ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
  880. }
  881. /*
  882. * Caller holds hw_lock.
  883. */
  884. static void ql_mac_cfg_soft_reset(struct ql3_adapter *qdev, u32 enable)
  885. {
  886. struct ql3xxx_port_registers __iomem *port_regs =
  887. qdev->mem_map_registers;
  888. u32 value;
  889. if (enable)
  890. value = (MAC_CONFIG_REG_SR | (MAC_CONFIG_REG_SR << 16));
  891. else
  892. value = (MAC_CONFIG_REG_SR << 16);
  893. if (qdev->mac_index)
  894. ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
  895. else
  896. ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
  897. }
  898. /*
  899. * Caller holds hw_lock.
  900. */
  901. static void ql_mac_cfg_gig(struct ql3_adapter *qdev, u32 enable)
  902. {
  903. struct ql3xxx_port_registers __iomem *port_regs =
  904. qdev->mem_map_registers;
  905. u32 value;
  906. if (enable)
  907. value = (MAC_CONFIG_REG_GM | (MAC_CONFIG_REG_GM << 16));
  908. else
  909. value = (MAC_CONFIG_REG_GM << 16);
  910. if (qdev->mac_index)
  911. ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
  912. else
  913. ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
  914. }
  915. /*
  916. * Caller holds hw_lock.
  917. */
  918. static void ql_mac_cfg_full_dup(struct ql3_adapter *qdev, u32 enable)
  919. {
  920. struct ql3xxx_port_registers __iomem *port_regs =
  921. qdev->mem_map_registers;
  922. u32 value;
  923. if (enable)
  924. value = (MAC_CONFIG_REG_FD | (MAC_CONFIG_REG_FD << 16));
  925. else
  926. value = (MAC_CONFIG_REG_FD << 16);
  927. if (qdev->mac_index)
  928. ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
  929. else
  930. ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
  931. }
  932. /*
  933. * Caller holds hw_lock.
  934. */
  935. static void ql_mac_cfg_pause(struct ql3_adapter *qdev, u32 enable)
  936. {
  937. struct ql3xxx_port_registers __iomem *port_regs =
  938. qdev->mem_map_registers;
  939. u32 value;
  940. if (enable)
  941. value =
  942. ((MAC_CONFIG_REG_TF | MAC_CONFIG_REG_RF) |
  943. ((MAC_CONFIG_REG_TF | MAC_CONFIG_REG_RF) << 16));
  944. else
  945. value = ((MAC_CONFIG_REG_TF | MAC_CONFIG_REG_RF) << 16);
  946. if (qdev->mac_index)
  947. ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
  948. else
  949. ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
  950. }
  951. /*
  952. * Caller holds hw_lock.
  953. */
  954. static int ql_is_fiber(struct ql3_adapter *qdev)
  955. {
  956. struct ql3xxx_port_registers __iomem *port_regs =
  957. qdev->mem_map_registers;
  958. u32 bitToCheck = 0;
  959. u32 temp;
  960. switch (qdev->mac_index) {
  961. case 0:
  962. bitToCheck = PORT_STATUS_SM0;
  963. break;
  964. case 1:
  965. bitToCheck = PORT_STATUS_SM1;
  966. break;
  967. }
  968. temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
  969. return (temp & bitToCheck) != 0;
  970. }
  971. static int ql_is_auto_cfg(struct ql3_adapter *qdev)
  972. {
  973. u16 reg;
  974. ql_mii_read_reg(qdev, 0x00, &reg);
  975. return (reg & 0x1000) != 0;
  976. }
  977. /*
  978. * Caller holds hw_lock.
  979. */
  980. static int ql_is_auto_neg_complete(struct ql3_adapter *qdev)
  981. {
  982. struct ql3xxx_port_registers __iomem *port_regs =
  983. qdev->mem_map_registers;
  984. u32 bitToCheck = 0;
  985. u32 temp;
  986. switch (qdev->mac_index) {
  987. case 0:
  988. bitToCheck = PORT_STATUS_AC0;
  989. break;
  990. case 1:
  991. bitToCheck = PORT_STATUS_AC1;
  992. break;
  993. }
  994. temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
  995. if (temp & bitToCheck) {
  996. netif_info(qdev, link, qdev->ndev, "Auto-Negotiate complete\n");
  997. return 1;
  998. }
  999. netif_info(qdev, link, qdev->ndev, "Auto-Negotiate incomplete\n");
  1000. return 0;
  1001. }
  1002. /*
  1003. * ql_is_neg_pause() returns 1 if pause was negotiated to be on
  1004. */
  1005. static int ql_is_neg_pause(struct ql3_adapter *qdev)
  1006. {
  1007. if (ql_is_fiber(qdev))
  1008. return ql_is_petbi_neg_pause(qdev);
  1009. else
  1010. return ql_is_phy_neg_pause(qdev);
  1011. }
  1012. static int ql_auto_neg_error(struct ql3_adapter *qdev)
  1013. {
  1014. struct ql3xxx_port_registers __iomem *port_regs =
  1015. qdev->mem_map_registers;
  1016. u32 bitToCheck = 0;
  1017. u32 temp;
  1018. switch (qdev->mac_index) {
  1019. case 0:
  1020. bitToCheck = PORT_STATUS_AE0;
  1021. break;
  1022. case 1:
  1023. bitToCheck = PORT_STATUS_AE1;
  1024. break;
  1025. }
  1026. temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
  1027. return (temp & bitToCheck) != 0;
  1028. }
  1029. static u32 ql_get_link_speed(struct ql3_adapter *qdev)
  1030. {
  1031. if (ql_is_fiber(qdev))
  1032. return SPEED_1000;
  1033. else
  1034. return ql_phy_get_speed(qdev);
  1035. }
  1036. static int ql_is_link_full_dup(struct ql3_adapter *qdev)
  1037. {
  1038. if (ql_is_fiber(qdev))
  1039. return 1;
  1040. else
  1041. return ql_is_full_dup(qdev);
  1042. }
  1043. /*
  1044. * Caller holds hw_lock.
  1045. */
  1046. static int ql_link_down_detect(struct ql3_adapter *qdev)
  1047. {
  1048. struct ql3xxx_port_registers __iomem *port_regs =
  1049. qdev->mem_map_registers;
  1050. u32 bitToCheck = 0;
  1051. u32 temp;
  1052. switch (qdev->mac_index) {
  1053. case 0:
  1054. bitToCheck = ISP_CONTROL_LINK_DN_0;
  1055. break;
  1056. case 1:
  1057. bitToCheck = ISP_CONTROL_LINK_DN_1;
  1058. break;
  1059. }
  1060. temp =
  1061. ql_read_common_reg(qdev, &port_regs->CommonRegs.ispControlStatus);
  1062. return (temp & bitToCheck) != 0;
  1063. }
  1064. /*
  1065. * Caller holds hw_lock.
  1066. */
  1067. static int ql_link_down_detect_clear(struct ql3_adapter *qdev)
  1068. {
  1069. struct ql3xxx_port_registers __iomem *port_regs =
  1070. qdev->mem_map_registers;
  1071. switch (qdev->mac_index) {
  1072. case 0:
  1073. ql_write_common_reg(qdev,
  1074. &port_regs->CommonRegs.ispControlStatus,
  1075. (ISP_CONTROL_LINK_DN_0) |
  1076. (ISP_CONTROL_LINK_DN_0 << 16));
  1077. break;
  1078. case 1:
  1079. ql_write_common_reg(qdev,
  1080. &port_regs->CommonRegs.ispControlStatus,
  1081. (ISP_CONTROL_LINK_DN_1) |
  1082. (ISP_CONTROL_LINK_DN_1 << 16));
  1083. break;
  1084. default:
  1085. return 1;
  1086. }
  1087. return 0;
  1088. }
  1089. /*
  1090. * Caller holds hw_lock.
  1091. */
  1092. static int ql_this_adapter_controls_port(struct ql3_adapter *qdev)
  1093. {
  1094. struct ql3xxx_port_registers __iomem *port_regs =
  1095. qdev->mem_map_registers;
  1096. u32 bitToCheck = 0;
  1097. u32 temp;
  1098. switch (qdev->mac_index) {
  1099. case 0:
  1100. bitToCheck = PORT_STATUS_F1_ENABLED;
  1101. break;
  1102. case 1:
  1103. bitToCheck = PORT_STATUS_F3_ENABLED;
  1104. break;
  1105. default:
  1106. break;
  1107. }
  1108. temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
  1109. if (temp & bitToCheck) {
  1110. netif_printk(qdev, link, KERN_DEBUG, qdev->ndev,
  1111. "not link master\n");
  1112. return 0;
  1113. }
  1114. netif_printk(qdev, link, KERN_DEBUG, qdev->ndev, "link master\n");
  1115. return 1;
  1116. }
  1117. static void ql_phy_reset_ex(struct ql3_adapter *qdev)
  1118. {
  1119. ql_mii_write_reg_ex(qdev, CONTROL_REG, PHY_CTRL_SOFT_RESET,
  1120. PHYAddr[qdev->mac_index]);
  1121. }
  1122. static void ql_phy_start_neg_ex(struct ql3_adapter *qdev)
  1123. {
  1124. u16 reg;
  1125. u16 portConfiguration;
  1126. if (qdev->phyType == PHY_AGERE_ET1011C)
  1127. ql_mii_write_reg(qdev, 0x13, 0x0000);
  1128. /* turn off external loopback */
  1129. if (qdev->mac_index == 0)
  1130. portConfiguration =
  1131. qdev->nvram_data.macCfg_port0.portConfiguration;
  1132. else
  1133. portConfiguration =
  1134. qdev->nvram_data.macCfg_port1.portConfiguration;
  1135. /* Some HBA's in the field are set to 0 and they need to
  1136. be reinterpreted with a default value */
  1137. if (portConfiguration == 0)
  1138. portConfiguration = PORT_CONFIG_DEFAULT;
  1139. /* Set the 1000 advertisements */
  1140. ql_mii_read_reg_ex(qdev, PHY_GIG_CONTROL, &reg,
  1141. PHYAddr[qdev->mac_index]);
  1142. reg &= ~PHY_GIG_ALL_PARAMS;
  1143. if (portConfiguration & PORT_CONFIG_1000MB_SPEED) {
  1144. if (portConfiguration & PORT_CONFIG_FULL_DUPLEX_ENABLED)
  1145. reg |= PHY_GIG_ADV_1000F;
  1146. else
  1147. reg |= PHY_GIG_ADV_1000H;
  1148. }
  1149. ql_mii_write_reg_ex(qdev, PHY_GIG_CONTROL, reg,
  1150. PHYAddr[qdev->mac_index]);
  1151. /* Set the 10/100 & pause negotiation advertisements */
  1152. ql_mii_read_reg_ex(qdev, PHY_NEG_ADVER, &reg,
  1153. PHYAddr[qdev->mac_index]);
  1154. reg &= ~PHY_NEG_ALL_PARAMS;
  1155. if (portConfiguration & PORT_CONFIG_SYM_PAUSE_ENABLED)
  1156. reg |= PHY_NEG_ASY_PAUSE | PHY_NEG_SYM_PAUSE;
  1157. if (portConfiguration & PORT_CONFIG_FULL_DUPLEX_ENABLED) {
  1158. if (portConfiguration & PORT_CONFIG_100MB_SPEED)
  1159. reg |= PHY_NEG_ADV_100F;
  1160. if (portConfiguration & PORT_CONFIG_10MB_SPEED)
  1161. reg |= PHY_NEG_ADV_10F;
  1162. }
  1163. if (portConfiguration & PORT_CONFIG_HALF_DUPLEX_ENABLED) {
  1164. if (portConfiguration & PORT_CONFIG_100MB_SPEED)
  1165. reg |= PHY_NEG_ADV_100H;
  1166. if (portConfiguration & PORT_CONFIG_10MB_SPEED)
  1167. reg |= PHY_NEG_ADV_10H;
  1168. }
  1169. if (portConfiguration & PORT_CONFIG_1000MB_SPEED)
  1170. reg |= 1;
  1171. ql_mii_write_reg_ex(qdev, PHY_NEG_ADVER, reg,
  1172. PHYAddr[qdev->mac_index]);
  1173. ql_mii_read_reg_ex(qdev, CONTROL_REG, &reg, PHYAddr[qdev->mac_index]);
  1174. ql_mii_write_reg_ex(qdev, CONTROL_REG,
  1175. reg | PHY_CTRL_RESTART_NEG | PHY_CTRL_AUTO_NEG,
  1176. PHYAddr[qdev->mac_index]);
  1177. }
  1178. static void ql_phy_init_ex(struct ql3_adapter *qdev)
  1179. {
  1180. ql_phy_reset_ex(qdev);
  1181. PHY_Setup(qdev);
  1182. ql_phy_start_neg_ex(qdev);
  1183. }
  1184. /*
  1185. * Caller holds hw_lock.
  1186. */
  1187. static u32 ql_get_link_state(struct ql3_adapter *qdev)
  1188. {
  1189. struct ql3xxx_port_registers __iomem *port_regs =
  1190. qdev->mem_map_registers;
  1191. u32 bitToCheck = 0;
  1192. u32 temp, linkState;
  1193. switch (qdev->mac_index) {
  1194. case 0:
  1195. bitToCheck = PORT_STATUS_UP0;
  1196. break;
  1197. case 1:
  1198. bitToCheck = PORT_STATUS_UP1;
  1199. break;
  1200. }
  1201. temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
  1202. if (temp & bitToCheck)
  1203. linkState = LS_UP;
  1204. else
  1205. linkState = LS_DOWN;
  1206. return linkState;
  1207. }
  1208. static int ql_port_start(struct ql3_adapter *qdev)
  1209. {
  1210. if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
  1211. (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
  1212. 2) << 7)) {
  1213. netdev_err(qdev->ndev, "Could not get hw lock for GIO\n");
  1214. return -1;
  1215. }
  1216. if (ql_is_fiber(qdev)) {
  1217. ql_petbi_init(qdev);
  1218. } else {
  1219. /* Copper port */
  1220. ql_phy_init_ex(qdev);
  1221. }
  1222. ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
  1223. return 0;
  1224. }
  1225. static int ql_finish_auto_neg(struct ql3_adapter *qdev)
  1226. {
  1227. if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
  1228. (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
  1229. 2) << 7))
  1230. return -1;
  1231. if (!ql_auto_neg_error(qdev)) {
  1232. if (test_bit(QL_LINK_MASTER, &qdev->flags)) {
  1233. /* configure the MAC */
  1234. netif_printk(qdev, link, KERN_DEBUG, qdev->ndev,
  1235. "Configuring link\n");
  1236. ql_mac_cfg_soft_reset(qdev, 1);
  1237. ql_mac_cfg_gig(qdev,
  1238. (ql_get_link_speed
  1239. (qdev) ==
  1240. SPEED_1000));
  1241. ql_mac_cfg_full_dup(qdev,
  1242. ql_is_link_full_dup
  1243. (qdev));
  1244. ql_mac_cfg_pause(qdev,
  1245. ql_is_neg_pause
  1246. (qdev));
  1247. ql_mac_cfg_soft_reset(qdev, 0);
  1248. /* enable the MAC */
  1249. netif_printk(qdev, link, KERN_DEBUG, qdev->ndev,
  1250. "Enabling mac\n");
  1251. ql_mac_enable(qdev, 1);
  1252. }
  1253. qdev->port_link_state = LS_UP;
  1254. netif_start_queue(qdev->ndev);
  1255. netif_carrier_on(qdev->ndev);
  1256. netif_info(qdev, link, qdev->ndev,
  1257. "Link is up at %d Mbps, %s duplex\n",
  1258. ql_get_link_speed(qdev),
  1259. ql_is_link_full_dup(qdev) ? "full" : "half");
  1260. } else { /* Remote error detected */
  1261. if (test_bit(QL_LINK_MASTER, &qdev->flags)) {
  1262. netif_printk(qdev, link, KERN_DEBUG, qdev->ndev,
  1263. "Remote error detected. Calling ql_port_start()\n");
  1264. /*
  1265. * ql_port_start() is shared code and needs
  1266. * to lock the PHY on it's own.
  1267. */
  1268. ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
  1269. if (ql_port_start(qdev)) /* Restart port */
  1270. return -1;
  1271. return 0;
  1272. }
  1273. }
  1274. ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
  1275. return 0;
  1276. }
  1277. static void ql_link_state_machine_work(struct work_struct *work)
  1278. {
  1279. struct ql3_adapter *qdev =
  1280. container_of(work, struct ql3_adapter, link_state_work.work);
  1281. u32 curr_link_state;
  1282. unsigned long hw_flags;
  1283. spin_lock_irqsave(&qdev->hw_lock, hw_flags);
  1284. curr_link_state = ql_get_link_state(qdev);
  1285. if (test_bit(QL_RESET_ACTIVE, &qdev->flags)) {
  1286. netif_info(qdev, link, qdev->ndev,
  1287. "Reset in progress, skip processing link state\n");
  1288. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  1289. /* Restart timer on 2 second interval. */
  1290. mod_timer(&qdev->adapter_timer, jiffies + HZ * 1);
  1291. return;
  1292. }
  1293. switch (qdev->port_link_state) {
  1294. default:
  1295. if (test_bit(QL_LINK_MASTER, &qdev->flags))
  1296. ql_port_start(qdev);
  1297. qdev->port_link_state = LS_DOWN;
  1298. /* Fall Through */
  1299. case LS_DOWN:
  1300. if (curr_link_state == LS_UP) {
  1301. netif_info(qdev, link, qdev->ndev, "Link is up\n");
  1302. if (ql_is_auto_neg_complete(qdev))
  1303. ql_finish_auto_neg(qdev);
  1304. if (qdev->port_link_state == LS_UP)
  1305. ql_link_down_detect_clear(qdev);
  1306. qdev->port_link_state = LS_UP;
  1307. }
  1308. break;
  1309. case LS_UP:
  1310. /*
  1311. * See if the link is currently down or went down and came
  1312. * back up
  1313. */
  1314. if (curr_link_state == LS_DOWN) {
  1315. netif_info(qdev, link, qdev->ndev, "Link is down\n");
  1316. qdev->port_link_state = LS_DOWN;
  1317. }
  1318. if (ql_link_down_detect(qdev))
  1319. qdev->port_link_state = LS_DOWN;
  1320. break;
  1321. }
  1322. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  1323. /* Restart timer on 2 second interval. */
  1324. mod_timer(&qdev->adapter_timer, jiffies + HZ * 1);
  1325. }
  1326. /*
  1327. * Caller must take hw_lock and QL_PHY_GIO_SEM.
  1328. */
  1329. static void ql_get_phy_owner(struct ql3_adapter *qdev)
  1330. {
  1331. if (ql_this_adapter_controls_port(qdev))
  1332. set_bit(QL_LINK_MASTER, &qdev->flags);
  1333. else
  1334. clear_bit(QL_LINK_MASTER, &qdev->flags);
  1335. }
  1336. /*
  1337. * Caller must take hw_lock and QL_PHY_GIO_SEM.
  1338. */
  1339. static void ql_init_scan_mode(struct ql3_adapter *qdev)
  1340. {
  1341. ql_mii_enable_scan_mode(qdev);
  1342. if (test_bit(QL_LINK_OPTICAL, &qdev->flags)) {
  1343. if (ql_this_adapter_controls_port(qdev))
  1344. ql_petbi_init_ex(qdev);
  1345. } else {
  1346. if (ql_this_adapter_controls_port(qdev))
  1347. ql_phy_init_ex(qdev);
  1348. }
  1349. }
  1350. /*
  1351. * MII_Setup needs to be called before taking the PHY out of reset
  1352. * so that the management interface clock speed can be set properly.
  1353. * It would be better if we had a way to disable MDC until after the
  1354. * PHY is out of reset, but we don't have that capability.
  1355. */
  1356. static int ql_mii_setup(struct ql3_adapter *qdev)
  1357. {
  1358. u32 reg;
  1359. struct ql3xxx_port_registers __iomem *port_regs =
  1360. qdev->mem_map_registers;
  1361. if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
  1362. (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
  1363. 2) << 7))
  1364. return -1;
  1365. if (qdev->device_id == QL3032_DEVICE_ID)
  1366. ql_write_page0_reg(qdev,
  1367. &port_regs->macMIIMgmtControlReg, 0x0f00000);
  1368. /* Divide 125MHz clock by 28 to meet PHY timing requirements */
  1369. reg = MAC_MII_CONTROL_CLK_SEL_DIV28;
  1370. ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
  1371. reg | ((MAC_MII_CONTROL_CLK_SEL_MASK) << 16));
  1372. ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
  1373. return 0;
  1374. }
  1375. #define SUPPORTED_OPTICAL_MODES (SUPPORTED_1000baseT_Full | \
  1376. SUPPORTED_FIBRE | \
  1377. SUPPORTED_Autoneg)
  1378. #define SUPPORTED_TP_MODES (SUPPORTED_10baseT_Half | \
  1379. SUPPORTED_10baseT_Full | \
  1380. SUPPORTED_100baseT_Half | \
  1381. SUPPORTED_100baseT_Full | \
  1382. SUPPORTED_1000baseT_Half | \
  1383. SUPPORTED_1000baseT_Full | \
  1384. SUPPORTED_Autoneg | \
  1385. SUPPORTED_TP); \
  1386. static u32 ql_supported_modes(struct ql3_adapter *qdev)
  1387. {
  1388. if (test_bit(QL_LINK_OPTICAL, &qdev->flags))
  1389. return SUPPORTED_OPTICAL_MODES;
  1390. return SUPPORTED_TP_MODES;
  1391. }
  1392. static int ql_get_auto_cfg_status(struct ql3_adapter *qdev)
  1393. {
  1394. int status;
  1395. unsigned long hw_flags;
  1396. spin_lock_irqsave(&qdev->hw_lock, hw_flags);
  1397. if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
  1398. (QL_RESOURCE_BITS_BASE_CODE |
  1399. (qdev->mac_index) * 2) << 7)) {
  1400. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  1401. return 0;
  1402. }
  1403. status = ql_is_auto_cfg(qdev);
  1404. ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
  1405. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  1406. return status;
  1407. }
  1408. static u32 ql_get_speed(struct ql3_adapter *qdev)
  1409. {
  1410. u32 status;
  1411. unsigned long hw_flags;
  1412. spin_lock_irqsave(&qdev->hw_lock, hw_flags);
  1413. if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
  1414. (QL_RESOURCE_BITS_BASE_CODE |
  1415. (qdev->mac_index) * 2) << 7)) {
  1416. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  1417. return 0;
  1418. }
  1419. status = ql_get_link_speed(qdev);
  1420. ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
  1421. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  1422. return status;
  1423. }
  1424. static int ql_get_full_dup(struct ql3_adapter *qdev)
  1425. {
  1426. int status;
  1427. unsigned long hw_flags;
  1428. spin_lock_irqsave(&qdev->hw_lock, hw_flags);
  1429. if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
  1430. (QL_RESOURCE_BITS_BASE_CODE |
  1431. (qdev->mac_index) * 2) << 7)) {
  1432. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  1433. return 0;
  1434. }
  1435. status = ql_is_link_full_dup(qdev);
  1436. ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
  1437. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  1438. return status;
  1439. }
  1440. static int ql_get_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
  1441. {
  1442. struct ql3_adapter *qdev = netdev_priv(ndev);
  1443. ecmd->transceiver = XCVR_INTERNAL;
  1444. ecmd->supported = ql_supported_modes(qdev);
  1445. if (test_bit(QL_LINK_OPTICAL, &qdev->flags)) {
  1446. ecmd->port = PORT_FIBRE;
  1447. } else {
  1448. ecmd->port = PORT_TP;
  1449. ecmd->phy_address = qdev->PHYAddr;
  1450. }
  1451. ecmd->advertising = ql_supported_modes(qdev);
  1452. ecmd->autoneg = ql_get_auto_cfg_status(qdev);
  1453. ethtool_cmd_speed_set(ecmd, ql_get_speed(qdev));
  1454. ecmd->duplex = ql_get_full_dup(qdev);
  1455. return 0;
  1456. }
  1457. static void ql_get_drvinfo(struct net_device *ndev,
  1458. struct ethtool_drvinfo *drvinfo)
  1459. {
  1460. struct ql3_adapter *qdev = netdev_priv(ndev);
  1461. strncpy(drvinfo->driver, ql3xxx_driver_name, 32);
  1462. strncpy(drvinfo->version, ql3xxx_driver_version, 32);
  1463. strncpy(drvinfo->fw_version, "N/A", 32);
  1464. strncpy(drvinfo->bus_info, pci_name(qdev->pdev), 32);
  1465. drvinfo->regdump_len = 0;
  1466. drvinfo->eedump_len = 0;
  1467. }
  1468. static u32 ql_get_msglevel(struct net_device *ndev)
  1469. {
  1470. struct ql3_adapter *qdev = netdev_priv(ndev);
  1471. return qdev->msg_enable;
  1472. }
  1473. static void ql_set_msglevel(struct net_device *ndev, u32 value)
  1474. {
  1475. struct ql3_adapter *qdev = netdev_priv(ndev);
  1476. qdev->msg_enable = value;
  1477. }
  1478. static void ql_get_pauseparam(struct net_device *ndev,
  1479. struct ethtool_pauseparam *pause)
  1480. {
  1481. struct ql3_adapter *qdev = netdev_priv(ndev);
  1482. struct ql3xxx_port_registers __iomem *port_regs =
  1483. qdev->mem_map_registers;
  1484. u32 reg;
  1485. if (qdev->mac_index == 0)
  1486. reg = ql_read_page0_reg(qdev, &port_regs->mac0ConfigReg);
  1487. else
  1488. reg = ql_read_page0_reg(qdev, &port_regs->mac1ConfigReg);
  1489. pause->autoneg = ql_get_auto_cfg_status(qdev);
  1490. pause->rx_pause = (reg & MAC_CONFIG_REG_RF) >> 2;
  1491. pause->tx_pause = (reg & MAC_CONFIG_REG_TF) >> 1;
  1492. }
  1493. static const struct ethtool_ops ql3xxx_ethtool_ops = {
  1494. .get_settings = ql_get_settings,
  1495. .get_drvinfo = ql_get_drvinfo,
  1496. .get_link = ethtool_op_get_link,
  1497. .get_msglevel = ql_get_msglevel,
  1498. .set_msglevel = ql_set_msglevel,
  1499. .get_pauseparam = ql_get_pauseparam,
  1500. };
  1501. static int ql_populate_free_queue(struct ql3_adapter *qdev)
  1502. {
  1503. struct ql_rcv_buf_cb *lrg_buf_cb = qdev->lrg_buf_free_head;
  1504. dma_addr_t map;
  1505. int err;
  1506. while (lrg_buf_cb) {
  1507. if (!lrg_buf_cb->skb) {
  1508. lrg_buf_cb->skb =
  1509. netdev_alloc_skb(qdev->ndev,
  1510. qdev->lrg_buffer_len);
  1511. if (unlikely(!lrg_buf_cb->skb)) {
  1512. netdev_printk(KERN_DEBUG, qdev->ndev,
  1513. "Failed netdev_alloc_skb()\n");
  1514. break;
  1515. } else {
  1516. /*
  1517. * We save some space to copy the ethhdr from
  1518. * first buffer
  1519. */
  1520. skb_reserve(lrg_buf_cb->skb, QL_HEADER_SPACE);
  1521. map = pci_map_single(qdev->pdev,
  1522. lrg_buf_cb->skb->data,
  1523. qdev->lrg_buffer_len -
  1524. QL_HEADER_SPACE,
  1525. PCI_DMA_FROMDEVICE);
  1526. err = pci_dma_mapping_error(qdev->pdev, map);
  1527. if (err) {
  1528. netdev_err(qdev->ndev,
  1529. "PCI mapping failed with error: %d\n",
  1530. err);
  1531. dev_kfree_skb(lrg_buf_cb->skb);
  1532. lrg_buf_cb->skb = NULL;
  1533. break;
  1534. }
  1535. lrg_buf_cb->buf_phy_addr_low =
  1536. cpu_to_le32(LS_64BITS(map));
  1537. lrg_buf_cb->buf_phy_addr_high =
  1538. cpu_to_le32(MS_64BITS(map));
  1539. dma_unmap_addr_set(lrg_buf_cb, mapaddr, map);
  1540. dma_unmap_len_set(lrg_buf_cb, maplen,
  1541. qdev->lrg_buffer_len -
  1542. QL_HEADER_SPACE);
  1543. --qdev->lrg_buf_skb_check;
  1544. if (!qdev->lrg_buf_skb_check)
  1545. return 1;
  1546. }
  1547. }
  1548. lrg_buf_cb = lrg_buf_cb->next;
  1549. }
  1550. return 0;
  1551. }
  1552. /*
  1553. * Caller holds hw_lock.
  1554. */
  1555. static void ql_update_small_bufq_prod_index(struct ql3_adapter *qdev)
  1556. {
  1557. struct ql3xxx_port_registers __iomem *port_regs =
  1558. qdev->mem_map_registers;
  1559. if (qdev->small_buf_release_cnt >= 16) {
  1560. while (qdev->small_buf_release_cnt >= 16) {
  1561. qdev->small_buf_q_producer_index++;
  1562. if (qdev->small_buf_q_producer_index ==
  1563. NUM_SBUFQ_ENTRIES)
  1564. qdev->small_buf_q_producer_index = 0;
  1565. qdev->small_buf_release_cnt -= 8;
  1566. }
  1567. wmb();
  1568. writel(qdev->small_buf_q_producer_index,
  1569. &port_regs->CommonRegs.rxSmallQProducerIndex);
  1570. }
  1571. }
  1572. /*
  1573. * Caller holds hw_lock.
  1574. */
  1575. static void ql_update_lrg_bufq_prod_index(struct ql3_adapter *qdev)
  1576. {
  1577. struct bufq_addr_element *lrg_buf_q_ele;
  1578. int i;
  1579. struct ql_rcv_buf_cb *lrg_buf_cb;
  1580. struct ql3xxx_port_registers __iomem *port_regs =
  1581. qdev->mem_map_registers;
  1582. if ((qdev->lrg_buf_free_count >= 8) &&
  1583. (qdev->lrg_buf_release_cnt >= 16)) {
  1584. if (qdev->lrg_buf_skb_check)
  1585. if (!ql_populate_free_queue(qdev))
  1586. return;
  1587. lrg_buf_q_ele = qdev->lrg_buf_next_free;
  1588. while ((qdev->lrg_buf_release_cnt >= 16) &&
  1589. (qdev->lrg_buf_free_count >= 8)) {
  1590. for (i = 0; i < 8; i++) {
  1591. lrg_buf_cb =
  1592. ql_get_from_lrg_buf_free_list(qdev);
  1593. lrg_buf_q_ele->addr_high =
  1594. lrg_buf_cb->buf_phy_addr_high;
  1595. lrg_buf_q_ele->addr_low =
  1596. lrg_buf_cb->buf_phy_addr_low;
  1597. lrg_buf_q_ele++;
  1598. qdev->lrg_buf_release_cnt--;
  1599. }
  1600. qdev->lrg_buf_q_producer_index++;
  1601. if (qdev->lrg_buf_q_producer_index ==
  1602. qdev->num_lbufq_entries)
  1603. qdev->lrg_buf_q_producer_index = 0;
  1604. if (qdev->lrg_buf_q_producer_index ==
  1605. (qdev->num_lbufq_entries - 1)) {
  1606. lrg_buf_q_ele = qdev->lrg_buf_q_virt_addr;
  1607. }
  1608. }
  1609. wmb();
  1610. qdev->lrg_buf_next_free = lrg_buf_q_ele;
  1611. writel(qdev->lrg_buf_q_producer_index,
  1612. &port_regs->CommonRegs.rxLargeQProducerIndex);
  1613. }
  1614. }
  1615. static void ql_process_mac_tx_intr(struct ql3_adapter *qdev,
  1616. struct ob_mac_iocb_rsp *mac_rsp)
  1617. {
  1618. struct ql_tx_buf_cb *tx_cb;
  1619. int i;
  1620. int retval = 0;
  1621. if (mac_rsp->flags & OB_MAC_IOCB_RSP_S) {
  1622. netdev_warn(qdev->ndev,
  1623. "Frame too short but it was padded and sent\n");
  1624. }
  1625. tx_cb = &qdev->tx_buf[mac_rsp->transaction_id];
  1626. /* Check the transmit response flags for any errors */
  1627. if (mac_rsp->flags & OB_MAC_IOCB_RSP_S) {
  1628. netdev_err(qdev->ndev,
  1629. "Frame too short to be legal, frame not sent\n");
  1630. qdev->ndev->stats.tx_errors++;
  1631. retval = -EIO;
  1632. goto frame_not_sent;
  1633. }
  1634. if (tx_cb->seg_count == 0) {
  1635. netdev_err(qdev->ndev, "tx_cb->seg_count == 0: %d\n",
  1636. mac_rsp->transaction_id);
  1637. qdev->ndev->stats.tx_errors++;
  1638. retval = -EIO;
  1639. goto invalid_seg_count;
  1640. }
  1641. pci_unmap_single(qdev->pdev,
  1642. dma_unmap_addr(&tx_cb->map[0], mapaddr),
  1643. dma_unmap_len(&tx_cb->map[0], maplen),
  1644. PCI_DMA_TODEVICE);
  1645. tx_cb->seg_count--;
  1646. if (tx_cb->seg_count) {
  1647. for (i = 1; i < tx_cb->seg_count; i++) {
  1648. pci_unmap_page(qdev->pdev,
  1649. dma_unmap_addr(&tx_cb->map[i],
  1650. mapaddr),
  1651. dma_unmap_len(&tx_cb->map[i], maplen),
  1652. PCI_DMA_TODEVICE);
  1653. }
  1654. }
  1655. qdev->ndev->stats.tx_packets++;
  1656. qdev->ndev->stats.tx_bytes += tx_cb->skb->len;
  1657. frame_not_sent:
  1658. dev_kfree_skb_irq(tx_cb->skb);
  1659. tx_cb->skb = NULL;
  1660. invalid_seg_count:
  1661. atomic_inc(&qdev->tx_count);
  1662. }
  1663. static void ql_get_sbuf(struct ql3_adapter *qdev)
  1664. {
  1665. if (++qdev->small_buf_index == NUM_SMALL_BUFFERS)
  1666. qdev->small_buf_index = 0;
  1667. qdev->small_buf_release_cnt++;
  1668. }
  1669. static struct ql_rcv_buf_cb *ql_get_lbuf(struct ql3_adapter *qdev)
  1670. {
  1671. struct ql_rcv_buf_cb *lrg_buf_cb = NULL;
  1672. lrg_buf_cb = &qdev->lrg_buf[qdev->lrg_buf_index];
  1673. qdev->lrg_buf_release_cnt++;
  1674. if (++qdev->lrg_buf_index == qdev->num_large_buffers)
  1675. qdev->lrg_buf_index = 0;
  1676. return lrg_buf_cb;
  1677. }
  1678. /*
  1679. * The difference between 3022 and 3032 for inbound completions:
  1680. * 3022 uses two buffers per completion. The first buffer contains
  1681. * (some) header info, the second the remainder of the headers plus
  1682. * the data. For this chip we reserve some space at the top of the
  1683. * receive buffer so that the header info in buffer one can be
  1684. * prepended to the buffer two. Buffer two is the sent up while
  1685. * buffer one is returned to the hardware to be reused.
  1686. * 3032 receives all of it's data and headers in one buffer for a
  1687. * simpler process. 3032 also supports checksum verification as
  1688. * can be seen in ql_process_macip_rx_intr().
  1689. */
  1690. static void ql_process_mac_rx_intr(struct ql3_adapter *qdev,
  1691. struct ib_mac_iocb_rsp *ib_mac_rsp_ptr)
  1692. {
  1693. struct ql_rcv_buf_cb *lrg_buf_cb1 = NULL;
  1694. struct ql_rcv_buf_cb *lrg_buf_cb2 = NULL;
  1695. struct sk_buff *skb;
  1696. u16 length = le16_to_cpu(ib_mac_rsp_ptr->length);
  1697. /*
  1698. * Get the inbound address list (small buffer).
  1699. */
  1700. ql_get_sbuf(qdev);
  1701. if (qdev->device_id == QL3022_DEVICE_ID)
  1702. lrg_buf_cb1 = ql_get_lbuf(qdev);
  1703. /* start of second buffer */
  1704. lrg_buf_cb2 = ql_get_lbuf(qdev);
  1705. skb = lrg_buf_cb2->skb;
  1706. qdev->ndev->stats.rx_packets++;
  1707. qdev->ndev->stats.rx_bytes += length;
  1708. skb_put(skb, length);
  1709. pci_unmap_single(qdev->pdev,
  1710. dma_unmap_addr(lrg_buf_cb2, mapaddr),
  1711. dma_unmap_len(lrg_buf_cb2, maplen),
  1712. PCI_DMA_FROMDEVICE);
  1713. prefetch(skb->data);
  1714. skb_checksum_none_assert(skb);
  1715. skb->protocol = eth_type_trans(skb, qdev->ndev);
  1716. netif_receive_skb(skb);
  1717. lrg_buf_cb2->skb = NULL;
  1718. if (qdev->device_id == QL3022_DEVICE_ID)
  1719. ql_release_to_lrg_buf_free_list(qdev, lrg_buf_cb1);
  1720. ql_release_to_lrg_buf_free_list(qdev, lrg_buf_cb2);
  1721. }
  1722. static void ql_process_macip_rx_intr(struct ql3_adapter *qdev,
  1723. struct ib_ip_iocb_rsp *ib_ip_rsp_ptr)
  1724. {
  1725. struct ql_rcv_buf_cb *lrg_buf_cb1 = NULL;
  1726. struct ql_rcv_buf_cb *lrg_buf_cb2 = NULL;
  1727. struct sk_buff *skb1 = NULL, *skb2;
  1728. struct net_device *ndev = qdev->ndev;
  1729. u16 length = le16_to_cpu(ib_ip_rsp_ptr->length);
  1730. u16 size = 0;
  1731. /*
  1732. * Get the inbound address list (small buffer).
  1733. */
  1734. ql_get_sbuf(qdev);
  1735. if (qdev->device_id == QL3022_DEVICE_ID) {
  1736. /* start of first buffer on 3022 */
  1737. lrg_buf_cb1 = ql_get_lbuf(qdev);
  1738. skb1 = lrg_buf_cb1->skb;
  1739. size = ETH_HLEN;
  1740. if (*((u16 *) skb1->data) != 0xFFFF)
  1741. size += VLAN_ETH_HLEN - ETH_HLEN;
  1742. }
  1743. /* start of second buffer */
  1744. lrg_buf_cb2 = ql_get_lbuf(qdev);
  1745. skb2 = lrg_buf_cb2->skb;
  1746. skb_put(skb2, length); /* Just the second buffer length here. */
  1747. pci_unmap_single(qdev->pdev,
  1748. dma_unmap_addr(lrg_buf_cb2, mapaddr),
  1749. dma_unmap_len(lrg_buf_cb2, maplen),
  1750. PCI_DMA_FROMDEVICE);
  1751. prefetch(skb2->data);
  1752. skb_checksum_none_assert(skb2);
  1753. if (qdev->device_id == QL3022_DEVICE_ID) {
  1754. /*
  1755. * Copy the ethhdr from first buffer to second. This
  1756. * is necessary for 3022 IP completions.
  1757. */
  1758. skb_copy_from_linear_data_offset(skb1, VLAN_ID_LEN,
  1759. skb_push(skb2, size), size);
  1760. } else {
  1761. u16 checksum = le16_to_cpu(ib_ip_rsp_ptr->checksum);
  1762. if (checksum &
  1763. (IB_IP_IOCB_RSP_3032_ICE |
  1764. IB_IP_IOCB_RSP_3032_CE)) {
  1765. netdev_err(ndev,
  1766. "%s: Bad checksum for this %s packet, checksum = %x\n",
  1767. __func__,
  1768. ((checksum & IB_IP_IOCB_RSP_3032_TCP) ?
  1769. "TCP" : "UDP"), checksum);
  1770. } else if ((checksum & IB_IP_IOCB_RSP_3032_TCP) ||
  1771. (checksum & IB_IP_IOCB_RSP_3032_UDP &&
  1772. !(checksum & IB_IP_IOCB_RSP_3032_NUC))) {
  1773. skb2->ip_summed = CHECKSUM_UNNECESSARY;
  1774. }
  1775. }
  1776. skb2->protocol = eth_type_trans(skb2, qdev->ndev);
  1777. netif_receive_skb(skb2);
  1778. ndev->stats.rx_packets++;
  1779. ndev->stats.rx_bytes += length;
  1780. lrg_buf_cb2->skb = NULL;
  1781. if (qdev->device_id == QL3022_DEVICE_ID)
  1782. ql_release_to_lrg_buf_free_list(qdev, lrg_buf_cb1);
  1783. ql_release_to_lrg_buf_free_list(qdev, lrg_buf_cb2);
  1784. }
  1785. static int ql_tx_rx_clean(struct ql3_adapter *qdev,
  1786. int *tx_cleaned, int *rx_cleaned, int work_to_do)
  1787. {
  1788. struct net_rsp_iocb *net_rsp;
  1789. struct net_device *ndev = qdev->ndev;
  1790. int work_done = 0;
  1791. /* While there are entries in the completion queue. */
  1792. while ((le32_to_cpu(*(qdev->prsp_producer_index)) !=
  1793. qdev->rsp_consumer_index) && (work_done < work_to_do)) {
  1794. net_rsp = qdev->rsp_current;
  1795. rmb();
  1796. /*
  1797. * Fix 4032 chip's undocumented "feature" where bit-8 is set
  1798. * if the inbound completion is for a VLAN.
  1799. */
  1800. if (qdev->device_id == QL3032_DEVICE_ID)
  1801. net_rsp->opcode &= 0x7f;
  1802. switch (net_rsp->opcode) {
  1803. case OPCODE_OB_MAC_IOCB_FN0:
  1804. case OPCODE_OB_MAC_IOCB_FN2:
  1805. ql_process_mac_tx_intr(qdev, (struct ob_mac_iocb_rsp *)
  1806. net_rsp);
  1807. (*tx_cleaned)++;
  1808. break;
  1809. case OPCODE_IB_MAC_IOCB:
  1810. case OPCODE_IB_3032_MAC_IOCB:
  1811. ql_process_mac_rx_intr(qdev, (struct ib_mac_iocb_rsp *)
  1812. net_rsp);
  1813. (*rx_cleaned)++;
  1814. break;
  1815. case OPCODE_IB_IP_IOCB:
  1816. case OPCODE_IB_3032_IP_IOCB:
  1817. ql_process_macip_rx_intr(qdev, (struct ib_ip_iocb_rsp *)
  1818. net_rsp);
  1819. (*rx_cleaned)++;
  1820. break;
  1821. default: {
  1822. u32 *tmp = (u32 *)net_rsp;
  1823. netdev_err(ndev,
  1824. "Hit default case, not handled!\n"
  1825. " dropping the packet, opcode = %x\n"
  1826. "0x%08lx 0x%08lx 0x%08lx 0x%08lx\n",
  1827. net_rsp->opcode,
  1828. (unsigned long int)tmp[0],
  1829. (unsigned long int)tmp[1],
  1830. (unsigned long int)tmp[2],
  1831. (unsigned long int)tmp[3]);
  1832. }
  1833. }
  1834. qdev->rsp_consumer_index++;
  1835. if (qdev->rsp_consumer_index == NUM_RSP_Q_ENTRIES) {
  1836. qdev->rsp_consumer_index = 0;
  1837. qdev->rsp_current = qdev->rsp_q_virt_addr;
  1838. } else {
  1839. qdev->rsp_current++;
  1840. }
  1841. work_done = *tx_cleaned + *rx_cleaned;
  1842. }
  1843. return work_done;
  1844. }
  1845. static int ql_poll(struct napi_struct *napi, int budget)
  1846. {
  1847. struct ql3_adapter *qdev = container_of(napi, struct ql3_adapter, napi);
  1848. int rx_cleaned = 0, tx_cleaned = 0;
  1849. unsigned long hw_flags;
  1850. struct ql3xxx_port_registers __iomem *port_regs =
  1851. qdev->mem_map_registers;
  1852. ql_tx_rx_clean(qdev, &tx_cleaned, &rx_cleaned, budget);
  1853. if (tx_cleaned + rx_cleaned != budget) {
  1854. spin_lock_irqsave(&qdev->hw_lock, hw_flags);
  1855. __napi_complete(napi);
  1856. ql_update_small_bufq_prod_index(qdev);
  1857. ql_update_lrg_bufq_prod_index(qdev);
  1858. writel(qdev->rsp_consumer_index,
  1859. &port_regs->CommonRegs.rspQConsumerIndex);
  1860. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  1861. ql_enable_interrupts(qdev);
  1862. }
  1863. return tx_cleaned + rx_cleaned;
  1864. }
  1865. static irqreturn_t ql3xxx_isr(int irq, void *dev_id)
  1866. {
  1867. struct net_device *ndev = dev_id;
  1868. struct ql3_adapter *qdev = netdev_priv(ndev);
  1869. struct ql3xxx_port_registers __iomem *port_regs =
  1870. qdev->mem_map_registers;
  1871. u32 value;
  1872. int handled = 1;
  1873. u32 var;
  1874. value = ql_read_common_reg_l(qdev,
  1875. &port_regs->CommonRegs.ispControlStatus);
  1876. if (value & (ISP_CONTROL_FE | ISP_CONTROL_RI)) {
  1877. spin_lock(&qdev->adapter_lock);
  1878. netif_stop_queue(qdev->ndev);
  1879. netif_carrier_off(qdev->ndev);
  1880. ql_disable_interrupts(qdev);
  1881. qdev->port_link_state = LS_DOWN;
  1882. set_bit(QL_RESET_ACTIVE, &qdev->flags) ;
  1883. if (value & ISP_CONTROL_FE) {
  1884. /*
  1885. * Chip Fatal Error.
  1886. */
  1887. var =
  1888. ql_read_page0_reg_l(qdev,
  1889. &port_regs->PortFatalErrStatus);
  1890. netdev_warn(ndev,
  1891. "Resetting chip. PortFatalErrStatus register = 0x%x\n",
  1892. var);
  1893. set_bit(QL_RESET_START, &qdev->flags) ;
  1894. } else {
  1895. /*
  1896. * Soft Reset Requested.
  1897. */
  1898. set_bit(QL_RESET_PER_SCSI, &qdev->flags) ;
  1899. netdev_err(ndev,
  1900. "Another function issued a reset to the chip. ISR value = %x\n",
  1901. value);
  1902. }
  1903. queue_delayed_work(qdev->workqueue, &qdev->reset_work, 0);
  1904. spin_unlock(&qdev->adapter_lock);
  1905. } else if (value & ISP_IMR_DISABLE_CMPL_INT) {
  1906. ql_disable_interrupts(qdev);
  1907. if (likely(napi_schedule_prep(&qdev->napi)))
  1908. __napi_schedule(&qdev->napi);
  1909. } else
  1910. return IRQ_NONE;
  1911. return IRQ_RETVAL(handled);
  1912. }
  1913. /*
  1914. * Get the total number of segments needed for the given number of fragments.
  1915. * This is necessary because outbound address lists (OAL) will be used when
  1916. * more than two frags are given. Each address list has 5 addr/len pairs.
  1917. * The 5th pair in each OAL is used to point to the next OAL if more frags
  1918. * are coming. That is why the frags:segment count ratio is not linear.
  1919. */
  1920. static int ql_get_seg_count(struct ql3_adapter *qdev, unsigned short frags)
  1921. {
  1922. if (qdev->device_id == QL3022_DEVICE_ID)
  1923. return 1;
  1924. if (frags <= 2)
  1925. return frags + 1;
  1926. else if (frags <= 6)
  1927. return frags + 2;
  1928. else if (frags <= 10)
  1929. return frags + 3;
  1930. else if (frags <= 14)
  1931. return frags + 4;
  1932. else if (frags <= 18)
  1933. return frags + 5;
  1934. return -1;
  1935. }
  1936. static void ql_hw_csum_setup(const struct sk_buff *skb,
  1937. struct ob_mac_iocb_req *mac_iocb_ptr)
  1938. {
  1939. const struct iphdr *ip = ip_hdr(skb);
  1940. mac_iocb_ptr->ip_hdr_off = skb_network_offset(skb);
  1941. mac_iocb_ptr->ip_hdr_len = ip->ihl;
  1942. if (ip->protocol == IPPROTO_TCP) {
  1943. mac_iocb_ptr->flags1 |= OB_3032MAC_IOCB_REQ_TC |
  1944. OB_3032MAC_IOCB_REQ_IC;
  1945. } else {
  1946. mac_iocb_ptr->flags1 |= OB_3032MAC_IOCB_REQ_UC |
  1947. OB_3032MAC_IOCB_REQ_IC;
  1948. }
  1949. }
  1950. /*
  1951. * Map the buffers for this transmit.
  1952. * This will return NETDEV_TX_BUSY or NETDEV_TX_OK based on success.
  1953. */
  1954. static int ql_send_map(struct ql3_adapter *qdev,
  1955. struct ob_mac_iocb_req *mac_iocb_ptr,
  1956. struct ql_tx_buf_cb *tx_cb,
  1957. struct sk_buff *skb)
  1958. {
  1959. struct oal *oal;
  1960. struct oal_entry *oal_entry;
  1961. int len = skb_headlen(skb);
  1962. dma_addr_t map;
  1963. int err;
  1964. int completed_segs, i;
  1965. int seg_cnt, seg = 0;
  1966. int frag_cnt = (int)skb_shinfo(skb)->nr_frags;
  1967. seg_cnt = tx_cb->seg_count;
  1968. /*
  1969. * Map the skb buffer first.
  1970. */
  1971. map = pci_map_single(qdev->pdev, skb->data, len, PCI_DMA_TODEVICE);
  1972. err = pci_dma_mapping_error(qdev->pdev, map);
  1973. if (err) {
  1974. netdev_err(qdev->ndev, "PCI mapping failed with error: %d\n",
  1975. err);
  1976. return NETDEV_TX_BUSY;
  1977. }
  1978. oal_entry = (struct oal_entry *)&mac_iocb_ptr->buf_addr0_low;
  1979. oal_entry->dma_lo = cpu_to_le32(LS_64BITS(map));
  1980. oal_entry->dma_hi = cpu_to_le32(MS_64BITS(map));
  1981. oal_entry->len = cpu_to_le32(len);
  1982. dma_unmap_addr_set(&tx_cb->map[seg], mapaddr, map);
  1983. dma_unmap_len_set(&tx_cb->map[seg], maplen, len);
  1984. seg++;
  1985. if (seg_cnt == 1) {
  1986. /* Terminate the last segment. */
  1987. oal_entry->len |= cpu_to_le32(OAL_LAST_ENTRY);
  1988. return NETDEV_TX_OK;
  1989. }
  1990. oal = tx_cb->oal;
  1991. for (completed_segs = 0;
  1992. completed_segs < frag_cnt;
  1993. completed_segs++, seg++) {
  1994. skb_frag_t *frag = &skb_shinfo(skb)->frags[completed_segs];
  1995. oal_entry++;
  1996. /*
  1997. * Check for continuation requirements.
  1998. * It's strange but necessary.
  1999. * Continuation entry points to outbound address list.
  2000. */
  2001. if ((seg == 2 && seg_cnt > 3) ||
  2002. (seg == 7 && seg_cnt > 8) ||
  2003. (seg == 12 && seg_cnt > 13) ||
  2004. (seg == 17 && seg_cnt > 18)) {
  2005. map = pci_map_single(qdev->pdev, oal,
  2006. sizeof(struct oal),
  2007. PCI_DMA_TODEVICE);
  2008. err = pci_dma_mapping_error(qdev->pdev, map);
  2009. if (err) {
  2010. netdev_err(qdev->ndev,
  2011. "PCI mapping outbound address list with error: %d\n",
  2012. err);
  2013. goto map_error;
  2014. }
  2015. oal_entry->dma_lo = cpu_to_le32(LS_64BITS(map));
  2016. oal_entry->dma_hi = cpu_to_le32(MS_64BITS(map));
  2017. oal_entry->len = cpu_to_le32(sizeof(struct oal) |
  2018. OAL_CONT_ENTRY);
  2019. dma_unmap_addr_set(&tx_cb->map[seg], mapaddr, map);
  2020. dma_unmap_len_set(&tx_cb->map[seg], maplen,
  2021. sizeof(struct oal));
  2022. oal_entry = (struct oal_entry *)oal;
  2023. oal++;
  2024. seg++;
  2025. }
  2026. map = pci_map_page(qdev->pdev, frag->page,
  2027. frag->page_offset, frag->size,
  2028. PCI_DMA_TODEVICE);
  2029. err = pci_dma_mapping_error(qdev->pdev, map);
  2030. if (err) {
  2031. netdev_err(qdev->ndev,
  2032. "PCI mapping frags failed with error: %d\n",
  2033. err);
  2034. goto map_error;
  2035. }
  2036. oal_entry->dma_lo = cpu_to_le32(LS_64BITS(map));
  2037. oal_entry->dma_hi = cpu_to_le32(MS_64BITS(map));
  2038. oal_entry->len = cpu_to_le32(frag->size);
  2039. dma_unmap_addr_set(&tx_cb->map[seg], mapaddr, map);
  2040. dma_unmap_len_set(&tx_cb->map[seg], maplen, frag->size);
  2041. }
  2042. /* Terminate the last segment. */
  2043. oal_entry->len |= cpu_to_le32(OAL_LAST_ENTRY);
  2044. return NETDEV_TX_OK;
  2045. map_error:
  2046. /* A PCI mapping failed and now we will need to back out
  2047. * We need to traverse through the oal's and associated pages which
  2048. * have been mapped and now we must unmap them to clean up properly
  2049. */
  2050. seg = 1;
  2051. oal_entry = (struct oal_entry *)&mac_iocb_ptr->buf_addr0_low;
  2052. oal = tx_cb->oal;
  2053. for (i = 0; i < completed_segs; i++, seg++) {
  2054. oal_entry++;
  2055. /*
  2056. * Check for continuation requirements.
  2057. * It's strange but necessary.
  2058. */
  2059. if ((seg == 2 && seg_cnt > 3) ||
  2060. (seg == 7 && seg_cnt > 8) ||
  2061. (seg == 12 && seg_cnt > 13) ||
  2062. (seg == 17 && seg_cnt > 18)) {
  2063. pci_unmap_single(qdev->pdev,
  2064. dma_unmap_addr(&tx_cb->map[seg], mapaddr),
  2065. dma_unmap_len(&tx_cb->map[seg], maplen),
  2066. PCI_DMA_TODEVICE);
  2067. oal++;
  2068. seg++;
  2069. }
  2070. pci_unmap_page(qdev->pdev,
  2071. dma_unmap_addr(&tx_cb->map[seg], mapaddr),
  2072. dma_unmap_len(&tx_cb->map[seg], maplen),
  2073. PCI_DMA_TODEVICE);
  2074. }
  2075. pci_unmap_single(qdev->pdev,
  2076. dma_unmap_addr(&tx_cb->map[0], mapaddr),
  2077. dma_unmap_addr(&tx_cb->map[0], maplen),
  2078. PCI_DMA_TODEVICE);
  2079. return NETDEV_TX_BUSY;
  2080. }
  2081. /*
  2082. * The difference between 3022 and 3032 sends:
  2083. * 3022 only supports a simple single segment transmission.
  2084. * 3032 supports checksumming and scatter/gather lists (fragments).
  2085. * The 3032 supports sglists by using the 3 addr/len pairs (ALP)
  2086. * in the IOCB plus a chain of outbound address lists (OAL) that
  2087. * each contain 5 ALPs. The last ALP of the IOCB (3rd) or OAL (5th)
  2088. * will be used to point to an OAL when more ALP entries are required.
  2089. * The IOCB is always the top of the chain followed by one or more
  2090. * OALs (when necessary).
  2091. */
  2092. static netdev_tx_t ql3xxx_send(struct sk_buff *skb,
  2093. struct net_device *ndev)
  2094. {
  2095. struct ql3_adapter *qdev = netdev_priv(ndev);
  2096. struct ql3xxx_port_registers __iomem *port_regs =
  2097. qdev->mem_map_registers;
  2098. struct ql_tx_buf_cb *tx_cb;
  2099. u32 tot_len = skb->len;
  2100. struct ob_mac_iocb_req *mac_iocb_ptr;
  2101. if (unlikely(atomic_read(&qdev->tx_count) < 2))
  2102. return NETDEV_TX_BUSY;
  2103. tx_cb = &qdev->tx_buf[qdev->req_producer_index];
  2104. tx_cb->seg_count = ql_get_seg_count(qdev,
  2105. skb_shinfo(skb)->nr_frags);
  2106. if (tx_cb->seg_count == -1) {
  2107. netdev_err(ndev, "%s: invalid segment count!\n", __func__);
  2108. return NETDEV_TX_OK;
  2109. }
  2110. mac_iocb_ptr = tx_cb->queue_entry;
  2111. memset((void *)mac_iocb_ptr, 0, sizeof(struct ob_mac_iocb_req));
  2112. mac_iocb_ptr->opcode = qdev->mac_ob_opcode;
  2113. mac_iocb_ptr->flags = OB_MAC_IOCB_REQ_X;
  2114. mac_iocb_ptr->flags |= qdev->mb_bit_mask;
  2115. mac_iocb_ptr->transaction_id = qdev->req_producer_index;
  2116. mac_iocb_ptr->data_len = cpu_to_le16((u16) tot_len);
  2117. tx_cb->skb = skb;
  2118. if (qdev->device_id == QL3032_DEVICE_ID &&
  2119. skb->ip_summed == CHECKSUM_PARTIAL)
  2120. ql_hw_csum_setup(skb, mac_iocb_ptr);
  2121. if (ql_send_map(qdev, mac_iocb_ptr, tx_cb, skb) != NETDEV_TX_OK) {
  2122. netdev_err(ndev, "%s: Could not map the segments!\n", __func__);
  2123. return NETDEV_TX_BUSY;
  2124. }
  2125. wmb();
  2126. qdev->req_producer_index++;
  2127. if (qdev->req_producer_index == NUM_REQ_Q_ENTRIES)
  2128. qdev->req_producer_index = 0;
  2129. wmb();
  2130. ql_write_common_reg_l(qdev,
  2131. &port_regs->CommonRegs.reqQProducerIndex,
  2132. qdev->req_producer_index);
  2133. netif_printk(qdev, tx_queued, KERN_DEBUG, ndev,
  2134. "tx queued, slot %d, len %d\n",
  2135. qdev->req_producer_index, skb->len);
  2136. atomic_dec(&qdev->tx_count);
  2137. return NETDEV_TX_OK;
  2138. }
  2139. static int ql_alloc_net_req_rsp_queues(struct ql3_adapter *qdev)
  2140. {
  2141. qdev->req_q_size =
  2142. (u32) (NUM_REQ_Q_ENTRIES * sizeof(struct ob_mac_iocb_req));
  2143. qdev->req_q_virt_addr =
  2144. pci_alloc_consistent(qdev->pdev,
  2145. (size_t) qdev->req_q_size,
  2146. &qdev->req_q_phy_addr);
  2147. if ((qdev->req_q_virt_addr == NULL) ||
  2148. LS_64BITS(qdev->req_q_phy_addr) & (qdev->req_q_size - 1)) {
  2149. netdev_err(qdev->ndev, "reqQ failed\n");
  2150. return -ENOMEM;
  2151. }
  2152. qdev->rsp_q_size = NUM_RSP_Q_ENTRIES * sizeof(struct net_rsp_iocb);
  2153. qdev->rsp_q_virt_addr =
  2154. pci_alloc_consistent(qdev->pdev,
  2155. (size_t) qdev->rsp_q_size,
  2156. &qdev->rsp_q_phy_addr);
  2157. if ((qdev->rsp_q_virt_addr == NULL) ||
  2158. LS_64BITS(qdev->rsp_q_phy_addr) & (qdev->rsp_q_size - 1)) {
  2159. netdev_err(qdev->ndev, "rspQ allocation failed\n");
  2160. pci_free_consistent(qdev->pdev, (size_t) qdev->req_q_size,
  2161. qdev->req_q_virt_addr,
  2162. qdev->req_q_phy_addr);
  2163. return -ENOMEM;
  2164. }
  2165. set_bit(QL_ALLOC_REQ_RSP_Q_DONE, &qdev->flags);
  2166. return 0;
  2167. }
  2168. static void ql_free_net_req_rsp_queues(struct ql3_adapter *qdev)
  2169. {
  2170. if (!test_bit(QL_ALLOC_REQ_RSP_Q_DONE, &qdev->flags)) {
  2171. netdev_info(qdev->ndev, "Already done\n");
  2172. return;
  2173. }
  2174. pci_free_consistent(qdev->pdev,
  2175. qdev->req_q_size,
  2176. qdev->req_q_virt_addr, qdev->req_q_phy_addr);
  2177. qdev->req_q_virt_addr = NULL;
  2178. pci_free_consistent(qdev->pdev,
  2179. qdev->rsp_q_size,
  2180. qdev->rsp_q_virt_addr, qdev->rsp_q_phy_addr);
  2181. qdev->rsp_q_virt_addr = NULL;
  2182. clear_bit(QL_ALLOC_REQ_RSP_Q_DONE, &qdev->flags);
  2183. }
  2184. static int ql_alloc_buffer_queues(struct ql3_adapter *qdev)
  2185. {
  2186. /* Create Large Buffer Queue */
  2187. qdev->lrg_buf_q_size =
  2188. qdev->num_lbufq_entries * sizeof(struct lrg_buf_q_entry);
  2189. if (qdev->lrg_buf_q_size < PAGE_SIZE)
  2190. qdev->lrg_buf_q_alloc_size = PAGE_SIZE;
  2191. else
  2192. qdev->lrg_buf_q_alloc_size = qdev->lrg_buf_q_size * 2;
  2193. qdev->lrg_buf =
  2194. kmalloc(qdev->num_large_buffers * sizeof(struct ql_rcv_buf_cb),
  2195. GFP_KERNEL);
  2196. if (qdev->lrg_buf == NULL) {
  2197. netdev_err(qdev->ndev, "qdev->lrg_buf alloc failed\n");
  2198. return -ENOMEM;
  2199. }
  2200. qdev->lrg_buf_q_alloc_virt_addr =
  2201. pci_alloc_consistent(qdev->pdev,
  2202. qdev->lrg_buf_q_alloc_size,
  2203. &qdev->lrg_buf_q_alloc_phy_addr);
  2204. if (qdev->lrg_buf_q_alloc_virt_addr == NULL) {
  2205. netdev_err(qdev->ndev, "lBufQ failed\n");
  2206. return -ENOMEM;
  2207. }
  2208. qdev->lrg_buf_q_virt_addr = qdev->lrg_buf_q_alloc_virt_addr;
  2209. qdev->lrg_buf_q_phy_addr = qdev->lrg_buf_q_alloc_phy_addr;
  2210. /* Create Small Buffer Queue */
  2211. qdev->small_buf_q_size =
  2212. NUM_SBUFQ_ENTRIES * sizeof(struct lrg_buf_q_entry);
  2213. if (qdev->small_buf_q_size < PAGE_SIZE)
  2214. qdev->small_buf_q_alloc_size = PAGE_SIZE;
  2215. else
  2216. qdev->small_buf_q_alloc_size = qdev->small_buf_q_size * 2;
  2217. qdev->small_buf_q_alloc_virt_addr =
  2218. pci_alloc_consistent(qdev->pdev,
  2219. qdev->small_buf_q_alloc_size,
  2220. &qdev->small_buf_q_alloc_phy_addr);
  2221. if (qdev->small_buf_q_alloc_virt_addr == NULL) {
  2222. netdev_err(qdev->ndev, "Small Buffer Queue allocation failed\n");
  2223. pci_free_consistent(qdev->pdev, qdev->lrg_buf_q_alloc_size,
  2224. qdev->lrg_buf_q_alloc_virt_addr,
  2225. qdev->lrg_buf_q_alloc_phy_addr);
  2226. return -ENOMEM;
  2227. }
  2228. qdev->small_buf_q_virt_addr = qdev->small_buf_q_alloc_virt_addr;
  2229. qdev->small_buf_q_phy_addr = qdev->small_buf_q_alloc_phy_addr;
  2230. set_bit(QL_ALLOC_BUFQS_DONE, &qdev->flags);
  2231. return 0;
  2232. }
  2233. static void ql_free_buffer_queues(struct ql3_adapter *qdev)
  2234. {
  2235. if (!test_bit(QL_ALLOC_BUFQS_DONE, &qdev->flags)) {
  2236. netdev_info(qdev->ndev, "Already done\n");
  2237. return;
  2238. }
  2239. kfree(qdev->lrg_buf);
  2240. pci_free_consistent(qdev->pdev,
  2241. qdev->lrg_buf_q_alloc_size,
  2242. qdev->lrg_buf_q_alloc_virt_addr,
  2243. qdev->lrg_buf_q_alloc_phy_addr);
  2244. qdev->lrg_buf_q_virt_addr = NULL;
  2245. pci_free_consistent(qdev->pdev,
  2246. qdev->small_buf_q_alloc_size,
  2247. qdev->small_buf_q_alloc_virt_addr,
  2248. qdev->small_buf_q_alloc_phy_addr);
  2249. qdev->small_buf_q_virt_addr = NULL;
  2250. clear_bit(QL_ALLOC_BUFQS_DONE, &qdev->flags);
  2251. }
  2252. static int ql_alloc_small_buffers(struct ql3_adapter *qdev)
  2253. {
  2254. int i;
  2255. struct bufq_addr_element *small_buf_q_entry;
  2256. /* Currently we allocate on one of memory and use it for smallbuffers */
  2257. qdev->small_buf_total_size =
  2258. (QL_ADDR_ELE_PER_BUFQ_ENTRY * NUM_SBUFQ_ENTRIES *
  2259. QL_SMALL_BUFFER_SIZE);
  2260. qdev->small_buf_virt_addr =
  2261. pci_alloc_consistent(qdev->pdev,
  2262. qdev->small_buf_total_size,
  2263. &qdev->small_buf_phy_addr);
  2264. if (qdev->small_buf_virt_addr == NULL) {
  2265. netdev_err(qdev->ndev, "Failed to get small buffer memory\n");
  2266. return -ENOMEM;
  2267. }
  2268. qdev->small_buf_phy_addr_low = LS_64BITS(qdev->small_buf_phy_addr);
  2269. qdev->small_buf_phy_addr_high = MS_64BITS(qdev->small_buf_phy_addr);
  2270. small_buf_q_entry = qdev->small_buf_q_virt_addr;
  2271. /* Initialize the small buffer queue. */
  2272. for (i = 0; i < (QL_ADDR_ELE_PER_BUFQ_ENTRY * NUM_SBUFQ_ENTRIES); i++) {
  2273. small_buf_q_entry->addr_high =
  2274. cpu_to_le32(qdev->small_buf_phy_addr_high);
  2275. small_buf_q_entry->addr_low =
  2276. cpu_to_le32(qdev->small_buf_phy_addr_low +
  2277. (i * QL_SMALL_BUFFER_SIZE));
  2278. small_buf_q_entry++;
  2279. }
  2280. qdev->small_buf_index = 0;
  2281. set_bit(QL_ALLOC_SMALL_BUF_DONE, &qdev->flags);
  2282. return 0;
  2283. }
  2284. static void ql_free_small_buffers(struct ql3_adapter *qdev)
  2285. {
  2286. if (!test_bit(QL_ALLOC_SMALL_BUF_DONE, &qdev->flags)) {
  2287. netdev_info(qdev->ndev, "Already done\n");
  2288. return;
  2289. }
  2290. if (qdev->small_buf_virt_addr != NULL) {
  2291. pci_free_consistent(qdev->pdev,
  2292. qdev->small_buf_total_size,
  2293. qdev->small_buf_virt_addr,
  2294. qdev->small_buf_phy_addr);
  2295. qdev->small_buf_virt_addr = NULL;
  2296. }
  2297. }
  2298. static void ql_free_large_buffers(struct ql3_adapter *qdev)
  2299. {
  2300. int i = 0;
  2301. struct ql_rcv_buf_cb *lrg_buf_cb;
  2302. for (i = 0; i < qdev->num_large_buffers; i++) {
  2303. lrg_buf_cb = &qdev->lrg_buf[i];
  2304. if (lrg_buf_cb->skb) {
  2305. dev_kfree_skb(lrg_buf_cb->skb);
  2306. pci_unmap_single(qdev->pdev,
  2307. dma_unmap_addr(lrg_buf_cb, mapaddr),
  2308. dma_unmap_len(lrg_buf_cb, maplen),
  2309. PCI_DMA_FROMDEVICE);
  2310. memset(lrg_buf_cb, 0, sizeof(struct ql_rcv_buf_cb));
  2311. } else {
  2312. break;
  2313. }
  2314. }
  2315. }
  2316. static void ql_init_large_buffers(struct ql3_adapter *qdev)
  2317. {
  2318. int i;
  2319. struct ql_rcv_buf_cb *lrg_buf_cb;
  2320. struct bufq_addr_element *buf_addr_ele = qdev->lrg_buf_q_virt_addr;
  2321. for (i = 0; i < qdev->num_large_buffers; i++) {
  2322. lrg_buf_cb = &qdev->lrg_buf[i];
  2323. buf_addr_ele->addr_high = lrg_buf_cb->buf_phy_addr_high;
  2324. buf_addr_ele->addr_low = lrg_buf_cb->buf_phy_addr_low;
  2325. buf_addr_ele++;
  2326. }
  2327. qdev->lrg_buf_index = 0;
  2328. qdev->lrg_buf_skb_check = 0;
  2329. }
  2330. static int ql_alloc_large_buffers(struct ql3_adapter *qdev)
  2331. {
  2332. int i;
  2333. struct ql_rcv_buf_cb *lrg_buf_cb;
  2334. struct sk_buff *skb;
  2335. dma_addr_t map;
  2336. int err;
  2337. for (i = 0; i < qdev->num_large_buffers; i++) {
  2338. skb = netdev_alloc_skb(qdev->ndev,
  2339. qdev->lrg_buffer_len);
  2340. if (unlikely(!skb)) {
  2341. /* Better luck next round */
  2342. netdev_err(qdev->ndev,
  2343. "large buff alloc failed for %d bytes at index %d\n",
  2344. qdev->lrg_buffer_len * 2, i);
  2345. ql_free_large_buffers(qdev);
  2346. return -ENOMEM;
  2347. } else {
  2348. lrg_buf_cb = &qdev->lrg_buf[i];
  2349. memset(lrg_buf_cb, 0, sizeof(struct ql_rcv_buf_cb));
  2350. lrg_buf_cb->index = i;
  2351. lrg_buf_cb->skb = skb;
  2352. /*
  2353. * We save some space to copy the ethhdr from first
  2354. * buffer
  2355. */
  2356. skb_reserve(skb, QL_HEADER_SPACE);
  2357. map = pci_map_single(qdev->pdev,
  2358. skb->data,
  2359. qdev->lrg_buffer_len -
  2360. QL_HEADER_SPACE,
  2361. PCI_DMA_FROMDEVICE);
  2362. err = pci_dma_mapping_error(qdev->pdev, map);
  2363. if (err) {
  2364. netdev_err(qdev->ndev,
  2365. "PCI mapping failed with error: %d\n",
  2366. err);
  2367. ql_free_large_buffers(qdev);
  2368. return -ENOMEM;
  2369. }
  2370. dma_unmap_addr_set(lrg_buf_cb, mapaddr, map);
  2371. dma_unmap_len_set(lrg_buf_cb, maplen,
  2372. qdev->lrg_buffer_len -
  2373. QL_HEADER_SPACE);
  2374. lrg_buf_cb->buf_phy_addr_low =
  2375. cpu_to_le32(LS_64BITS(map));
  2376. lrg_buf_cb->buf_phy_addr_high =
  2377. cpu_to_le32(MS_64BITS(map));
  2378. }
  2379. }
  2380. return 0;
  2381. }
  2382. static void ql_free_send_free_list(struct ql3_adapter *qdev)
  2383. {
  2384. struct ql_tx_buf_cb *tx_cb;
  2385. int i;
  2386. tx_cb = &qdev->tx_buf[0];
  2387. for (i = 0; i < NUM_REQ_Q_ENTRIES; i++) {
  2388. kfree(tx_cb->oal);
  2389. tx_cb->oal = NULL;
  2390. tx_cb++;
  2391. }
  2392. }
  2393. static int ql_create_send_free_list(struct ql3_adapter *qdev)
  2394. {
  2395. struct ql_tx_buf_cb *tx_cb;
  2396. int i;
  2397. struct ob_mac_iocb_req *req_q_curr = qdev->req_q_virt_addr;
  2398. /* Create free list of transmit buffers */
  2399. for (i = 0; i < NUM_REQ_Q_ENTRIES; i++) {
  2400. tx_cb = &qdev->tx_buf[i];
  2401. tx_cb->skb = NULL;
  2402. tx_cb->queue_entry = req_q_curr;
  2403. req_q_curr++;
  2404. tx_cb->oal = kmalloc(512, GFP_KERNEL);
  2405. if (tx_cb->oal == NULL)
  2406. return -1;
  2407. }
  2408. return 0;
  2409. }
  2410. static int ql_alloc_mem_resources(struct ql3_adapter *qdev)
  2411. {
  2412. if (qdev->ndev->mtu == NORMAL_MTU_SIZE) {
  2413. qdev->num_lbufq_entries = NUM_LBUFQ_ENTRIES;
  2414. qdev->lrg_buffer_len = NORMAL_MTU_SIZE;
  2415. } else if (qdev->ndev->mtu == JUMBO_MTU_SIZE) {
  2416. /*
  2417. * Bigger buffers, so less of them.
  2418. */
  2419. qdev->num_lbufq_entries = JUMBO_NUM_LBUFQ_ENTRIES;
  2420. qdev->lrg_buffer_len = JUMBO_MTU_SIZE;
  2421. } else {
  2422. netdev_err(qdev->ndev, "Invalid mtu size: %d. Only %d and %d are accepted.\n",
  2423. qdev->ndev->mtu, NORMAL_MTU_SIZE, JUMBO_MTU_SIZE);
  2424. return -ENOMEM;
  2425. }
  2426. qdev->num_large_buffers =
  2427. qdev->num_lbufq_entries * QL_ADDR_ELE_PER_BUFQ_ENTRY;
  2428. qdev->lrg_buffer_len += VLAN_ETH_HLEN + VLAN_ID_LEN + QL_HEADER_SPACE;
  2429. qdev->max_frame_size =
  2430. (qdev->lrg_buffer_len - QL_HEADER_SPACE) + ETHERNET_CRC_SIZE;
  2431. /*
  2432. * First allocate a page of shared memory and use it for shadow
  2433. * locations of Network Request Queue Consumer Address Register and
  2434. * Network Completion Queue Producer Index Register
  2435. */
  2436. qdev->shadow_reg_virt_addr =
  2437. pci_alloc_consistent(qdev->pdev,
  2438. PAGE_SIZE, &qdev->shadow_reg_phy_addr);
  2439. if (qdev->shadow_reg_virt_addr != NULL) {
  2440. qdev->preq_consumer_index = (u16 *) qdev->shadow_reg_virt_addr;
  2441. qdev->req_consumer_index_phy_addr_high =
  2442. MS_64BITS(qdev->shadow_reg_phy_addr);
  2443. qdev->req_consumer_index_phy_addr_low =
  2444. LS_64BITS(qdev->shadow_reg_phy_addr);
  2445. qdev->prsp_producer_index =
  2446. (__le32 *) (((u8 *) qdev->preq_consumer_index) + 8);
  2447. qdev->rsp_producer_index_phy_addr_high =
  2448. qdev->req_consumer_index_phy_addr_high;
  2449. qdev->rsp_producer_index_phy_addr_low =
  2450. qdev->req_consumer_index_phy_addr_low + 8;
  2451. } else {
  2452. netdev_err(qdev->ndev, "shadowReg Alloc failed\n");
  2453. return -ENOMEM;
  2454. }
  2455. if (ql_alloc_net_req_rsp_queues(qdev) != 0) {
  2456. netdev_err(qdev->ndev, "ql_alloc_net_req_rsp_queues failed\n");
  2457. goto err_req_rsp;
  2458. }
  2459. if (ql_alloc_buffer_queues(qdev) != 0) {
  2460. netdev_err(qdev->ndev, "ql_alloc_buffer_queues failed\n");
  2461. goto err_buffer_queues;
  2462. }
  2463. if (ql_alloc_small_buffers(qdev) != 0) {
  2464. netdev_err(qdev->ndev, "ql_alloc_small_buffers failed\n");
  2465. goto err_small_buffers;
  2466. }
  2467. if (ql_alloc_large_buffers(qdev) != 0) {
  2468. netdev_err(qdev->ndev, "ql_alloc_large_buffers failed\n");
  2469. goto err_small_buffers;
  2470. }
  2471. /* Initialize the large buffer queue. */
  2472. ql_init_large_buffers(qdev);
  2473. if (ql_create_send_free_list(qdev))
  2474. goto err_free_list;
  2475. qdev->rsp_current = qdev->rsp_q_virt_addr;
  2476. return 0;
  2477. err_free_list:
  2478. ql_free_send_free_list(qdev);
  2479. err_small_buffers:
  2480. ql_free_buffer_queues(qdev);
  2481. err_buffer_queues:
  2482. ql_free_net_req_rsp_queues(qdev);
  2483. err_req_rsp:
  2484. pci_free_consistent(qdev->pdev,
  2485. PAGE_SIZE,
  2486. qdev->shadow_reg_virt_addr,
  2487. qdev->shadow_reg_phy_addr);
  2488. return -ENOMEM;
  2489. }
  2490. static void ql_free_mem_resources(struct ql3_adapter *qdev)
  2491. {
  2492. ql_free_send_free_list(qdev);
  2493. ql_free_large_buffers(qdev);
  2494. ql_free_small_buffers(qdev);
  2495. ql_free_buffer_queues(qdev);
  2496. ql_free_net_req_rsp_queues(qdev);
  2497. if (qdev->shadow_reg_virt_addr != NULL) {
  2498. pci_free_consistent(qdev->pdev,
  2499. PAGE_SIZE,
  2500. qdev->shadow_reg_virt_addr,
  2501. qdev->shadow_reg_phy_addr);
  2502. qdev->shadow_reg_virt_addr = NULL;
  2503. }
  2504. }
  2505. static int ql_init_misc_registers(struct ql3_adapter *qdev)
  2506. {
  2507. struct ql3xxx_local_ram_registers __iomem *local_ram =
  2508. (void __iomem *)qdev->mem_map_registers;
  2509. if (ql_sem_spinlock(qdev, QL_DDR_RAM_SEM_MASK,
  2510. (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
  2511. 2) << 4))
  2512. return -1;
  2513. ql_write_page2_reg(qdev,
  2514. &local_ram->bufletSize, qdev->nvram_data.bufletSize);
  2515. ql_write_page2_reg(qdev,
  2516. &local_ram->maxBufletCount,
  2517. qdev->nvram_data.bufletCount);
  2518. ql_write_page2_reg(qdev,
  2519. &local_ram->freeBufletThresholdLow,
  2520. (qdev->nvram_data.tcpWindowThreshold25 << 16) |
  2521. (qdev->nvram_data.tcpWindowThreshold0));
  2522. ql_write_page2_reg(qdev,
  2523. &local_ram->freeBufletThresholdHigh,
  2524. qdev->nvram_data.tcpWindowThreshold50);
  2525. ql_write_page2_reg(qdev,
  2526. &local_ram->ipHashTableBase,
  2527. (qdev->nvram_data.ipHashTableBaseHi << 16) |
  2528. qdev->nvram_data.ipHashTableBaseLo);
  2529. ql_write_page2_reg(qdev,
  2530. &local_ram->ipHashTableCount,
  2531. qdev->nvram_data.ipHashTableSize);
  2532. ql_write_page2_reg(qdev,
  2533. &local_ram->tcpHashTableBase,
  2534. (qdev->nvram_data.tcpHashTableBaseHi << 16) |
  2535. qdev->nvram_data.tcpHashTableBaseLo);
  2536. ql_write_page2_reg(qdev,
  2537. &local_ram->tcpHashTableCount,
  2538. qdev->nvram_data.tcpHashTableSize);
  2539. ql_write_page2_reg(qdev,
  2540. &local_ram->ncbBase,
  2541. (qdev->nvram_data.ncbTableBaseHi << 16) |
  2542. qdev->nvram_data.ncbTableBaseLo);
  2543. ql_write_page2_reg(qdev,
  2544. &local_ram->maxNcbCount,
  2545. qdev->nvram_data.ncbTableSize);
  2546. ql_write_page2_reg(qdev,
  2547. &local_ram->drbBase,
  2548. (qdev->nvram_data.drbTableBaseHi << 16) |
  2549. qdev->nvram_data.drbTableBaseLo);
  2550. ql_write_page2_reg(qdev,
  2551. &local_ram->maxDrbCount,
  2552. qdev->nvram_data.drbTableSize);
  2553. ql_sem_unlock(qdev, QL_DDR_RAM_SEM_MASK);
  2554. return 0;
  2555. }
  2556. static int ql_adapter_initialize(struct ql3_adapter *qdev)
  2557. {
  2558. u32 value;
  2559. struct ql3xxx_port_registers __iomem *port_regs =
  2560. qdev->mem_map_registers;
  2561. __iomem u32 *spir = &port_regs->CommonRegs.serialPortInterfaceReg;
  2562. struct ql3xxx_host_memory_registers __iomem *hmem_regs =
  2563. (void __iomem *)port_regs;
  2564. u32 delay = 10;
  2565. int status = 0;
  2566. unsigned long hw_flags = 0;
  2567. if (ql_mii_setup(qdev))
  2568. return -1;
  2569. /* Bring out PHY out of reset */
  2570. ql_write_common_reg(qdev, spir,
  2571. (ISP_SERIAL_PORT_IF_WE |
  2572. (ISP_SERIAL_PORT_IF_WE << 16)));
  2573. /* Give the PHY time to come out of reset. */
  2574. mdelay(100);
  2575. qdev->port_link_state = LS_DOWN;
  2576. netif_carrier_off(qdev->ndev);
  2577. /* V2 chip fix for ARS-39168. */
  2578. ql_write_common_reg(qdev, spir,
  2579. (ISP_SERIAL_PORT_IF_SDE |
  2580. (ISP_SERIAL_PORT_IF_SDE << 16)));
  2581. /* Request Queue Registers */
  2582. *((u32 *)(qdev->preq_consumer_index)) = 0;
  2583. atomic_set(&qdev->tx_count, NUM_REQ_Q_ENTRIES);
  2584. qdev->req_producer_index = 0;
  2585. ql_write_page1_reg(qdev,
  2586. &hmem_regs->reqConsumerIndexAddrHigh,
  2587. qdev->req_consumer_index_phy_addr_high);
  2588. ql_write_page1_reg(qdev,
  2589. &hmem_regs->reqConsumerIndexAddrLow,
  2590. qdev->req_consumer_index_phy_addr_low);
  2591. ql_write_page1_reg(qdev,
  2592. &hmem_regs->reqBaseAddrHigh,
  2593. MS_64BITS(qdev->req_q_phy_addr));
  2594. ql_write_page1_reg(qdev,
  2595. &hmem_regs->reqBaseAddrLow,
  2596. LS_64BITS(qdev->req_q_phy_addr));
  2597. ql_write_page1_reg(qdev, &hmem_regs->reqLength, NUM_REQ_Q_ENTRIES);
  2598. /* Response Queue Registers */
  2599. *((__le16 *) (qdev->prsp_producer_index)) = 0;
  2600. qdev->rsp_consumer_index = 0;
  2601. qdev->rsp_current = qdev->rsp_q_virt_addr;
  2602. ql_write_page1_reg(qdev,
  2603. &hmem_regs->rspProducerIndexAddrHigh,
  2604. qdev->rsp_producer_index_phy_addr_high);
  2605. ql_write_page1_reg(qdev,
  2606. &hmem_regs->rspProducerIndexAddrLow,
  2607. qdev->rsp_producer_index_phy_addr_low);
  2608. ql_write_page1_reg(qdev,
  2609. &hmem_regs->rspBaseAddrHigh,
  2610. MS_64BITS(qdev->rsp_q_phy_addr));
  2611. ql_write_page1_reg(qdev,
  2612. &hmem_regs->rspBaseAddrLow,
  2613. LS_64BITS(qdev->rsp_q_phy_addr));
  2614. ql_write_page1_reg(qdev, &hmem_regs->rspLength, NUM_RSP_Q_ENTRIES);
  2615. /* Large Buffer Queue */
  2616. ql_write_page1_reg(qdev,
  2617. &hmem_regs->rxLargeQBaseAddrHigh,
  2618. MS_64BITS(qdev->lrg_buf_q_phy_addr));
  2619. ql_write_page1_reg(qdev,
  2620. &hmem_regs->rxLargeQBaseAddrLow,
  2621. LS_64BITS(qdev->lrg_buf_q_phy_addr));
  2622. ql_write_page1_reg(qdev,
  2623. &hmem_regs->rxLargeQLength,
  2624. qdev->num_lbufq_entries);
  2625. ql_write_page1_reg(qdev,
  2626. &hmem_regs->rxLargeBufferLength,
  2627. qdev->lrg_buffer_len);
  2628. /* Small Buffer Queue */
  2629. ql_write_page1_reg(qdev,
  2630. &hmem_regs->rxSmallQBaseAddrHigh,
  2631. MS_64BITS(qdev->small_buf_q_phy_addr));
  2632. ql_write_page1_reg(qdev,
  2633. &hmem_regs->rxSmallQBaseAddrLow,
  2634. LS_64BITS(qdev->small_buf_q_phy_addr));
  2635. ql_write_page1_reg(qdev, &hmem_regs->rxSmallQLength, NUM_SBUFQ_ENTRIES);
  2636. ql_write_page1_reg(qdev,
  2637. &hmem_regs->rxSmallBufferLength,
  2638. QL_SMALL_BUFFER_SIZE);
  2639. qdev->small_buf_q_producer_index = NUM_SBUFQ_ENTRIES - 1;
  2640. qdev->small_buf_release_cnt = 8;
  2641. qdev->lrg_buf_q_producer_index = qdev->num_lbufq_entries - 1;
  2642. qdev->lrg_buf_release_cnt = 8;
  2643. qdev->lrg_buf_next_free =
  2644. (struct bufq_addr_element *)qdev->lrg_buf_q_virt_addr;
  2645. qdev->small_buf_index = 0;
  2646. qdev->lrg_buf_index = 0;
  2647. qdev->lrg_buf_free_count = 0;
  2648. qdev->lrg_buf_free_head = NULL;
  2649. qdev->lrg_buf_free_tail = NULL;
  2650. ql_write_common_reg(qdev,
  2651. &port_regs->CommonRegs.
  2652. rxSmallQProducerIndex,
  2653. qdev->small_buf_q_producer_index);
  2654. ql_write_common_reg(qdev,
  2655. &port_regs->CommonRegs.
  2656. rxLargeQProducerIndex,
  2657. qdev->lrg_buf_q_producer_index);
  2658. /*
  2659. * Find out if the chip has already been initialized. If it has, then
  2660. * we skip some of the initialization.
  2661. */
  2662. clear_bit(QL_LINK_MASTER, &qdev->flags);
  2663. value = ql_read_page0_reg(qdev, &port_regs->portStatus);
  2664. if ((value & PORT_STATUS_IC) == 0) {
  2665. /* Chip has not been configured yet, so let it rip. */
  2666. if (ql_init_misc_registers(qdev)) {
  2667. status = -1;
  2668. goto out;
  2669. }
  2670. value = qdev->nvram_data.tcpMaxWindowSize;
  2671. ql_write_page0_reg(qdev, &port_regs->tcpMaxWindow, value);
  2672. value = (0xFFFF << 16) | qdev->nvram_data.extHwConfig;
  2673. if (ql_sem_spinlock(qdev, QL_FLASH_SEM_MASK,
  2674. (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index)
  2675. * 2) << 13)) {
  2676. status = -1;
  2677. goto out;
  2678. }
  2679. ql_write_page0_reg(qdev, &port_regs->ExternalHWConfig, value);
  2680. ql_write_page0_reg(qdev, &port_regs->InternalChipConfig,
  2681. (((INTERNAL_CHIP_SD | INTERNAL_CHIP_WE) <<
  2682. 16) | (INTERNAL_CHIP_SD |
  2683. INTERNAL_CHIP_WE)));
  2684. ql_sem_unlock(qdev, QL_FLASH_SEM_MASK);
  2685. }
  2686. if (qdev->mac_index)
  2687. ql_write_page0_reg(qdev,
  2688. &port_regs->mac1MaxFrameLengthReg,
  2689. qdev->max_frame_size);
  2690. else
  2691. ql_write_page0_reg(qdev,
  2692. &port_regs->mac0MaxFrameLengthReg,
  2693. qdev->max_frame_size);
  2694. if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
  2695. (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
  2696. 2) << 7)) {
  2697. status = -1;
  2698. goto out;
  2699. }
  2700. PHY_Setup(qdev);
  2701. ql_init_scan_mode(qdev);
  2702. ql_get_phy_owner(qdev);
  2703. /* Load the MAC Configuration */
  2704. /* Program lower 32 bits of the MAC address */
  2705. ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
  2706. (MAC_ADDR_INDIRECT_PTR_REG_RP_MASK << 16));
  2707. ql_write_page0_reg(qdev, &port_regs->macAddrDataReg,
  2708. ((qdev->ndev->dev_addr[2] << 24)
  2709. | (qdev->ndev->dev_addr[3] << 16)
  2710. | (qdev->ndev->dev_addr[4] << 8)
  2711. | qdev->ndev->dev_addr[5]));
  2712. /* Program top 16 bits of the MAC address */
  2713. ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
  2714. ((MAC_ADDR_INDIRECT_PTR_REG_RP_MASK << 16) | 1));
  2715. ql_write_page0_reg(qdev, &port_regs->macAddrDataReg,
  2716. ((qdev->ndev->dev_addr[0] << 8)
  2717. | qdev->ndev->dev_addr[1]));
  2718. /* Enable Primary MAC */
  2719. ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
  2720. ((MAC_ADDR_INDIRECT_PTR_REG_PE << 16) |
  2721. MAC_ADDR_INDIRECT_PTR_REG_PE));
  2722. /* Clear Primary and Secondary IP addresses */
  2723. ql_write_page0_reg(qdev, &port_regs->ipAddrIndexReg,
  2724. ((IP_ADDR_INDEX_REG_MASK << 16) |
  2725. (qdev->mac_index << 2)));
  2726. ql_write_page0_reg(qdev, &port_regs->ipAddrDataReg, 0);
  2727. ql_write_page0_reg(qdev, &port_regs->ipAddrIndexReg,
  2728. ((IP_ADDR_INDEX_REG_MASK << 16) |
  2729. ((qdev->mac_index << 2) + 1)));
  2730. ql_write_page0_reg(qdev, &port_regs->ipAddrDataReg, 0);
  2731. ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
  2732. /* Indicate Configuration Complete */
  2733. ql_write_page0_reg(qdev,
  2734. &port_regs->portControl,
  2735. ((PORT_CONTROL_CC << 16) | PORT_CONTROL_CC));
  2736. do {
  2737. value = ql_read_page0_reg(qdev, &port_regs->portStatus);
  2738. if (value & PORT_STATUS_IC)
  2739. break;
  2740. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  2741. msleep(500);
  2742. spin_lock_irqsave(&qdev->hw_lock, hw_flags);
  2743. } while (--delay);
  2744. if (delay == 0) {
  2745. netdev_err(qdev->ndev, "Hw Initialization timeout\n");
  2746. status = -1;
  2747. goto out;
  2748. }
  2749. /* Enable Ethernet Function */
  2750. if (qdev->device_id == QL3032_DEVICE_ID) {
  2751. value =
  2752. (QL3032_PORT_CONTROL_EF | QL3032_PORT_CONTROL_KIE |
  2753. QL3032_PORT_CONTROL_EIv6 | QL3032_PORT_CONTROL_EIv4 |
  2754. QL3032_PORT_CONTROL_ET);
  2755. ql_write_page0_reg(qdev, &port_regs->functionControl,
  2756. ((value << 16) | value));
  2757. } else {
  2758. value =
  2759. (PORT_CONTROL_EF | PORT_CONTROL_ET | PORT_CONTROL_EI |
  2760. PORT_CONTROL_HH);
  2761. ql_write_page0_reg(qdev, &port_regs->portControl,
  2762. ((value << 16) | value));
  2763. }
  2764. out:
  2765. return status;
  2766. }
  2767. /*
  2768. * Caller holds hw_lock.
  2769. */
  2770. static int ql_adapter_reset(struct ql3_adapter *qdev)
  2771. {
  2772. struct ql3xxx_port_registers __iomem *port_regs =
  2773. qdev->mem_map_registers;
  2774. int status = 0;
  2775. u16 value;
  2776. int max_wait_time;
  2777. set_bit(QL_RESET_ACTIVE, &qdev->flags);
  2778. clear_bit(QL_RESET_DONE, &qdev->flags);
  2779. /*
  2780. * Issue soft reset to chip.
  2781. */
  2782. netdev_printk(KERN_DEBUG, qdev->ndev, "Issue soft reset to chip\n");
  2783. ql_write_common_reg(qdev,
  2784. &port_regs->CommonRegs.ispControlStatus,
  2785. ((ISP_CONTROL_SR << 16) | ISP_CONTROL_SR));
  2786. /* Wait 3 seconds for reset to complete. */
  2787. netdev_printk(KERN_DEBUG, qdev->ndev,
  2788. "Wait 10 milliseconds for reset to complete\n");
  2789. /* Wait until the firmware tells us the Soft Reset is done */
  2790. max_wait_time = 5;
  2791. do {
  2792. value =
  2793. ql_read_common_reg(qdev,
  2794. &port_regs->CommonRegs.ispControlStatus);
  2795. if ((value & ISP_CONTROL_SR) == 0)
  2796. break;
  2797. ssleep(1);
  2798. } while ((--max_wait_time));
  2799. /*
  2800. * Also, make sure that the Network Reset Interrupt bit has been
  2801. * cleared after the soft reset has taken place.
  2802. */
  2803. value =
  2804. ql_read_common_reg(qdev, &port_regs->CommonRegs.ispControlStatus);
  2805. if (value & ISP_CONTROL_RI) {
  2806. netdev_printk(KERN_DEBUG, qdev->ndev,
  2807. "clearing RI after reset\n");
  2808. ql_write_common_reg(qdev,
  2809. &port_regs->CommonRegs.
  2810. ispControlStatus,
  2811. ((ISP_CONTROL_RI << 16) | ISP_CONTROL_RI));
  2812. }
  2813. if (max_wait_time == 0) {
  2814. /* Issue Force Soft Reset */
  2815. ql_write_common_reg(qdev,
  2816. &port_regs->CommonRegs.
  2817. ispControlStatus,
  2818. ((ISP_CONTROL_FSR << 16) |
  2819. ISP_CONTROL_FSR));
  2820. /*
  2821. * Wait until the firmware tells us the Force Soft Reset is
  2822. * done
  2823. */
  2824. max_wait_time = 5;
  2825. do {
  2826. value = ql_read_common_reg(qdev,
  2827. &port_regs->CommonRegs.
  2828. ispControlStatus);
  2829. if ((value & ISP_CONTROL_FSR) == 0)
  2830. break;
  2831. ssleep(1);
  2832. } while ((--max_wait_time));
  2833. }
  2834. if (max_wait_time == 0)
  2835. status = 1;
  2836. clear_bit(QL_RESET_ACTIVE, &qdev->flags);
  2837. set_bit(QL_RESET_DONE, &qdev->flags);
  2838. return status;
  2839. }
  2840. static void ql_set_mac_info(struct ql3_adapter *qdev)
  2841. {
  2842. struct ql3xxx_port_registers __iomem *port_regs =
  2843. qdev->mem_map_registers;
  2844. u32 value, port_status;
  2845. u8 func_number;
  2846. /* Get the function number */
  2847. value =
  2848. ql_read_common_reg_l(qdev, &port_regs->CommonRegs.ispControlStatus);
  2849. func_number = (u8) ((value >> 4) & OPCODE_FUNC_ID_MASK);
  2850. port_status = ql_read_page0_reg(qdev, &port_regs->portStatus);
  2851. switch (value & ISP_CONTROL_FN_MASK) {
  2852. case ISP_CONTROL_FN0_NET:
  2853. qdev->mac_index = 0;
  2854. qdev->mac_ob_opcode = OUTBOUND_MAC_IOCB | func_number;
  2855. qdev->mb_bit_mask = FN0_MA_BITS_MASK;
  2856. qdev->PHYAddr = PORT0_PHY_ADDRESS;
  2857. if (port_status & PORT_STATUS_SM0)
  2858. set_bit(QL_LINK_OPTICAL, &qdev->flags);
  2859. else
  2860. clear_bit(QL_LINK_OPTICAL, &qdev->flags);
  2861. break;
  2862. case ISP_CONTROL_FN1_NET:
  2863. qdev->mac_index = 1;
  2864. qdev->mac_ob_opcode = OUTBOUND_MAC_IOCB | func_number;
  2865. qdev->mb_bit_mask = FN1_MA_BITS_MASK;
  2866. qdev->PHYAddr = PORT1_PHY_ADDRESS;
  2867. if (port_status & PORT_STATUS_SM1)
  2868. set_bit(QL_LINK_OPTICAL, &qdev->flags);
  2869. else
  2870. clear_bit(QL_LINK_OPTICAL, &qdev->flags);
  2871. break;
  2872. case ISP_CONTROL_FN0_SCSI:
  2873. case ISP_CONTROL_FN1_SCSI:
  2874. default:
  2875. netdev_printk(KERN_DEBUG, qdev->ndev,
  2876. "Invalid function number, ispControlStatus = 0x%x\n",
  2877. value);
  2878. break;
  2879. }
  2880. qdev->numPorts = qdev->nvram_data.version_and_numPorts >> 8;
  2881. }
  2882. static void ql_display_dev_info(struct net_device *ndev)
  2883. {
  2884. struct ql3_adapter *qdev = netdev_priv(ndev);
  2885. struct pci_dev *pdev = qdev->pdev;
  2886. netdev_info(ndev,
  2887. "%s Adapter %d RevisionID %d found %s on PCI slot %d\n",
  2888. DRV_NAME, qdev->index, qdev->chip_rev_id,
  2889. qdev->device_id == QL3032_DEVICE_ID ? "QLA3032" : "QLA3022",
  2890. qdev->pci_slot);
  2891. netdev_info(ndev, "%s Interface\n",
  2892. test_bit(QL_LINK_OPTICAL, &qdev->flags) ? "OPTICAL" : "COPPER");
  2893. /*
  2894. * Print PCI bus width/type.
  2895. */
  2896. netdev_info(ndev, "Bus interface is %s %s\n",
  2897. ((qdev->pci_width == 64) ? "64-bit" : "32-bit"),
  2898. ((qdev->pci_x) ? "PCI-X" : "PCI"));
  2899. netdev_info(ndev, "mem IO base address adjusted = 0x%p\n",
  2900. qdev->mem_map_registers);
  2901. netdev_info(ndev, "Interrupt number = %d\n", pdev->irq);
  2902. netif_info(qdev, probe, ndev, "MAC address %pM\n", ndev->dev_addr);
  2903. }
  2904. static int ql_adapter_down(struct ql3_adapter *qdev, int do_reset)
  2905. {
  2906. struct net_device *ndev = qdev->ndev;
  2907. int retval = 0;
  2908. netif_stop_queue(ndev);
  2909. netif_carrier_off(ndev);
  2910. clear_bit(QL_ADAPTER_UP, &qdev->flags);
  2911. clear_bit(QL_LINK_MASTER, &qdev->flags);
  2912. ql_disable_interrupts(qdev);
  2913. free_irq(qdev->pdev->irq, ndev);
  2914. if (qdev->msi && test_bit(QL_MSI_ENABLED, &qdev->flags)) {
  2915. netdev_info(qdev->ndev, "calling pci_disable_msi()\n");
  2916. clear_bit(QL_MSI_ENABLED, &qdev->flags);
  2917. pci_disable_msi(qdev->pdev);
  2918. }
  2919. del_timer_sync(&qdev->adapter_timer);
  2920. napi_disable(&qdev->napi);
  2921. if (do_reset) {
  2922. int soft_reset;
  2923. unsigned long hw_flags;
  2924. spin_lock_irqsave(&qdev->hw_lock, hw_flags);
  2925. if (ql_wait_for_drvr_lock(qdev)) {
  2926. soft_reset = ql_adapter_reset(qdev);
  2927. if (soft_reset) {
  2928. netdev_err(ndev, "ql_adapter_reset(%d) FAILED!\n",
  2929. qdev->index);
  2930. }
  2931. netdev_err(ndev,
  2932. "Releasing driver lock via chip reset\n");
  2933. } else {
  2934. netdev_err(ndev,
  2935. "Could not acquire driver lock to do reset!\n");
  2936. retval = -1;
  2937. }
  2938. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  2939. }
  2940. ql_free_mem_resources(qdev);
  2941. return retval;
  2942. }
  2943. static int ql_adapter_up(struct ql3_adapter *qdev)
  2944. {
  2945. struct net_device *ndev = qdev->ndev;
  2946. int err;
  2947. unsigned long irq_flags = IRQF_SHARED;
  2948. unsigned long hw_flags;
  2949. if (ql_alloc_mem_resources(qdev)) {
  2950. netdev_err(ndev, "Unable to allocate buffers\n");
  2951. return -ENOMEM;
  2952. }
  2953. if (qdev->msi) {
  2954. if (pci_enable_msi(qdev->pdev)) {
  2955. netdev_err(ndev,
  2956. "User requested MSI, but MSI failed to initialize. Continuing without MSI.\n");
  2957. qdev->msi = 0;
  2958. } else {
  2959. netdev_info(ndev, "MSI Enabled...\n");
  2960. set_bit(QL_MSI_ENABLED, &qdev->flags);
  2961. irq_flags &= ~IRQF_SHARED;
  2962. }
  2963. }
  2964. err = request_irq(qdev->pdev->irq, ql3xxx_isr,
  2965. irq_flags, ndev->name, ndev);
  2966. if (err) {
  2967. netdev_err(ndev,
  2968. "Failed to reserve interrupt %d - already in use\n",
  2969. qdev->pdev->irq);
  2970. goto err_irq;
  2971. }
  2972. spin_lock_irqsave(&qdev->hw_lock, hw_flags);
  2973. err = ql_wait_for_drvr_lock(qdev);
  2974. if (err) {
  2975. err = ql_adapter_initialize(qdev);
  2976. if (err) {
  2977. netdev_err(ndev, "Unable to initialize adapter\n");
  2978. goto err_init;
  2979. }
  2980. netdev_err(ndev, "Releasing driver lock\n");
  2981. ql_sem_unlock(qdev, QL_DRVR_SEM_MASK);
  2982. } else {
  2983. netdev_err(ndev, "Could not acquire driver lock\n");
  2984. goto err_lock;
  2985. }
  2986. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  2987. set_bit(QL_ADAPTER_UP, &qdev->flags);
  2988. mod_timer(&qdev->adapter_timer, jiffies + HZ * 1);
  2989. napi_enable(&qdev->napi);
  2990. ql_enable_interrupts(qdev);
  2991. return 0;
  2992. err_init:
  2993. ql_sem_unlock(qdev, QL_DRVR_SEM_MASK);
  2994. err_lock:
  2995. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  2996. free_irq(qdev->pdev->irq, ndev);
  2997. err_irq:
  2998. if (qdev->msi && test_bit(QL_MSI_ENABLED, &qdev->flags)) {
  2999. netdev_info(ndev, "calling pci_disable_msi()\n");
  3000. clear_bit(QL_MSI_ENABLED, &qdev->flags);
  3001. pci_disable_msi(qdev->pdev);
  3002. }
  3003. return err;
  3004. }
  3005. static int ql_cycle_adapter(struct ql3_adapter *qdev, int reset)
  3006. {
  3007. if (ql_adapter_down(qdev, reset) || ql_adapter_up(qdev)) {
  3008. netdev_err(qdev->ndev,
  3009. "Driver up/down cycle failed, closing device\n");
  3010. rtnl_lock();
  3011. dev_close(qdev->ndev);
  3012. rtnl_unlock();
  3013. return -1;
  3014. }
  3015. return 0;
  3016. }
  3017. static int ql3xxx_close(struct net_device *ndev)
  3018. {
  3019. struct ql3_adapter *qdev = netdev_priv(ndev);
  3020. /*
  3021. * Wait for device to recover from a reset.
  3022. * (Rarely happens, but possible.)
  3023. */
  3024. while (!test_bit(QL_ADAPTER_UP, &qdev->flags))
  3025. msleep(50);
  3026. ql_adapter_down(qdev, QL_DO_RESET);
  3027. return 0;
  3028. }
  3029. static int ql3xxx_open(struct net_device *ndev)
  3030. {
  3031. struct ql3_adapter *qdev = netdev_priv(ndev);
  3032. return ql_adapter_up(qdev);
  3033. }
  3034. static int ql3xxx_set_mac_address(struct net_device *ndev, void *p)
  3035. {
  3036. struct ql3_adapter *qdev = netdev_priv(ndev);
  3037. struct ql3xxx_port_registers __iomem *port_regs =
  3038. qdev->mem_map_registers;
  3039. struct sockaddr *addr = p;
  3040. unsigned long hw_flags;
  3041. if (netif_running(ndev))
  3042. return -EBUSY;
  3043. if (!is_valid_ether_addr(addr->sa_data))
  3044. return -EADDRNOTAVAIL;
  3045. memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
  3046. spin_lock_irqsave(&qdev->hw_lock, hw_flags);
  3047. /* Program lower 32 bits of the MAC address */
  3048. ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
  3049. (MAC_ADDR_INDIRECT_PTR_REG_RP_MASK << 16));
  3050. ql_write_page0_reg(qdev, &port_regs->macAddrDataReg,
  3051. ((ndev->dev_addr[2] << 24) | (ndev->
  3052. dev_addr[3] << 16) |
  3053. (ndev->dev_addr[4] << 8) | ndev->dev_addr[5]));
  3054. /* Program top 16 bits of the MAC address */
  3055. ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
  3056. ((MAC_ADDR_INDIRECT_PTR_REG_RP_MASK << 16) | 1));
  3057. ql_write_page0_reg(qdev, &port_regs->macAddrDataReg,
  3058. ((ndev->dev_addr[0] << 8) | ndev->dev_addr[1]));
  3059. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  3060. return 0;
  3061. }
  3062. static void ql3xxx_tx_timeout(struct net_device *ndev)
  3063. {
  3064. struct ql3_adapter *qdev = netdev_priv(ndev);
  3065. netdev_err(ndev, "Resetting...\n");
  3066. /*
  3067. * Stop the queues, we've got a problem.
  3068. */
  3069. netif_stop_queue(ndev);
  3070. /*
  3071. * Wake up the worker to process this event.
  3072. */
  3073. queue_delayed_work(qdev->workqueue, &qdev->tx_timeout_work, 0);
  3074. }
  3075. static void ql_reset_work(struct work_struct *work)
  3076. {
  3077. struct ql3_adapter *qdev =
  3078. container_of(work, struct ql3_adapter, reset_work.work);
  3079. struct net_device *ndev = qdev->ndev;
  3080. u32 value;
  3081. struct ql_tx_buf_cb *tx_cb;
  3082. int max_wait_time, i;
  3083. struct ql3xxx_port_registers __iomem *port_regs =
  3084. qdev->mem_map_registers;
  3085. unsigned long hw_flags;
  3086. if (test_bit((QL_RESET_PER_SCSI | QL_RESET_START), &qdev->flags)) {
  3087. clear_bit(QL_LINK_MASTER, &qdev->flags);
  3088. /*
  3089. * Loop through the active list and return the skb.
  3090. */
  3091. for (i = 0; i < NUM_REQ_Q_ENTRIES; i++) {
  3092. int j;
  3093. tx_cb = &qdev->tx_buf[i];
  3094. if (tx_cb->skb) {
  3095. netdev_printk(KERN_DEBUG, ndev,
  3096. "Freeing lost SKB\n");
  3097. pci_unmap_single(qdev->pdev,
  3098. dma_unmap_addr(&tx_cb->map[0],
  3099. mapaddr),
  3100. dma_unmap_len(&tx_cb->map[0], maplen),
  3101. PCI_DMA_TODEVICE);
  3102. for (j = 1; j < tx_cb->seg_count; j++) {
  3103. pci_unmap_page(qdev->pdev,
  3104. dma_unmap_addr(&tx_cb->map[j],
  3105. mapaddr),
  3106. dma_unmap_len(&tx_cb->map[j],
  3107. maplen),
  3108. PCI_DMA_TODEVICE);
  3109. }
  3110. dev_kfree_skb(tx_cb->skb);
  3111. tx_cb->skb = NULL;
  3112. }
  3113. }
  3114. netdev_err(ndev, "Clearing NRI after reset\n");
  3115. spin_lock_irqsave(&qdev->hw_lock, hw_flags);
  3116. ql_write_common_reg(qdev,
  3117. &port_regs->CommonRegs.
  3118. ispControlStatus,
  3119. ((ISP_CONTROL_RI << 16) | ISP_CONTROL_RI));
  3120. /*
  3121. * Wait the for Soft Reset to Complete.
  3122. */
  3123. max_wait_time = 10;
  3124. do {
  3125. value = ql_read_common_reg(qdev,
  3126. &port_regs->CommonRegs.
  3127. ispControlStatus);
  3128. if ((value & ISP_CONTROL_SR) == 0) {
  3129. netdev_printk(KERN_DEBUG, ndev,
  3130. "reset completed\n");
  3131. break;
  3132. }
  3133. if (value & ISP_CONTROL_RI) {
  3134. netdev_printk(KERN_DEBUG, ndev,
  3135. "clearing NRI after reset\n");
  3136. ql_write_common_reg(qdev,
  3137. &port_regs->
  3138. CommonRegs.
  3139. ispControlStatus,
  3140. ((ISP_CONTROL_RI <<
  3141. 16) | ISP_CONTROL_RI));
  3142. }
  3143. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  3144. ssleep(1);
  3145. spin_lock_irqsave(&qdev->hw_lock, hw_flags);
  3146. } while (--max_wait_time);
  3147. spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
  3148. if (value & ISP_CONTROL_SR) {
  3149. /*
  3150. * Set the reset flags and clear the board again.
  3151. * Nothing else to do...
  3152. */
  3153. netdev_err(ndev,
  3154. "Timed out waiting for reset to complete\n");
  3155. netdev_err(ndev, "Do a reset\n");
  3156. clear_bit(QL_RESET_PER_SCSI, &qdev->flags);
  3157. clear_bit(QL_RESET_START, &qdev->flags);
  3158. ql_cycle_adapter(qdev, QL_DO_RESET);
  3159. return;
  3160. }
  3161. clear_bit(QL_RESET_ACTIVE, &qdev->flags);
  3162. clear_bit(QL_RESET_PER_SCSI, &qdev->flags);
  3163. clear_bit(QL_RESET_START, &qdev->flags);
  3164. ql_cycle_adapter(qdev, QL_NO_RESET);
  3165. }
  3166. }
  3167. static void ql_tx_timeout_work(struct work_struct *work)
  3168. {
  3169. struct ql3_adapter *qdev =
  3170. container_of(work, struct ql3_adapter, tx_timeout_work.work);
  3171. ql_cycle_adapter(qdev, QL_DO_RESET);
  3172. }
  3173. static void ql_get_board_info(struct ql3_adapter *qdev)
  3174. {
  3175. struct ql3xxx_port_registers __iomem *port_regs =
  3176. qdev->mem_map_registers;
  3177. u32 value;
  3178. value = ql_read_page0_reg_l(qdev, &port_regs->portStatus);
  3179. qdev->chip_rev_id = ((value & PORT_STATUS_REV_ID_MASK) >> 12);
  3180. if (value & PORT_STATUS_64)
  3181. qdev->pci_width = 64;
  3182. else
  3183. qdev->pci_width = 32;
  3184. if (value & PORT_STATUS_X)
  3185. qdev->pci_x = 1;
  3186. else
  3187. qdev->pci_x = 0;
  3188. qdev->pci_slot = (u8) PCI_SLOT(qdev->pdev->devfn);
  3189. }
  3190. static void ql3xxx_timer(unsigned long ptr)
  3191. {
  3192. struct ql3_adapter *qdev = (struct ql3_adapter *)ptr;
  3193. queue_delayed_work(qdev->workqueue, &qdev->link_state_work, 0);
  3194. }
  3195. static const struct net_device_ops ql3xxx_netdev_ops = {
  3196. .ndo_open = ql3xxx_open,
  3197. .ndo_start_xmit = ql3xxx_send,
  3198. .ndo_stop = ql3xxx_close,
  3199. .ndo_set_multicast_list = NULL, /* not allowed on NIC side */
  3200. .ndo_change_mtu = eth_change_mtu,
  3201. .ndo_validate_addr = eth_validate_addr,
  3202. .ndo_set_mac_address = ql3xxx_set_mac_address,
  3203. .ndo_tx_timeout = ql3xxx_tx_timeout,
  3204. };
  3205. static int __devinit ql3xxx_probe(struct pci_dev *pdev,
  3206. const struct pci_device_id *pci_entry)
  3207. {
  3208. struct net_device *ndev = NULL;
  3209. struct ql3_adapter *qdev = NULL;
  3210. static int cards_found;
  3211. int uninitialized_var(pci_using_dac), err;
  3212. err = pci_enable_device(pdev);
  3213. if (err) {
  3214. pr_err("%s cannot enable PCI device\n", pci_name(pdev));
  3215. goto err_out;
  3216. }
  3217. err = pci_request_regions(pdev, DRV_NAME);
  3218. if (err) {
  3219. pr_err("%s cannot obtain PCI resources\n", pci_name(pdev));
  3220. goto err_out_disable_pdev;
  3221. }
  3222. pci_set_master(pdev);
  3223. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  3224. pci_using_dac = 1;
  3225. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  3226. } else if (!(err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)))) {
  3227. pci_using_dac = 0;
  3228. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  3229. }
  3230. if (err) {
  3231. pr_err("%s no usable DMA configuration\n", pci_name(pdev));
  3232. goto err_out_free_regions;
  3233. }
  3234. ndev = alloc_etherdev(sizeof(struct ql3_adapter));
  3235. if (!ndev) {
  3236. pr_err("%s could not alloc etherdev\n", pci_name(pdev));
  3237. err = -ENOMEM;
  3238. goto err_out_free_regions;
  3239. }
  3240. SET_NETDEV_DEV(ndev, &pdev->dev);
  3241. pci_set_drvdata(pdev, ndev);
  3242. qdev = netdev_priv(ndev);
  3243. qdev->index = cards_found;
  3244. qdev->ndev = ndev;
  3245. qdev->pdev = pdev;
  3246. qdev->device_id = pci_entry->device;
  3247. qdev->port_link_state = LS_DOWN;
  3248. if (msi)
  3249. qdev->msi = 1;
  3250. qdev->msg_enable = netif_msg_init(debug, default_msg);
  3251. if (pci_using_dac)
  3252. ndev->features |= NETIF_F_HIGHDMA;
  3253. if (qdev->device_id == QL3032_DEVICE_ID)
  3254. ndev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
  3255. qdev->mem_map_registers = pci_ioremap_bar(pdev, 1);
  3256. if (!qdev->mem_map_registers) {
  3257. pr_err("%s: cannot map device registers\n", pci_name(pdev));
  3258. err = -EIO;
  3259. goto err_out_free_ndev;
  3260. }
  3261. spin_lock_init(&qdev->adapter_lock);
  3262. spin_lock_init(&qdev->hw_lock);
  3263. /* Set driver entry points */
  3264. ndev->netdev_ops = &ql3xxx_netdev_ops;
  3265. SET_ETHTOOL_OPS(ndev, &ql3xxx_ethtool_ops);
  3266. ndev->watchdog_timeo = 5 * HZ;
  3267. netif_napi_add(ndev, &qdev->napi, ql_poll, 64);
  3268. ndev->irq = pdev->irq;
  3269. /* make sure the EEPROM is good */
  3270. if (ql_get_nvram_params(qdev)) {
  3271. pr_alert("%s: Adapter #%d, Invalid NVRAM parameters\n",
  3272. __func__, qdev->index);
  3273. err = -EIO;
  3274. goto err_out_iounmap;
  3275. }
  3276. ql_set_mac_info(qdev);
  3277. /* Validate and set parameters */
  3278. if (qdev->mac_index) {
  3279. ndev->mtu = qdev->nvram_data.macCfg_port1.etherMtu_mac ;
  3280. ql_set_mac_addr(ndev, qdev->nvram_data.funcCfg_fn2.macAddress);
  3281. } else {
  3282. ndev->mtu = qdev->nvram_data.macCfg_port0.etherMtu_mac ;
  3283. ql_set_mac_addr(ndev, qdev->nvram_data.funcCfg_fn0.macAddress);
  3284. }
  3285. memcpy(ndev->perm_addr, ndev->dev_addr, ndev->addr_len);
  3286. ndev->tx_queue_len = NUM_REQ_Q_ENTRIES;
  3287. /* Record PCI bus information. */
  3288. ql_get_board_info(qdev);
  3289. /*
  3290. * Set the Maximum Memory Read Byte Count value. We do this to handle
  3291. * jumbo frames.
  3292. */
  3293. if (qdev->pci_x)
  3294. pci_write_config_word(pdev, (int)0x4e, (u16) 0x0036);
  3295. err = register_netdev(ndev);
  3296. if (err) {
  3297. pr_err("%s: cannot register net device\n", pci_name(pdev));
  3298. goto err_out_iounmap;
  3299. }
  3300. /* we're going to reset, so assume we have no link for now */
  3301. netif_carrier_off(ndev);
  3302. netif_stop_queue(ndev);
  3303. qdev->workqueue = create_singlethread_workqueue(ndev->name);
  3304. INIT_DELAYED_WORK(&qdev->reset_work, ql_reset_work);
  3305. INIT_DELAYED_WORK(&qdev->tx_timeout_work, ql_tx_timeout_work);
  3306. INIT_DELAYED_WORK(&qdev->link_state_work, ql_link_state_machine_work);
  3307. init_timer(&qdev->adapter_timer);
  3308. qdev->adapter_timer.function = ql3xxx_timer;
  3309. qdev->adapter_timer.expires = jiffies + HZ * 2; /* two second delay */
  3310. qdev->adapter_timer.data = (unsigned long)qdev;
  3311. if (!cards_found) {
  3312. pr_alert("%s\n", DRV_STRING);
  3313. pr_alert("Driver name: %s, Version: %s\n",
  3314. DRV_NAME, DRV_VERSION);
  3315. }
  3316. ql_display_dev_info(ndev);
  3317. cards_found++;
  3318. return 0;
  3319. err_out_iounmap:
  3320. iounmap(qdev->mem_map_registers);
  3321. err_out_free_ndev:
  3322. free_netdev(ndev);
  3323. err_out_free_regions:
  3324. pci_release_regions(pdev);
  3325. err_out_disable_pdev:
  3326. pci_disable_device(pdev);
  3327. pci_set_drvdata(pdev, NULL);
  3328. err_out:
  3329. return err;
  3330. }
  3331. static void __devexit ql3xxx_remove(struct pci_dev *pdev)
  3332. {
  3333. struct net_device *ndev = pci_get_drvdata(pdev);
  3334. struct ql3_adapter *qdev = netdev_priv(ndev);
  3335. unregister_netdev(ndev);
  3336. ql_disable_interrupts(qdev);
  3337. if (qdev->workqueue) {
  3338. cancel_delayed_work(&qdev->reset_work);
  3339. cancel_delayed_work(&qdev->tx_timeout_work);
  3340. destroy_workqueue(qdev->workqueue);
  3341. qdev->workqueue = NULL;
  3342. }
  3343. iounmap(qdev->mem_map_registers);
  3344. pci_release_regions(pdev);
  3345. pci_set_drvdata(pdev, NULL);
  3346. free_netdev(ndev);
  3347. }
  3348. static struct pci_driver ql3xxx_driver = {
  3349. .name = DRV_NAME,
  3350. .id_table = ql3xxx_pci_tbl,
  3351. .probe = ql3xxx_probe,
  3352. .remove = __devexit_p(ql3xxx_remove),
  3353. };
  3354. static int __init ql3xxx_init_module(void)
  3355. {
  3356. return pci_register_driver(&ql3xxx_driver);
  3357. }
  3358. static void __exit ql3xxx_exit(void)
  3359. {
  3360. pci_unregister_driver(&ql3xxx_driver);
  3361. }
  3362. module_init(ql3xxx_init_module);
  3363. module_exit(ql3xxx_exit);