qla_init.c 96 KB

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