qla_init.c 100 KB

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