qla_init.c 99 KB

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