qla_init.c 107 KB

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