qla_init.c 104 KB

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