qla_init.c 107 KB

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