qla_init.c 100 KB

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