qla_init.c 104 KB

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