qla_init.c 100 KB

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