qla_init.c 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921
  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. /* Use alternate WWN? */
  1297. if (nv->host_p[1] & BIT_7) {
  1298. memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
  1299. memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
  1300. }
  1301. /* Prepare nodename */
  1302. if ((icb->firmware_options[1] & BIT_6) == 0) {
  1303. /*
  1304. * Firmware will apply the following mask if the nodename was
  1305. * not provided.
  1306. */
  1307. memcpy(icb->node_name, icb->port_name, WWN_SIZE);
  1308. icb->node_name[0] &= 0xF0;
  1309. }
  1310. /*
  1311. * Set host adapter parameters.
  1312. */
  1313. ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
  1314. /* Always load RISC code on non ISP2[12]00 chips. */
  1315. if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
  1316. ha->flags.disable_risc_code_load = 0;
  1317. ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
  1318. ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
  1319. ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
  1320. ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
  1321. ha->operating_mode =
  1322. (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
  1323. memcpy(ha->fw_seriallink_options, nv->seriallink_options,
  1324. sizeof(ha->fw_seriallink_options));
  1325. /* save HBA serial number */
  1326. ha->serial0 = icb->port_name[5];
  1327. ha->serial1 = icb->port_name[6];
  1328. ha->serial2 = icb->port_name[7];
  1329. ha->node_name = icb->node_name;
  1330. ha->port_name = icb->port_name;
  1331. icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
  1332. ha->retry_count = nv->retry_count;
  1333. /* Set minimum login_timeout to 4 seconds. */
  1334. if (nv->login_timeout < ql2xlogintimeout)
  1335. nv->login_timeout = ql2xlogintimeout;
  1336. if (nv->login_timeout < 4)
  1337. nv->login_timeout = 4;
  1338. ha->login_timeout = nv->login_timeout;
  1339. icb->login_timeout = nv->login_timeout;
  1340. /* Set minimum RATOV to 200 tenths of a second. */
  1341. ha->r_a_tov = 200;
  1342. ha->loop_reset_delay = nv->reset_delay;
  1343. /* Link Down Timeout = 0:
  1344. *
  1345. * When Port Down timer expires we will start returning
  1346. * I/O's to OS with "DID_NO_CONNECT".
  1347. *
  1348. * Link Down Timeout != 0:
  1349. *
  1350. * The driver waits for the link to come up after link down
  1351. * before returning I/Os to OS with "DID_NO_CONNECT".
  1352. */
  1353. if (nv->link_down_timeout == 0) {
  1354. ha->loop_down_abort_time =
  1355. (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
  1356. } else {
  1357. ha->link_down_timeout = nv->link_down_timeout;
  1358. ha->loop_down_abort_time =
  1359. (LOOP_DOWN_TIME - ha->link_down_timeout);
  1360. }
  1361. /*
  1362. * Need enough time to try and get the port back.
  1363. */
  1364. ha->port_down_retry_count = nv->port_down_retry_count;
  1365. if (qlport_down_retry)
  1366. ha->port_down_retry_count = qlport_down_retry;
  1367. /* Set login_retry_count */
  1368. ha->login_retry_count = nv->retry_count;
  1369. if (ha->port_down_retry_count == nv->port_down_retry_count &&
  1370. ha->port_down_retry_count > 3)
  1371. ha->login_retry_count = ha->port_down_retry_count;
  1372. else if (ha->port_down_retry_count > (int)ha->login_retry_count)
  1373. ha->login_retry_count = ha->port_down_retry_count;
  1374. if (ql2xloginretrycount)
  1375. ha->login_retry_count = ql2xloginretrycount;
  1376. icb->lun_enables = __constant_cpu_to_le16(0);
  1377. icb->command_resource_count = 0;
  1378. icb->immediate_notify_resource_count = 0;
  1379. icb->timeout = __constant_cpu_to_le16(0);
  1380. if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
  1381. /* Enable RIO */
  1382. icb->firmware_options[0] &= ~BIT_3;
  1383. icb->add_firmware_options[0] &=
  1384. ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
  1385. icb->add_firmware_options[0] |= BIT_2;
  1386. icb->response_accumulation_timer = 3;
  1387. icb->interrupt_delay_timer = 5;
  1388. ha->flags.process_response_queue = 1;
  1389. } else {
  1390. /* Enable ZIO. */
  1391. if (!ha->flags.init_done) {
  1392. ha->zio_mode = icb->add_firmware_options[0] &
  1393. (BIT_3 | BIT_2 | BIT_1 | BIT_0);
  1394. ha->zio_timer = icb->interrupt_delay_timer ?
  1395. icb->interrupt_delay_timer: 2;
  1396. }
  1397. icb->add_firmware_options[0] &=
  1398. ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
  1399. ha->flags.process_response_queue = 0;
  1400. if (ha->zio_mode != QLA_ZIO_DISABLED) {
  1401. ha->zio_mode = QLA_ZIO_MODE_6;
  1402. DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
  1403. "delay (%d us).\n", ha->host_no, ha->zio_mode,
  1404. ha->zio_timer * 100));
  1405. qla_printk(KERN_INFO, ha,
  1406. "ZIO mode %d enabled; timer delay (%d us).\n",
  1407. ha->zio_mode, ha->zio_timer * 100);
  1408. icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
  1409. icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
  1410. ha->flags.process_response_queue = 1;
  1411. }
  1412. }
  1413. if (rval) {
  1414. DEBUG2_3(printk(KERN_WARNING
  1415. "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
  1416. }
  1417. return (rval);
  1418. }
  1419. static void
  1420. qla2x00_rport_add(void *data)
  1421. {
  1422. fc_port_t *fcport = data;
  1423. qla2x00_reg_remote_port(fcport->ha, fcport);
  1424. }
  1425. static void
  1426. qla2x00_rport_del(void *data)
  1427. {
  1428. fc_port_t *fcport = data;
  1429. struct fc_rport *rport;
  1430. unsigned long flags;
  1431. spin_lock_irqsave(&fcport->rport_lock, flags);
  1432. rport = fcport->drport;
  1433. fcport->drport = NULL;
  1434. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1435. if (rport)
  1436. fc_remote_port_delete(rport);
  1437. }
  1438. /**
  1439. * qla2x00_alloc_fcport() - Allocate a generic fcport.
  1440. * @ha: HA context
  1441. * @flags: allocation flags
  1442. *
  1443. * Returns a pointer to the allocated fcport, or NULL, if none available.
  1444. */
  1445. fc_port_t *
  1446. qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
  1447. {
  1448. fc_port_t *fcport;
  1449. fcport = kmalloc(sizeof(fc_port_t), flags);
  1450. if (fcport == NULL)
  1451. return (fcport);
  1452. /* Setup fcport template structure. */
  1453. memset(fcport, 0, sizeof (fc_port_t));
  1454. fcport->ha = ha;
  1455. fcport->port_type = FCT_UNKNOWN;
  1456. fcport->loop_id = FC_NO_LOOP_ID;
  1457. fcport->iodesc_idx_sent = IODESC_INVALID_INDEX;
  1458. atomic_set(&fcport->state, FCS_UNCONFIGURED);
  1459. fcport->flags = FCF_RLC_SUPPORT;
  1460. fcport->supported_classes = FC_COS_UNSPECIFIED;
  1461. spin_lock_init(&fcport->rport_lock);
  1462. INIT_WORK(&fcport->rport_add_work, qla2x00_rport_add, fcport);
  1463. INIT_WORK(&fcport->rport_del_work, qla2x00_rport_del, fcport);
  1464. return (fcport);
  1465. }
  1466. /*
  1467. * qla2x00_configure_loop
  1468. * Updates Fibre Channel Device Database with what is actually on loop.
  1469. *
  1470. * Input:
  1471. * ha = adapter block pointer.
  1472. *
  1473. * Returns:
  1474. * 0 = success.
  1475. * 1 = error.
  1476. * 2 = database was full and device was not configured.
  1477. */
  1478. static int
  1479. qla2x00_configure_loop(scsi_qla_host_t *ha)
  1480. {
  1481. int rval;
  1482. unsigned long flags, save_flags;
  1483. rval = QLA_SUCCESS;
  1484. /* Get Initiator ID */
  1485. if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
  1486. rval = qla2x00_configure_hba(ha);
  1487. if (rval != QLA_SUCCESS) {
  1488. DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
  1489. ha->host_no));
  1490. return (rval);
  1491. }
  1492. }
  1493. save_flags = flags = ha->dpc_flags;
  1494. DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
  1495. ha->host_no, flags));
  1496. /*
  1497. * If we have both an RSCN and PORT UPDATE pending then handle them
  1498. * both at the same time.
  1499. */
  1500. clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
  1501. clear_bit(RSCN_UPDATE, &ha->dpc_flags);
  1502. /* Determine what we need to do */
  1503. if (ha->current_topology == ISP_CFG_FL &&
  1504. (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
  1505. ha->flags.rscn_queue_overflow = 1;
  1506. set_bit(RSCN_UPDATE, &flags);
  1507. } else if (ha->current_topology == ISP_CFG_F &&
  1508. (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
  1509. ha->flags.rscn_queue_overflow = 1;
  1510. set_bit(RSCN_UPDATE, &flags);
  1511. clear_bit(LOCAL_LOOP_UPDATE, &flags);
  1512. } else if (!ha->flags.online ||
  1513. (test_bit(ABORT_ISP_ACTIVE, &flags))) {
  1514. ha->flags.rscn_queue_overflow = 1;
  1515. set_bit(RSCN_UPDATE, &flags);
  1516. set_bit(LOCAL_LOOP_UPDATE, &flags);
  1517. }
  1518. if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
  1519. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1520. rval = QLA_FUNCTION_FAILED;
  1521. } else {
  1522. rval = qla2x00_configure_local_loop(ha);
  1523. }
  1524. }
  1525. if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
  1526. if (LOOP_TRANSITION(ha)) {
  1527. rval = QLA_FUNCTION_FAILED;
  1528. } else {
  1529. rval = qla2x00_configure_fabric(ha);
  1530. }
  1531. }
  1532. if (rval == QLA_SUCCESS) {
  1533. if (atomic_read(&ha->loop_down_timer) ||
  1534. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1535. rval = QLA_FUNCTION_FAILED;
  1536. } else {
  1537. atomic_set(&ha->loop_state, LOOP_READY);
  1538. DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
  1539. }
  1540. }
  1541. if (rval) {
  1542. DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
  1543. __func__, ha->host_no));
  1544. } else {
  1545. DEBUG3(printk("%s: exiting normally\n", __func__));
  1546. }
  1547. /* Restore state if a resync event occured during processing */
  1548. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1549. if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
  1550. set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
  1551. if (test_bit(RSCN_UPDATE, &save_flags))
  1552. set_bit(RSCN_UPDATE, &ha->dpc_flags);
  1553. }
  1554. return (rval);
  1555. }
  1556. /*
  1557. * qla2x00_configure_local_loop
  1558. * Updates Fibre Channel Device Database with local loop devices.
  1559. *
  1560. * Input:
  1561. * ha = adapter block pointer.
  1562. *
  1563. * Returns:
  1564. * 0 = success.
  1565. */
  1566. static int
  1567. qla2x00_configure_local_loop(scsi_qla_host_t *ha)
  1568. {
  1569. int rval, rval2;
  1570. int found_devs;
  1571. int found;
  1572. fc_port_t *fcport, *new_fcport;
  1573. uint16_t index;
  1574. uint16_t entries;
  1575. char *id_iter;
  1576. uint16_t loop_id;
  1577. uint8_t domain, area, al_pa;
  1578. found_devs = 0;
  1579. new_fcport = NULL;
  1580. entries = MAX_FIBRE_DEVICES;
  1581. DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
  1582. DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
  1583. /* Get list of logged in devices. */
  1584. memset(ha->gid_list, 0, GID_LIST_SIZE);
  1585. rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
  1586. &entries);
  1587. if (rval != QLA_SUCCESS)
  1588. goto cleanup_allocation;
  1589. DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
  1590. ha->host_no, entries));
  1591. DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
  1592. entries * sizeof(struct gid_list_info)));
  1593. /* Allocate temporary fcport for any new fcports discovered. */
  1594. new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
  1595. if (new_fcport == NULL) {
  1596. rval = QLA_MEMORY_ALLOC_FAILED;
  1597. goto cleanup_allocation;
  1598. }
  1599. new_fcport->flags &= ~FCF_FABRIC_DEVICE;
  1600. /*
  1601. * Mark local devices that were present with FCF_DEVICE_LOST for now.
  1602. */
  1603. list_for_each_entry(fcport, &ha->fcports, list) {
  1604. if (atomic_read(&fcport->state) == FCS_ONLINE &&
  1605. fcport->port_type != FCT_BROADCAST &&
  1606. (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
  1607. DEBUG(printk("scsi(%ld): Marking port lost, "
  1608. "loop_id=0x%04x\n",
  1609. ha->host_no, fcport->loop_id));
  1610. atomic_set(&fcport->state, FCS_DEVICE_LOST);
  1611. fcport->flags &= ~FCF_FARP_DONE;
  1612. }
  1613. }
  1614. /* Add devices to port list. */
  1615. id_iter = (char *)ha->gid_list;
  1616. for (index = 0; index < entries; index++) {
  1617. domain = ((struct gid_list_info *)id_iter)->domain;
  1618. area = ((struct gid_list_info *)id_iter)->area;
  1619. al_pa = ((struct gid_list_info *)id_iter)->al_pa;
  1620. if (IS_QLA2100(ha) || IS_QLA2200(ha))
  1621. loop_id = (uint16_t)
  1622. ((struct gid_list_info *)id_iter)->loop_id_2100;
  1623. else
  1624. loop_id = le16_to_cpu(
  1625. ((struct gid_list_info *)id_iter)->loop_id);
  1626. id_iter += ha->gid_list_info_size;
  1627. /* Bypass reserved domain fields. */
  1628. if ((domain & 0xf0) == 0xf0)
  1629. continue;
  1630. /* Bypass if not same domain and area of adapter. */
  1631. if (area && domain &&
  1632. (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
  1633. continue;
  1634. /* Bypass invalid local loop ID. */
  1635. if (loop_id > LAST_LOCAL_LOOP_ID)
  1636. continue;
  1637. /* Fill in member data. */
  1638. new_fcport->d_id.b.domain = domain;
  1639. new_fcport->d_id.b.area = area;
  1640. new_fcport->d_id.b.al_pa = al_pa;
  1641. new_fcport->loop_id = loop_id;
  1642. rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
  1643. if (rval2 != QLA_SUCCESS) {
  1644. DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
  1645. "information -- get_port_database=%x, "
  1646. "loop_id=0x%04x\n",
  1647. ha->host_no, rval2, new_fcport->loop_id));
  1648. DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
  1649. ha->host_no));
  1650. set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  1651. continue;
  1652. }
  1653. /* Check for matching device in port list. */
  1654. found = 0;
  1655. fcport = NULL;
  1656. list_for_each_entry(fcport, &ha->fcports, list) {
  1657. if (memcmp(new_fcport->port_name, fcport->port_name,
  1658. WWN_SIZE))
  1659. continue;
  1660. fcport->flags &= ~(FCF_FABRIC_DEVICE |
  1661. FCF_PERSISTENT_BOUND);
  1662. fcport->loop_id = new_fcport->loop_id;
  1663. fcport->port_type = new_fcport->port_type;
  1664. fcport->d_id.b24 = new_fcport->d_id.b24;
  1665. memcpy(fcport->node_name, new_fcport->node_name,
  1666. WWN_SIZE);
  1667. found++;
  1668. break;
  1669. }
  1670. if (!found) {
  1671. /* New device, add to fcports list. */
  1672. new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
  1673. list_add_tail(&new_fcport->list, &ha->fcports);
  1674. /* Allocate a new replacement fcport. */
  1675. fcport = new_fcport;
  1676. new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
  1677. if (new_fcport == NULL) {
  1678. rval = QLA_MEMORY_ALLOC_FAILED;
  1679. goto cleanup_allocation;
  1680. }
  1681. new_fcport->flags &= ~FCF_FABRIC_DEVICE;
  1682. }
  1683. qla2x00_update_fcport(ha, fcport);
  1684. found_devs++;
  1685. }
  1686. cleanup_allocation:
  1687. kfree(new_fcport);
  1688. if (rval != QLA_SUCCESS) {
  1689. DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
  1690. "rval=%x\n", ha->host_no, rval));
  1691. }
  1692. if (found_devs) {
  1693. ha->device_flags |= DFLG_LOCAL_DEVICES;
  1694. ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
  1695. }
  1696. return (rval);
  1697. }
  1698. static void
  1699. qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
  1700. {
  1701. fc_port_t *fcport;
  1702. qla2x00_mark_all_devices_lost(ha, 0);
  1703. list_for_each_entry(fcport, &ha->fcports, list) {
  1704. if (fcport->port_type != FCT_TARGET)
  1705. continue;
  1706. qla2x00_update_fcport(ha, fcport);
  1707. }
  1708. }
  1709. /*
  1710. * qla2x00_update_fcport
  1711. * Updates device on list.
  1712. *
  1713. * Input:
  1714. * ha = adapter block pointer.
  1715. * fcport = port structure pointer.
  1716. *
  1717. * Return:
  1718. * 0 - Success
  1719. * BIT_0 - error
  1720. *
  1721. * Context:
  1722. * Kernel context.
  1723. */
  1724. void
  1725. qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
  1726. {
  1727. fcport->ha = ha;
  1728. fcport->login_retry = 0;
  1729. fcport->port_login_retry_count = ha->port_down_retry_count *
  1730. PORT_RETRY_TIME;
  1731. atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
  1732. PORT_RETRY_TIME);
  1733. fcport->flags &= ~FCF_LOGIN_NEEDED;
  1734. if (fcport->port_type == FCT_INITIATOR ||
  1735. fcport->port_type == FCT_BROADCAST)
  1736. fcport->device_type = TYPE_PROCESSOR;
  1737. atomic_set(&fcport->state, FCS_ONLINE);
  1738. if (ha->flags.init_done)
  1739. qla2x00_reg_remote_port(ha, fcport);
  1740. }
  1741. void
  1742. qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
  1743. {
  1744. struct fc_rport_identifiers rport_ids;
  1745. struct fc_rport *rport;
  1746. unsigned long flags;
  1747. if (fcport->drport)
  1748. qla2x00_rport_del(fcport);
  1749. if (fcport->rport)
  1750. return;
  1751. rport_ids.node_name = wwn_to_u64(fcport->node_name);
  1752. rport_ids.port_name = wwn_to_u64(fcport->port_name);
  1753. rport_ids.port_id = fcport->d_id.b.domain << 16 |
  1754. fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
  1755. rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
  1756. rport = fc_remote_port_add(ha->host, 0, &rport_ids);
  1757. if (!rport) {
  1758. qla_printk(KERN_WARNING, ha,
  1759. "Unable to allocate fc remote port!\n");
  1760. return;
  1761. }
  1762. spin_lock_irqsave(&fcport->rport_lock, flags);
  1763. fcport->rport = rport;
  1764. *((fc_port_t **)rport->dd_data) = fcport;
  1765. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1766. rport->supported_classes = fcport->supported_classes;
  1767. rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
  1768. if (fcport->port_type == FCT_INITIATOR)
  1769. rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  1770. if (fcport->port_type == FCT_TARGET)
  1771. rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
  1772. fc_remote_port_rolechg(rport, rport_ids.roles);
  1773. if (rport->scsi_target_id != -1 &&
  1774. rport->scsi_target_id < ha->host->max_id)
  1775. fcport->os_target_id = rport->scsi_target_id;
  1776. }
  1777. /*
  1778. * qla2x00_configure_fabric
  1779. * Setup SNS devices with loop ID's.
  1780. *
  1781. * Input:
  1782. * ha = adapter block pointer.
  1783. *
  1784. * Returns:
  1785. * 0 = success.
  1786. * BIT_0 = error
  1787. */
  1788. static int
  1789. qla2x00_configure_fabric(scsi_qla_host_t *ha)
  1790. {
  1791. int rval, rval2;
  1792. fc_port_t *fcport, *fcptemp;
  1793. uint16_t next_loopid;
  1794. uint16_t mb[MAILBOX_REGISTER_COUNT];
  1795. uint16_t loop_id;
  1796. LIST_HEAD(new_fcports);
  1797. /* If FL port exists, then SNS is present */
  1798. if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
  1799. loop_id = NPH_F_PORT;
  1800. else
  1801. loop_id = SNS_FL_PORT;
  1802. rval = qla2x00_get_port_name(ha, loop_id, NULL, 0);
  1803. if (rval != QLA_SUCCESS) {
  1804. DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
  1805. "Port\n", ha->host_no));
  1806. ha->device_flags &= ~SWITCH_FOUND;
  1807. return (QLA_SUCCESS);
  1808. }
  1809. /* Mark devices that need re-synchronization. */
  1810. rval2 = qla2x00_device_resync(ha);
  1811. if (rval2 == QLA_RSCNS_HANDLED) {
  1812. /* No point doing the scan, just continue. */
  1813. return (QLA_SUCCESS);
  1814. }
  1815. do {
  1816. /* FDMI support. */
  1817. if (ql2xfdmienable &&
  1818. test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
  1819. qla2x00_fdmi_register(ha);
  1820. /* Ensure we are logged into the SNS. */
  1821. if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
  1822. loop_id = NPH_SNS;
  1823. else
  1824. loop_id = SIMPLE_NAME_SERVER;
  1825. ha->isp_ops.fabric_login(ha, loop_id, 0xff, 0xff,
  1826. 0xfc, mb, BIT_1 | BIT_0);
  1827. if (mb[0] != MBS_COMMAND_COMPLETE) {
  1828. DEBUG2(qla_printk(KERN_INFO, ha,
  1829. "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
  1830. "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
  1831. mb[0], mb[1], mb[2], mb[6], mb[7]));
  1832. return (QLA_SUCCESS);
  1833. }
  1834. if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
  1835. if (qla2x00_rft_id(ha)) {
  1836. /* EMPTY */
  1837. DEBUG2(printk("scsi(%ld): Register FC-4 "
  1838. "TYPE failed.\n", ha->host_no));
  1839. }
  1840. if (qla2x00_rff_id(ha)) {
  1841. /* EMPTY */
  1842. DEBUG2(printk("scsi(%ld): Register FC-4 "
  1843. "Features failed.\n", ha->host_no));
  1844. }
  1845. if (qla2x00_rnn_id(ha)) {
  1846. /* EMPTY */
  1847. DEBUG2(printk("scsi(%ld): Register Node Name "
  1848. "failed.\n", ha->host_no));
  1849. } else if (qla2x00_rsnn_nn(ha)) {
  1850. /* EMPTY */
  1851. DEBUG2(printk("scsi(%ld): Register Symbolic "
  1852. "Node Name failed.\n", ha->host_no));
  1853. }
  1854. }
  1855. rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
  1856. if (rval != QLA_SUCCESS)
  1857. break;
  1858. /*
  1859. * Logout all previous fabric devices marked lost, except
  1860. * tape devices.
  1861. */
  1862. list_for_each_entry(fcport, &ha->fcports, list) {
  1863. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  1864. break;
  1865. if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
  1866. continue;
  1867. if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
  1868. qla2x00_mark_device_lost(ha, fcport,
  1869. ql2xplogiabsentdevice, 0);
  1870. if (fcport->loop_id != FC_NO_LOOP_ID &&
  1871. (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
  1872. fcport->port_type != FCT_INITIATOR &&
  1873. fcport->port_type != FCT_BROADCAST) {
  1874. ha->isp_ops.fabric_logout(ha,
  1875. fcport->loop_id,
  1876. fcport->d_id.b.domain,
  1877. fcport->d_id.b.area,
  1878. fcport->d_id.b.al_pa);
  1879. fcport->loop_id = FC_NO_LOOP_ID;
  1880. }
  1881. }
  1882. }
  1883. /* Starting free loop ID. */
  1884. next_loopid = ha->min_external_loopid;
  1885. /*
  1886. * Scan through our port list and login entries that need to be
  1887. * logged in.
  1888. */
  1889. list_for_each_entry(fcport, &ha->fcports, list) {
  1890. if (atomic_read(&ha->loop_down_timer) ||
  1891. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  1892. break;
  1893. if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
  1894. (fcport->flags & FCF_LOGIN_NEEDED) == 0)
  1895. continue;
  1896. if (fcport->loop_id == FC_NO_LOOP_ID) {
  1897. fcport->loop_id = next_loopid;
  1898. rval = qla2x00_find_new_loop_id(ha, fcport);
  1899. if (rval != QLA_SUCCESS) {
  1900. /* Ran out of IDs to use */
  1901. break;
  1902. }
  1903. }
  1904. /* Login and update database */
  1905. qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
  1906. }
  1907. /* Exit if out of loop IDs. */
  1908. if (rval != QLA_SUCCESS) {
  1909. break;
  1910. }
  1911. /*
  1912. * Login and add the new devices to our port list.
  1913. */
  1914. list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
  1915. if (atomic_read(&ha->loop_down_timer) ||
  1916. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  1917. break;
  1918. /* Find a new loop ID to use. */
  1919. fcport->loop_id = next_loopid;
  1920. rval = qla2x00_find_new_loop_id(ha, fcport);
  1921. if (rval != QLA_SUCCESS) {
  1922. /* Ran out of IDs to use */
  1923. break;
  1924. }
  1925. /* Remove device from the new list and add it to DB */
  1926. list_del(&fcport->list);
  1927. list_add_tail(&fcport->list, &ha->fcports);
  1928. /* Login and update database */
  1929. qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
  1930. }
  1931. } while (0);
  1932. /* Free all new device structures not processed. */
  1933. list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
  1934. list_del(&fcport->list);
  1935. kfree(fcport);
  1936. }
  1937. if (rval) {
  1938. DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
  1939. "rval=%d\n", ha->host_no, rval));
  1940. }
  1941. return (rval);
  1942. }
  1943. /*
  1944. * qla2x00_find_all_fabric_devs
  1945. *
  1946. * Input:
  1947. * ha = adapter block pointer.
  1948. * dev = database device entry pointer.
  1949. *
  1950. * Returns:
  1951. * 0 = success.
  1952. *
  1953. * Context:
  1954. * Kernel context.
  1955. */
  1956. static int
  1957. qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
  1958. {
  1959. int rval;
  1960. uint16_t loop_id;
  1961. fc_port_t *fcport, *new_fcport, *fcptemp;
  1962. int found;
  1963. sw_info_t *swl;
  1964. int swl_idx;
  1965. int first_dev, last_dev;
  1966. port_id_t wrap, nxt_d_id;
  1967. rval = QLA_SUCCESS;
  1968. /* Try GID_PT to get device list, else GAN. */
  1969. swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
  1970. if (swl == NULL) {
  1971. /*EMPTY*/
  1972. DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
  1973. "on GA_NXT\n", ha->host_no));
  1974. } else {
  1975. memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
  1976. if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
  1977. kfree(swl);
  1978. swl = NULL;
  1979. } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
  1980. kfree(swl);
  1981. swl = NULL;
  1982. } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
  1983. kfree(swl);
  1984. swl = NULL;
  1985. }
  1986. }
  1987. swl_idx = 0;
  1988. /* Allocate temporary fcport for any new fcports discovered. */
  1989. new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
  1990. if (new_fcport == NULL) {
  1991. kfree(swl);
  1992. return (QLA_MEMORY_ALLOC_FAILED);
  1993. }
  1994. new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
  1995. /* Set start port ID scan at adapter ID. */
  1996. first_dev = 1;
  1997. last_dev = 0;
  1998. /* Starting free loop ID. */
  1999. loop_id = ha->min_external_loopid;
  2000. for (; loop_id <= ha->last_loop_id; loop_id++) {
  2001. if (qla2x00_is_reserved_id(ha, loop_id))
  2002. continue;
  2003. if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
  2004. break;
  2005. if (swl != NULL) {
  2006. if (last_dev) {
  2007. wrap.b24 = new_fcport->d_id.b24;
  2008. } else {
  2009. new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
  2010. memcpy(new_fcport->node_name,
  2011. swl[swl_idx].node_name, WWN_SIZE);
  2012. memcpy(new_fcport->port_name,
  2013. swl[swl_idx].port_name, WWN_SIZE);
  2014. if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
  2015. last_dev = 1;
  2016. }
  2017. swl_idx++;
  2018. }
  2019. } else {
  2020. /* Send GA_NXT to the switch */
  2021. rval = qla2x00_ga_nxt(ha, new_fcport);
  2022. if (rval != QLA_SUCCESS) {
  2023. qla_printk(KERN_WARNING, ha,
  2024. "SNS scan failed -- assuming zero-entry "
  2025. "result...\n");
  2026. list_for_each_entry_safe(fcport, fcptemp,
  2027. new_fcports, list) {
  2028. list_del(&fcport->list);
  2029. kfree(fcport);
  2030. }
  2031. rval = QLA_SUCCESS;
  2032. break;
  2033. }
  2034. }
  2035. /* If wrap on switch device list, exit. */
  2036. if (first_dev) {
  2037. wrap.b24 = new_fcport->d_id.b24;
  2038. first_dev = 0;
  2039. } else if (new_fcport->d_id.b24 == wrap.b24) {
  2040. DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
  2041. ha->host_no, new_fcport->d_id.b.domain,
  2042. new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
  2043. break;
  2044. }
  2045. /* Bypass if host adapter. */
  2046. if (new_fcport->d_id.b24 == ha->d_id.b24)
  2047. continue;
  2048. /* Bypass if same domain and area of adapter. */
  2049. if (((new_fcport->d_id.b24 & 0xffff00) ==
  2050. (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
  2051. ISP_CFG_FL)
  2052. continue;
  2053. /* Bypass reserved domain fields. */
  2054. if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
  2055. continue;
  2056. /* Locate matching device in database. */
  2057. found = 0;
  2058. list_for_each_entry(fcport, &ha->fcports, list) {
  2059. if (memcmp(new_fcport->port_name, fcport->port_name,
  2060. WWN_SIZE))
  2061. continue;
  2062. found++;
  2063. /*
  2064. * If address the same and state FCS_ONLINE, nothing
  2065. * changed.
  2066. */
  2067. if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
  2068. atomic_read(&fcport->state) == FCS_ONLINE) {
  2069. break;
  2070. }
  2071. /*
  2072. * If device was not a fabric device before.
  2073. */
  2074. if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
  2075. fcport->d_id.b24 = new_fcport->d_id.b24;
  2076. fcport->loop_id = FC_NO_LOOP_ID;
  2077. fcport->flags |= (FCF_FABRIC_DEVICE |
  2078. FCF_LOGIN_NEEDED);
  2079. fcport->flags &= ~FCF_PERSISTENT_BOUND;
  2080. break;
  2081. }
  2082. /*
  2083. * Port ID changed or device was marked to be updated;
  2084. * Log it out if still logged in and mark it for
  2085. * relogin later.
  2086. */
  2087. fcport->d_id.b24 = new_fcport->d_id.b24;
  2088. fcport->flags |= FCF_LOGIN_NEEDED;
  2089. if (fcport->loop_id != FC_NO_LOOP_ID &&
  2090. (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
  2091. fcport->port_type != FCT_INITIATOR &&
  2092. fcport->port_type != FCT_BROADCAST) {
  2093. ha->isp_ops.fabric_logout(ha, fcport->loop_id,
  2094. fcport->d_id.b.domain, fcport->d_id.b.area,
  2095. fcport->d_id.b.al_pa);
  2096. fcport->loop_id = FC_NO_LOOP_ID;
  2097. }
  2098. break;
  2099. }
  2100. if (found)
  2101. continue;
  2102. /* If device was not in our fcports list, then add it. */
  2103. list_add_tail(&new_fcport->list, new_fcports);
  2104. /* Allocate a new replacement fcport. */
  2105. nxt_d_id.b24 = new_fcport->d_id.b24;
  2106. new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
  2107. if (new_fcport == NULL) {
  2108. kfree(swl);
  2109. return (QLA_MEMORY_ALLOC_FAILED);
  2110. }
  2111. new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
  2112. new_fcport->d_id.b24 = nxt_d_id.b24;
  2113. }
  2114. kfree(swl);
  2115. kfree(new_fcport);
  2116. if (!list_empty(new_fcports))
  2117. ha->device_flags |= DFLG_FABRIC_DEVICES;
  2118. return (rval);
  2119. }
  2120. /*
  2121. * qla2x00_find_new_loop_id
  2122. * Scan through our port list and find a new usable loop ID.
  2123. *
  2124. * Input:
  2125. * ha: adapter state pointer.
  2126. * dev: port structure pointer.
  2127. *
  2128. * Returns:
  2129. * qla2x00 local function return status code.
  2130. *
  2131. * Context:
  2132. * Kernel context.
  2133. */
  2134. int
  2135. qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
  2136. {
  2137. int rval;
  2138. int found;
  2139. fc_port_t *fcport;
  2140. uint16_t first_loop_id;
  2141. rval = QLA_SUCCESS;
  2142. /* Save starting loop ID. */
  2143. first_loop_id = dev->loop_id;
  2144. for (;;) {
  2145. /* Skip loop ID if already used by adapter. */
  2146. if (dev->loop_id == ha->loop_id) {
  2147. dev->loop_id++;
  2148. }
  2149. /* Skip reserved loop IDs. */
  2150. while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
  2151. dev->loop_id++;
  2152. }
  2153. /* Reset loop ID if passed the end. */
  2154. if (dev->loop_id > ha->last_loop_id) {
  2155. /* first loop ID. */
  2156. dev->loop_id = ha->min_external_loopid;
  2157. }
  2158. /* Check for loop ID being already in use. */
  2159. found = 0;
  2160. fcport = NULL;
  2161. list_for_each_entry(fcport, &ha->fcports, list) {
  2162. if (fcport->loop_id == dev->loop_id && fcport != dev) {
  2163. /* ID possibly in use */
  2164. found++;
  2165. break;
  2166. }
  2167. }
  2168. /* If not in use then it is free to use. */
  2169. if (!found) {
  2170. break;
  2171. }
  2172. /* ID in use. Try next value. */
  2173. dev->loop_id++;
  2174. /* If wrap around. No free ID to use. */
  2175. if (dev->loop_id == first_loop_id) {
  2176. dev->loop_id = FC_NO_LOOP_ID;
  2177. rval = QLA_FUNCTION_FAILED;
  2178. break;
  2179. }
  2180. }
  2181. return (rval);
  2182. }
  2183. /*
  2184. * qla2x00_device_resync
  2185. * Marks devices in the database that needs resynchronization.
  2186. *
  2187. * Input:
  2188. * ha = adapter block pointer.
  2189. *
  2190. * Context:
  2191. * Kernel context.
  2192. */
  2193. static int
  2194. qla2x00_device_resync(scsi_qla_host_t *ha)
  2195. {
  2196. int rval;
  2197. int rval2;
  2198. uint32_t mask;
  2199. fc_port_t *fcport;
  2200. uint32_t rscn_entry;
  2201. uint8_t rscn_out_iter;
  2202. uint8_t format;
  2203. port_id_t d_id;
  2204. rval = QLA_RSCNS_HANDLED;
  2205. while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
  2206. ha->flags.rscn_queue_overflow) {
  2207. rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
  2208. format = MSB(MSW(rscn_entry));
  2209. d_id.b.domain = LSB(MSW(rscn_entry));
  2210. d_id.b.area = MSB(LSW(rscn_entry));
  2211. d_id.b.al_pa = LSB(LSW(rscn_entry));
  2212. DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
  2213. "[%02x/%02x%02x%02x].\n",
  2214. ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
  2215. d_id.b.area, d_id.b.al_pa));
  2216. ha->rscn_out_ptr++;
  2217. if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
  2218. ha->rscn_out_ptr = 0;
  2219. /* Skip duplicate entries. */
  2220. for (rscn_out_iter = ha->rscn_out_ptr;
  2221. !ha->flags.rscn_queue_overflow &&
  2222. rscn_out_iter != ha->rscn_in_ptr;
  2223. rscn_out_iter = (rscn_out_iter ==
  2224. (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
  2225. if (rscn_entry != ha->rscn_queue[rscn_out_iter])
  2226. break;
  2227. DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
  2228. "entry found at [%d].\n", ha->host_no,
  2229. rscn_out_iter));
  2230. ha->rscn_out_ptr = rscn_out_iter;
  2231. }
  2232. /* Queue overflow, set switch default case. */
  2233. if (ha->flags.rscn_queue_overflow) {
  2234. DEBUG(printk("scsi(%ld): device_resync: rscn "
  2235. "overflow.\n", ha->host_no));
  2236. format = 3;
  2237. ha->flags.rscn_queue_overflow = 0;
  2238. }
  2239. switch (format) {
  2240. case 0:
  2241. if (ql2xprocessrscn &&
  2242. !IS_QLA2100(ha) && !IS_QLA2200(ha) &&
  2243. !IS_QLA6312(ha) && !IS_QLA6322(ha) &&
  2244. !IS_QLA24XX(ha) && !IS_QLA54XX(ha) &&
  2245. ha->flags.init_done) {
  2246. /* Handle port RSCN via asyncronous IOCBs */
  2247. rval2 = qla2x00_handle_port_rscn(ha, rscn_entry,
  2248. NULL, 0);
  2249. if (rval2 == QLA_SUCCESS)
  2250. continue;
  2251. }
  2252. mask = 0xffffff;
  2253. break;
  2254. case 1:
  2255. mask = 0xffff00;
  2256. break;
  2257. case 2:
  2258. mask = 0xff0000;
  2259. break;
  2260. default:
  2261. mask = 0x0;
  2262. d_id.b24 = 0;
  2263. ha->rscn_out_ptr = ha->rscn_in_ptr;
  2264. break;
  2265. }
  2266. rval = QLA_SUCCESS;
  2267. /* Abort any outstanding IO descriptors. */
  2268. if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
  2269. qla2x00_cancel_io_descriptors(ha);
  2270. list_for_each_entry(fcport, &ha->fcports, list) {
  2271. if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
  2272. (fcport->d_id.b24 & mask) != d_id.b24 ||
  2273. fcport->port_type == FCT_BROADCAST)
  2274. continue;
  2275. if (atomic_read(&fcport->state) == FCS_ONLINE) {
  2276. if (format != 3 ||
  2277. fcport->port_type != FCT_INITIATOR) {
  2278. qla2x00_mark_device_lost(ha, fcport,
  2279. 0, 0);
  2280. }
  2281. }
  2282. fcport->flags &= ~FCF_FARP_DONE;
  2283. }
  2284. }
  2285. return (rval);
  2286. }
  2287. /*
  2288. * qla2x00_fabric_dev_login
  2289. * Login fabric target device and update FC port database.
  2290. *
  2291. * Input:
  2292. * ha: adapter state pointer.
  2293. * fcport: port structure list pointer.
  2294. * next_loopid: contains value of a new loop ID that can be used
  2295. * by the next login attempt.
  2296. *
  2297. * Returns:
  2298. * qla2x00 local function return status code.
  2299. *
  2300. * Context:
  2301. * Kernel context.
  2302. */
  2303. static int
  2304. qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
  2305. uint16_t *next_loopid)
  2306. {
  2307. int rval;
  2308. int retry;
  2309. uint8_t opts;
  2310. rval = QLA_SUCCESS;
  2311. retry = 0;
  2312. rval = qla2x00_fabric_login(ha, fcport, next_loopid);
  2313. if (rval == QLA_SUCCESS) {
  2314. /* Send an ADISC to tape devices.*/
  2315. opts = 0;
  2316. if (fcport->flags & FCF_TAPE_PRESENT)
  2317. opts |= BIT_1;
  2318. rval = qla2x00_get_port_database(ha, fcport, opts);
  2319. if (rval != QLA_SUCCESS) {
  2320. ha->isp_ops.fabric_logout(ha, fcport->loop_id,
  2321. fcport->d_id.b.domain, fcport->d_id.b.area,
  2322. fcport->d_id.b.al_pa);
  2323. qla2x00_mark_device_lost(ha, fcport, 1, 0);
  2324. } else {
  2325. qla2x00_update_fcport(ha, fcport);
  2326. }
  2327. }
  2328. return (rval);
  2329. }
  2330. /*
  2331. * qla2x00_fabric_login
  2332. * Issue fabric login command.
  2333. *
  2334. * Input:
  2335. * ha = adapter block pointer.
  2336. * device = pointer to FC device type structure.
  2337. *
  2338. * Returns:
  2339. * 0 - Login successfully
  2340. * 1 - Login failed
  2341. * 2 - Initiator device
  2342. * 3 - Fatal error
  2343. */
  2344. int
  2345. qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
  2346. uint16_t *next_loopid)
  2347. {
  2348. int rval;
  2349. int retry;
  2350. uint16_t tmp_loopid;
  2351. uint16_t mb[MAILBOX_REGISTER_COUNT];
  2352. retry = 0;
  2353. tmp_loopid = 0;
  2354. for (;;) {
  2355. DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
  2356. "for port %02x%02x%02x.\n",
  2357. ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
  2358. fcport->d_id.b.area, fcport->d_id.b.al_pa));
  2359. /* Login fcport on switch. */
  2360. ha->isp_ops.fabric_login(ha, fcport->loop_id,
  2361. fcport->d_id.b.domain, fcport->d_id.b.area,
  2362. fcport->d_id.b.al_pa, mb, BIT_0);
  2363. if (mb[0] == MBS_PORT_ID_USED) {
  2364. /*
  2365. * Device has another loop ID. The firmware team
  2366. * recommends the driver perform an implicit login with
  2367. * the specified ID again. The ID we just used is save
  2368. * here so we return with an ID that can be tried by
  2369. * the next login.
  2370. */
  2371. retry++;
  2372. tmp_loopid = fcport->loop_id;
  2373. fcport->loop_id = mb[1];
  2374. DEBUG(printk("Fabric Login: port in use - next "
  2375. "loop id=0x%04x, port Id=%02x%02x%02x.\n",
  2376. fcport->loop_id, fcport->d_id.b.domain,
  2377. fcport->d_id.b.area, fcport->d_id.b.al_pa));
  2378. } else if (mb[0] == MBS_COMMAND_COMPLETE) {
  2379. /*
  2380. * Login succeeded.
  2381. */
  2382. if (retry) {
  2383. /* A retry occurred before. */
  2384. *next_loopid = tmp_loopid;
  2385. } else {
  2386. /*
  2387. * No retry occurred before. Just increment the
  2388. * ID value for next login.
  2389. */
  2390. *next_loopid = (fcport->loop_id + 1);
  2391. }
  2392. if (mb[1] & BIT_0) {
  2393. fcport->port_type = FCT_INITIATOR;
  2394. } else {
  2395. fcport->port_type = FCT_TARGET;
  2396. if (mb[1] & BIT_1) {
  2397. fcport->flags |= FCF_TAPE_PRESENT;
  2398. }
  2399. }
  2400. if (mb[10] & BIT_0)
  2401. fcport->supported_classes |= FC_COS_CLASS2;
  2402. if (mb[10] & BIT_1)
  2403. fcport->supported_classes |= FC_COS_CLASS3;
  2404. rval = QLA_SUCCESS;
  2405. break;
  2406. } else if (mb[0] == MBS_LOOP_ID_USED) {
  2407. /*
  2408. * Loop ID already used, try next loop ID.
  2409. */
  2410. fcport->loop_id++;
  2411. rval = qla2x00_find_new_loop_id(ha, fcport);
  2412. if (rval != QLA_SUCCESS) {
  2413. /* Ran out of loop IDs to use */
  2414. break;
  2415. }
  2416. } else if (mb[0] == MBS_COMMAND_ERROR) {
  2417. /*
  2418. * Firmware possibly timed out during login. If NO
  2419. * retries are left to do then the device is declared
  2420. * dead.
  2421. */
  2422. *next_loopid = fcport->loop_id;
  2423. ha->isp_ops.fabric_logout(ha, fcport->loop_id,
  2424. fcport->d_id.b.domain, fcport->d_id.b.area,
  2425. fcport->d_id.b.al_pa);
  2426. qla2x00_mark_device_lost(ha, fcport, 1, 0);
  2427. rval = 1;
  2428. break;
  2429. } else {
  2430. /*
  2431. * unrecoverable / not handled error
  2432. */
  2433. DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
  2434. "loop_id=%x jiffies=%lx.\n",
  2435. __func__, ha->host_no, mb[0],
  2436. fcport->d_id.b.domain, fcport->d_id.b.area,
  2437. fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
  2438. *next_loopid = fcport->loop_id;
  2439. ha->isp_ops.fabric_logout(ha, fcport->loop_id,
  2440. fcport->d_id.b.domain, fcport->d_id.b.area,
  2441. fcport->d_id.b.al_pa);
  2442. fcport->loop_id = FC_NO_LOOP_ID;
  2443. fcport->login_retry = 0;
  2444. rval = 3;
  2445. break;
  2446. }
  2447. }
  2448. return (rval);
  2449. }
  2450. /*
  2451. * qla2x00_local_device_login
  2452. * Issue local device login command.
  2453. *
  2454. * Input:
  2455. * ha = adapter block pointer.
  2456. * loop_id = loop id of device to login to.
  2457. *
  2458. * Returns (Where's the #define!!!!):
  2459. * 0 - Login successfully
  2460. * 1 - Login failed
  2461. * 3 - Fatal error
  2462. */
  2463. int
  2464. qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
  2465. {
  2466. int rval;
  2467. uint16_t mb[MAILBOX_REGISTER_COUNT];
  2468. memset(mb, 0, sizeof(mb));
  2469. rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
  2470. if (rval == QLA_SUCCESS) {
  2471. /* Interrogate mailbox registers for any errors */
  2472. if (mb[0] == MBS_COMMAND_ERROR)
  2473. rval = 1;
  2474. else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
  2475. /* device not in PCB table */
  2476. rval = 3;
  2477. }
  2478. return (rval);
  2479. }
  2480. /*
  2481. * qla2x00_loop_resync
  2482. * Resync with fibre channel devices.
  2483. *
  2484. * Input:
  2485. * ha = adapter block pointer.
  2486. *
  2487. * Returns:
  2488. * 0 = success
  2489. */
  2490. int
  2491. qla2x00_loop_resync(scsi_qla_host_t *ha)
  2492. {
  2493. int rval;
  2494. uint32_t wait_time;
  2495. rval = QLA_SUCCESS;
  2496. atomic_set(&ha->loop_state, LOOP_UPDATE);
  2497. clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
  2498. if (ha->flags.online) {
  2499. if (!(rval = qla2x00_fw_ready(ha))) {
  2500. /* Wait at most MAX_TARGET RSCNs for a stable link. */
  2501. wait_time = 256;
  2502. do {
  2503. atomic_set(&ha->loop_state, LOOP_UPDATE);
  2504. /* Issue a marker after FW becomes ready. */
  2505. qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
  2506. ha->marker_needed = 0;
  2507. /* Remap devices on Loop. */
  2508. clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  2509. qla2x00_configure_loop(ha);
  2510. wait_time--;
  2511. } while (!atomic_read(&ha->loop_down_timer) &&
  2512. !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
  2513. wait_time &&
  2514. (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
  2515. }
  2516. }
  2517. if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
  2518. return (QLA_FUNCTION_FAILED);
  2519. }
  2520. if (rval) {
  2521. DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
  2522. }
  2523. return (rval);
  2524. }
  2525. void
  2526. qla2x00_rescan_fcports(scsi_qla_host_t *ha)
  2527. {
  2528. int rescan_done;
  2529. fc_port_t *fcport;
  2530. rescan_done = 0;
  2531. list_for_each_entry(fcport, &ha->fcports, list) {
  2532. if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
  2533. continue;
  2534. qla2x00_update_fcport(ha, fcport);
  2535. fcport->flags &= ~FCF_RESCAN_NEEDED;
  2536. rescan_done = 1;
  2537. }
  2538. qla2x00_probe_for_all_luns(ha);
  2539. }
  2540. void
  2541. qla2x00_update_fcports(scsi_qla_host_t *ha)
  2542. {
  2543. fc_port_t *fcport;
  2544. /* Go with deferred removal of rport references. */
  2545. list_for_each_entry(fcport, &ha->fcports, list)
  2546. if (fcport->drport)
  2547. qla2x00_rport_del(fcport);
  2548. }
  2549. /*
  2550. * qla2x00_abort_isp
  2551. * Resets ISP and aborts all outstanding commands.
  2552. *
  2553. * Input:
  2554. * ha = adapter block pointer.
  2555. *
  2556. * Returns:
  2557. * 0 = success
  2558. */
  2559. int
  2560. qla2x00_abort_isp(scsi_qla_host_t *ha)
  2561. {
  2562. unsigned long flags = 0;
  2563. uint16_t cnt;
  2564. srb_t *sp;
  2565. uint8_t status = 0;
  2566. if (ha->flags.online) {
  2567. ha->flags.online = 0;
  2568. clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  2569. qla_printk(KERN_INFO, ha,
  2570. "Performing ISP error recovery - ha= %p.\n", ha);
  2571. ha->isp_ops.reset_chip(ha);
  2572. atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
  2573. if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
  2574. atomic_set(&ha->loop_state, LOOP_DOWN);
  2575. qla2x00_mark_all_devices_lost(ha, 0);
  2576. } else {
  2577. if (!atomic_read(&ha->loop_down_timer))
  2578. atomic_set(&ha->loop_down_timer,
  2579. LOOP_DOWN_TIME);
  2580. }
  2581. spin_lock_irqsave(&ha->hardware_lock, flags);
  2582. /* Requeue all commands in outstanding command list. */
  2583. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  2584. sp = ha->outstanding_cmds[cnt];
  2585. if (sp) {
  2586. ha->outstanding_cmds[cnt] = NULL;
  2587. sp->flags = 0;
  2588. sp->cmd->result = DID_RESET << 16;
  2589. sp->cmd->host_scribble = (unsigned char *)NULL;
  2590. qla2x00_sp_compl(ha, sp);
  2591. }
  2592. }
  2593. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2594. ha->isp_ops.nvram_config(ha);
  2595. if (!qla2x00_restart_isp(ha)) {
  2596. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  2597. if (!atomic_read(&ha->loop_down_timer)) {
  2598. /*
  2599. * Issue marker command only when we are going
  2600. * to start the I/O .
  2601. */
  2602. ha->marker_needed = 1;
  2603. }
  2604. ha->flags.online = 1;
  2605. ha->isp_ops.enable_intrs(ha);
  2606. ha->isp_abort_cnt = 0;
  2607. clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
  2608. } else { /* failed the ISP abort */
  2609. ha->flags.online = 1;
  2610. if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
  2611. if (ha->isp_abort_cnt == 0) {
  2612. qla_printk(KERN_WARNING, ha,
  2613. "ISP error recovery failed - "
  2614. "board disabled\n");
  2615. /*
  2616. * The next call disables the board
  2617. * completely.
  2618. */
  2619. ha->isp_ops.reset_adapter(ha);
  2620. ha->flags.online = 0;
  2621. clear_bit(ISP_ABORT_RETRY,
  2622. &ha->dpc_flags);
  2623. status = 0;
  2624. } else { /* schedule another ISP abort */
  2625. ha->isp_abort_cnt--;
  2626. DEBUG(printk("qla%ld: ISP abort - "
  2627. "retry remaining %d\n",
  2628. ha->host_no, ha->isp_abort_cnt);)
  2629. status = 1;
  2630. }
  2631. } else {
  2632. ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
  2633. DEBUG(printk("qla2x00(%ld): ISP error recovery "
  2634. "- retrying (%d) more times\n",
  2635. ha->host_no, ha->isp_abort_cnt);)
  2636. set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
  2637. status = 1;
  2638. }
  2639. }
  2640. }
  2641. if (status) {
  2642. qla_printk(KERN_INFO, ha,
  2643. "qla2x00_abort_isp: **** FAILED ****\n");
  2644. } else {
  2645. DEBUG(printk(KERN_INFO
  2646. "qla2x00_abort_isp(%ld): exiting.\n",
  2647. ha->host_no);)
  2648. }
  2649. return(status);
  2650. }
  2651. /*
  2652. * qla2x00_restart_isp
  2653. * restarts the ISP after a reset
  2654. *
  2655. * Input:
  2656. * ha = adapter block pointer.
  2657. *
  2658. * Returns:
  2659. * 0 = success
  2660. */
  2661. static int
  2662. qla2x00_restart_isp(scsi_qla_host_t *ha)
  2663. {
  2664. uint8_t status = 0;
  2665. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  2666. unsigned long flags = 0;
  2667. uint32_t wait_time;
  2668. /* If firmware needs to be loaded */
  2669. if (qla2x00_isp_firmware(ha)) {
  2670. ha->flags.online = 0;
  2671. if (!(status = ha->isp_ops.chip_diag(ha))) {
  2672. if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
  2673. status = qla2x00_setup_chip(ha);
  2674. goto done;
  2675. }
  2676. spin_lock_irqsave(&ha->hardware_lock, flags);
  2677. if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
  2678. /*
  2679. * Disable SRAM, Instruction RAM and GP RAM
  2680. * parity.
  2681. */
  2682. WRT_REG_WORD(&reg->hccr,
  2683. (HCCR_ENABLE_PARITY + 0x0));
  2684. RD_REG_WORD(&reg->hccr);
  2685. }
  2686. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2687. status = qla2x00_setup_chip(ha);
  2688. spin_lock_irqsave(&ha->hardware_lock, flags);
  2689. if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
  2690. /* Enable proper parity */
  2691. if (IS_QLA2300(ha))
  2692. /* SRAM parity */
  2693. WRT_REG_WORD(&reg->hccr,
  2694. (HCCR_ENABLE_PARITY + 0x1));
  2695. else
  2696. /*
  2697. * SRAM, Instruction RAM and GP RAM
  2698. * parity.
  2699. */
  2700. WRT_REG_WORD(&reg->hccr,
  2701. (HCCR_ENABLE_PARITY + 0x7));
  2702. RD_REG_WORD(&reg->hccr);
  2703. }
  2704. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2705. }
  2706. }
  2707. done:
  2708. if (!status && !(status = qla2x00_init_rings(ha))) {
  2709. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  2710. if (!(status = qla2x00_fw_ready(ha))) {
  2711. DEBUG(printk("%s(): Start configure loop, "
  2712. "status = %d\n", __func__, status);)
  2713. /* Issue a marker after FW becomes ready. */
  2714. qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
  2715. ha->flags.online = 1;
  2716. /* Wait at most MAX_TARGET RSCNs for a stable link. */
  2717. wait_time = 256;
  2718. do {
  2719. clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  2720. qla2x00_configure_loop(ha);
  2721. wait_time--;
  2722. } while (!atomic_read(&ha->loop_down_timer) &&
  2723. !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
  2724. wait_time &&
  2725. (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
  2726. }
  2727. /* if no cable then assume it's good */
  2728. if ((ha->device_flags & DFLG_NO_CABLE))
  2729. status = 0;
  2730. DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
  2731. __func__,
  2732. status);)
  2733. }
  2734. return (status);
  2735. }
  2736. /*
  2737. * qla2x00_reset_adapter
  2738. * Reset adapter.
  2739. *
  2740. * Input:
  2741. * ha = adapter block pointer.
  2742. */
  2743. void
  2744. qla2x00_reset_adapter(scsi_qla_host_t *ha)
  2745. {
  2746. unsigned long flags = 0;
  2747. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  2748. ha->flags.online = 0;
  2749. ha->isp_ops.disable_intrs(ha);
  2750. spin_lock_irqsave(&ha->hardware_lock, flags);
  2751. WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
  2752. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  2753. WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
  2754. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  2755. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2756. }
  2757. void
  2758. qla24xx_reset_adapter(scsi_qla_host_t *ha)
  2759. {
  2760. unsigned long flags = 0;
  2761. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  2762. ha->flags.online = 0;
  2763. ha->isp_ops.disable_intrs(ha);
  2764. spin_lock_irqsave(&ha->hardware_lock, flags);
  2765. WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
  2766. RD_REG_DWORD(&reg->hccr);
  2767. WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
  2768. RD_REG_DWORD(&reg->hccr);
  2769. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2770. }
  2771. int
  2772. qla24xx_nvram_config(scsi_qla_host_t *ha)
  2773. {
  2774. int rval;
  2775. struct init_cb_24xx *icb;
  2776. struct nvram_24xx *nv;
  2777. uint32_t *dptr;
  2778. uint8_t *dptr1, *dptr2;
  2779. uint32_t chksum;
  2780. uint16_t cnt;
  2781. rval = QLA_SUCCESS;
  2782. icb = (struct init_cb_24xx *)ha->init_cb;
  2783. nv = (struct nvram_24xx *)ha->request_ring;
  2784. /* Determine NVRAM starting address. */
  2785. ha->nvram_size = sizeof(struct nvram_24xx);
  2786. ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
  2787. ha->vpd_size = FA_NVRAM_VPD_SIZE;
  2788. ha->vpd_base = FA_NVRAM_VPD0_ADDR;
  2789. if (PCI_FUNC(ha->pdev->devfn)) {
  2790. ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
  2791. ha->vpd_base = FA_NVRAM_VPD1_ADDR;
  2792. }
  2793. /* Get NVRAM data and calculate checksum. */
  2794. dptr = (uint32_t *)nv;
  2795. ha->isp_ops.read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
  2796. ha->nvram_size);
  2797. for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
  2798. chksum += le32_to_cpu(*dptr++);
  2799. DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
  2800. DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
  2801. ha->nvram_size));
  2802. /* Bad NVRAM data, set defaults parameters. */
  2803. if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
  2804. || nv->id[3] != ' ' ||
  2805. nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
  2806. /* Reset NVRAM data. */
  2807. qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
  2808. "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
  2809. le16_to_cpu(nv->nvram_version));
  2810. qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
  2811. "invalid -- WWPN) defaults.\n");
  2812. /*
  2813. * Set default initialization control block.
  2814. */
  2815. memset(nv, 0, ha->nvram_size);
  2816. nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
  2817. nv->version = __constant_cpu_to_le16(ICB_VERSION);
  2818. nv->frame_payload_size = __constant_cpu_to_le16(2048);
  2819. nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
  2820. nv->exchange_count = __constant_cpu_to_le16(0);
  2821. nv->hard_address = __constant_cpu_to_le16(124);
  2822. nv->port_name[0] = 0x21;
  2823. nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
  2824. nv->port_name[2] = 0x00;
  2825. nv->port_name[3] = 0xe0;
  2826. nv->port_name[4] = 0x8b;
  2827. nv->port_name[5] = 0x1c;
  2828. nv->port_name[6] = 0x55;
  2829. nv->port_name[7] = 0x86;
  2830. nv->node_name[0] = 0x20;
  2831. nv->node_name[1] = 0x00;
  2832. nv->node_name[2] = 0x00;
  2833. nv->node_name[3] = 0xe0;
  2834. nv->node_name[4] = 0x8b;
  2835. nv->node_name[5] = 0x1c;
  2836. nv->node_name[6] = 0x55;
  2837. nv->node_name[7] = 0x86;
  2838. nv->login_retry_count = __constant_cpu_to_le16(8);
  2839. nv->link_down_timeout = __constant_cpu_to_le16(200);
  2840. nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
  2841. nv->login_timeout = __constant_cpu_to_le16(0);
  2842. nv->firmware_options_1 =
  2843. __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
  2844. nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
  2845. nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
  2846. nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
  2847. nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
  2848. nv->efi_parameters = __constant_cpu_to_le32(0);
  2849. nv->reset_delay = 5;
  2850. nv->max_luns_per_target = __constant_cpu_to_le16(128);
  2851. nv->port_down_retry_count = __constant_cpu_to_le16(30);
  2852. nv->link_down_timeout = __constant_cpu_to_le16(30);
  2853. rval = 1;
  2854. }
  2855. /* Reset Initialization control block */
  2856. memset(icb, 0, sizeof(struct init_cb_24xx));
  2857. /* Copy 1st segment. */
  2858. dptr1 = (uint8_t *)icb;
  2859. dptr2 = (uint8_t *)&nv->version;
  2860. cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
  2861. while (cnt--)
  2862. *dptr1++ = *dptr2++;
  2863. icb->login_retry_count = nv->login_retry_count;
  2864. icb->link_down_timeout = nv->link_down_timeout;
  2865. /* Copy 2nd segment. */
  2866. dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
  2867. dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
  2868. cnt = (uint8_t *)&icb->reserved_3 -
  2869. (uint8_t *)&icb->interrupt_delay_timer;
  2870. while (cnt--)
  2871. *dptr1++ = *dptr2++;
  2872. /*
  2873. * Setup driver NVRAM options.
  2874. */
  2875. if (memcmp(nv->model_name, BINZERO, sizeof(nv->model_name)) != 0) {
  2876. char *st, *en;
  2877. uint16_t index;
  2878. strncpy(ha->model_number, nv->model_name,
  2879. sizeof(nv->model_name));
  2880. st = en = ha->model_number;
  2881. en += sizeof(nv->model_name) - 1;
  2882. while (en > st) {
  2883. if (*en != 0x20 && *en != 0x00)
  2884. break;
  2885. *en-- = '\0';
  2886. }
  2887. index = (ha->pdev->subsystem_device & 0xff);
  2888. if (index < QLA_MODEL_NAMES)
  2889. ha->model_desc = qla2x00_model_name[index * 2 + 1];
  2890. } else
  2891. strcpy(ha->model_number, "QLA2462");
  2892. /* Use alternate WWN? */
  2893. if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
  2894. memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
  2895. memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
  2896. }
  2897. /* Prepare nodename */
  2898. if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
  2899. /*
  2900. * Firmware will apply the following mask if the nodename was
  2901. * not provided.
  2902. */
  2903. memcpy(icb->node_name, icb->port_name, WWN_SIZE);
  2904. icb->node_name[0] &= 0xF0;
  2905. }
  2906. /* Set host adapter parameters. */
  2907. ha->flags.disable_risc_code_load = 0;
  2908. ha->flags.enable_lip_reset = 1;
  2909. ha->flags.enable_lip_full_login = 1;
  2910. ha->flags.enable_target_reset = 1;
  2911. ha->flags.enable_led_scheme = 0;
  2912. ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
  2913. (BIT_6 | BIT_5 | BIT_4)) >> 4;
  2914. memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
  2915. sizeof(ha->fw_seriallink_options24));
  2916. /* save HBA serial number */
  2917. ha->serial0 = icb->port_name[5];
  2918. ha->serial1 = icb->port_name[6];
  2919. ha->serial2 = icb->port_name[7];
  2920. ha->node_name = icb->node_name;
  2921. ha->port_name = icb->port_name;
  2922. icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
  2923. ha->retry_count = le16_to_cpu(nv->login_retry_count);
  2924. /* Set minimum login_timeout to 4 seconds. */
  2925. if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
  2926. nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
  2927. if (le16_to_cpu(nv->login_timeout) < 4)
  2928. nv->login_timeout = __constant_cpu_to_le16(4);
  2929. ha->login_timeout = le16_to_cpu(nv->login_timeout);
  2930. icb->login_timeout = cpu_to_le16(nv->login_timeout);
  2931. /* Set minimum RATOV to 200 tenths of a second. */
  2932. ha->r_a_tov = 200;
  2933. ha->loop_reset_delay = nv->reset_delay;
  2934. /* Link Down Timeout = 0:
  2935. *
  2936. * When Port Down timer expires we will start returning
  2937. * I/O's to OS with "DID_NO_CONNECT".
  2938. *
  2939. * Link Down Timeout != 0:
  2940. *
  2941. * The driver waits for the link to come up after link down
  2942. * before returning I/Os to OS with "DID_NO_CONNECT".
  2943. */
  2944. if (le16_to_cpu(nv->link_down_timeout) == 0) {
  2945. ha->loop_down_abort_time =
  2946. (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
  2947. } else {
  2948. ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
  2949. ha->loop_down_abort_time =
  2950. (LOOP_DOWN_TIME - ha->link_down_timeout);
  2951. }
  2952. /* Need enough time to try and get the port back. */
  2953. ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
  2954. if (qlport_down_retry)
  2955. ha->port_down_retry_count = qlport_down_retry;
  2956. /* Set login_retry_count */
  2957. ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
  2958. if (ha->port_down_retry_count ==
  2959. le16_to_cpu(nv->port_down_retry_count) &&
  2960. ha->port_down_retry_count > 3)
  2961. ha->login_retry_count = ha->port_down_retry_count;
  2962. else if (ha->port_down_retry_count > (int)ha->login_retry_count)
  2963. ha->login_retry_count = ha->port_down_retry_count;
  2964. if (ql2xloginretrycount)
  2965. ha->login_retry_count = ql2xloginretrycount;
  2966. /* Enable ZIO. */
  2967. if (!ha->flags.init_done) {
  2968. ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
  2969. (BIT_3 | BIT_2 | BIT_1 | BIT_0);
  2970. ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
  2971. le16_to_cpu(icb->interrupt_delay_timer): 2;
  2972. }
  2973. icb->firmware_options_2 &= __constant_cpu_to_le32(
  2974. ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
  2975. ha->flags.process_response_queue = 0;
  2976. if (ha->zio_mode != QLA_ZIO_DISABLED) {
  2977. ha->zio_mode = QLA_ZIO_MODE_6;
  2978. DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
  2979. "(%d us).\n", ha->host_no, ha->zio_mode,
  2980. ha->zio_timer * 100));
  2981. qla_printk(KERN_INFO, ha,
  2982. "ZIO mode %d enabled; timer delay (%d us).\n",
  2983. ha->zio_mode, ha->zio_timer * 100);
  2984. icb->firmware_options_2 |= cpu_to_le32(
  2985. (uint32_t)ha->zio_mode);
  2986. icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
  2987. ha->flags.process_response_queue = 1;
  2988. }
  2989. if (rval) {
  2990. DEBUG2_3(printk(KERN_WARNING
  2991. "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
  2992. }
  2993. return (rval);
  2994. }
  2995. int
  2996. qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
  2997. {
  2998. int rval;
  2999. int segments, fragment;
  3000. uint32_t faddr;
  3001. uint32_t *dcode, dlen;
  3002. uint32_t risc_addr;
  3003. uint32_t risc_size;
  3004. uint32_t i;
  3005. rval = QLA_SUCCESS;
  3006. segments = FA_RISC_CODE_SEGMENTS;
  3007. faddr = FA_RISC_CODE_ADDR;
  3008. dcode = (uint32_t *)ha->request_ring;
  3009. *srisc_addr = 0;
  3010. /* Validate firmware image by checking version. */
  3011. qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
  3012. for (i = 0; i < 4; i++)
  3013. dcode[i] = be32_to_cpu(dcode[i]);
  3014. if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
  3015. dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
  3016. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  3017. dcode[3] == 0)) {
  3018. qla_printk(KERN_WARNING, ha,
  3019. "Unable to verify integrity of flash firmware image!\n");
  3020. qla_printk(KERN_WARNING, ha,
  3021. "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
  3022. dcode[1], dcode[2], dcode[3]);
  3023. return QLA_FUNCTION_FAILED;
  3024. }
  3025. while (segments && rval == QLA_SUCCESS) {
  3026. /* Read segment's load information. */
  3027. qla24xx_read_flash_data(ha, dcode, faddr, 4);
  3028. risc_addr = be32_to_cpu(dcode[2]);
  3029. *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
  3030. risc_size = be32_to_cpu(dcode[3]);
  3031. fragment = 0;
  3032. while (risc_size > 0 && rval == QLA_SUCCESS) {
  3033. dlen = (uint32_t)(ha->fw_transfer_size >> 2);
  3034. if (dlen > risc_size)
  3035. dlen = risc_size;
  3036. DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
  3037. "addr %x, number of dwords 0x%x, offset 0x%x.\n",
  3038. ha->host_no, risc_addr, dlen, faddr));
  3039. qla24xx_read_flash_data(ha, dcode, faddr, dlen);
  3040. for (i = 0; i < dlen; i++)
  3041. dcode[i] = swab32(dcode[i]);
  3042. rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
  3043. dlen);
  3044. if (rval) {
  3045. DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
  3046. "segment %d of firmware\n", ha->host_no,
  3047. fragment));
  3048. qla_printk(KERN_WARNING, ha,
  3049. "[ERROR] Failed to load segment %d of "
  3050. "firmware\n", fragment);
  3051. break;
  3052. }
  3053. faddr += dlen;
  3054. risc_addr += dlen;
  3055. risc_size -= dlen;
  3056. fragment++;
  3057. }
  3058. /* Next segment. */
  3059. segments--;
  3060. }
  3061. return rval;
  3062. }
  3063. #if defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE)
  3064. int
  3065. qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
  3066. {
  3067. int rval, num, i;
  3068. uint32_t cnt;
  3069. uint16_t *risc_code;
  3070. uint32_t risc_addr, risc_size;
  3071. uint16_t *req_ring;
  3072. struct qla_fw_info *fw_iter;
  3073. rval = QLA_SUCCESS;
  3074. /* Load firmware sequences */
  3075. fw_iter = ha->brd_info->fw_info;
  3076. *srisc_addr = *ha->brd_info->fw_info->fwstart;
  3077. while (fw_iter->addressing != FW_INFO_ADDR_NOMORE) {
  3078. risc_code = fw_iter->fwcode;
  3079. risc_size = *fw_iter->fwlen;
  3080. if (fw_iter->addressing == FW_INFO_ADDR_NORMAL)
  3081. risc_addr = *fw_iter->fwstart;
  3082. else
  3083. risc_addr = *fw_iter->lfwstart;
  3084. num = 0;
  3085. rval = 0;
  3086. while (risc_size > 0 && !rval) {
  3087. cnt = (uint16_t)(ha->fw_transfer_size >> 1);
  3088. if (cnt > risc_size)
  3089. cnt = risc_size;
  3090. DEBUG7(printk("scsi(%ld): Loading risc segment@ "
  3091. "addr %p, number of bytes 0x%x, offset 0x%lx.\n",
  3092. ha->host_no, risc_code, cnt, risc_addr));
  3093. req_ring = (uint16_t *)ha->request_ring;
  3094. for (i = 0; i < cnt; i++)
  3095. req_ring[i] = cpu_to_le16(risc_code[i]);
  3096. rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
  3097. cnt);
  3098. if (rval) {
  3099. DEBUG(printk("scsi(%ld): [ERROR] Failed to "
  3100. "load segment %d of firmware\n",
  3101. ha->host_no, num));
  3102. qla_printk(KERN_WARNING, ha,
  3103. "[ERROR] Failed to load segment %d of "
  3104. "firmware\n", num);
  3105. qla2x00_dump_regs(ha);
  3106. break;
  3107. }
  3108. risc_code += cnt;
  3109. risc_addr += cnt;
  3110. risc_size -= cnt;
  3111. num++;
  3112. }
  3113. /* Next firmware sequence */
  3114. fw_iter++;
  3115. }
  3116. return rval;
  3117. }
  3118. int
  3119. qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
  3120. {
  3121. int rval, num, i;
  3122. uint32_t cnt;
  3123. uint32_t *risc_code;
  3124. uint32_t risc_addr, risc_size;
  3125. uint32_t *req_ring;
  3126. struct qla_fw_info *fw_iter;
  3127. rval = QLA_SUCCESS;
  3128. /* Load firmware sequences */
  3129. fw_iter = ha->brd_info->fw_info;
  3130. *srisc_addr = *((uint32_t *)fw_iter->lfwstart);
  3131. while (fw_iter->addressing != FW_INFO_ADDR_NOMORE) {
  3132. risc_code = (uint32_t *)fw_iter->fwcode;
  3133. risc_size = *((uint32_t *)fw_iter->fwlen);
  3134. risc_addr = *((uint32_t *)fw_iter->lfwstart);
  3135. num = 0;
  3136. rval = 0;
  3137. while (risc_size > 0 && !rval) {
  3138. cnt = (uint32_t)(ha->fw_transfer_size >> 2);
  3139. if (cnt > risc_size)
  3140. cnt = risc_size;
  3141. DEBUG7(printk("scsi(%ld): Loading risc segment@ "
  3142. "addr %p, number of bytes 0x%x, offset 0x%lx.\n",
  3143. ha->host_no, risc_code, cnt, risc_addr));
  3144. req_ring = (uint32_t *)ha->request_ring;
  3145. for (i = 0; i < cnt; i++)
  3146. req_ring[i] = cpu_to_le32(risc_code[i]);
  3147. rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
  3148. cnt);
  3149. if (rval) {
  3150. DEBUG(printk("scsi(%ld): [ERROR] Failed to "
  3151. "load segment %d of firmware\n",
  3152. ha->host_no, num));
  3153. qla_printk(KERN_WARNING, ha,
  3154. "[ERROR] Failed to load segment %d of "
  3155. "firmware\n", num);
  3156. qla2x00_dump_regs(ha);
  3157. break;
  3158. }
  3159. risc_code += cnt;
  3160. risc_addr += cnt;
  3161. risc_size -= cnt;
  3162. num++;
  3163. }
  3164. /* Next firmware sequence */
  3165. fw_iter++;
  3166. }
  3167. return rval;
  3168. }
  3169. #else /* !defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE) */
  3170. #define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
  3171. int
  3172. qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
  3173. {
  3174. int rval;
  3175. int i, fragment;
  3176. uint16_t *wcode, *fwcode;
  3177. uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
  3178. struct fw_blob *blob;
  3179. /* Load firmware blob. */
  3180. blob = qla2x00_request_firmware(ha);
  3181. if (!blob) {
  3182. qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
  3183. qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
  3184. "from: " QLA_FW_URL ".\n");
  3185. return QLA_FUNCTION_FAILED;
  3186. }
  3187. rval = QLA_SUCCESS;
  3188. wcode = (uint16_t *)ha->request_ring;
  3189. *srisc_addr = 0;
  3190. fwcode = (uint16_t *)blob->fw->data;
  3191. fwclen = 0;
  3192. /* Validate firmware image by checking version. */
  3193. if (blob->fw->size < 8 * sizeof(uint16_t)) {
  3194. qla_printk(KERN_WARNING, ha,
  3195. "Unable to verify integrity of firmware image (%Zd)!\n",
  3196. blob->fw->size);
  3197. goto fail_fw_integrity;
  3198. }
  3199. for (i = 0; i < 4; i++)
  3200. wcode[i] = be16_to_cpu(fwcode[i + 4]);
  3201. if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
  3202. wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
  3203. wcode[2] == 0 && wcode[3] == 0)) {
  3204. qla_printk(KERN_WARNING, ha,
  3205. "Unable to verify integrity of firmware image!\n");
  3206. qla_printk(KERN_WARNING, ha,
  3207. "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
  3208. wcode[1], wcode[2], wcode[3]);
  3209. goto fail_fw_integrity;
  3210. }
  3211. seg = blob->segs;
  3212. while (*seg && rval == QLA_SUCCESS) {
  3213. risc_addr = *seg;
  3214. *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
  3215. risc_size = be16_to_cpu(fwcode[3]);
  3216. /* Validate firmware image size. */
  3217. fwclen += risc_size * sizeof(uint16_t);
  3218. if (blob->fw->size < fwclen) {
  3219. qla_printk(KERN_WARNING, ha,
  3220. "Unable to verify integrity of firmware image "
  3221. "(%Zd)!\n", blob->fw->size);
  3222. goto fail_fw_integrity;
  3223. }
  3224. fragment = 0;
  3225. while (risc_size > 0 && rval == QLA_SUCCESS) {
  3226. wlen = (uint16_t)(ha->fw_transfer_size >> 1);
  3227. if (wlen > risc_size)
  3228. wlen = risc_size;
  3229. DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
  3230. "addr %x, number of words 0x%x.\n", ha->host_no,
  3231. risc_addr, wlen));
  3232. for (i = 0; i < wlen; i++)
  3233. wcode[i] = swab16(fwcode[i]);
  3234. rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
  3235. wlen);
  3236. if (rval) {
  3237. DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
  3238. "segment %d of firmware\n", ha->host_no,
  3239. fragment));
  3240. qla_printk(KERN_WARNING, ha,
  3241. "[ERROR] Failed to load segment %d of "
  3242. "firmware\n", fragment);
  3243. break;
  3244. }
  3245. fwcode += wlen;
  3246. risc_addr += wlen;
  3247. risc_size -= wlen;
  3248. fragment++;
  3249. }
  3250. /* Next segment. */
  3251. seg++;
  3252. }
  3253. return rval;
  3254. fail_fw_integrity:
  3255. return QLA_FUNCTION_FAILED;
  3256. }
  3257. int
  3258. qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
  3259. {
  3260. int rval;
  3261. int segments, fragment;
  3262. uint32_t *dcode, dlen;
  3263. uint32_t risc_addr;
  3264. uint32_t risc_size;
  3265. uint32_t i;
  3266. struct fw_blob *blob;
  3267. uint32_t *fwcode, fwclen;
  3268. /* Load firmware blob. */
  3269. blob = qla2x00_request_firmware(ha);
  3270. if (!blob) {
  3271. qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
  3272. qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
  3273. "from: " QLA_FW_URL ".\n");
  3274. /* Try to load RISC code from flash. */
  3275. qla_printk(KERN_ERR, ha, "Attempting to load (potentially "
  3276. "outdated) firmware from flash.\n");
  3277. return qla24xx_load_risc_flash(ha, srisc_addr);
  3278. }
  3279. rval = QLA_SUCCESS;
  3280. segments = FA_RISC_CODE_SEGMENTS;
  3281. dcode = (uint32_t *)ha->request_ring;
  3282. *srisc_addr = 0;
  3283. fwcode = (uint32_t *)blob->fw->data;
  3284. fwclen = 0;
  3285. /* Validate firmware image by checking version. */
  3286. if (blob->fw->size < 8 * sizeof(uint32_t)) {
  3287. qla_printk(KERN_WARNING, ha,
  3288. "Unable to verify integrity of firmware image (%Zd)!\n",
  3289. blob->fw->size);
  3290. goto fail_fw_integrity;
  3291. }
  3292. for (i = 0; i < 4; i++)
  3293. dcode[i] = be32_to_cpu(fwcode[i + 4]);
  3294. if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
  3295. dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
  3296. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  3297. dcode[3] == 0)) {
  3298. qla_printk(KERN_WARNING, ha,
  3299. "Unable to verify integrity of firmware image!\n");
  3300. qla_printk(KERN_WARNING, ha,
  3301. "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
  3302. dcode[1], dcode[2], dcode[3]);
  3303. goto fail_fw_integrity;
  3304. }
  3305. while (segments && rval == QLA_SUCCESS) {
  3306. risc_addr = be32_to_cpu(fwcode[2]);
  3307. *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
  3308. risc_size = be32_to_cpu(fwcode[3]);
  3309. /* Validate firmware image size. */
  3310. fwclen += risc_size * sizeof(uint32_t);
  3311. if (blob->fw->size < fwclen) {
  3312. qla_printk(KERN_WARNING, ha,
  3313. "Unable to verify integrity of firmware image "
  3314. "(%Zd)!\n", blob->fw->size);
  3315. goto fail_fw_integrity;
  3316. }
  3317. fragment = 0;
  3318. while (risc_size > 0 && rval == QLA_SUCCESS) {
  3319. dlen = (uint32_t)(ha->fw_transfer_size >> 2);
  3320. if (dlen > risc_size)
  3321. dlen = risc_size;
  3322. DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
  3323. "addr %x, number of dwords 0x%x.\n", ha->host_no,
  3324. risc_addr, dlen));
  3325. for (i = 0; i < dlen; i++)
  3326. dcode[i] = swab32(fwcode[i]);
  3327. rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
  3328. dlen);
  3329. if (rval) {
  3330. DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
  3331. "segment %d of firmware\n", ha->host_no,
  3332. fragment));
  3333. qla_printk(KERN_WARNING, ha,
  3334. "[ERROR] Failed to load segment %d of "
  3335. "firmware\n", fragment);
  3336. break;
  3337. }
  3338. fwcode += dlen;
  3339. risc_addr += dlen;
  3340. risc_size -= dlen;
  3341. fragment++;
  3342. }
  3343. /* Next segment. */
  3344. segments--;
  3345. }
  3346. return rval;
  3347. fail_fw_integrity:
  3348. return QLA_FUNCTION_FAILED;
  3349. }
  3350. #endif