qla_init.c 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2008 QLogic Corporation
  4. *
  5. * See LICENSE.qla2xxx for copyright and licensing details.
  6. */
  7. #include "qla_def.h"
  8. #include <linux/delay.h>
  9. #include <linux/vmalloc.h>
  10. #include "qla_devtbl.h"
  11. #ifdef CONFIG_SPARC
  12. #include <asm/prom.h>
  13. #endif
  14. /* XXX(hch): this is ugly, but we don't want to pull in exioctl.h */
  15. #ifndef EXT_IS_LUN_BIT_SET
  16. #define EXT_IS_LUN_BIT_SET(P,L) \
  17. (((P)->mask[L/8] & (0x80 >> (L%8)))?1:0)
  18. #define EXT_SET_LUN_BIT(P,L) \
  19. ((P)->mask[L/8] |= (0x80 >> (L%8)))
  20. #endif
  21. /*
  22. * QLogic ISP2x00 Hardware Support Function Prototypes.
  23. */
  24. static int qla2x00_isp_firmware(scsi_qla_host_t *);
  25. static void qla2x00_resize_request_q(scsi_qla_host_t *);
  26. static int qla2x00_setup_chip(scsi_qla_host_t *);
  27. static void qla2x00_init_response_q_entries(scsi_qla_host_t *);
  28. static int qla2x00_init_rings(scsi_qla_host_t *);
  29. static int qla2x00_fw_ready(scsi_qla_host_t *);
  30. static int qla2x00_configure_hba(scsi_qla_host_t *);
  31. static int qla2x00_configure_loop(scsi_qla_host_t *);
  32. static int qla2x00_configure_local_loop(scsi_qla_host_t *);
  33. static int qla2x00_configure_fabric(scsi_qla_host_t *);
  34. static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *, struct list_head *);
  35. static int qla2x00_device_resync(scsi_qla_host_t *);
  36. static int qla2x00_fabric_dev_login(scsi_qla_host_t *, fc_port_t *,
  37. uint16_t *);
  38. static int qla2x00_restart_isp(scsi_qla_host_t *);
  39. static int qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev);
  40. /****************************************************************************/
  41. /* QLogic ISP2x00 Hardware Support Functions. */
  42. /****************************************************************************/
  43. /*
  44. * qla2x00_initialize_adapter
  45. * Initialize board.
  46. *
  47. * Input:
  48. * ha = adapter block pointer.
  49. *
  50. * Returns:
  51. * 0 = success
  52. */
  53. int
  54. qla2x00_initialize_adapter(scsi_qla_host_t *ha)
  55. {
  56. int rval;
  57. /* Clear adapter flags. */
  58. ha->flags.online = 0;
  59. ha->flags.reset_active = 0;
  60. atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
  61. atomic_set(&ha->loop_state, LOOP_DOWN);
  62. ha->device_flags = DFLG_NO_CABLE;
  63. ha->dpc_flags = 0;
  64. ha->flags.management_server_logged_in = 0;
  65. ha->marker_needed = 0;
  66. ha->mbx_flags = 0;
  67. ha->isp_abort_cnt = 0;
  68. ha->beacon_blink_led = 0;
  69. set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
  70. qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
  71. rval = ha->isp_ops->pci_config(ha);
  72. if (rval) {
  73. DEBUG2(printk("scsi(%ld): Unable to configure PCI space.\n",
  74. ha->host_no));
  75. return (rval);
  76. }
  77. ha->isp_ops->reset_chip(ha);
  78. ha->isp_ops->get_flash_version(ha, ha->request_ring);
  79. qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
  80. ha->isp_ops->nvram_config(ha);
  81. if (ha->flags.disable_serdes) {
  82. /* Mask HBA via NVRAM settings? */
  83. qla_printk(KERN_INFO, ha, "Masking HBA WWPN "
  84. "%02x%02x%02x%02x%02x%02x%02x%02x (via NVRAM).\n",
  85. ha->port_name[0], ha->port_name[1],
  86. ha->port_name[2], ha->port_name[3],
  87. ha->port_name[4], ha->port_name[5],
  88. ha->port_name[6], ha->port_name[7]);
  89. return QLA_FUNCTION_FAILED;
  90. }
  91. qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
  92. if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
  93. rval = ha->isp_ops->chip_diag(ha);
  94. if (rval)
  95. return (rval);
  96. rval = qla2x00_setup_chip(ha);
  97. if (rval)
  98. return (rval);
  99. }
  100. rval = qla2x00_init_rings(ha);
  101. return (rval);
  102. }
  103. /**
  104. * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
  105. * @ha: HA context
  106. *
  107. * Returns 0 on success.
  108. */
  109. int
  110. qla2100_pci_config(scsi_qla_host_t *ha)
  111. {
  112. uint16_t w;
  113. uint32_t d;
  114. unsigned long flags;
  115. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  116. pci_set_master(ha->pdev);
  117. pci_try_set_mwi(ha->pdev);
  118. pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
  119. w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
  120. pci_write_config_word(ha->pdev, PCI_COMMAND, w);
  121. /* Reset expansion ROM address decode enable */
  122. pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
  123. d &= ~PCI_ROM_ADDRESS_ENABLE;
  124. pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
  125. /* Get PCI bus information. */
  126. spin_lock_irqsave(&ha->hardware_lock, flags);
  127. ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
  128. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  129. return QLA_SUCCESS;
  130. }
  131. /**
  132. * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
  133. * @ha: HA context
  134. *
  135. * Returns 0 on success.
  136. */
  137. int
  138. qla2300_pci_config(scsi_qla_host_t *ha)
  139. {
  140. uint16_t w;
  141. uint32_t d;
  142. unsigned long flags = 0;
  143. uint32_t cnt;
  144. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  145. pci_set_master(ha->pdev);
  146. pci_try_set_mwi(ha->pdev);
  147. pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
  148. w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
  149. if (IS_QLA2322(ha) || IS_QLA6322(ha))
  150. w &= ~PCI_COMMAND_INTX_DISABLE;
  151. pci_write_config_word(ha->pdev, PCI_COMMAND, w);
  152. /*
  153. * If this is a 2300 card and not 2312, reset the
  154. * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
  155. * the 2310 also reports itself as a 2300 so we need to get the
  156. * fb revision level -- a 6 indicates it really is a 2300 and
  157. * not a 2310.
  158. */
  159. if (IS_QLA2300(ha)) {
  160. spin_lock_irqsave(&ha->hardware_lock, flags);
  161. /* Pause RISC. */
  162. WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
  163. for (cnt = 0; cnt < 30000; cnt++) {
  164. if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
  165. break;
  166. udelay(10);
  167. }
  168. /* Select FPM registers. */
  169. WRT_REG_WORD(&reg->ctrl_status, 0x20);
  170. RD_REG_WORD(&reg->ctrl_status);
  171. /* Get the fb rev level */
  172. ha->fb_rev = RD_FB_CMD_REG(ha, reg);
  173. if (ha->fb_rev == FPM_2300)
  174. pci_clear_mwi(ha->pdev);
  175. /* Deselect FPM registers. */
  176. WRT_REG_WORD(&reg->ctrl_status, 0x0);
  177. RD_REG_WORD(&reg->ctrl_status);
  178. /* Release RISC module. */
  179. WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
  180. for (cnt = 0; cnt < 30000; cnt++) {
  181. if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
  182. break;
  183. udelay(10);
  184. }
  185. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  186. }
  187. pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
  188. /* Reset expansion ROM address decode enable */
  189. pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
  190. d &= ~PCI_ROM_ADDRESS_ENABLE;
  191. pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
  192. /* Get PCI bus information. */
  193. spin_lock_irqsave(&ha->hardware_lock, flags);
  194. ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
  195. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  196. return QLA_SUCCESS;
  197. }
  198. /**
  199. * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
  200. * @ha: HA context
  201. *
  202. * Returns 0 on success.
  203. */
  204. int
  205. qla24xx_pci_config(scsi_qla_host_t *ha)
  206. {
  207. uint16_t w;
  208. uint32_t d;
  209. unsigned long flags = 0;
  210. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  211. pci_set_master(ha->pdev);
  212. pci_try_set_mwi(ha->pdev);
  213. pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
  214. w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
  215. w &= ~PCI_COMMAND_INTX_DISABLE;
  216. pci_write_config_word(ha->pdev, PCI_COMMAND, w);
  217. pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
  218. /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
  219. if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
  220. pcix_set_mmrbc(ha->pdev, 2048);
  221. /* PCIe -- adjust Maximum Read Request Size (2048). */
  222. if (pci_find_capability(ha->pdev, PCI_CAP_ID_EXP))
  223. pcie_set_readrq(ha->pdev, 2048);
  224. /* Reset expansion ROM address decode enable */
  225. pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
  226. d &= ~PCI_ROM_ADDRESS_ENABLE;
  227. pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
  228. ha->chip_revision = ha->pdev->revision;
  229. /* Get PCI bus information. */
  230. spin_lock_irqsave(&ha->hardware_lock, flags);
  231. ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
  232. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  233. return QLA_SUCCESS;
  234. }
  235. /**
  236. * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
  237. * @ha: HA context
  238. *
  239. * Returns 0 on success.
  240. */
  241. int
  242. qla25xx_pci_config(scsi_qla_host_t *ha)
  243. {
  244. uint16_t w;
  245. uint32_t d;
  246. pci_set_master(ha->pdev);
  247. pci_try_set_mwi(ha->pdev);
  248. pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
  249. w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
  250. w &= ~PCI_COMMAND_INTX_DISABLE;
  251. pci_write_config_word(ha->pdev, PCI_COMMAND, w);
  252. /* PCIe -- adjust Maximum Read Request Size (2048). */
  253. if (pci_find_capability(ha->pdev, PCI_CAP_ID_EXP))
  254. pcie_set_readrq(ha->pdev, 2048);
  255. /* Reset expansion ROM address decode enable */
  256. pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
  257. d &= ~PCI_ROM_ADDRESS_ENABLE;
  258. pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
  259. ha->chip_revision = ha->pdev->revision;
  260. return QLA_SUCCESS;
  261. }
  262. /**
  263. * qla2x00_isp_firmware() - Choose firmware image.
  264. * @ha: HA context
  265. *
  266. * Returns 0 on success.
  267. */
  268. static int
  269. qla2x00_isp_firmware(scsi_qla_host_t *ha)
  270. {
  271. int rval;
  272. /* Assume loading risc code */
  273. rval = QLA_FUNCTION_FAILED;
  274. if (ha->flags.disable_risc_code_load) {
  275. DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
  276. ha->host_no));
  277. qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
  278. /* Verify checksum of loaded RISC code. */
  279. rval = qla2x00_verify_checksum(ha, ha->fw_srisc_address);
  280. }
  281. if (rval) {
  282. DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
  283. ha->host_no));
  284. }
  285. return (rval);
  286. }
  287. /**
  288. * qla2x00_reset_chip() - Reset ISP chip.
  289. * @ha: HA context
  290. *
  291. * Returns 0 on success.
  292. */
  293. void
  294. qla2x00_reset_chip(scsi_qla_host_t *ha)
  295. {
  296. unsigned long flags = 0;
  297. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  298. uint32_t cnt;
  299. uint16_t cmd;
  300. ha->isp_ops->disable_intrs(ha);
  301. spin_lock_irqsave(&ha->hardware_lock, flags);
  302. /* Turn off master enable */
  303. cmd = 0;
  304. pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
  305. cmd &= ~PCI_COMMAND_MASTER;
  306. pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
  307. if (!IS_QLA2100(ha)) {
  308. /* Pause RISC. */
  309. WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
  310. if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
  311. for (cnt = 0; cnt < 30000; cnt++) {
  312. if ((RD_REG_WORD(&reg->hccr) &
  313. HCCR_RISC_PAUSE) != 0)
  314. break;
  315. udelay(100);
  316. }
  317. } else {
  318. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  319. udelay(10);
  320. }
  321. /* Select FPM registers. */
  322. WRT_REG_WORD(&reg->ctrl_status, 0x20);
  323. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  324. /* FPM Soft Reset. */
  325. WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
  326. RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
  327. /* Toggle Fpm Reset. */
  328. if (!IS_QLA2200(ha)) {
  329. WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
  330. RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
  331. }
  332. /* Select frame buffer registers. */
  333. WRT_REG_WORD(&reg->ctrl_status, 0x10);
  334. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  335. /* Reset frame buffer FIFOs. */
  336. if (IS_QLA2200(ha)) {
  337. WRT_FB_CMD_REG(ha, reg, 0xa000);
  338. RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
  339. } else {
  340. WRT_FB_CMD_REG(ha, reg, 0x00fc);
  341. /* Read back fb_cmd until zero or 3 seconds max */
  342. for (cnt = 0; cnt < 3000; cnt++) {
  343. if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
  344. break;
  345. udelay(100);
  346. }
  347. }
  348. /* Select RISC module registers. */
  349. WRT_REG_WORD(&reg->ctrl_status, 0);
  350. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  351. /* Reset RISC processor. */
  352. WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
  353. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  354. /* Release RISC processor. */
  355. WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
  356. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  357. }
  358. WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
  359. WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
  360. /* Reset ISP chip. */
  361. WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
  362. /* Wait for RISC to recover from reset. */
  363. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  364. /*
  365. * It is necessary to for a delay here since the card doesn't
  366. * respond to PCI reads during a reset. On some architectures
  367. * this will result in an MCA.
  368. */
  369. udelay(20);
  370. for (cnt = 30000; cnt; cnt--) {
  371. if ((RD_REG_WORD(&reg->ctrl_status) &
  372. CSR_ISP_SOFT_RESET) == 0)
  373. break;
  374. udelay(100);
  375. }
  376. } else
  377. udelay(10);
  378. /* Reset RISC processor. */
  379. WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
  380. WRT_REG_WORD(&reg->semaphore, 0);
  381. /* Release RISC processor. */
  382. WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
  383. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  384. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  385. for (cnt = 0; cnt < 30000; cnt++) {
  386. if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
  387. break;
  388. udelay(100);
  389. }
  390. } else
  391. udelay(100);
  392. /* Turn on master enable */
  393. cmd |= PCI_COMMAND_MASTER;
  394. pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
  395. /* Disable RISC pause on FPM parity error. */
  396. if (!IS_QLA2100(ha)) {
  397. WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
  398. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  399. }
  400. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  401. }
  402. /**
  403. * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
  404. * @ha: HA context
  405. *
  406. * Returns 0 on success.
  407. */
  408. static inline void
  409. qla24xx_reset_risc(scsi_qla_host_t *ha)
  410. {
  411. int hw_evt = 0;
  412. unsigned long flags = 0;
  413. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  414. uint32_t cnt, d2;
  415. uint16_t wd;
  416. spin_lock_irqsave(&ha->hardware_lock, flags);
  417. /* Reset RISC. */
  418. WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
  419. for (cnt = 0; cnt < 30000; cnt++) {
  420. if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
  421. break;
  422. udelay(10);
  423. }
  424. WRT_REG_DWORD(&reg->ctrl_status,
  425. CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
  426. pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
  427. udelay(100);
  428. /* Wait for firmware to complete NVRAM accesses. */
  429. d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
  430. for (cnt = 10000 ; cnt && d2; cnt--) {
  431. udelay(5);
  432. d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
  433. barrier();
  434. }
  435. if (cnt == 0)
  436. hw_evt = 1;
  437. /* Wait for soft-reset to complete. */
  438. d2 = RD_REG_DWORD(&reg->ctrl_status);
  439. for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
  440. udelay(5);
  441. d2 = RD_REG_DWORD(&reg->ctrl_status);
  442. barrier();
  443. }
  444. if (cnt == 0 || hw_evt)
  445. qla2xxx_hw_event_log(ha, HW_EVENT_RESET_ERR,
  446. RD_REG_WORD(&reg->mailbox1), RD_REG_WORD(&reg->mailbox2),
  447. RD_REG_WORD(&reg->mailbox3));
  448. WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
  449. RD_REG_DWORD(&reg->hccr);
  450. WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
  451. RD_REG_DWORD(&reg->hccr);
  452. WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
  453. RD_REG_DWORD(&reg->hccr);
  454. d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
  455. for (cnt = 6000000 ; cnt && d2; cnt--) {
  456. udelay(5);
  457. d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
  458. barrier();
  459. }
  460. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  461. }
  462. /**
  463. * qla24xx_reset_chip() - Reset ISP24xx chip.
  464. * @ha: HA context
  465. *
  466. * Returns 0 on success.
  467. */
  468. void
  469. qla24xx_reset_chip(scsi_qla_host_t *ha)
  470. {
  471. ha->isp_ops->disable_intrs(ha);
  472. /* Perform RISC reset. */
  473. qla24xx_reset_risc(ha);
  474. }
  475. /**
  476. * qla2x00_chip_diag() - Test chip for proper operation.
  477. * @ha: HA context
  478. *
  479. * Returns 0 on success.
  480. */
  481. int
  482. qla2x00_chip_diag(scsi_qla_host_t *ha)
  483. {
  484. int rval;
  485. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  486. unsigned long flags = 0;
  487. uint16_t data;
  488. uint32_t cnt;
  489. uint16_t mb[5];
  490. /* Assume a failed state */
  491. rval = QLA_FUNCTION_FAILED;
  492. DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
  493. ha->host_no, (u_long)&reg->flash_address));
  494. spin_lock_irqsave(&ha->hardware_lock, flags);
  495. /* Reset ISP chip. */
  496. WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
  497. /*
  498. * We need to have a delay here since the card will not respond while
  499. * in reset causing an MCA on some architectures.
  500. */
  501. udelay(20);
  502. data = qla2x00_debounce_register(&reg->ctrl_status);
  503. for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
  504. udelay(5);
  505. data = RD_REG_WORD(&reg->ctrl_status);
  506. barrier();
  507. }
  508. if (!cnt)
  509. goto chip_diag_failed;
  510. DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
  511. ha->host_no));
  512. /* Reset RISC processor. */
  513. WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
  514. WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
  515. /* Workaround for QLA2312 PCI parity error */
  516. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  517. data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
  518. for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
  519. udelay(5);
  520. data = RD_MAILBOX_REG(ha, reg, 0);
  521. barrier();
  522. }
  523. } else
  524. udelay(10);
  525. if (!cnt)
  526. goto chip_diag_failed;
  527. /* Check product ID of chip */
  528. DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
  529. mb[1] = RD_MAILBOX_REG(ha, reg, 1);
  530. mb[2] = RD_MAILBOX_REG(ha, reg, 2);
  531. mb[3] = RD_MAILBOX_REG(ha, reg, 3);
  532. mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
  533. if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
  534. mb[3] != PROD_ID_3) {
  535. qla_printk(KERN_WARNING, ha,
  536. "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
  537. goto chip_diag_failed;
  538. }
  539. ha->product_id[0] = mb[1];
  540. ha->product_id[1] = mb[2];
  541. ha->product_id[2] = mb[3];
  542. ha->product_id[3] = mb[4];
  543. /* Adjust fw RISC transfer size */
  544. if (ha->request_q_length > 1024)
  545. ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
  546. else
  547. ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
  548. ha->request_q_length;
  549. if (IS_QLA2200(ha) &&
  550. RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
  551. /* Limit firmware transfer size with a 2200A */
  552. DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
  553. ha->host_no));
  554. ha->device_type |= DT_ISP2200A;
  555. ha->fw_transfer_size = 128;
  556. }
  557. /* Wrap Incoming Mailboxes Test. */
  558. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  559. DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
  560. rval = qla2x00_mbx_reg_test(ha);
  561. if (rval) {
  562. DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
  563. ha->host_no));
  564. qla_printk(KERN_WARNING, ha,
  565. "Failed mailbox send register test\n");
  566. }
  567. else {
  568. /* Flag a successful rval */
  569. rval = QLA_SUCCESS;
  570. }
  571. spin_lock_irqsave(&ha->hardware_lock, flags);
  572. chip_diag_failed:
  573. if (rval)
  574. DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
  575. "****\n", ha->host_no));
  576. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  577. return (rval);
  578. }
  579. /**
  580. * qla24xx_chip_diag() - Test ISP24xx for proper operation.
  581. * @ha: HA context
  582. *
  583. * Returns 0 on success.
  584. */
  585. int
  586. qla24xx_chip_diag(scsi_qla_host_t *ha)
  587. {
  588. int rval;
  589. /* Perform RISC reset. */
  590. qla24xx_reset_risc(ha);
  591. ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
  592. rval = qla2x00_mbx_reg_test(ha);
  593. if (rval) {
  594. DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
  595. ha->host_no));
  596. qla_printk(KERN_WARNING, ha,
  597. "Failed mailbox send register test\n");
  598. } else {
  599. /* Flag a successful rval */
  600. rval = QLA_SUCCESS;
  601. }
  602. return rval;
  603. }
  604. void
  605. qla2x00_alloc_fw_dump(scsi_qla_host_t *ha)
  606. {
  607. int rval;
  608. uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
  609. eft_size, fce_size;
  610. dma_addr_t tc_dma;
  611. void *tc;
  612. if (ha->fw_dump) {
  613. qla_printk(KERN_WARNING, ha,
  614. "Firmware dump previously allocated.\n");
  615. return;
  616. }
  617. ha->fw_dumped = 0;
  618. fixed_size = mem_size = eft_size = fce_size = 0;
  619. if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
  620. fixed_size = sizeof(struct qla2100_fw_dump);
  621. } else if (IS_QLA23XX(ha)) {
  622. fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
  623. mem_size = (ha->fw_memory_size - 0x11000 + 1) *
  624. sizeof(uint16_t);
  625. } else if (IS_FWI2_CAPABLE(ha)) {
  626. fixed_size = IS_QLA25XX(ha) ?
  627. offsetof(struct qla25xx_fw_dump, ext_mem):
  628. offsetof(struct qla24xx_fw_dump, ext_mem);
  629. mem_size = (ha->fw_memory_size - 0x100000 + 1) *
  630. sizeof(uint32_t);
  631. /* Allocate memory for Extended Trace Buffer. */
  632. tc = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
  633. GFP_KERNEL);
  634. if (!tc) {
  635. qla_printk(KERN_WARNING, ha, "Unable to allocate "
  636. "(%d KB) for EFT.\n", EFT_SIZE / 1024);
  637. goto cont_alloc;
  638. }
  639. memset(tc, 0, EFT_SIZE);
  640. rval = qla2x00_enable_eft_trace(ha, tc_dma, EFT_NUM_BUFFERS);
  641. if (rval) {
  642. qla_printk(KERN_WARNING, ha, "Unable to initialize "
  643. "EFT (%d).\n", rval);
  644. dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
  645. tc_dma);
  646. goto cont_alloc;
  647. }
  648. qla_printk(KERN_INFO, ha, "Allocated (%d KB) for EFT...\n",
  649. EFT_SIZE / 1024);
  650. eft_size = EFT_SIZE;
  651. ha->eft_dma = tc_dma;
  652. ha->eft = tc;
  653. /* Allocate memory for Fibre Channel Event Buffer. */
  654. if (!IS_QLA25XX(ha))
  655. goto cont_alloc;
  656. tc = dma_alloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
  657. GFP_KERNEL);
  658. if (!tc) {
  659. qla_printk(KERN_WARNING, ha, "Unable to allocate "
  660. "(%d KB) for FCE.\n", FCE_SIZE / 1024);
  661. goto cont_alloc;
  662. }
  663. memset(tc, 0, FCE_SIZE);
  664. rval = qla2x00_enable_fce_trace(ha, tc_dma, FCE_NUM_BUFFERS,
  665. ha->fce_mb, &ha->fce_bufs);
  666. if (rval) {
  667. qla_printk(KERN_WARNING, ha, "Unable to initialize "
  668. "FCE (%d).\n", rval);
  669. dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc,
  670. tc_dma);
  671. ha->flags.fce_enabled = 0;
  672. goto cont_alloc;
  673. }
  674. qla_printk(KERN_INFO, ha, "Allocated (%d KB) for FCE...\n",
  675. FCE_SIZE / 1024);
  676. fce_size = sizeof(struct qla2xxx_fce_chain) + EFT_SIZE;
  677. ha->flags.fce_enabled = 1;
  678. ha->fce_dma = tc_dma;
  679. ha->fce = tc;
  680. }
  681. cont_alloc:
  682. req_q_size = ha->request_q_length * sizeof(request_t);
  683. rsp_q_size = ha->response_q_length * sizeof(response_t);
  684. dump_size = offsetof(struct qla2xxx_fw_dump, isp);
  685. dump_size += fixed_size + mem_size + req_q_size + rsp_q_size +
  686. eft_size + fce_size;
  687. ha->fw_dump = vmalloc(dump_size);
  688. if (!ha->fw_dump) {
  689. qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
  690. "firmware dump!!!\n", dump_size / 1024);
  691. if (ha->eft) {
  692. dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
  693. ha->eft_dma);
  694. ha->eft = NULL;
  695. ha->eft_dma = 0;
  696. }
  697. return;
  698. }
  699. qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware dump...\n",
  700. dump_size / 1024);
  701. ha->fw_dump_len = dump_size;
  702. ha->fw_dump->signature[0] = 'Q';
  703. ha->fw_dump->signature[1] = 'L';
  704. ha->fw_dump->signature[2] = 'G';
  705. ha->fw_dump->signature[3] = 'C';
  706. ha->fw_dump->version = __constant_htonl(1);
  707. ha->fw_dump->fixed_size = htonl(fixed_size);
  708. ha->fw_dump->mem_size = htonl(mem_size);
  709. ha->fw_dump->req_q_size = htonl(req_q_size);
  710. ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
  711. ha->fw_dump->eft_size = htonl(eft_size);
  712. ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
  713. ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
  714. ha->fw_dump->header_size =
  715. htonl(offsetof(struct qla2xxx_fw_dump, isp));
  716. }
  717. /**
  718. * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
  719. * @ha: HA context
  720. *
  721. * Returns 0 on success.
  722. */
  723. static void
  724. qla2x00_resize_request_q(scsi_qla_host_t *ha)
  725. {
  726. int rval;
  727. uint16_t fw_iocb_cnt = 0;
  728. uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
  729. dma_addr_t request_dma;
  730. request_t *request_ring;
  731. /* Valid only on recent ISPs. */
  732. if (IS_QLA2100(ha) || IS_QLA2200(ha))
  733. return;
  734. /* Retrieve IOCB counts available to the firmware. */
  735. rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt,
  736. &ha->max_npiv_vports);
  737. if (rval)
  738. return;
  739. /* No point in continuing if current settings are sufficient. */
  740. if (fw_iocb_cnt < 1024)
  741. return;
  742. if (ha->request_q_length >= request_q_length)
  743. return;
  744. /* Attempt to claim larger area for request queue. */
  745. request_ring = dma_alloc_coherent(&ha->pdev->dev,
  746. (request_q_length + 1) * sizeof(request_t), &request_dma,
  747. GFP_KERNEL);
  748. if (request_ring == NULL)
  749. return;
  750. /* Resize successful, report extensions. */
  751. qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
  752. (ha->fw_memory_size + 1) / 1024);
  753. qla_printk(KERN_INFO, ha, "Resizing request queue depth "
  754. "(%d -> %d)...\n", ha->request_q_length, request_q_length);
  755. /* Clear old allocations. */
  756. dma_free_coherent(&ha->pdev->dev,
  757. (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
  758. ha->request_dma);
  759. /* Begin using larger queue. */
  760. ha->request_q_length = request_q_length;
  761. ha->request_ring = request_ring;
  762. ha->request_dma = request_dma;
  763. }
  764. /**
  765. * qla2x00_setup_chip() - Load and start RISC firmware.
  766. * @ha: HA context
  767. *
  768. * Returns 0 on success.
  769. */
  770. static int
  771. qla2x00_setup_chip(scsi_qla_host_t *ha)
  772. {
  773. int rval;
  774. uint32_t srisc_address = 0;
  775. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  776. unsigned long flags;
  777. if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
  778. /* Disable SRAM, Instruction RAM and GP RAM parity. */
  779. spin_lock_irqsave(&ha->hardware_lock, flags);
  780. WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
  781. RD_REG_WORD(&reg->hccr);
  782. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  783. }
  784. /* Load firmware sequences */
  785. rval = ha->isp_ops->load_risc(ha, &srisc_address);
  786. if (rval == QLA_SUCCESS) {
  787. DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
  788. "code.\n", ha->host_no));
  789. rval = qla2x00_verify_checksum(ha, srisc_address);
  790. if (rval == QLA_SUCCESS) {
  791. /* Start firmware execution. */
  792. DEBUG(printk("scsi(%ld): Checksum OK, start "
  793. "firmware.\n", ha->host_no));
  794. rval = qla2x00_execute_fw(ha, srisc_address);
  795. /* Retrieve firmware information. */
  796. if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
  797. qla2x00_get_fw_version(ha,
  798. &ha->fw_major_version,
  799. &ha->fw_minor_version,
  800. &ha->fw_subminor_version,
  801. &ha->fw_attributes, &ha->fw_memory_size);
  802. qla2x00_resize_request_q(ha);
  803. ha->flags.npiv_supported = 0;
  804. if ((IS_QLA24XX(ha) || IS_QLA25XX(ha)) &&
  805. (ha->fw_attributes & BIT_2)) {
  806. ha->flags.npiv_supported = 1;
  807. if ((!ha->max_npiv_vports) ||
  808. ((ha->max_npiv_vports + 1) %
  809. MIN_MULTI_ID_FABRIC))
  810. ha->max_npiv_vports =
  811. MIN_MULTI_ID_FABRIC - 1;
  812. }
  813. if (ql2xallocfwdump)
  814. qla2x00_alloc_fw_dump(ha);
  815. }
  816. } else {
  817. DEBUG2(printk(KERN_INFO
  818. "scsi(%ld): ISP Firmware failed checksum.\n",
  819. ha->host_no));
  820. }
  821. }
  822. if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
  823. /* Enable proper parity. */
  824. spin_lock_irqsave(&ha->hardware_lock, flags);
  825. if (IS_QLA2300(ha))
  826. /* SRAM parity */
  827. WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
  828. else
  829. /* SRAM, Instruction RAM and GP RAM parity */
  830. WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
  831. RD_REG_WORD(&reg->hccr);
  832. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  833. }
  834. if (rval) {
  835. DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
  836. ha->host_no));
  837. }
  838. return (rval);
  839. }
  840. /**
  841. * qla2x00_init_response_q_entries() - Initializes response queue entries.
  842. * @ha: HA context
  843. *
  844. * Beginning of request ring has initialization control block already built
  845. * by nvram config routine.
  846. *
  847. * Returns 0 on success.
  848. */
  849. static void
  850. qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
  851. {
  852. uint16_t cnt;
  853. response_t *pkt;
  854. pkt = ha->response_ring_ptr;
  855. for (cnt = 0; cnt < ha->response_q_length; cnt++) {
  856. pkt->signature = RESPONSE_PROCESSED;
  857. pkt++;
  858. }
  859. }
  860. /**
  861. * qla2x00_update_fw_options() - Read and process firmware options.
  862. * @ha: HA context
  863. *
  864. * Returns 0 on success.
  865. */
  866. void
  867. qla2x00_update_fw_options(scsi_qla_host_t *ha)
  868. {
  869. uint16_t swing, emphasis, tx_sens, rx_sens;
  870. memset(ha->fw_options, 0, sizeof(ha->fw_options));
  871. qla2x00_get_fw_options(ha, ha->fw_options);
  872. if (IS_QLA2100(ha) || IS_QLA2200(ha))
  873. return;
  874. /* Serial Link options. */
  875. DEBUG3(printk("scsi(%ld): Serial link options:\n",
  876. ha->host_no));
  877. DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
  878. sizeof(ha->fw_seriallink_options)));
  879. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  880. if (ha->fw_seriallink_options[3] & BIT_2) {
  881. ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
  882. /* 1G settings */
  883. swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
  884. emphasis = (ha->fw_seriallink_options[2] &
  885. (BIT_4 | BIT_3)) >> 3;
  886. tx_sens = ha->fw_seriallink_options[0] &
  887. (BIT_3 | BIT_2 | BIT_1 | BIT_0);
  888. rx_sens = (ha->fw_seriallink_options[0] &
  889. (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
  890. ha->fw_options[10] = (emphasis << 14) | (swing << 8);
  891. if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
  892. if (rx_sens == 0x0)
  893. rx_sens = 0x3;
  894. ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
  895. } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
  896. ha->fw_options[10] |= BIT_5 |
  897. ((rx_sens & (BIT_1 | BIT_0)) << 2) |
  898. (tx_sens & (BIT_1 | BIT_0));
  899. /* 2G settings */
  900. swing = (ha->fw_seriallink_options[2] &
  901. (BIT_7 | BIT_6 | BIT_5)) >> 5;
  902. emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
  903. tx_sens = ha->fw_seriallink_options[1] &
  904. (BIT_3 | BIT_2 | BIT_1 | BIT_0);
  905. rx_sens = (ha->fw_seriallink_options[1] &
  906. (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
  907. ha->fw_options[11] = (emphasis << 14) | (swing << 8);
  908. if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
  909. if (rx_sens == 0x0)
  910. rx_sens = 0x3;
  911. ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
  912. } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
  913. ha->fw_options[11] |= BIT_5 |
  914. ((rx_sens & (BIT_1 | BIT_0)) << 2) |
  915. (tx_sens & (BIT_1 | BIT_0));
  916. }
  917. /* FCP2 options. */
  918. /* Return command IOCBs without waiting for an ABTS to complete. */
  919. ha->fw_options[3] |= BIT_13;
  920. /* LED scheme. */
  921. if (ha->flags.enable_led_scheme)
  922. ha->fw_options[2] |= BIT_12;
  923. /* Detect ISP6312. */
  924. if (IS_QLA6312(ha))
  925. ha->fw_options[2] |= BIT_13;
  926. /* Update firmware options. */
  927. qla2x00_set_fw_options(ha, ha->fw_options);
  928. }
  929. void
  930. qla24xx_update_fw_options(scsi_qla_host_t *ha)
  931. {
  932. int rval;
  933. /* Update Serial Link options. */
  934. if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
  935. return;
  936. rval = qla2x00_set_serdes_params(ha,
  937. le16_to_cpu(ha->fw_seriallink_options24[1]),
  938. le16_to_cpu(ha->fw_seriallink_options24[2]),
  939. le16_to_cpu(ha->fw_seriallink_options24[3]));
  940. if (rval != QLA_SUCCESS) {
  941. qla_printk(KERN_WARNING, ha,
  942. "Unable to update Serial Link options (%x).\n", rval);
  943. }
  944. }
  945. void
  946. qla2x00_config_rings(struct scsi_qla_host *ha)
  947. {
  948. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  949. /* Setup ring parameters in initialization control block. */
  950. ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
  951. ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
  952. ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
  953. ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
  954. ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
  955. ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
  956. ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
  957. ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
  958. WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
  959. WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
  960. WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
  961. WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
  962. RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
  963. }
  964. void
  965. qla24xx_config_rings(struct scsi_qla_host *ha)
  966. {
  967. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  968. struct init_cb_24xx *icb;
  969. /* Setup ring parameters in initialization control block. */
  970. icb = (struct init_cb_24xx *)ha->init_cb;
  971. icb->request_q_outpointer = __constant_cpu_to_le16(0);
  972. icb->response_q_inpointer = __constant_cpu_to_le16(0);
  973. icb->request_q_length = cpu_to_le16(ha->request_q_length);
  974. icb->response_q_length = cpu_to_le16(ha->response_q_length);
  975. icb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
  976. icb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
  977. icb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
  978. icb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
  979. WRT_REG_DWORD(&reg->req_q_in, 0);
  980. WRT_REG_DWORD(&reg->req_q_out, 0);
  981. WRT_REG_DWORD(&reg->rsp_q_in, 0);
  982. WRT_REG_DWORD(&reg->rsp_q_out, 0);
  983. RD_REG_DWORD(&reg->rsp_q_out);
  984. }
  985. /**
  986. * qla2x00_init_rings() - Initializes firmware.
  987. * @ha: HA context
  988. *
  989. * Beginning of request ring has initialization control block already built
  990. * by nvram config routine.
  991. *
  992. * Returns 0 on success.
  993. */
  994. static int
  995. qla2x00_init_rings(scsi_qla_host_t *ha)
  996. {
  997. int rval;
  998. unsigned long flags = 0;
  999. int cnt;
  1000. struct mid_init_cb_24xx *mid_init_cb =
  1001. (struct mid_init_cb_24xx *) ha->init_cb;
  1002. spin_lock_irqsave(&ha->hardware_lock, flags);
  1003. /* Clear outstanding commands array. */
  1004. for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
  1005. ha->outstanding_cmds[cnt] = NULL;
  1006. ha->current_outstanding_cmd = 0;
  1007. /* Clear RSCN queue. */
  1008. ha->rscn_in_ptr = 0;
  1009. ha->rscn_out_ptr = 0;
  1010. /* Initialize firmware. */
  1011. ha->request_ring_ptr = ha->request_ring;
  1012. ha->req_ring_index = 0;
  1013. ha->req_q_cnt = ha->request_q_length;
  1014. ha->response_ring_ptr = ha->response_ring;
  1015. ha->rsp_ring_index = 0;
  1016. /* Initialize response queue entries */
  1017. qla2x00_init_response_q_entries(ha);
  1018. ha->isp_ops->config_rings(ha);
  1019. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1020. /* Update any ISP specific firmware options before initialization. */
  1021. ha->isp_ops->update_fw_options(ha);
  1022. DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
  1023. if (ha->flags.npiv_supported)
  1024. mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
  1025. mid_init_cb->options = __constant_cpu_to_le16(BIT_1);
  1026. rval = qla2x00_init_firmware(ha, ha->init_cb_size);
  1027. if (rval) {
  1028. DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
  1029. ha->host_no));
  1030. } else {
  1031. DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
  1032. ha->host_no));
  1033. }
  1034. return (rval);
  1035. }
  1036. /**
  1037. * qla2x00_fw_ready() - Waits for firmware ready.
  1038. * @ha: HA context
  1039. *
  1040. * Returns 0 on success.
  1041. */
  1042. static int
  1043. qla2x00_fw_ready(scsi_qla_host_t *ha)
  1044. {
  1045. int rval;
  1046. unsigned long wtime, mtime;
  1047. uint16_t min_wait; /* Minimum wait time if loop is down */
  1048. uint16_t wait_time; /* Wait time if loop is coming ready */
  1049. uint16_t fw_state;
  1050. rval = QLA_SUCCESS;
  1051. /* 20 seconds for loop down. */
  1052. min_wait = 20;
  1053. /*
  1054. * Firmware should take at most one RATOV to login, plus 5 seconds for
  1055. * our own processing.
  1056. */
  1057. if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
  1058. wait_time = min_wait;
  1059. }
  1060. /* Min wait time if loop down */
  1061. mtime = jiffies + (min_wait * HZ);
  1062. /* wait time before firmware ready */
  1063. wtime = jiffies + (wait_time * HZ);
  1064. /* Wait for ISP to finish LIP */
  1065. if (!ha->flags.init_done)
  1066. qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
  1067. DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
  1068. ha->host_no));
  1069. do {
  1070. rval = qla2x00_get_firmware_state(ha, &fw_state);
  1071. if (rval == QLA_SUCCESS) {
  1072. if (fw_state < FSTATE_LOSS_OF_SYNC) {
  1073. ha->device_flags &= ~DFLG_NO_CABLE;
  1074. }
  1075. if (fw_state == FSTATE_READY) {
  1076. DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
  1077. ha->host_no));
  1078. qla2x00_get_retry_cnt(ha, &ha->retry_count,
  1079. &ha->login_timeout, &ha->r_a_tov);
  1080. rval = QLA_SUCCESS;
  1081. break;
  1082. }
  1083. rval = QLA_FUNCTION_FAILED;
  1084. if (atomic_read(&ha->loop_down_timer) &&
  1085. fw_state != FSTATE_READY) {
  1086. /* Loop down. Timeout on min_wait for states
  1087. * other than Wait for Login.
  1088. */
  1089. if (time_after_eq(jiffies, mtime)) {
  1090. qla_printk(KERN_INFO, ha,
  1091. "Cable is unplugged...\n");
  1092. ha->device_flags |= DFLG_NO_CABLE;
  1093. break;
  1094. }
  1095. }
  1096. } else {
  1097. /* Mailbox cmd failed. Timeout on min_wait. */
  1098. if (time_after_eq(jiffies, mtime))
  1099. break;
  1100. }
  1101. if (time_after_eq(jiffies, wtime))
  1102. break;
  1103. /* Delay for a while */
  1104. msleep(500);
  1105. DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
  1106. ha->host_no, fw_state, jiffies));
  1107. } while (1);
  1108. DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
  1109. ha->host_no, fw_state, jiffies));
  1110. if (rval) {
  1111. DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
  1112. ha->host_no));
  1113. }
  1114. return (rval);
  1115. }
  1116. /*
  1117. * qla2x00_configure_hba
  1118. * Setup adapter context.
  1119. *
  1120. * Input:
  1121. * ha = adapter state pointer.
  1122. *
  1123. * Returns:
  1124. * 0 = success
  1125. *
  1126. * Context:
  1127. * Kernel context.
  1128. */
  1129. static int
  1130. qla2x00_configure_hba(scsi_qla_host_t *ha)
  1131. {
  1132. int rval;
  1133. uint16_t loop_id;
  1134. uint16_t topo;
  1135. uint16_t sw_cap;
  1136. uint8_t al_pa;
  1137. uint8_t area;
  1138. uint8_t domain;
  1139. char connect_type[22];
  1140. /* Get host addresses. */
  1141. rval = qla2x00_get_adapter_id(ha,
  1142. &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
  1143. if (rval != QLA_SUCCESS) {
  1144. if (LOOP_TRANSITION(ha) || atomic_read(&ha->loop_down_timer) ||
  1145. (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
  1146. DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
  1147. __func__, ha->host_no));
  1148. } else {
  1149. qla_printk(KERN_WARNING, ha,
  1150. "ERROR -- Unable to get host loop ID.\n");
  1151. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  1152. }
  1153. return (rval);
  1154. }
  1155. if (topo == 4) {
  1156. qla_printk(KERN_INFO, ha,
  1157. "Cannot get topology - retrying.\n");
  1158. return (QLA_FUNCTION_FAILED);
  1159. }
  1160. ha->loop_id = loop_id;
  1161. /* initialize */
  1162. ha->min_external_loopid = SNS_FIRST_LOOP_ID;
  1163. ha->operating_mode = LOOP;
  1164. ha->switch_cap = 0;
  1165. switch (topo) {
  1166. case 0:
  1167. DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
  1168. ha->host_no));
  1169. ha->current_topology = ISP_CFG_NL;
  1170. strcpy(connect_type, "(Loop)");
  1171. break;
  1172. case 1:
  1173. DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
  1174. ha->host_no));
  1175. ha->switch_cap = sw_cap;
  1176. ha->current_topology = ISP_CFG_FL;
  1177. strcpy(connect_type, "(FL_Port)");
  1178. break;
  1179. case 2:
  1180. DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
  1181. ha->host_no));
  1182. ha->operating_mode = P2P;
  1183. ha->current_topology = ISP_CFG_N;
  1184. strcpy(connect_type, "(N_Port-to-N_Port)");
  1185. break;
  1186. case 3:
  1187. DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
  1188. ha->host_no));
  1189. ha->switch_cap = sw_cap;
  1190. ha->operating_mode = P2P;
  1191. ha->current_topology = ISP_CFG_F;
  1192. strcpy(connect_type, "(F_Port)");
  1193. break;
  1194. default:
  1195. DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
  1196. "Using NL.\n",
  1197. ha->host_no, topo));
  1198. ha->current_topology = ISP_CFG_NL;
  1199. strcpy(connect_type, "(Loop)");
  1200. break;
  1201. }
  1202. /* Save Host port and loop ID. */
  1203. /* byte order - Big Endian */
  1204. ha->d_id.b.domain = domain;
  1205. ha->d_id.b.area = area;
  1206. ha->d_id.b.al_pa = al_pa;
  1207. if (!ha->flags.init_done)
  1208. qla_printk(KERN_INFO, ha,
  1209. "Topology - %s, Host Loop address 0x%x\n",
  1210. connect_type, ha->loop_id);
  1211. if (rval) {
  1212. DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
  1213. } else {
  1214. DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
  1215. }
  1216. return(rval);
  1217. }
  1218. static inline void
  1219. qla2x00_set_model_info(scsi_qla_host_t *ha, uint8_t *model, size_t len, char *def)
  1220. {
  1221. char *st, *en;
  1222. uint16_t index;
  1223. if (memcmp(model, BINZERO, len) != 0) {
  1224. strncpy(ha->model_number, model, len);
  1225. st = en = ha->model_number;
  1226. en += len - 1;
  1227. while (en > st) {
  1228. if (*en != 0x20 && *en != 0x00)
  1229. break;
  1230. *en-- = '\0';
  1231. }
  1232. index = (ha->pdev->subsystem_device & 0xff);
  1233. if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
  1234. index < QLA_MODEL_NAMES)
  1235. ha->model_desc = qla2x00_model_name[index * 2 + 1];
  1236. } else {
  1237. index = (ha->pdev->subsystem_device & 0xff);
  1238. if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
  1239. index < QLA_MODEL_NAMES) {
  1240. strcpy(ha->model_number,
  1241. qla2x00_model_name[index * 2]);
  1242. ha->model_desc = qla2x00_model_name[index * 2 + 1];
  1243. } else {
  1244. strcpy(ha->model_number, def);
  1245. }
  1246. }
  1247. }
  1248. /* On sparc systems, obtain port and node WWN from firmware
  1249. * properties.
  1250. */
  1251. static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *ha, nvram_t *nv)
  1252. {
  1253. #ifdef CONFIG_SPARC
  1254. struct pci_dev *pdev = ha->pdev;
  1255. struct device_node *dp = pci_device_to_OF_node(pdev);
  1256. const u8 *val;
  1257. int len;
  1258. val = of_get_property(dp, "port-wwn", &len);
  1259. if (val && len >= WWN_SIZE)
  1260. memcpy(nv->port_name, val, WWN_SIZE);
  1261. val = of_get_property(dp, "node-wwn", &len);
  1262. if (val && len >= WWN_SIZE)
  1263. memcpy(nv->node_name, val, WWN_SIZE);
  1264. #endif
  1265. }
  1266. /*
  1267. * NVRAM configuration for ISP 2xxx
  1268. *
  1269. * Input:
  1270. * ha = adapter block pointer.
  1271. *
  1272. * Output:
  1273. * initialization control block in response_ring
  1274. * host adapters parameters in host adapter block
  1275. *
  1276. * Returns:
  1277. * 0 = success.
  1278. */
  1279. int
  1280. qla2x00_nvram_config(scsi_qla_host_t *ha)
  1281. {
  1282. int rval;
  1283. uint8_t chksum = 0;
  1284. uint16_t cnt;
  1285. uint8_t *dptr1, *dptr2;
  1286. init_cb_t *icb = ha->init_cb;
  1287. nvram_t *nv = ha->nvram;
  1288. uint8_t *ptr = ha->nvram;
  1289. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1290. rval = QLA_SUCCESS;
  1291. /* Determine NVRAM starting address. */
  1292. ha->nvram_size = sizeof(nvram_t);
  1293. ha->nvram_base = 0;
  1294. if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
  1295. if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
  1296. ha->nvram_base = 0x80;
  1297. /* Get NVRAM data and calculate checksum. */
  1298. ha->isp_ops->read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
  1299. for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
  1300. chksum += *ptr++;
  1301. DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
  1302. DEBUG5(qla2x00_dump_buffer((uint8_t *)nv, ha->nvram_size));
  1303. /* Bad NVRAM data, set defaults parameters. */
  1304. if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
  1305. nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
  1306. /* Reset NVRAM data. */
  1307. qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
  1308. "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
  1309. nv->nvram_version);
  1310. qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
  1311. "invalid -- WWPN) defaults.\n");
  1312. if (chksum)
  1313. qla2xxx_hw_event_log(ha, HW_EVENT_NVRAM_CHKSUM_ERR, 0,
  1314. MSW(chksum), LSW(chksum));
  1315. /*
  1316. * Set default initialization control block.
  1317. */
  1318. memset(nv, 0, ha->nvram_size);
  1319. nv->parameter_block_version = ICB_VERSION;
  1320. if (IS_QLA23XX(ha)) {
  1321. nv->firmware_options[0] = BIT_2 | BIT_1;
  1322. nv->firmware_options[1] = BIT_7 | BIT_5;
  1323. nv->add_firmware_options[0] = BIT_5;
  1324. nv->add_firmware_options[1] = BIT_5 | BIT_4;
  1325. nv->frame_payload_size = __constant_cpu_to_le16(2048);
  1326. nv->special_options[1] = BIT_7;
  1327. } else if (IS_QLA2200(ha)) {
  1328. nv->firmware_options[0] = BIT_2 | BIT_1;
  1329. nv->firmware_options[1] = BIT_7 | BIT_5;
  1330. nv->add_firmware_options[0] = BIT_5;
  1331. nv->add_firmware_options[1] = BIT_5 | BIT_4;
  1332. nv->frame_payload_size = __constant_cpu_to_le16(1024);
  1333. } else if (IS_QLA2100(ha)) {
  1334. nv->firmware_options[0] = BIT_3 | BIT_1;
  1335. nv->firmware_options[1] = BIT_5;
  1336. nv->frame_payload_size = __constant_cpu_to_le16(1024);
  1337. }
  1338. nv->max_iocb_allocation = __constant_cpu_to_le16(256);
  1339. nv->execution_throttle = __constant_cpu_to_le16(16);
  1340. nv->retry_count = 8;
  1341. nv->retry_delay = 1;
  1342. nv->port_name[0] = 33;
  1343. nv->port_name[3] = 224;
  1344. nv->port_name[4] = 139;
  1345. qla2xxx_nvram_wwn_from_ofw(ha, nv);
  1346. nv->login_timeout = 4;
  1347. /*
  1348. * Set default host adapter parameters
  1349. */
  1350. nv->host_p[1] = BIT_2;
  1351. nv->reset_delay = 5;
  1352. nv->port_down_retry_count = 8;
  1353. nv->max_luns_per_target = __constant_cpu_to_le16(8);
  1354. nv->link_down_timeout = 60;
  1355. rval = 1;
  1356. }
  1357. #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
  1358. /*
  1359. * The SN2 does not provide BIOS emulation which means you can't change
  1360. * potentially bogus BIOS settings. Force the use of default settings
  1361. * for link rate and frame size. Hope that the rest of the settings
  1362. * are valid.
  1363. */
  1364. if (ia64_platform_is("sn2")) {
  1365. nv->frame_payload_size = __constant_cpu_to_le16(2048);
  1366. if (IS_QLA23XX(ha))
  1367. nv->special_options[1] = BIT_7;
  1368. }
  1369. #endif
  1370. /* Reset Initialization control block */
  1371. memset(icb, 0, ha->init_cb_size);
  1372. /*
  1373. * Setup driver NVRAM options.
  1374. */
  1375. nv->firmware_options[0] |= (BIT_6 | BIT_1);
  1376. nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
  1377. nv->firmware_options[1] |= (BIT_5 | BIT_0);
  1378. nv->firmware_options[1] &= ~BIT_4;
  1379. if (IS_QLA23XX(ha)) {
  1380. nv->firmware_options[0] |= BIT_2;
  1381. nv->firmware_options[0] &= ~BIT_3;
  1382. nv->add_firmware_options[1] |= BIT_5 | BIT_4;
  1383. if (IS_QLA2300(ha)) {
  1384. if (ha->fb_rev == FPM_2310) {
  1385. strcpy(ha->model_number, "QLA2310");
  1386. } else {
  1387. strcpy(ha->model_number, "QLA2300");
  1388. }
  1389. } else {
  1390. qla2x00_set_model_info(ha, nv->model_number,
  1391. sizeof(nv->model_number), "QLA23xx");
  1392. }
  1393. } else if (IS_QLA2200(ha)) {
  1394. nv->firmware_options[0] |= BIT_2;
  1395. /*
  1396. * 'Point-to-point preferred, else loop' is not a safe
  1397. * connection mode setting.
  1398. */
  1399. if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
  1400. (BIT_5 | BIT_4)) {
  1401. /* Force 'loop preferred, else point-to-point'. */
  1402. nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
  1403. nv->add_firmware_options[0] |= BIT_5;
  1404. }
  1405. strcpy(ha->model_number, "QLA22xx");
  1406. } else /*if (IS_QLA2100(ha))*/ {
  1407. strcpy(ha->model_number, "QLA2100");
  1408. }
  1409. /*
  1410. * Copy over NVRAM RISC parameter block to initialization control block.
  1411. */
  1412. dptr1 = (uint8_t *)icb;
  1413. dptr2 = (uint8_t *)&nv->parameter_block_version;
  1414. cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
  1415. while (cnt--)
  1416. *dptr1++ = *dptr2++;
  1417. /* Copy 2nd half. */
  1418. dptr1 = (uint8_t *)icb->add_firmware_options;
  1419. cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
  1420. while (cnt--)
  1421. *dptr1++ = *dptr2++;
  1422. /* Use alternate WWN? */
  1423. if (nv->host_p[1] & BIT_7) {
  1424. memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
  1425. memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
  1426. }
  1427. /* Prepare nodename */
  1428. if ((icb->firmware_options[1] & BIT_6) == 0) {
  1429. /*
  1430. * Firmware will apply the following mask if the nodename was
  1431. * not provided.
  1432. */
  1433. memcpy(icb->node_name, icb->port_name, WWN_SIZE);
  1434. icb->node_name[0] &= 0xF0;
  1435. }
  1436. /*
  1437. * Set host adapter parameters.
  1438. */
  1439. if (nv->host_p[0] & BIT_7)
  1440. ql2xextended_error_logging = 1;
  1441. ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
  1442. /* Always load RISC code on non ISP2[12]00 chips. */
  1443. if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
  1444. ha->flags.disable_risc_code_load = 0;
  1445. ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
  1446. ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
  1447. ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
  1448. ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
  1449. ha->flags.disable_serdes = 0;
  1450. ha->operating_mode =
  1451. (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
  1452. memcpy(ha->fw_seriallink_options, nv->seriallink_options,
  1453. sizeof(ha->fw_seriallink_options));
  1454. /* save HBA serial number */
  1455. ha->serial0 = icb->port_name[5];
  1456. ha->serial1 = icb->port_name[6];
  1457. ha->serial2 = icb->port_name[7];
  1458. ha->node_name = icb->node_name;
  1459. ha->port_name = icb->port_name;
  1460. icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
  1461. ha->retry_count = nv->retry_count;
  1462. /* Set minimum login_timeout to 4 seconds. */
  1463. if (nv->login_timeout < ql2xlogintimeout)
  1464. nv->login_timeout = ql2xlogintimeout;
  1465. if (nv->login_timeout < 4)
  1466. nv->login_timeout = 4;
  1467. ha->login_timeout = nv->login_timeout;
  1468. icb->login_timeout = nv->login_timeout;
  1469. /* Set minimum RATOV to 100 tenths of a second. */
  1470. ha->r_a_tov = 100;
  1471. ha->loop_reset_delay = nv->reset_delay;
  1472. /* Link Down Timeout = 0:
  1473. *
  1474. * When Port Down timer expires we will start returning
  1475. * I/O's to OS with "DID_NO_CONNECT".
  1476. *
  1477. * Link Down Timeout != 0:
  1478. *
  1479. * The driver waits for the link to come up after link down
  1480. * before returning I/Os to OS with "DID_NO_CONNECT".
  1481. */
  1482. if (nv->link_down_timeout == 0) {
  1483. ha->loop_down_abort_time =
  1484. (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
  1485. } else {
  1486. ha->link_down_timeout = nv->link_down_timeout;
  1487. ha->loop_down_abort_time =
  1488. (LOOP_DOWN_TIME - ha->link_down_timeout);
  1489. }
  1490. /*
  1491. * Need enough time to try and get the port back.
  1492. */
  1493. ha->port_down_retry_count = nv->port_down_retry_count;
  1494. if (qlport_down_retry)
  1495. ha->port_down_retry_count = qlport_down_retry;
  1496. /* Set login_retry_count */
  1497. ha->login_retry_count = nv->retry_count;
  1498. if (ha->port_down_retry_count == nv->port_down_retry_count &&
  1499. ha->port_down_retry_count > 3)
  1500. ha->login_retry_count = ha->port_down_retry_count;
  1501. else if (ha->port_down_retry_count > (int)ha->login_retry_count)
  1502. ha->login_retry_count = ha->port_down_retry_count;
  1503. if (ql2xloginretrycount)
  1504. ha->login_retry_count = ql2xloginretrycount;
  1505. icb->lun_enables = __constant_cpu_to_le16(0);
  1506. icb->command_resource_count = 0;
  1507. icb->immediate_notify_resource_count = 0;
  1508. icb->timeout = __constant_cpu_to_le16(0);
  1509. if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
  1510. /* Enable RIO */
  1511. icb->firmware_options[0] &= ~BIT_3;
  1512. icb->add_firmware_options[0] &=
  1513. ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
  1514. icb->add_firmware_options[0] |= BIT_2;
  1515. icb->response_accumulation_timer = 3;
  1516. icb->interrupt_delay_timer = 5;
  1517. ha->flags.process_response_queue = 1;
  1518. } else {
  1519. /* Enable ZIO. */
  1520. if (!ha->flags.init_done) {
  1521. ha->zio_mode = icb->add_firmware_options[0] &
  1522. (BIT_3 | BIT_2 | BIT_1 | BIT_0);
  1523. ha->zio_timer = icb->interrupt_delay_timer ?
  1524. icb->interrupt_delay_timer: 2;
  1525. }
  1526. icb->add_firmware_options[0] &=
  1527. ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
  1528. ha->flags.process_response_queue = 0;
  1529. if (ha->zio_mode != QLA_ZIO_DISABLED) {
  1530. ha->zio_mode = QLA_ZIO_MODE_6;
  1531. DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
  1532. "delay (%d us).\n", ha->host_no, ha->zio_mode,
  1533. ha->zio_timer * 100));
  1534. qla_printk(KERN_INFO, ha,
  1535. "ZIO mode %d enabled; timer delay (%d us).\n",
  1536. ha->zio_mode, ha->zio_timer * 100);
  1537. icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
  1538. icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
  1539. ha->flags.process_response_queue = 1;
  1540. }
  1541. }
  1542. if (rval) {
  1543. DEBUG2_3(printk(KERN_WARNING
  1544. "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
  1545. }
  1546. return (rval);
  1547. }
  1548. static void
  1549. qla2x00_rport_del(void *data)
  1550. {
  1551. fc_port_t *fcport = data;
  1552. struct fc_rport *rport;
  1553. unsigned long flags;
  1554. spin_lock_irqsave(&fcport->rport_lock, flags);
  1555. rport = fcport->drport;
  1556. fcport->drport = NULL;
  1557. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1558. if (rport)
  1559. fc_remote_port_delete(rport);
  1560. }
  1561. /**
  1562. * qla2x00_alloc_fcport() - Allocate a generic fcport.
  1563. * @ha: HA context
  1564. * @flags: allocation flags
  1565. *
  1566. * Returns a pointer to the allocated fcport, or NULL, if none available.
  1567. */
  1568. static fc_port_t *
  1569. qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
  1570. {
  1571. fc_port_t *fcport;
  1572. fcport = kzalloc(sizeof(fc_port_t), flags);
  1573. if (!fcport)
  1574. return NULL;
  1575. /* Setup fcport template structure. */
  1576. fcport->ha = ha;
  1577. fcport->vp_idx = ha->vp_idx;
  1578. fcport->port_type = FCT_UNKNOWN;
  1579. fcport->loop_id = FC_NO_LOOP_ID;
  1580. atomic_set(&fcport->state, FCS_UNCONFIGURED);
  1581. fcport->flags = FCF_RLC_SUPPORT;
  1582. fcport->supported_classes = FC_COS_UNSPECIFIED;
  1583. spin_lock_init(&fcport->rport_lock);
  1584. return fcport;
  1585. }
  1586. /*
  1587. * qla2x00_configure_loop
  1588. * Updates Fibre Channel Device Database with what is actually on loop.
  1589. *
  1590. * Input:
  1591. * ha = adapter block pointer.
  1592. *
  1593. * Returns:
  1594. * 0 = success.
  1595. * 1 = error.
  1596. * 2 = database was full and device was not configured.
  1597. */
  1598. static int
  1599. qla2x00_configure_loop(scsi_qla_host_t *ha)
  1600. {
  1601. int rval;
  1602. unsigned long flags, save_flags;
  1603. rval = QLA_SUCCESS;
  1604. /* Get Initiator ID */
  1605. if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
  1606. rval = qla2x00_configure_hba(ha);
  1607. if (rval != QLA_SUCCESS) {
  1608. DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
  1609. ha->host_no));
  1610. return (rval);
  1611. }
  1612. }
  1613. save_flags = flags = ha->dpc_flags;
  1614. DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
  1615. ha->host_no, flags));
  1616. /*
  1617. * If we have both an RSCN and PORT UPDATE pending then handle them
  1618. * both at the same time.
  1619. */
  1620. clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
  1621. clear_bit(RSCN_UPDATE, &ha->dpc_flags);
  1622. /* Determine what we need to do */
  1623. if (ha->current_topology == ISP_CFG_FL &&
  1624. (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
  1625. ha->flags.rscn_queue_overflow = 1;
  1626. set_bit(RSCN_UPDATE, &flags);
  1627. } else if (ha->current_topology == ISP_CFG_F &&
  1628. (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
  1629. ha->flags.rscn_queue_overflow = 1;
  1630. set_bit(RSCN_UPDATE, &flags);
  1631. clear_bit(LOCAL_LOOP_UPDATE, &flags);
  1632. } else if (ha->current_topology == ISP_CFG_N) {
  1633. clear_bit(RSCN_UPDATE, &flags);
  1634. } else if (!ha->flags.online ||
  1635. (test_bit(ABORT_ISP_ACTIVE, &flags))) {
  1636. ha->flags.rscn_queue_overflow = 1;
  1637. set_bit(RSCN_UPDATE, &flags);
  1638. set_bit(LOCAL_LOOP_UPDATE, &flags);
  1639. }
  1640. if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
  1641. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1642. rval = QLA_FUNCTION_FAILED;
  1643. } else {
  1644. rval = qla2x00_configure_local_loop(ha);
  1645. }
  1646. }
  1647. if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
  1648. if (LOOP_TRANSITION(ha)) {
  1649. rval = QLA_FUNCTION_FAILED;
  1650. } else {
  1651. rval = qla2x00_configure_fabric(ha);
  1652. }
  1653. }
  1654. if (rval == QLA_SUCCESS) {
  1655. if (atomic_read(&ha->loop_down_timer) ||
  1656. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1657. rval = QLA_FUNCTION_FAILED;
  1658. } else {
  1659. atomic_set(&ha->loop_state, LOOP_READY);
  1660. DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
  1661. }
  1662. }
  1663. if (rval) {
  1664. DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
  1665. __func__, ha->host_no));
  1666. } else {
  1667. DEBUG3(printk("%s: exiting normally\n", __func__));
  1668. }
  1669. /* Restore state if a resync event occured during processing */
  1670. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1671. if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
  1672. set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
  1673. if (test_bit(RSCN_UPDATE, &save_flags))
  1674. set_bit(RSCN_UPDATE, &ha->dpc_flags);
  1675. }
  1676. return (rval);
  1677. }
  1678. /*
  1679. * qla2x00_configure_local_loop
  1680. * Updates Fibre Channel Device Database with local loop devices.
  1681. *
  1682. * Input:
  1683. * ha = adapter block pointer.
  1684. *
  1685. * Returns:
  1686. * 0 = success.
  1687. */
  1688. static int
  1689. qla2x00_configure_local_loop(scsi_qla_host_t *ha)
  1690. {
  1691. int rval, rval2;
  1692. int found_devs;
  1693. int found;
  1694. fc_port_t *fcport, *new_fcport;
  1695. uint16_t index;
  1696. uint16_t entries;
  1697. char *id_iter;
  1698. uint16_t loop_id;
  1699. uint8_t domain, area, al_pa;
  1700. scsi_qla_host_t *pha = to_qla_parent(ha);
  1701. found_devs = 0;
  1702. new_fcport = NULL;
  1703. entries = MAX_FIBRE_DEVICES;
  1704. DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
  1705. DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
  1706. /* Get list of logged in devices. */
  1707. memset(ha->gid_list, 0, GID_LIST_SIZE);
  1708. rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
  1709. &entries);
  1710. if (rval != QLA_SUCCESS)
  1711. goto cleanup_allocation;
  1712. DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
  1713. ha->host_no, entries));
  1714. DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
  1715. entries * sizeof(struct gid_list_info)));
  1716. /* Allocate temporary fcport for any new fcports discovered. */
  1717. new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
  1718. if (new_fcport == NULL) {
  1719. rval = QLA_MEMORY_ALLOC_FAILED;
  1720. goto cleanup_allocation;
  1721. }
  1722. new_fcport->flags &= ~FCF_FABRIC_DEVICE;
  1723. /*
  1724. * Mark local devices that were present with FCF_DEVICE_LOST for now.
  1725. */
  1726. list_for_each_entry(fcport, &pha->fcports, list) {
  1727. if (fcport->vp_idx != ha->vp_idx)
  1728. continue;
  1729. if (atomic_read(&fcport->state) == FCS_ONLINE &&
  1730. fcport->port_type != FCT_BROADCAST &&
  1731. (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
  1732. DEBUG(printk("scsi(%ld): Marking port lost, "
  1733. "loop_id=0x%04x\n",
  1734. ha->host_no, fcport->loop_id));
  1735. atomic_set(&fcport->state, FCS_DEVICE_LOST);
  1736. fcport->flags &= ~FCF_FARP_DONE;
  1737. }
  1738. }
  1739. /* Add devices to port list. */
  1740. id_iter = (char *)ha->gid_list;
  1741. for (index = 0; index < entries; index++) {
  1742. domain = ((struct gid_list_info *)id_iter)->domain;
  1743. area = ((struct gid_list_info *)id_iter)->area;
  1744. al_pa = ((struct gid_list_info *)id_iter)->al_pa;
  1745. if (IS_QLA2100(ha) || IS_QLA2200(ha))
  1746. loop_id = (uint16_t)
  1747. ((struct gid_list_info *)id_iter)->loop_id_2100;
  1748. else
  1749. loop_id = le16_to_cpu(
  1750. ((struct gid_list_info *)id_iter)->loop_id);
  1751. id_iter += ha->gid_list_info_size;
  1752. /* Bypass reserved domain fields. */
  1753. if ((domain & 0xf0) == 0xf0)
  1754. continue;
  1755. /* Bypass if not same domain and area of adapter. */
  1756. if (area && domain &&
  1757. (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
  1758. continue;
  1759. /* Bypass invalid local loop ID. */
  1760. if (loop_id > LAST_LOCAL_LOOP_ID)
  1761. continue;
  1762. /* Fill in member data. */
  1763. new_fcport->d_id.b.domain = domain;
  1764. new_fcport->d_id.b.area = area;
  1765. new_fcport->d_id.b.al_pa = al_pa;
  1766. new_fcport->loop_id = loop_id;
  1767. new_fcport->vp_idx = ha->vp_idx;
  1768. rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
  1769. if (rval2 != QLA_SUCCESS) {
  1770. DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
  1771. "information -- get_port_database=%x, "
  1772. "loop_id=0x%04x\n",
  1773. ha->host_no, rval2, new_fcport->loop_id));
  1774. DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
  1775. ha->host_no));
  1776. set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  1777. continue;
  1778. }
  1779. /* Check for matching device in port list. */
  1780. found = 0;
  1781. fcport = NULL;
  1782. list_for_each_entry(fcport, &pha->fcports, list) {
  1783. if (fcport->vp_idx != ha->vp_idx)
  1784. continue;
  1785. if (memcmp(new_fcport->port_name, fcport->port_name,
  1786. WWN_SIZE))
  1787. continue;
  1788. fcport->flags &= ~(FCF_FABRIC_DEVICE |
  1789. FCF_PERSISTENT_BOUND);
  1790. fcport->loop_id = new_fcport->loop_id;
  1791. fcport->port_type = new_fcport->port_type;
  1792. fcport->d_id.b24 = new_fcport->d_id.b24;
  1793. memcpy(fcport->node_name, new_fcport->node_name,
  1794. WWN_SIZE);
  1795. found++;
  1796. break;
  1797. }
  1798. if (!found) {
  1799. /* New device, add to fcports list. */
  1800. new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
  1801. if (ha->parent) {
  1802. new_fcport->ha = ha;
  1803. new_fcport->vp_idx = ha->vp_idx;
  1804. list_add_tail(&new_fcport->vp_fcport,
  1805. &ha->vp_fcports);
  1806. }
  1807. list_add_tail(&new_fcport->list, &pha->fcports);
  1808. /* Allocate a new replacement fcport. */
  1809. fcport = new_fcport;
  1810. new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
  1811. if (new_fcport == NULL) {
  1812. rval = QLA_MEMORY_ALLOC_FAILED;
  1813. goto cleanup_allocation;
  1814. }
  1815. new_fcport->flags &= ~FCF_FABRIC_DEVICE;
  1816. }
  1817. /* Base iIDMA settings on HBA port speed. */
  1818. fcport->fp_speed = ha->link_data_rate;
  1819. qla2x00_update_fcport(ha, fcport);
  1820. found_devs++;
  1821. }
  1822. cleanup_allocation:
  1823. kfree(new_fcport);
  1824. if (rval != QLA_SUCCESS) {
  1825. DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
  1826. "rval=%x\n", ha->host_no, rval));
  1827. }
  1828. if (found_devs) {
  1829. ha->device_flags |= DFLG_LOCAL_DEVICES;
  1830. ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
  1831. }
  1832. return (rval);
  1833. }
  1834. static void
  1835. qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
  1836. {
  1837. fc_port_t *fcport;
  1838. qla2x00_mark_all_devices_lost(ha, 0);
  1839. list_for_each_entry(fcport, &ha->fcports, list) {
  1840. if (fcport->port_type != FCT_TARGET)
  1841. continue;
  1842. qla2x00_update_fcport(ha, fcport);
  1843. }
  1844. }
  1845. static void
  1846. qla2x00_iidma_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
  1847. {
  1848. #define LS_UNKNOWN 2
  1849. static char *link_speeds[5] = { "1", "2", "?", "4", "8" };
  1850. int rval;
  1851. uint16_t mb[6];
  1852. if (!IS_IIDMA_CAPABLE(ha))
  1853. return;
  1854. if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
  1855. fcport->fp_speed > ha->link_data_rate)
  1856. return;
  1857. rval = qla2x00_set_idma_speed(ha, fcport->loop_id, fcport->fp_speed,
  1858. mb);
  1859. if (rval != QLA_SUCCESS) {
  1860. DEBUG2(printk("scsi(%ld): Unable to adjust iIDMA "
  1861. "%02x%02x%02x%02x%02x%02x%02x%02x -- %04x %x %04x %04x.\n",
  1862. ha->host_no, fcport->port_name[0], fcport->port_name[1],
  1863. fcport->port_name[2], fcport->port_name[3],
  1864. fcport->port_name[4], fcport->port_name[5],
  1865. fcport->port_name[6], fcport->port_name[7], rval,
  1866. fcport->fp_speed, mb[0], mb[1]));
  1867. } else {
  1868. DEBUG2(qla_printk(KERN_INFO, ha,
  1869. "iIDMA adjusted to %s GB/s on "
  1870. "%02x%02x%02x%02x%02x%02x%02x%02x.\n",
  1871. link_speeds[fcport->fp_speed], fcport->port_name[0],
  1872. fcport->port_name[1], fcport->port_name[2],
  1873. fcport->port_name[3], fcport->port_name[4],
  1874. fcport->port_name[5], fcport->port_name[6],
  1875. fcport->port_name[7]));
  1876. }
  1877. }
  1878. static void
  1879. qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
  1880. {
  1881. struct fc_rport_identifiers rport_ids;
  1882. struct fc_rport *rport;
  1883. unsigned long flags;
  1884. if (fcport->drport)
  1885. qla2x00_rport_del(fcport);
  1886. if (fcport->rport)
  1887. return;
  1888. rport_ids.node_name = wwn_to_u64(fcport->node_name);
  1889. rport_ids.port_name = wwn_to_u64(fcport->port_name);
  1890. rport_ids.port_id = fcport->d_id.b.domain << 16 |
  1891. fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
  1892. rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
  1893. rport = fc_remote_port_add(ha->host, 0, &rport_ids);
  1894. if (!rport) {
  1895. qla_printk(KERN_WARNING, ha,
  1896. "Unable to allocate fc remote port!\n");
  1897. return;
  1898. }
  1899. spin_lock_irqsave(&fcport->rport_lock, flags);
  1900. fcport->rport = rport;
  1901. *((fc_port_t **)rport->dd_data) = fcport;
  1902. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1903. rport->supported_classes = fcport->supported_classes;
  1904. rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
  1905. if (fcport->port_type == FCT_INITIATOR)
  1906. rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  1907. if (fcport->port_type == FCT_TARGET)
  1908. rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
  1909. fc_remote_port_rolechg(rport, rport_ids.roles);
  1910. }
  1911. /*
  1912. * qla2x00_update_fcport
  1913. * Updates device on list.
  1914. *
  1915. * Input:
  1916. * ha = adapter block pointer.
  1917. * fcport = port structure pointer.
  1918. *
  1919. * Return:
  1920. * 0 - Success
  1921. * BIT_0 - error
  1922. *
  1923. * Context:
  1924. * Kernel context.
  1925. */
  1926. void
  1927. qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
  1928. {
  1929. scsi_qla_host_t *pha = to_qla_parent(ha);
  1930. fcport->ha = ha;
  1931. fcport->login_retry = 0;
  1932. fcport->port_login_retry_count = pha->port_down_retry_count *
  1933. PORT_RETRY_TIME;
  1934. atomic_set(&fcport->port_down_timer, pha->port_down_retry_count *
  1935. PORT_RETRY_TIME);
  1936. fcport->flags &= ~FCF_LOGIN_NEEDED;
  1937. qla2x00_iidma_fcport(ha, fcport);
  1938. atomic_set(&fcport->state, FCS_ONLINE);
  1939. qla2x00_reg_remote_port(ha, fcport);
  1940. }
  1941. /*
  1942. * qla2x00_configure_fabric
  1943. * Setup SNS devices with loop ID's.
  1944. *
  1945. * Input:
  1946. * ha = adapter block pointer.
  1947. *
  1948. * Returns:
  1949. * 0 = success.
  1950. * BIT_0 = error
  1951. */
  1952. static int
  1953. qla2x00_configure_fabric(scsi_qla_host_t *ha)
  1954. {
  1955. int rval, rval2;
  1956. fc_port_t *fcport, *fcptemp;
  1957. uint16_t next_loopid;
  1958. uint16_t mb[MAILBOX_REGISTER_COUNT];
  1959. uint16_t loop_id;
  1960. LIST_HEAD(new_fcports);
  1961. scsi_qla_host_t *pha = to_qla_parent(ha);
  1962. /* If FL port exists, then SNS is present */
  1963. if (IS_FWI2_CAPABLE(ha))
  1964. loop_id = NPH_F_PORT;
  1965. else
  1966. loop_id = SNS_FL_PORT;
  1967. rval = qla2x00_get_port_name(ha, loop_id, ha->fabric_node_name, 1);
  1968. if (rval != QLA_SUCCESS) {
  1969. DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
  1970. "Port\n", ha->host_no));
  1971. ha->device_flags &= ~SWITCH_FOUND;
  1972. return (QLA_SUCCESS);
  1973. }
  1974. ha->device_flags |= SWITCH_FOUND;
  1975. /* Mark devices that need re-synchronization. */
  1976. rval2 = qla2x00_device_resync(ha);
  1977. if (rval2 == QLA_RSCNS_HANDLED) {
  1978. /* No point doing the scan, just continue. */
  1979. return (QLA_SUCCESS);
  1980. }
  1981. do {
  1982. /* FDMI support. */
  1983. if (ql2xfdmienable &&
  1984. test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
  1985. qla2x00_fdmi_register(ha);
  1986. /* Ensure we are logged into the SNS. */
  1987. if (IS_FWI2_CAPABLE(ha))
  1988. loop_id = NPH_SNS;
  1989. else
  1990. loop_id = SIMPLE_NAME_SERVER;
  1991. ha->isp_ops->fabric_login(ha, loop_id, 0xff, 0xff,
  1992. 0xfc, mb, BIT_1 | BIT_0);
  1993. if (mb[0] != MBS_COMMAND_COMPLETE) {
  1994. DEBUG2(qla_printk(KERN_INFO, ha,
  1995. "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
  1996. "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
  1997. mb[0], mb[1], mb[2], mb[6], mb[7]));
  1998. return (QLA_SUCCESS);
  1999. }
  2000. if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
  2001. if (qla2x00_rft_id(ha)) {
  2002. /* EMPTY */
  2003. DEBUG2(printk("scsi(%ld): Register FC-4 "
  2004. "TYPE failed.\n", ha->host_no));
  2005. }
  2006. if (qla2x00_rff_id(ha)) {
  2007. /* EMPTY */
  2008. DEBUG2(printk("scsi(%ld): Register FC-4 "
  2009. "Features failed.\n", ha->host_no));
  2010. }
  2011. if (qla2x00_rnn_id(ha)) {
  2012. /* EMPTY */
  2013. DEBUG2(printk("scsi(%ld): Register Node Name "
  2014. "failed.\n", ha->host_no));
  2015. } else if (qla2x00_rsnn_nn(ha)) {
  2016. /* EMPTY */
  2017. DEBUG2(printk("scsi(%ld): Register Symbolic "
  2018. "Node Name failed.\n", ha->host_no));
  2019. }
  2020. }
  2021. rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
  2022. if (rval != QLA_SUCCESS)
  2023. break;
  2024. /*
  2025. * Logout all previous fabric devices marked lost, except
  2026. * tape devices.
  2027. */
  2028. list_for_each_entry(fcport, &pha->fcports, list) {
  2029. if (fcport->vp_idx !=ha->vp_idx)
  2030. continue;
  2031. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  2032. break;
  2033. if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
  2034. continue;
  2035. if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
  2036. qla2x00_mark_device_lost(ha, fcport,
  2037. ql2xplogiabsentdevice, 0);
  2038. if (fcport->loop_id != FC_NO_LOOP_ID &&
  2039. (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
  2040. fcport->port_type != FCT_INITIATOR &&
  2041. fcport->port_type != FCT_BROADCAST) {
  2042. ha->isp_ops->fabric_logout(ha,
  2043. fcport->loop_id,
  2044. fcport->d_id.b.domain,
  2045. fcport->d_id.b.area,
  2046. fcport->d_id.b.al_pa);
  2047. fcport->loop_id = FC_NO_LOOP_ID;
  2048. }
  2049. }
  2050. }
  2051. /* Starting free loop ID. */
  2052. next_loopid = pha->min_external_loopid;
  2053. /*
  2054. * Scan through our port list and login entries that need to be
  2055. * logged in.
  2056. */
  2057. list_for_each_entry(fcport, &pha->fcports, list) {
  2058. if (fcport->vp_idx != ha->vp_idx)
  2059. continue;
  2060. if (atomic_read(&ha->loop_down_timer) ||
  2061. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  2062. break;
  2063. if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
  2064. (fcport->flags & FCF_LOGIN_NEEDED) == 0)
  2065. continue;
  2066. if (fcport->loop_id == FC_NO_LOOP_ID) {
  2067. fcport->loop_id = next_loopid;
  2068. rval = qla2x00_find_new_loop_id(ha, fcport);
  2069. if (rval != QLA_SUCCESS) {
  2070. /* Ran out of IDs to use */
  2071. break;
  2072. }
  2073. }
  2074. /* Login and update database */
  2075. qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
  2076. }
  2077. /* Exit if out of loop IDs. */
  2078. if (rval != QLA_SUCCESS) {
  2079. break;
  2080. }
  2081. /*
  2082. * Login and add the new devices to our port list.
  2083. */
  2084. list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
  2085. if (atomic_read(&ha->loop_down_timer) ||
  2086. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  2087. break;
  2088. /* Find a new loop ID to use. */
  2089. fcport->loop_id = next_loopid;
  2090. rval = qla2x00_find_new_loop_id(ha, fcport);
  2091. if (rval != QLA_SUCCESS) {
  2092. /* Ran out of IDs to use */
  2093. break;
  2094. }
  2095. /* Login and update database */
  2096. qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
  2097. if (ha->parent) {
  2098. fcport->ha = ha;
  2099. fcport->vp_idx = ha->vp_idx;
  2100. list_add_tail(&fcport->vp_fcport,
  2101. &ha->vp_fcports);
  2102. list_move_tail(&fcport->list,
  2103. &ha->parent->fcports);
  2104. } else
  2105. list_move_tail(&fcport->list, &ha->fcports);
  2106. }
  2107. } while (0);
  2108. /* Free all new device structures not processed. */
  2109. list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
  2110. list_del(&fcport->list);
  2111. kfree(fcport);
  2112. }
  2113. if (rval) {
  2114. DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
  2115. "rval=%d\n", ha->host_no, rval));
  2116. }
  2117. return (rval);
  2118. }
  2119. /*
  2120. * qla2x00_find_all_fabric_devs
  2121. *
  2122. * Input:
  2123. * ha = adapter block pointer.
  2124. * dev = database device entry pointer.
  2125. *
  2126. * Returns:
  2127. * 0 = success.
  2128. *
  2129. * Context:
  2130. * Kernel context.
  2131. */
  2132. static int
  2133. qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
  2134. {
  2135. int rval;
  2136. uint16_t loop_id;
  2137. fc_port_t *fcport, *new_fcport, *fcptemp;
  2138. int found;
  2139. sw_info_t *swl;
  2140. int swl_idx;
  2141. int first_dev, last_dev;
  2142. port_id_t wrap, nxt_d_id;
  2143. int vp_index;
  2144. int empty_vp_index;
  2145. int found_vp;
  2146. scsi_qla_host_t *vha;
  2147. scsi_qla_host_t *pha = to_qla_parent(ha);
  2148. rval = QLA_SUCCESS;
  2149. /* Try GID_PT to get device list, else GAN. */
  2150. swl = kcalloc(MAX_FIBRE_DEVICES, sizeof(sw_info_t), GFP_ATOMIC);
  2151. if (!swl) {
  2152. /*EMPTY*/
  2153. DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
  2154. "on GA_NXT\n", ha->host_no));
  2155. } else {
  2156. if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
  2157. kfree(swl);
  2158. swl = NULL;
  2159. } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
  2160. kfree(swl);
  2161. swl = NULL;
  2162. } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
  2163. kfree(swl);
  2164. swl = NULL;
  2165. } else if (qla2x00_gfpn_id(ha, swl) == QLA_SUCCESS) {
  2166. qla2x00_gpsc(ha, swl);
  2167. }
  2168. }
  2169. swl_idx = 0;
  2170. /* Allocate temporary fcport for any new fcports discovered. */
  2171. new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
  2172. if (new_fcport == NULL) {
  2173. kfree(swl);
  2174. return (QLA_MEMORY_ALLOC_FAILED);
  2175. }
  2176. new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
  2177. new_fcport->vp_idx = ha->vp_idx;
  2178. /* Set start port ID scan at adapter ID. */
  2179. first_dev = 1;
  2180. last_dev = 0;
  2181. /* Starting free loop ID. */
  2182. loop_id = pha->min_external_loopid;
  2183. for (; loop_id <= ha->last_loop_id; loop_id++) {
  2184. if (qla2x00_is_reserved_id(ha, loop_id))
  2185. continue;
  2186. if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
  2187. break;
  2188. if (swl != NULL) {
  2189. if (last_dev) {
  2190. wrap.b24 = new_fcport->d_id.b24;
  2191. } else {
  2192. new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
  2193. memcpy(new_fcport->node_name,
  2194. swl[swl_idx].node_name, WWN_SIZE);
  2195. memcpy(new_fcport->port_name,
  2196. swl[swl_idx].port_name, WWN_SIZE);
  2197. memcpy(new_fcport->fabric_port_name,
  2198. swl[swl_idx].fabric_port_name, WWN_SIZE);
  2199. new_fcport->fp_speed = swl[swl_idx].fp_speed;
  2200. if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
  2201. last_dev = 1;
  2202. }
  2203. swl_idx++;
  2204. }
  2205. } else {
  2206. /* Send GA_NXT to the switch */
  2207. rval = qla2x00_ga_nxt(ha, new_fcport);
  2208. if (rval != QLA_SUCCESS) {
  2209. qla_printk(KERN_WARNING, ha,
  2210. "SNS scan failed -- assuming zero-entry "
  2211. "result...\n");
  2212. list_for_each_entry_safe(fcport, fcptemp,
  2213. new_fcports, list) {
  2214. list_del(&fcport->list);
  2215. kfree(fcport);
  2216. }
  2217. rval = QLA_SUCCESS;
  2218. break;
  2219. }
  2220. }
  2221. /* If wrap on switch device list, exit. */
  2222. if (first_dev) {
  2223. wrap.b24 = new_fcport->d_id.b24;
  2224. first_dev = 0;
  2225. } else if (new_fcport->d_id.b24 == wrap.b24) {
  2226. DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
  2227. ha->host_no, new_fcport->d_id.b.domain,
  2228. new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
  2229. break;
  2230. }
  2231. /* Bypass if same physical adapter. */
  2232. if (new_fcport->d_id.b24 == pha->d_id.b24)
  2233. continue;
  2234. /* Bypass virtual ports of the same host. */
  2235. if (pha->num_vhosts) {
  2236. for_each_mapped_vp_idx(pha, vp_index) {
  2237. empty_vp_index = 1;
  2238. found_vp = 0;
  2239. list_for_each_entry(vha, &pha->vp_list,
  2240. vp_list) {
  2241. if (vp_index == vha->vp_idx) {
  2242. empty_vp_index = 0;
  2243. found_vp = 1;
  2244. break;
  2245. }
  2246. }
  2247. if (empty_vp_index)
  2248. continue;
  2249. if (found_vp &&
  2250. new_fcport->d_id.b24 == vha->d_id.b24)
  2251. break;
  2252. }
  2253. if (vp_index <= pha->max_npiv_vports)
  2254. continue;
  2255. }
  2256. /* Bypass if same domain and area of adapter. */
  2257. if (((new_fcport->d_id.b24 & 0xffff00) ==
  2258. (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
  2259. ISP_CFG_FL)
  2260. continue;
  2261. /* Bypass reserved domain fields. */
  2262. if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
  2263. continue;
  2264. /* Locate matching device in database. */
  2265. found = 0;
  2266. list_for_each_entry(fcport, &pha->fcports, list) {
  2267. if (new_fcport->vp_idx != fcport->vp_idx)
  2268. continue;
  2269. if (memcmp(new_fcport->port_name, fcport->port_name,
  2270. WWN_SIZE))
  2271. continue;
  2272. found++;
  2273. /* Update port state. */
  2274. memcpy(fcport->fabric_port_name,
  2275. new_fcport->fabric_port_name, WWN_SIZE);
  2276. fcport->fp_speed = new_fcport->fp_speed;
  2277. /*
  2278. * If address the same and state FCS_ONLINE, nothing
  2279. * changed.
  2280. */
  2281. if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
  2282. atomic_read(&fcport->state) == FCS_ONLINE) {
  2283. break;
  2284. }
  2285. /*
  2286. * If device was not a fabric device before.
  2287. */
  2288. if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
  2289. fcport->d_id.b24 = new_fcport->d_id.b24;
  2290. fcport->loop_id = FC_NO_LOOP_ID;
  2291. fcport->flags |= (FCF_FABRIC_DEVICE |
  2292. FCF_LOGIN_NEEDED);
  2293. fcport->flags &= ~FCF_PERSISTENT_BOUND;
  2294. break;
  2295. }
  2296. /*
  2297. * Port ID changed or device was marked to be updated;
  2298. * Log it out if still logged in and mark it for
  2299. * relogin later.
  2300. */
  2301. fcport->d_id.b24 = new_fcport->d_id.b24;
  2302. fcport->flags |= FCF_LOGIN_NEEDED;
  2303. if (fcport->loop_id != FC_NO_LOOP_ID &&
  2304. (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
  2305. fcport->port_type != FCT_INITIATOR &&
  2306. fcport->port_type != FCT_BROADCAST) {
  2307. ha->isp_ops->fabric_logout(ha, fcport->loop_id,
  2308. fcport->d_id.b.domain, fcport->d_id.b.area,
  2309. fcport->d_id.b.al_pa);
  2310. fcport->loop_id = FC_NO_LOOP_ID;
  2311. }
  2312. break;
  2313. }
  2314. if (found)
  2315. continue;
  2316. /* If device was not in our fcports list, then add it. */
  2317. list_add_tail(&new_fcport->list, new_fcports);
  2318. /* Allocate a new replacement fcport. */
  2319. nxt_d_id.b24 = new_fcport->d_id.b24;
  2320. new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
  2321. if (new_fcport == NULL) {
  2322. kfree(swl);
  2323. return (QLA_MEMORY_ALLOC_FAILED);
  2324. }
  2325. new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
  2326. new_fcport->d_id.b24 = nxt_d_id.b24;
  2327. new_fcport->vp_idx = ha->vp_idx;
  2328. }
  2329. kfree(swl);
  2330. kfree(new_fcport);
  2331. if (!list_empty(new_fcports))
  2332. ha->device_flags |= DFLG_FABRIC_DEVICES;
  2333. return (rval);
  2334. }
  2335. /*
  2336. * qla2x00_find_new_loop_id
  2337. * Scan through our port list and find a new usable loop ID.
  2338. *
  2339. * Input:
  2340. * ha: adapter state pointer.
  2341. * dev: port structure pointer.
  2342. *
  2343. * Returns:
  2344. * qla2x00 local function return status code.
  2345. *
  2346. * Context:
  2347. * Kernel context.
  2348. */
  2349. static int
  2350. qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
  2351. {
  2352. int rval;
  2353. int found;
  2354. fc_port_t *fcport;
  2355. uint16_t first_loop_id;
  2356. scsi_qla_host_t *pha = to_qla_parent(ha);
  2357. rval = QLA_SUCCESS;
  2358. /* Save starting loop ID. */
  2359. first_loop_id = dev->loop_id;
  2360. for (;;) {
  2361. /* Skip loop ID if already used by adapter. */
  2362. if (dev->loop_id == ha->loop_id) {
  2363. dev->loop_id++;
  2364. }
  2365. /* Skip reserved loop IDs. */
  2366. while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
  2367. dev->loop_id++;
  2368. }
  2369. /* Reset loop ID if passed the end. */
  2370. if (dev->loop_id > ha->last_loop_id) {
  2371. /* first loop ID. */
  2372. dev->loop_id = ha->min_external_loopid;
  2373. }
  2374. /* Check for loop ID being already in use. */
  2375. found = 0;
  2376. fcport = NULL;
  2377. list_for_each_entry(fcport, &pha->fcports, list) {
  2378. if (fcport->loop_id == dev->loop_id && fcport != dev) {
  2379. /* ID possibly in use */
  2380. found++;
  2381. break;
  2382. }
  2383. }
  2384. /* If not in use then it is free to use. */
  2385. if (!found) {
  2386. break;
  2387. }
  2388. /* ID in use. Try next value. */
  2389. dev->loop_id++;
  2390. /* If wrap around. No free ID to use. */
  2391. if (dev->loop_id == first_loop_id) {
  2392. dev->loop_id = FC_NO_LOOP_ID;
  2393. rval = QLA_FUNCTION_FAILED;
  2394. break;
  2395. }
  2396. }
  2397. return (rval);
  2398. }
  2399. /*
  2400. * qla2x00_device_resync
  2401. * Marks devices in the database that needs resynchronization.
  2402. *
  2403. * Input:
  2404. * ha = adapter block pointer.
  2405. *
  2406. * Context:
  2407. * Kernel context.
  2408. */
  2409. static int
  2410. qla2x00_device_resync(scsi_qla_host_t *ha)
  2411. {
  2412. int rval;
  2413. uint32_t mask;
  2414. fc_port_t *fcport;
  2415. uint32_t rscn_entry;
  2416. uint8_t rscn_out_iter;
  2417. uint8_t format;
  2418. port_id_t d_id;
  2419. scsi_qla_host_t *pha = to_qla_parent(ha);
  2420. rval = QLA_RSCNS_HANDLED;
  2421. while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
  2422. ha->flags.rscn_queue_overflow) {
  2423. rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
  2424. format = MSB(MSW(rscn_entry));
  2425. d_id.b.domain = LSB(MSW(rscn_entry));
  2426. d_id.b.area = MSB(LSW(rscn_entry));
  2427. d_id.b.al_pa = LSB(LSW(rscn_entry));
  2428. DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
  2429. "[%02x/%02x%02x%02x].\n",
  2430. ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
  2431. d_id.b.area, d_id.b.al_pa));
  2432. ha->rscn_out_ptr++;
  2433. if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
  2434. ha->rscn_out_ptr = 0;
  2435. /* Skip duplicate entries. */
  2436. for (rscn_out_iter = ha->rscn_out_ptr;
  2437. !ha->flags.rscn_queue_overflow &&
  2438. rscn_out_iter != ha->rscn_in_ptr;
  2439. rscn_out_iter = (rscn_out_iter ==
  2440. (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
  2441. if (rscn_entry != ha->rscn_queue[rscn_out_iter])
  2442. break;
  2443. DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
  2444. "entry found at [%d].\n", ha->host_no,
  2445. rscn_out_iter));
  2446. ha->rscn_out_ptr = rscn_out_iter;
  2447. }
  2448. /* Queue overflow, set switch default case. */
  2449. if (ha->flags.rscn_queue_overflow) {
  2450. DEBUG(printk("scsi(%ld): device_resync: rscn "
  2451. "overflow.\n", ha->host_no));
  2452. format = 3;
  2453. ha->flags.rscn_queue_overflow = 0;
  2454. }
  2455. switch (format) {
  2456. case 0:
  2457. mask = 0xffffff;
  2458. break;
  2459. case 1:
  2460. mask = 0xffff00;
  2461. break;
  2462. case 2:
  2463. mask = 0xff0000;
  2464. break;
  2465. default:
  2466. mask = 0x0;
  2467. d_id.b24 = 0;
  2468. ha->rscn_out_ptr = ha->rscn_in_ptr;
  2469. break;
  2470. }
  2471. rval = QLA_SUCCESS;
  2472. list_for_each_entry(fcport, &pha->fcports, list) {
  2473. if (fcport->vp_idx != ha->vp_idx)
  2474. continue;
  2475. if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
  2476. (fcport->d_id.b24 & mask) != d_id.b24 ||
  2477. fcport->port_type == FCT_BROADCAST)
  2478. continue;
  2479. if (atomic_read(&fcport->state) == FCS_ONLINE) {
  2480. if (format != 3 ||
  2481. fcport->port_type != FCT_INITIATOR) {
  2482. qla2x00_mark_device_lost(ha, fcport,
  2483. 0, 0);
  2484. }
  2485. }
  2486. fcport->flags &= ~FCF_FARP_DONE;
  2487. }
  2488. }
  2489. return (rval);
  2490. }
  2491. /*
  2492. * qla2x00_fabric_dev_login
  2493. * Login fabric target device and update FC port database.
  2494. *
  2495. * Input:
  2496. * ha: adapter state pointer.
  2497. * fcport: port structure list pointer.
  2498. * next_loopid: contains value of a new loop ID that can be used
  2499. * by the next login attempt.
  2500. *
  2501. * Returns:
  2502. * qla2x00 local function return status code.
  2503. *
  2504. * Context:
  2505. * Kernel context.
  2506. */
  2507. static int
  2508. qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
  2509. uint16_t *next_loopid)
  2510. {
  2511. int rval;
  2512. int retry;
  2513. uint8_t opts;
  2514. rval = QLA_SUCCESS;
  2515. retry = 0;
  2516. rval = qla2x00_fabric_login(ha, fcport, next_loopid);
  2517. if (rval == QLA_SUCCESS) {
  2518. /* Send an ADISC to tape devices.*/
  2519. opts = 0;
  2520. if (fcport->flags & FCF_TAPE_PRESENT)
  2521. opts |= BIT_1;
  2522. rval = qla2x00_get_port_database(ha, fcport, opts);
  2523. if (rval != QLA_SUCCESS) {
  2524. ha->isp_ops->fabric_logout(ha, fcport->loop_id,
  2525. fcport->d_id.b.domain, fcport->d_id.b.area,
  2526. fcport->d_id.b.al_pa);
  2527. qla2x00_mark_device_lost(ha, fcport, 1, 0);
  2528. } else {
  2529. qla2x00_update_fcport(ha, fcport);
  2530. }
  2531. }
  2532. return (rval);
  2533. }
  2534. /*
  2535. * qla2x00_fabric_login
  2536. * Issue fabric login command.
  2537. *
  2538. * Input:
  2539. * ha = adapter block pointer.
  2540. * device = pointer to FC device type structure.
  2541. *
  2542. * Returns:
  2543. * 0 - Login successfully
  2544. * 1 - Login failed
  2545. * 2 - Initiator device
  2546. * 3 - Fatal error
  2547. */
  2548. int
  2549. qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
  2550. uint16_t *next_loopid)
  2551. {
  2552. int rval;
  2553. int retry;
  2554. uint16_t tmp_loopid;
  2555. uint16_t mb[MAILBOX_REGISTER_COUNT];
  2556. retry = 0;
  2557. tmp_loopid = 0;
  2558. for (;;) {
  2559. DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
  2560. "for port %02x%02x%02x.\n",
  2561. ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
  2562. fcport->d_id.b.area, fcport->d_id.b.al_pa));
  2563. /* Login fcport on switch. */
  2564. ha->isp_ops->fabric_login(ha, fcport->loop_id,
  2565. fcport->d_id.b.domain, fcport->d_id.b.area,
  2566. fcport->d_id.b.al_pa, mb, BIT_0);
  2567. if (mb[0] == MBS_PORT_ID_USED) {
  2568. /*
  2569. * Device has another loop ID. The firmware team
  2570. * recommends the driver perform an implicit login with
  2571. * the specified ID again. The ID we just used is save
  2572. * here so we return with an ID that can be tried by
  2573. * the next login.
  2574. */
  2575. retry++;
  2576. tmp_loopid = fcport->loop_id;
  2577. fcport->loop_id = mb[1];
  2578. DEBUG(printk("Fabric Login: port in use - next "
  2579. "loop id=0x%04x, port Id=%02x%02x%02x.\n",
  2580. fcport->loop_id, fcport->d_id.b.domain,
  2581. fcport->d_id.b.area, fcport->d_id.b.al_pa));
  2582. } else if (mb[0] == MBS_COMMAND_COMPLETE) {
  2583. /*
  2584. * Login succeeded.
  2585. */
  2586. if (retry) {
  2587. /* A retry occurred before. */
  2588. *next_loopid = tmp_loopid;
  2589. } else {
  2590. /*
  2591. * No retry occurred before. Just increment the
  2592. * ID value for next login.
  2593. */
  2594. *next_loopid = (fcport->loop_id + 1);
  2595. }
  2596. if (mb[1] & BIT_0) {
  2597. fcport->port_type = FCT_INITIATOR;
  2598. } else {
  2599. fcport->port_type = FCT_TARGET;
  2600. if (mb[1] & BIT_1) {
  2601. fcport->flags |= FCF_TAPE_PRESENT;
  2602. }
  2603. }
  2604. if (mb[10] & BIT_0)
  2605. fcport->supported_classes |= FC_COS_CLASS2;
  2606. if (mb[10] & BIT_1)
  2607. fcport->supported_classes |= FC_COS_CLASS3;
  2608. rval = QLA_SUCCESS;
  2609. break;
  2610. } else if (mb[0] == MBS_LOOP_ID_USED) {
  2611. /*
  2612. * Loop ID already used, try next loop ID.
  2613. */
  2614. fcport->loop_id++;
  2615. rval = qla2x00_find_new_loop_id(ha, fcport);
  2616. if (rval != QLA_SUCCESS) {
  2617. /* Ran out of loop IDs to use */
  2618. break;
  2619. }
  2620. } else if (mb[0] == MBS_COMMAND_ERROR) {
  2621. /*
  2622. * Firmware possibly timed out during login. If NO
  2623. * retries are left to do then the device is declared
  2624. * dead.
  2625. */
  2626. *next_loopid = fcport->loop_id;
  2627. ha->isp_ops->fabric_logout(ha, fcport->loop_id,
  2628. fcport->d_id.b.domain, fcport->d_id.b.area,
  2629. fcport->d_id.b.al_pa);
  2630. qla2x00_mark_device_lost(ha, fcport, 1, 0);
  2631. rval = 1;
  2632. break;
  2633. } else {
  2634. /*
  2635. * unrecoverable / not handled error
  2636. */
  2637. DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
  2638. "loop_id=%x jiffies=%lx.\n",
  2639. __func__, ha->host_no, mb[0],
  2640. fcport->d_id.b.domain, fcport->d_id.b.area,
  2641. fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
  2642. *next_loopid = fcport->loop_id;
  2643. ha->isp_ops->fabric_logout(ha, fcport->loop_id,
  2644. fcport->d_id.b.domain, fcport->d_id.b.area,
  2645. fcport->d_id.b.al_pa);
  2646. fcport->loop_id = FC_NO_LOOP_ID;
  2647. fcport->login_retry = 0;
  2648. rval = 3;
  2649. break;
  2650. }
  2651. }
  2652. return (rval);
  2653. }
  2654. /*
  2655. * qla2x00_local_device_login
  2656. * Issue local device login command.
  2657. *
  2658. * Input:
  2659. * ha = adapter block pointer.
  2660. * loop_id = loop id of device to login to.
  2661. *
  2662. * Returns (Where's the #define!!!!):
  2663. * 0 - Login successfully
  2664. * 1 - Login failed
  2665. * 3 - Fatal error
  2666. */
  2667. int
  2668. qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
  2669. {
  2670. int rval;
  2671. uint16_t mb[MAILBOX_REGISTER_COUNT];
  2672. memset(mb, 0, sizeof(mb));
  2673. rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
  2674. if (rval == QLA_SUCCESS) {
  2675. /* Interrogate mailbox registers for any errors */
  2676. if (mb[0] == MBS_COMMAND_ERROR)
  2677. rval = 1;
  2678. else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
  2679. /* device not in PCB table */
  2680. rval = 3;
  2681. }
  2682. return (rval);
  2683. }
  2684. /*
  2685. * qla2x00_loop_resync
  2686. * Resync with fibre channel devices.
  2687. *
  2688. * Input:
  2689. * ha = adapter block pointer.
  2690. *
  2691. * Returns:
  2692. * 0 = success
  2693. */
  2694. int
  2695. qla2x00_loop_resync(scsi_qla_host_t *ha)
  2696. {
  2697. int rval;
  2698. uint32_t wait_time;
  2699. rval = QLA_SUCCESS;
  2700. atomic_set(&ha->loop_state, LOOP_UPDATE);
  2701. clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
  2702. if (ha->flags.online) {
  2703. if (!(rval = qla2x00_fw_ready(ha))) {
  2704. /* Wait at most MAX_TARGET RSCNs for a stable link. */
  2705. wait_time = 256;
  2706. do {
  2707. atomic_set(&ha->loop_state, LOOP_UPDATE);
  2708. /* Issue a marker after FW becomes ready. */
  2709. qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
  2710. ha->marker_needed = 0;
  2711. /* Remap devices on Loop. */
  2712. clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  2713. qla2x00_configure_loop(ha);
  2714. wait_time--;
  2715. } while (!atomic_read(&ha->loop_down_timer) &&
  2716. !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
  2717. wait_time &&
  2718. (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
  2719. }
  2720. }
  2721. if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
  2722. return (QLA_FUNCTION_FAILED);
  2723. }
  2724. if (rval) {
  2725. DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
  2726. }
  2727. return (rval);
  2728. }
  2729. void
  2730. qla2x00_rescan_fcports(scsi_qla_host_t *ha)
  2731. {
  2732. int rescan_done;
  2733. fc_port_t *fcport;
  2734. rescan_done = 0;
  2735. list_for_each_entry(fcport, &ha->fcports, list) {
  2736. if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
  2737. continue;
  2738. qla2x00_update_fcport(ha, fcport);
  2739. fcport->flags &= ~FCF_RESCAN_NEEDED;
  2740. rescan_done = 1;
  2741. }
  2742. qla2x00_probe_for_all_luns(ha);
  2743. }
  2744. void
  2745. qla2x00_update_fcports(scsi_qla_host_t *ha)
  2746. {
  2747. fc_port_t *fcport;
  2748. /* Go with deferred removal of rport references. */
  2749. list_for_each_entry(fcport, &ha->fcports, list)
  2750. if (fcport->drport)
  2751. qla2x00_rport_del(fcport);
  2752. }
  2753. /*
  2754. * qla2x00_abort_isp
  2755. * Resets ISP and aborts all outstanding commands.
  2756. *
  2757. * Input:
  2758. * ha = adapter block pointer.
  2759. *
  2760. * Returns:
  2761. * 0 = success
  2762. */
  2763. int
  2764. qla2x00_abort_isp(scsi_qla_host_t *ha)
  2765. {
  2766. int rval;
  2767. uint8_t status = 0;
  2768. if (ha->flags.online) {
  2769. ha->flags.online = 0;
  2770. clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  2771. qla_printk(KERN_INFO, ha,
  2772. "Performing ISP error recovery - ha= %p.\n", ha);
  2773. ha->isp_ops->reset_chip(ha);
  2774. atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
  2775. if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
  2776. atomic_set(&ha->loop_state, LOOP_DOWN);
  2777. qla2x00_mark_all_devices_lost(ha, 0);
  2778. } else {
  2779. if (!atomic_read(&ha->loop_down_timer))
  2780. atomic_set(&ha->loop_down_timer,
  2781. LOOP_DOWN_TIME);
  2782. }
  2783. /* Requeue all commands in outstanding command list. */
  2784. qla2x00_abort_all_cmds(ha, DID_RESET << 16);
  2785. ha->isp_ops->get_flash_version(ha, ha->request_ring);
  2786. ha->isp_ops->nvram_config(ha);
  2787. if (!qla2x00_restart_isp(ha)) {
  2788. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  2789. if (!atomic_read(&ha->loop_down_timer)) {
  2790. /*
  2791. * Issue marker command only when we are going
  2792. * to start the I/O .
  2793. */
  2794. ha->marker_needed = 1;
  2795. }
  2796. ha->flags.online = 1;
  2797. ha->isp_ops->enable_intrs(ha);
  2798. ha->isp_abort_cnt = 0;
  2799. clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
  2800. if (ha->eft) {
  2801. memset(ha->eft, 0, EFT_SIZE);
  2802. rval = qla2x00_enable_eft_trace(ha,
  2803. ha->eft_dma, EFT_NUM_BUFFERS);
  2804. if (rval) {
  2805. qla_printk(KERN_WARNING, ha,
  2806. "Unable to reinitialize EFT "
  2807. "(%d).\n", rval);
  2808. }
  2809. }
  2810. if (ha->fce) {
  2811. ha->flags.fce_enabled = 1;
  2812. memset(ha->fce, 0,
  2813. fce_calc_size(ha->fce_bufs));
  2814. rval = qla2x00_enable_fce_trace(ha,
  2815. ha->fce_dma, ha->fce_bufs, ha->fce_mb,
  2816. &ha->fce_bufs);
  2817. if (rval) {
  2818. qla_printk(KERN_WARNING, ha,
  2819. "Unable to reinitialize FCE "
  2820. "(%d).\n", rval);
  2821. ha->flags.fce_enabled = 0;
  2822. }
  2823. }
  2824. } else { /* failed the ISP abort */
  2825. ha->flags.online = 1;
  2826. if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
  2827. if (ha->isp_abort_cnt == 0) {
  2828. qla_printk(KERN_WARNING, ha,
  2829. "ISP error recovery failed - "
  2830. "board disabled\n");
  2831. /*
  2832. * The next call disables the board
  2833. * completely.
  2834. */
  2835. ha->isp_ops->reset_adapter(ha);
  2836. ha->flags.online = 0;
  2837. clear_bit(ISP_ABORT_RETRY,
  2838. &ha->dpc_flags);
  2839. status = 0;
  2840. } else { /* schedule another ISP abort */
  2841. ha->isp_abort_cnt--;
  2842. DEBUG(printk("qla%ld: ISP abort - "
  2843. "retry remaining %d\n",
  2844. ha->host_no, ha->isp_abort_cnt));
  2845. status = 1;
  2846. }
  2847. } else {
  2848. ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
  2849. DEBUG(printk("qla2x00(%ld): ISP error recovery "
  2850. "- retrying (%d) more times\n",
  2851. ha->host_no, ha->isp_abort_cnt));
  2852. set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
  2853. status = 1;
  2854. }
  2855. }
  2856. }
  2857. if (status) {
  2858. qla_printk(KERN_INFO, ha,
  2859. "qla2x00_abort_isp: **** FAILED ****\n");
  2860. } else {
  2861. DEBUG(printk(KERN_INFO
  2862. "qla2x00_abort_isp(%ld): exiting.\n",
  2863. ha->host_no));
  2864. }
  2865. return(status);
  2866. }
  2867. /*
  2868. * qla2x00_restart_isp
  2869. * restarts the ISP after a reset
  2870. *
  2871. * Input:
  2872. * ha = adapter block pointer.
  2873. *
  2874. * Returns:
  2875. * 0 = success
  2876. */
  2877. static int
  2878. qla2x00_restart_isp(scsi_qla_host_t *ha)
  2879. {
  2880. uint8_t status = 0;
  2881. uint32_t wait_time;
  2882. /* If firmware needs to be loaded */
  2883. if (qla2x00_isp_firmware(ha)) {
  2884. ha->flags.online = 0;
  2885. if (!(status = ha->isp_ops->chip_diag(ha)))
  2886. status = qla2x00_setup_chip(ha);
  2887. }
  2888. if (!status && !(status = qla2x00_init_rings(ha))) {
  2889. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  2890. if (!(status = qla2x00_fw_ready(ha))) {
  2891. DEBUG(printk("%s(): Start configure loop, "
  2892. "status = %d\n", __func__, status));
  2893. /* Issue a marker after FW becomes ready. */
  2894. qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
  2895. ha->flags.online = 1;
  2896. /* Wait at most MAX_TARGET RSCNs for a stable link. */
  2897. wait_time = 256;
  2898. do {
  2899. clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  2900. qla2x00_configure_loop(ha);
  2901. wait_time--;
  2902. } while (!atomic_read(&ha->loop_down_timer) &&
  2903. !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
  2904. wait_time &&
  2905. (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
  2906. }
  2907. /* if no cable then assume it's good */
  2908. if ((ha->device_flags & DFLG_NO_CABLE))
  2909. status = 0;
  2910. DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
  2911. __func__,
  2912. status));
  2913. }
  2914. return (status);
  2915. }
  2916. /*
  2917. * qla2x00_reset_adapter
  2918. * Reset adapter.
  2919. *
  2920. * Input:
  2921. * ha = adapter block pointer.
  2922. */
  2923. void
  2924. qla2x00_reset_adapter(scsi_qla_host_t *ha)
  2925. {
  2926. unsigned long flags = 0;
  2927. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  2928. ha->flags.online = 0;
  2929. ha->isp_ops->disable_intrs(ha);
  2930. spin_lock_irqsave(&ha->hardware_lock, flags);
  2931. WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
  2932. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  2933. WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
  2934. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  2935. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2936. }
  2937. void
  2938. qla24xx_reset_adapter(scsi_qla_host_t *ha)
  2939. {
  2940. unsigned long flags = 0;
  2941. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  2942. ha->flags.online = 0;
  2943. ha->isp_ops->disable_intrs(ha);
  2944. spin_lock_irqsave(&ha->hardware_lock, flags);
  2945. WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
  2946. RD_REG_DWORD(&reg->hccr);
  2947. WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
  2948. RD_REG_DWORD(&reg->hccr);
  2949. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2950. }
  2951. /* On sparc systems, obtain port and node WWN from firmware
  2952. * properties.
  2953. */
  2954. static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *ha, struct nvram_24xx *nv)
  2955. {
  2956. #ifdef CONFIG_SPARC
  2957. struct pci_dev *pdev = ha->pdev;
  2958. struct device_node *dp = pci_device_to_OF_node(pdev);
  2959. const u8 *val;
  2960. int len;
  2961. val = of_get_property(dp, "port-wwn", &len);
  2962. if (val && len >= WWN_SIZE)
  2963. memcpy(nv->port_name, val, WWN_SIZE);
  2964. val = of_get_property(dp, "node-wwn", &len);
  2965. if (val && len >= WWN_SIZE)
  2966. memcpy(nv->node_name, val, WWN_SIZE);
  2967. #endif
  2968. }
  2969. int
  2970. qla24xx_nvram_config(scsi_qla_host_t *ha)
  2971. {
  2972. int rval;
  2973. struct init_cb_24xx *icb;
  2974. struct nvram_24xx *nv;
  2975. uint32_t *dptr;
  2976. uint8_t *dptr1, *dptr2;
  2977. uint32_t chksum;
  2978. uint16_t cnt;
  2979. rval = QLA_SUCCESS;
  2980. icb = (struct init_cb_24xx *)ha->init_cb;
  2981. nv = ha->nvram;
  2982. /* Determine NVRAM starting address. */
  2983. ha->nvram_size = sizeof(struct nvram_24xx);
  2984. ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
  2985. ha->vpd_size = FA_NVRAM_VPD_SIZE;
  2986. ha->vpd_base = FA_NVRAM_VPD0_ADDR;
  2987. if (PCI_FUNC(ha->pdev->devfn)) {
  2988. ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
  2989. ha->vpd_base = FA_NVRAM_VPD1_ADDR;
  2990. }
  2991. /* Get VPD data into cache */
  2992. ha->vpd = ha->nvram + VPD_OFFSET;
  2993. ha->isp_ops->read_nvram(ha, (uint8_t *)ha->vpd,
  2994. ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
  2995. /* Get NVRAM data into cache and calculate checksum. */
  2996. dptr = (uint32_t *)nv;
  2997. ha->isp_ops->read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
  2998. ha->nvram_size);
  2999. for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
  3000. chksum += le32_to_cpu(*dptr++);
  3001. DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
  3002. DEBUG5(qla2x00_dump_buffer((uint8_t *)nv, ha->nvram_size));
  3003. /* Bad NVRAM data, set defaults parameters. */
  3004. if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
  3005. || nv->id[3] != ' ' ||
  3006. nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
  3007. /* Reset NVRAM data. */
  3008. qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
  3009. "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
  3010. le16_to_cpu(nv->nvram_version));
  3011. qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
  3012. "invalid -- WWPN) defaults.\n");
  3013. /*
  3014. * Set default initialization control block.
  3015. */
  3016. memset(nv, 0, ha->nvram_size);
  3017. nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
  3018. nv->version = __constant_cpu_to_le16(ICB_VERSION);
  3019. nv->frame_payload_size = __constant_cpu_to_le16(2048);
  3020. nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
  3021. nv->exchange_count = __constant_cpu_to_le16(0);
  3022. nv->hard_address = __constant_cpu_to_le16(124);
  3023. nv->port_name[0] = 0x21;
  3024. nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
  3025. nv->port_name[2] = 0x00;
  3026. nv->port_name[3] = 0xe0;
  3027. nv->port_name[4] = 0x8b;
  3028. nv->port_name[5] = 0x1c;
  3029. nv->port_name[6] = 0x55;
  3030. nv->port_name[7] = 0x86;
  3031. nv->node_name[0] = 0x20;
  3032. nv->node_name[1] = 0x00;
  3033. nv->node_name[2] = 0x00;
  3034. nv->node_name[3] = 0xe0;
  3035. nv->node_name[4] = 0x8b;
  3036. nv->node_name[5] = 0x1c;
  3037. nv->node_name[6] = 0x55;
  3038. nv->node_name[7] = 0x86;
  3039. qla24xx_nvram_wwn_from_ofw(ha, nv);
  3040. nv->login_retry_count = __constant_cpu_to_le16(8);
  3041. nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
  3042. nv->login_timeout = __constant_cpu_to_le16(0);
  3043. nv->firmware_options_1 =
  3044. __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
  3045. nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
  3046. nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
  3047. nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
  3048. nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
  3049. nv->efi_parameters = __constant_cpu_to_le32(0);
  3050. nv->reset_delay = 5;
  3051. nv->max_luns_per_target = __constant_cpu_to_le16(128);
  3052. nv->port_down_retry_count = __constant_cpu_to_le16(30);
  3053. nv->link_down_timeout = __constant_cpu_to_le16(30);
  3054. rval = 1;
  3055. }
  3056. /* Reset Initialization control block */
  3057. memset(icb, 0, sizeof(struct init_cb_24xx));
  3058. /* Copy 1st segment. */
  3059. dptr1 = (uint8_t *)icb;
  3060. dptr2 = (uint8_t *)&nv->version;
  3061. cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
  3062. while (cnt--)
  3063. *dptr1++ = *dptr2++;
  3064. icb->login_retry_count = nv->login_retry_count;
  3065. icb->link_down_on_nos = nv->link_down_on_nos;
  3066. /* Copy 2nd segment. */
  3067. dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
  3068. dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
  3069. cnt = (uint8_t *)&icb->reserved_3 -
  3070. (uint8_t *)&icb->interrupt_delay_timer;
  3071. while (cnt--)
  3072. *dptr1++ = *dptr2++;
  3073. /*
  3074. * Setup driver NVRAM options.
  3075. */
  3076. qla2x00_set_model_info(ha, nv->model_name, sizeof(nv->model_name),
  3077. "QLA2462");
  3078. /* Use alternate WWN? */
  3079. if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
  3080. memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
  3081. memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
  3082. }
  3083. /* Prepare nodename */
  3084. if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
  3085. /*
  3086. * Firmware will apply the following mask if the nodename was
  3087. * not provided.
  3088. */
  3089. memcpy(icb->node_name, icb->port_name, WWN_SIZE);
  3090. icb->node_name[0] &= 0xF0;
  3091. }
  3092. /* Set host adapter parameters. */
  3093. ha->flags.disable_risc_code_load = 0;
  3094. ha->flags.enable_lip_reset = 0;
  3095. ha->flags.enable_lip_full_login =
  3096. le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
  3097. ha->flags.enable_target_reset =
  3098. le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
  3099. ha->flags.enable_led_scheme = 0;
  3100. ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
  3101. ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
  3102. (BIT_6 | BIT_5 | BIT_4)) >> 4;
  3103. memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
  3104. sizeof(ha->fw_seriallink_options24));
  3105. /* save HBA serial number */
  3106. ha->serial0 = icb->port_name[5];
  3107. ha->serial1 = icb->port_name[6];
  3108. ha->serial2 = icb->port_name[7];
  3109. ha->node_name = icb->node_name;
  3110. ha->port_name = icb->port_name;
  3111. icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
  3112. ha->retry_count = le16_to_cpu(nv->login_retry_count);
  3113. /* Set minimum login_timeout to 4 seconds. */
  3114. if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
  3115. nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
  3116. if (le16_to_cpu(nv->login_timeout) < 4)
  3117. nv->login_timeout = __constant_cpu_to_le16(4);
  3118. ha->login_timeout = le16_to_cpu(nv->login_timeout);
  3119. icb->login_timeout = cpu_to_le16(nv->login_timeout);
  3120. /* Set minimum RATOV to 100 tenths of a second. */
  3121. ha->r_a_tov = 100;
  3122. ha->loop_reset_delay = nv->reset_delay;
  3123. /* Link Down Timeout = 0:
  3124. *
  3125. * When Port Down timer expires we will start returning
  3126. * I/O's to OS with "DID_NO_CONNECT".
  3127. *
  3128. * Link Down Timeout != 0:
  3129. *
  3130. * The driver waits for the link to come up after link down
  3131. * before returning I/Os to OS with "DID_NO_CONNECT".
  3132. */
  3133. if (le16_to_cpu(nv->link_down_timeout) == 0) {
  3134. ha->loop_down_abort_time =
  3135. (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
  3136. } else {
  3137. ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
  3138. ha->loop_down_abort_time =
  3139. (LOOP_DOWN_TIME - ha->link_down_timeout);
  3140. }
  3141. /* Need enough time to try and get the port back. */
  3142. ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
  3143. if (qlport_down_retry)
  3144. ha->port_down_retry_count = qlport_down_retry;
  3145. /* Set login_retry_count */
  3146. ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
  3147. if (ha->port_down_retry_count ==
  3148. le16_to_cpu(nv->port_down_retry_count) &&
  3149. ha->port_down_retry_count > 3)
  3150. ha->login_retry_count = ha->port_down_retry_count;
  3151. else if (ha->port_down_retry_count > (int)ha->login_retry_count)
  3152. ha->login_retry_count = ha->port_down_retry_count;
  3153. if (ql2xloginretrycount)
  3154. ha->login_retry_count = ql2xloginretrycount;
  3155. /* Enable ZIO. */
  3156. if (!ha->flags.init_done) {
  3157. ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
  3158. (BIT_3 | BIT_2 | BIT_1 | BIT_0);
  3159. ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
  3160. le16_to_cpu(icb->interrupt_delay_timer): 2;
  3161. }
  3162. icb->firmware_options_2 &= __constant_cpu_to_le32(
  3163. ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
  3164. ha->flags.process_response_queue = 0;
  3165. if (ha->zio_mode != QLA_ZIO_DISABLED) {
  3166. ha->zio_mode = QLA_ZIO_MODE_6;
  3167. DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
  3168. "(%d us).\n", ha->host_no, ha->zio_mode,
  3169. ha->zio_timer * 100));
  3170. qla_printk(KERN_INFO, ha,
  3171. "ZIO mode %d enabled; timer delay (%d us).\n",
  3172. ha->zio_mode, ha->zio_timer * 100);
  3173. icb->firmware_options_2 |= cpu_to_le32(
  3174. (uint32_t)ha->zio_mode);
  3175. icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
  3176. ha->flags.process_response_queue = 1;
  3177. }
  3178. if (rval) {
  3179. DEBUG2_3(printk(KERN_WARNING
  3180. "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
  3181. }
  3182. return (rval);
  3183. }
  3184. static int
  3185. qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
  3186. {
  3187. int rval;
  3188. int segments, fragment;
  3189. uint32_t faddr;
  3190. uint32_t *dcode, dlen;
  3191. uint32_t risc_addr;
  3192. uint32_t risc_size;
  3193. uint32_t i;
  3194. rval = QLA_SUCCESS;
  3195. segments = FA_RISC_CODE_SEGMENTS;
  3196. faddr = FA_RISC_CODE_ADDR;
  3197. dcode = (uint32_t *)ha->request_ring;
  3198. *srisc_addr = 0;
  3199. /* Validate firmware image by checking version. */
  3200. qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
  3201. for (i = 0; i < 4; i++)
  3202. dcode[i] = be32_to_cpu(dcode[i]);
  3203. if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
  3204. dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
  3205. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  3206. dcode[3] == 0)) {
  3207. qla_printk(KERN_WARNING, ha,
  3208. "Unable to verify integrity of flash firmware image!\n");
  3209. qla_printk(KERN_WARNING, ha,
  3210. "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
  3211. dcode[1], dcode[2], dcode[3]);
  3212. return QLA_FUNCTION_FAILED;
  3213. }
  3214. while (segments && rval == QLA_SUCCESS) {
  3215. /* Read segment's load information. */
  3216. qla24xx_read_flash_data(ha, dcode, faddr, 4);
  3217. risc_addr = be32_to_cpu(dcode[2]);
  3218. *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
  3219. risc_size = be32_to_cpu(dcode[3]);
  3220. fragment = 0;
  3221. while (risc_size > 0 && rval == QLA_SUCCESS) {
  3222. dlen = (uint32_t)(ha->fw_transfer_size >> 2);
  3223. if (dlen > risc_size)
  3224. dlen = risc_size;
  3225. DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
  3226. "addr %x, number of dwords 0x%x, offset 0x%x.\n",
  3227. ha->host_no, risc_addr, dlen, faddr));
  3228. qla24xx_read_flash_data(ha, dcode, faddr, dlen);
  3229. for (i = 0; i < dlen; i++)
  3230. dcode[i] = swab32(dcode[i]);
  3231. rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
  3232. dlen);
  3233. if (rval) {
  3234. DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
  3235. "segment %d of firmware\n", ha->host_no,
  3236. fragment));
  3237. qla_printk(KERN_WARNING, ha,
  3238. "[ERROR] Failed to load segment %d of "
  3239. "firmware\n", fragment);
  3240. break;
  3241. }
  3242. faddr += dlen;
  3243. risc_addr += dlen;
  3244. risc_size -= dlen;
  3245. fragment++;
  3246. }
  3247. /* Next segment. */
  3248. segments--;
  3249. }
  3250. return rval;
  3251. }
  3252. #define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
  3253. int
  3254. qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
  3255. {
  3256. int rval;
  3257. int i, fragment;
  3258. uint16_t *wcode, *fwcode;
  3259. uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
  3260. struct fw_blob *blob;
  3261. /* Load firmware blob. */
  3262. blob = qla2x00_request_firmware(ha);
  3263. if (!blob) {
  3264. qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
  3265. qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
  3266. "from: " QLA_FW_URL ".\n");
  3267. return QLA_FUNCTION_FAILED;
  3268. }
  3269. rval = QLA_SUCCESS;
  3270. wcode = (uint16_t *)ha->request_ring;
  3271. *srisc_addr = 0;
  3272. fwcode = (uint16_t *)blob->fw->data;
  3273. fwclen = 0;
  3274. /* Validate firmware image by checking version. */
  3275. if (blob->fw->size < 8 * sizeof(uint16_t)) {
  3276. qla_printk(KERN_WARNING, ha,
  3277. "Unable to verify integrity of firmware image (%Zd)!\n",
  3278. blob->fw->size);
  3279. goto fail_fw_integrity;
  3280. }
  3281. for (i = 0; i < 4; i++)
  3282. wcode[i] = be16_to_cpu(fwcode[i + 4]);
  3283. if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
  3284. wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
  3285. wcode[2] == 0 && wcode[3] == 0)) {
  3286. qla_printk(KERN_WARNING, ha,
  3287. "Unable to verify integrity of firmware image!\n");
  3288. qla_printk(KERN_WARNING, ha,
  3289. "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
  3290. wcode[1], wcode[2], wcode[3]);
  3291. goto fail_fw_integrity;
  3292. }
  3293. seg = blob->segs;
  3294. while (*seg && rval == QLA_SUCCESS) {
  3295. risc_addr = *seg;
  3296. *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
  3297. risc_size = be16_to_cpu(fwcode[3]);
  3298. /* Validate firmware image size. */
  3299. fwclen += risc_size * sizeof(uint16_t);
  3300. if (blob->fw->size < fwclen) {
  3301. qla_printk(KERN_WARNING, ha,
  3302. "Unable to verify integrity of firmware image "
  3303. "(%Zd)!\n", blob->fw->size);
  3304. goto fail_fw_integrity;
  3305. }
  3306. fragment = 0;
  3307. while (risc_size > 0 && rval == QLA_SUCCESS) {
  3308. wlen = (uint16_t)(ha->fw_transfer_size >> 1);
  3309. if (wlen > risc_size)
  3310. wlen = risc_size;
  3311. DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
  3312. "addr %x, number of words 0x%x.\n", ha->host_no,
  3313. risc_addr, wlen));
  3314. for (i = 0; i < wlen; i++)
  3315. wcode[i] = swab16(fwcode[i]);
  3316. rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
  3317. wlen);
  3318. if (rval) {
  3319. DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
  3320. "segment %d of firmware\n", ha->host_no,
  3321. fragment));
  3322. qla_printk(KERN_WARNING, ha,
  3323. "[ERROR] Failed to load segment %d of "
  3324. "firmware\n", fragment);
  3325. break;
  3326. }
  3327. fwcode += wlen;
  3328. risc_addr += wlen;
  3329. risc_size -= wlen;
  3330. fragment++;
  3331. }
  3332. /* Next segment. */
  3333. seg++;
  3334. }
  3335. return rval;
  3336. fail_fw_integrity:
  3337. return QLA_FUNCTION_FAILED;
  3338. }
  3339. int
  3340. qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
  3341. {
  3342. int rval;
  3343. int segments, fragment;
  3344. uint32_t *dcode, dlen;
  3345. uint32_t risc_addr;
  3346. uint32_t risc_size;
  3347. uint32_t i;
  3348. struct fw_blob *blob;
  3349. uint32_t *fwcode, fwclen;
  3350. /* Load firmware blob. */
  3351. blob = qla2x00_request_firmware(ha);
  3352. if (!blob) {
  3353. qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
  3354. qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
  3355. "from: " QLA_FW_URL ".\n");
  3356. /* Try to load RISC code from flash. */
  3357. qla_printk(KERN_ERR, ha, "Attempting to load (potentially "
  3358. "outdated) firmware from flash.\n");
  3359. return qla24xx_load_risc_flash(ha, srisc_addr);
  3360. }
  3361. rval = QLA_SUCCESS;
  3362. segments = FA_RISC_CODE_SEGMENTS;
  3363. dcode = (uint32_t *)ha->request_ring;
  3364. *srisc_addr = 0;
  3365. fwcode = (uint32_t *)blob->fw->data;
  3366. fwclen = 0;
  3367. /* Validate firmware image by checking version. */
  3368. if (blob->fw->size < 8 * sizeof(uint32_t)) {
  3369. qla_printk(KERN_WARNING, ha,
  3370. "Unable to verify integrity of firmware image (%Zd)!\n",
  3371. blob->fw->size);
  3372. goto fail_fw_integrity;
  3373. }
  3374. for (i = 0; i < 4; i++)
  3375. dcode[i] = be32_to_cpu(fwcode[i + 4]);
  3376. if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
  3377. dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
  3378. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  3379. dcode[3] == 0)) {
  3380. qla_printk(KERN_WARNING, ha,
  3381. "Unable to verify integrity of firmware image!\n");
  3382. qla_printk(KERN_WARNING, ha,
  3383. "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
  3384. dcode[1], dcode[2], dcode[3]);
  3385. goto fail_fw_integrity;
  3386. }
  3387. while (segments && rval == QLA_SUCCESS) {
  3388. risc_addr = be32_to_cpu(fwcode[2]);
  3389. *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
  3390. risc_size = be32_to_cpu(fwcode[3]);
  3391. /* Validate firmware image size. */
  3392. fwclen += risc_size * sizeof(uint32_t);
  3393. if (blob->fw->size < fwclen) {
  3394. qla_printk(KERN_WARNING, ha,
  3395. "Unable to verify integrity of firmware image "
  3396. "(%Zd)!\n", blob->fw->size);
  3397. goto fail_fw_integrity;
  3398. }
  3399. fragment = 0;
  3400. while (risc_size > 0 && rval == QLA_SUCCESS) {
  3401. dlen = (uint32_t)(ha->fw_transfer_size >> 2);
  3402. if (dlen > risc_size)
  3403. dlen = risc_size;
  3404. DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
  3405. "addr %x, number of dwords 0x%x.\n", ha->host_no,
  3406. risc_addr, dlen));
  3407. for (i = 0; i < dlen; i++)
  3408. dcode[i] = swab32(fwcode[i]);
  3409. rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
  3410. dlen);
  3411. if (rval) {
  3412. DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
  3413. "segment %d of firmware\n", ha->host_no,
  3414. fragment));
  3415. qla_printk(KERN_WARNING, ha,
  3416. "[ERROR] Failed to load segment %d of "
  3417. "firmware\n", fragment);
  3418. break;
  3419. }
  3420. fwcode += dlen;
  3421. risc_addr += dlen;
  3422. risc_size -= dlen;
  3423. fragment++;
  3424. }
  3425. /* Next segment. */
  3426. segments--;
  3427. }
  3428. return rval;
  3429. fail_fw_integrity:
  3430. return QLA_FUNCTION_FAILED;
  3431. }
  3432. void
  3433. qla2x00_try_to_stop_firmware(scsi_qla_host_t *ha)
  3434. {
  3435. int ret, retries;
  3436. if (!IS_FWI2_CAPABLE(ha))
  3437. return;
  3438. if (!ha->fw_major_version)
  3439. return;
  3440. ret = qla2x00_stop_firmware(ha);
  3441. for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
  3442. retries ; retries--) {
  3443. qla2x00_reset_chip(ha);
  3444. if (qla2x00_chip_diag(ha) != QLA_SUCCESS)
  3445. continue;
  3446. if (qla2x00_setup_chip(ha) != QLA_SUCCESS)
  3447. continue;
  3448. qla_printk(KERN_INFO, ha,
  3449. "Attempting retry of stop-firmware command...\n");
  3450. ret = qla2x00_stop_firmware(ha);
  3451. }
  3452. }
  3453. int
  3454. qla24xx_configure_vhba(scsi_qla_host_t *ha)
  3455. {
  3456. int rval = QLA_SUCCESS;
  3457. uint16_t mb[MAILBOX_REGISTER_COUNT];
  3458. if (!ha->parent)
  3459. return -EINVAL;
  3460. rval = qla2x00_fw_ready(ha);
  3461. if (rval == QLA_SUCCESS) {
  3462. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  3463. qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
  3464. }
  3465. ha->flags.management_server_logged_in = 0;
  3466. /* Login to SNS first */
  3467. qla24xx_login_fabric(ha, NPH_SNS, 0xff, 0xff, 0xfc,
  3468. mb, BIT_1);
  3469. if (mb[0] != MBS_COMMAND_COMPLETE) {
  3470. DEBUG15(qla_printk(KERN_INFO, ha,
  3471. "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
  3472. "mb[2]=%x mb[6]=%x mb[7]=%x\n", NPH_SNS,
  3473. mb[0], mb[1], mb[2], mb[6], mb[7]));
  3474. return (QLA_FUNCTION_FAILED);
  3475. }
  3476. atomic_set(&ha->loop_down_timer, 0);
  3477. atomic_set(&ha->loop_state, LOOP_UP);
  3478. set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  3479. set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
  3480. rval = qla2x00_loop_resync(ha);
  3481. return rval;
  3482. }