qla_init.c 99 KB

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