qla_init.c 104 KB

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