qla_init.c 99 KB

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