qla_init.c 100 KB

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