qla_init.c 100 KB

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