qla_init.c 96 KB

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