qla_init.c 97 KB

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