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. /****************************************************************************/
  37. /* QLogic ISP2x00 Hardware Support Functions. */
  38. /****************************************************************************/
  39. /*
  40. * qla2x00_initialize_adapter
  41. * Initialize board.
  42. *
  43. * Input:
  44. * ha = adapter block pointer.
  45. *
  46. * Returns:
  47. * 0 = success
  48. */
  49. int
  50. qla2x00_initialize_adapter(scsi_qla_host_t *ha)
  51. {
  52. int rval;
  53. uint8_t restart_risc = 0;
  54. uint8_t retry;
  55. uint32_t wait_time;
  56. /* Clear adapter flags. */
  57. ha->flags.online = 0;
  58. ha->flags.reset_active = 0;
  59. atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
  60. atomic_set(&ha->loop_state, LOOP_DOWN);
  61. ha->device_flags = 0;
  62. ha->dpc_flags = 0;
  63. ha->flags.management_server_logged_in = 0;
  64. ha->marker_needed = 0;
  65. ha->mbx_flags = 0;
  66. ha->isp_abort_cnt = 0;
  67. ha->beacon_blink_led = 0;
  68. set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
  69. qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
  70. rval = ha->isp_ops.pci_config(ha);
  71. if (rval) {
  72. DEBUG2(printk("scsi(%ld): Unable to configure PCI space=n",
  73. ha->host_no));
  74. return (rval);
  75. }
  76. ha->isp_ops.reset_chip(ha);
  77. qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
  78. ha->isp_ops.nvram_config(ha);
  79. if (ha->flags.disable_serdes) {
  80. /* Mask HBA via NVRAM settings? */
  81. qla_printk(KERN_INFO, ha, "Masking HBA WWPN "
  82. "%02x%02x%02x%02x%02x%02x%02x%02x (via NVRAM).\n",
  83. ha->port_name[0], ha->port_name[1],
  84. ha->port_name[2], ha->port_name[3],
  85. ha->port_name[4], ha->port_name[5],
  86. ha->port_name[6], ha->port_name[7]);
  87. return QLA_FUNCTION_FAILED;
  88. }
  89. qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
  90. retry = 10;
  91. /*
  92. * Try to configure the loop.
  93. */
  94. do {
  95. restart_risc = 0;
  96. /* If firmware needs to be loaded */
  97. if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
  98. if ((rval = ha->isp_ops.chip_diag(ha)) == QLA_SUCCESS) {
  99. rval = qla2x00_setup_chip(ha);
  100. }
  101. }
  102. if (rval == QLA_SUCCESS &&
  103. (rval = qla2x00_init_rings(ha)) == QLA_SUCCESS) {
  104. check_fw_ready_again:
  105. /*
  106. * Wait for a successful LIP up to a maximum
  107. * of (in seconds): RISC login timeout value,
  108. * RISC retry count value, and port down retry
  109. * value OR a minimum of 4 seconds OR If no
  110. * cable, only 5 seconds.
  111. */
  112. rval = qla2x00_fw_ready(ha);
  113. if (rval == QLA_SUCCESS) {
  114. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  115. /* Issue a marker after FW becomes ready. */
  116. qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
  117. /*
  118. * Wait at most MAX_TARGET RSCNs for a stable
  119. * link.
  120. */
  121. wait_time = 256;
  122. do {
  123. clear_bit(LOOP_RESYNC_NEEDED,
  124. &ha->dpc_flags);
  125. rval = qla2x00_configure_loop(ha);
  126. if (test_and_clear_bit(ISP_ABORT_NEEDED,
  127. &ha->dpc_flags)) {
  128. restart_risc = 1;
  129. break;
  130. }
  131. /*
  132. * If loop state change while we were
  133. * discoverying devices then wait for
  134. * LIP to complete
  135. */
  136. if (atomic_read(&ha->loop_state) !=
  137. LOOP_READY && retry--) {
  138. goto check_fw_ready_again;
  139. }
  140. wait_time--;
  141. } while (!atomic_read(&ha->loop_down_timer) &&
  142. retry &&
  143. wait_time &&
  144. (test_bit(LOOP_RESYNC_NEEDED,
  145. &ha->dpc_flags)));
  146. if (wait_time == 0)
  147. rval = QLA_FUNCTION_FAILED;
  148. } else if (ha->device_flags & DFLG_NO_CABLE)
  149. /* If no cable, then all is good. */
  150. rval = QLA_SUCCESS;
  151. }
  152. } while (restart_risc && retry--);
  153. if (rval == QLA_SUCCESS) {
  154. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  155. qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
  156. ha->marker_needed = 0;
  157. ha->flags.online = 1;
  158. } else {
  159. DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
  160. }
  161. return (rval);
  162. }
  163. /**
  164. * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
  165. * @ha: HA context
  166. *
  167. * Returns 0 on success.
  168. */
  169. int
  170. qla2100_pci_config(scsi_qla_host_t *ha)
  171. {
  172. uint16_t w, mwi;
  173. uint32_t d;
  174. unsigned long flags;
  175. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  176. pci_set_master(ha->pdev);
  177. mwi = 0;
  178. if (pci_set_mwi(ha->pdev))
  179. mwi = PCI_COMMAND_INVALIDATE;
  180. pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
  181. w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
  182. pci_write_config_word(ha->pdev, PCI_COMMAND, w);
  183. /* Reset expansion ROM address decode enable */
  184. pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
  185. d &= ~PCI_ROM_ADDRESS_ENABLE;
  186. pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
  187. /* Get PCI bus information. */
  188. spin_lock_irqsave(&ha->hardware_lock, flags);
  189. ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
  190. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  191. return QLA_SUCCESS;
  192. }
  193. /**
  194. * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
  195. * @ha: HA context
  196. *
  197. * Returns 0 on success.
  198. */
  199. int
  200. qla2300_pci_config(scsi_qla_host_t *ha)
  201. {
  202. uint16_t w, mwi;
  203. uint32_t d;
  204. unsigned long flags = 0;
  205. uint32_t cnt;
  206. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  207. pci_set_master(ha->pdev);
  208. mwi = 0;
  209. if (pci_set_mwi(ha->pdev))
  210. mwi = PCI_COMMAND_INVALIDATE;
  211. pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
  212. w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
  213. if (IS_QLA2322(ha) || IS_QLA6322(ha))
  214. w &= ~PCI_COMMAND_INTX_DISABLE;
  215. /*
  216. * If this is a 2300 card and not 2312, reset the
  217. * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
  218. * the 2310 also reports itself as a 2300 so we need to get the
  219. * fb revision level -- a 6 indicates it really is a 2300 and
  220. * not a 2310.
  221. */
  222. if (IS_QLA2300(ha)) {
  223. spin_lock_irqsave(&ha->hardware_lock, flags);
  224. /* Pause RISC. */
  225. WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
  226. for (cnt = 0; cnt < 30000; cnt++) {
  227. if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
  228. break;
  229. udelay(10);
  230. }
  231. /* Select FPM registers. */
  232. WRT_REG_WORD(&reg->ctrl_status, 0x20);
  233. RD_REG_WORD(&reg->ctrl_status);
  234. /* Get the fb rev level */
  235. ha->fb_rev = RD_FB_CMD_REG(ha, reg);
  236. if (ha->fb_rev == FPM_2300)
  237. w &= ~PCI_COMMAND_INVALIDATE;
  238. /* Deselect FPM registers. */
  239. WRT_REG_WORD(&reg->ctrl_status, 0x0);
  240. RD_REG_WORD(&reg->ctrl_status);
  241. /* Release RISC module. */
  242. WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
  243. for (cnt = 0; cnt < 30000; cnt++) {
  244. if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
  245. break;
  246. udelay(10);
  247. }
  248. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  249. }
  250. pci_write_config_word(ha->pdev, PCI_COMMAND, w);
  251. pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
  252. /* Reset expansion ROM address decode enable */
  253. pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
  254. d &= ~PCI_ROM_ADDRESS_ENABLE;
  255. pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
  256. /* Get PCI bus information. */
  257. spin_lock_irqsave(&ha->hardware_lock, flags);
  258. ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
  259. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  260. return QLA_SUCCESS;
  261. }
  262. /**
  263. * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
  264. * @ha: HA context
  265. *
  266. * Returns 0 on success.
  267. */
  268. int
  269. qla24xx_pci_config(scsi_qla_host_t *ha)
  270. {
  271. uint16_t w, mwi;
  272. uint32_t d;
  273. unsigned long flags = 0;
  274. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  275. int pcix_cmd_reg, pcie_dctl_reg;
  276. pci_set_master(ha->pdev);
  277. mwi = 0;
  278. if (pci_set_mwi(ha->pdev))
  279. mwi = PCI_COMMAND_INVALIDATE;
  280. pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
  281. w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
  282. w &= ~PCI_COMMAND_INTX_DISABLE;
  283. pci_write_config_word(ha->pdev, PCI_COMMAND, w);
  284. pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
  285. /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
  286. pcix_cmd_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX);
  287. if (pcix_cmd_reg) {
  288. uint16_t pcix_cmd;
  289. pcix_cmd_reg += PCI_X_CMD;
  290. pci_read_config_word(ha->pdev, pcix_cmd_reg, &pcix_cmd);
  291. pcix_cmd &= ~PCI_X_CMD_MAX_READ;
  292. pcix_cmd |= 0x0008;
  293. pci_write_config_word(ha->pdev, pcix_cmd_reg, pcix_cmd);
  294. }
  295. /* PCIe -- adjust Maximum Read Request Size (2048). */
  296. pcie_dctl_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
  297. if (pcie_dctl_reg) {
  298. uint16_t pcie_dctl;
  299. pcie_dctl_reg += PCI_EXP_DEVCTL;
  300. pci_read_config_word(ha->pdev, pcie_dctl_reg, &pcie_dctl);
  301. pcie_dctl &= ~PCI_EXP_DEVCTL_READRQ;
  302. pcie_dctl |= 0x4000;
  303. pci_write_config_word(ha->pdev, pcie_dctl_reg, pcie_dctl);
  304. }
  305. /* Reset expansion ROM address decode enable */
  306. pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
  307. d &= ~PCI_ROM_ADDRESS_ENABLE;
  308. pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
  309. /* Get PCI bus information. */
  310. spin_lock_irqsave(&ha->hardware_lock, flags);
  311. ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
  312. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  313. return QLA_SUCCESS;
  314. }
  315. /**
  316. * qla2x00_isp_firmware() - Choose firmware image.
  317. * @ha: HA context
  318. *
  319. * Returns 0 on success.
  320. */
  321. static int
  322. qla2x00_isp_firmware(scsi_qla_host_t *ha)
  323. {
  324. int rval;
  325. /* Assume loading risc code */
  326. rval = QLA_FUNCTION_FAILED;
  327. if (ha->flags.disable_risc_code_load) {
  328. DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
  329. ha->host_no));
  330. qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
  331. /* Verify checksum of loaded RISC code. */
  332. rval = qla2x00_verify_checksum(ha, ha->fw_srisc_address);
  333. }
  334. if (rval) {
  335. DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
  336. ha->host_no));
  337. }
  338. return (rval);
  339. }
  340. /**
  341. * qla2x00_reset_chip() - Reset ISP chip.
  342. * @ha: HA context
  343. *
  344. * Returns 0 on success.
  345. */
  346. void
  347. qla2x00_reset_chip(scsi_qla_host_t *ha)
  348. {
  349. unsigned long flags = 0;
  350. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  351. uint32_t cnt;
  352. uint16_t cmd;
  353. ha->isp_ops.disable_intrs(ha);
  354. spin_lock_irqsave(&ha->hardware_lock, flags);
  355. /* Turn off master enable */
  356. cmd = 0;
  357. pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
  358. cmd &= ~PCI_COMMAND_MASTER;
  359. pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
  360. if (!IS_QLA2100(ha)) {
  361. /* Pause RISC. */
  362. WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
  363. if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
  364. for (cnt = 0; cnt < 30000; cnt++) {
  365. if ((RD_REG_WORD(&reg->hccr) &
  366. HCCR_RISC_PAUSE) != 0)
  367. break;
  368. udelay(100);
  369. }
  370. } else {
  371. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  372. udelay(10);
  373. }
  374. /* Select FPM registers. */
  375. WRT_REG_WORD(&reg->ctrl_status, 0x20);
  376. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  377. /* FPM Soft Reset. */
  378. WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
  379. RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
  380. /* Toggle Fpm Reset. */
  381. if (!IS_QLA2200(ha)) {
  382. WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
  383. RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
  384. }
  385. /* Select frame buffer registers. */
  386. WRT_REG_WORD(&reg->ctrl_status, 0x10);
  387. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  388. /* Reset frame buffer FIFOs. */
  389. if (IS_QLA2200(ha)) {
  390. WRT_FB_CMD_REG(ha, reg, 0xa000);
  391. RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
  392. } else {
  393. WRT_FB_CMD_REG(ha, reg, 0x00fc);
  394. /* Read back fb_cmd until zero or 3 seconds max */
  395. for (cnt = 0; cnt < 3000; cnt++) {
  396. if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
  397. break;
  398. udelay(100);
  399. }
  400. }
  401. /* Select RISC module registers. */
  402. WRT_REG_WORD(&reg->ctrl_status, 0);
  403. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  404. /* Reset RISC processor. */
  405. WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
  406. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  407. /* Release RISC processor. */
  408. WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
  409. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  410. }
  411. WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
  412. WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
  413. /* Reset ISP chip. */
  414. WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
  415. /* Wait for RISC to recover from reset. */
  416. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  417. /*
  418. * It is necessary to for a delay here since the card doesn't
  419. * respond to PCI reads during a reset. On some architectures
  420. * this will result in an MCA.
  421. */
  422. udelay(20);
  423. for (cnt = 30000; cnt; cnt--) {
  424. if ((RD_REG_WORD(&reg->ctrl_status) &
  425. CSR_ISP_SOFT_RESET) == 0)
  426. break;
  427. udelay(100);
  428. }
  429. } else
  430. udelay(10);
  431. /* Reset RISC processor. */
  432. WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
  433. WRT_REG_WORD(&reg->semaphore, 0);
  434. /* Release RISC processor. */
  435. WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
  436. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  437. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  438. for (cnt = 0; cnt < 30000; cnt++) {
  439. if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
  440. break;
  441. udelay(100);
  442. }
  443. } else
  444. udelay(100);
  445. /* Turn on master enable */
  446. cmd |= PCI_COMMAND_MASTER;
  447. pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
  448. /* Disable RISC pause on FPM parity error. */
  449. if (!IS_QLA2100(ha)) {
  450. WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
  451. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  452. }
  453. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  454. }
  455. /**
  456. * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
  457. * @ha: HA context
  458. *
  459. * Returns 0 on success.
  460. */
  461. static inline void
  462. qla24xx_reset_risc(scsi_qla_host_t *ha)
  463. {
  464. unsigned long flags = 0;
  465. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  466. uint32_t cnt, d2;
  467. uint16_t wd;
  468. spin_lock_irqsave(&ha->hardware_lock, flags);
  469. /* Reset RISC. */
  470. WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
  471. for (cnt = 0; cnt < 30000; cnt++) {
  472. if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
  473. break;
  474. udelay(10);
  475. }
  476. WRT_REG_DWORD(&reg->ctrl_status,
  477. CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
  478. pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
  479. udelay(100);
  480. /* Wait for firmware to complete NVRAM accesses. */
  481. d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
  482. for (cnt = 10000 ; cnt && d2; cnt--) {
  483. udelay(5);
  484. d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
  485. barrier();
  486. }
  487. /* Wait for soft-reset to complete. */
  488. d2 = RD_REG_DWORD(&reg->ctrl_status);
  489. for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
  490. udelay(5);
  491. d2 = RD_REG_DWORD(&reg->ctrl_status);
  492. barrier();
  493. }
  494. WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
  495. RD_REG_DWORD(&reg->hccr);
  496. WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
  497. RD_REG_DWORD(&reg->hccr);
  498. WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
  499. RD_REG_DWORD(&reg->hccr);
  500. d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
  501. for (cnt = 6000000 ; cnt && d2; cnt--) {
  502. udelay(5);
  503. d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
  504. barrier();
  505. }
  506. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  507. }
  508. /**
  509. * qla24xx_reset_chip() - Reset ISP24xx chip.
  510. * @ha: HA context
  511. *
  512. * Returns 0 on success.
  513. */
  514. void
  515. qla24xx_reset_chip(scsi_qla_host_t *ha)
  516. {
  517. ha->isp_ops.disable_intrs(ha);
  518. /* Perform RISC reset. */
  519. qla24xx_reset_risc(ha);
  520. }
  521. /**
  522. * qla2x00_chip_diag() - Test chip for proper operation.
  523. * @ha: HA context
  524. *
  525. * Returns 0 on success.
  526. */
  527. int
  528. qla2x00_chip_diag(scsi_qla_host_t *ha)
  529. {
  530. int rval;
  531. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  532. unsigned long flags = 0;
  533. uint16_t data;
  534. uint32_t cnt;
  535. uint16_t mb[5];
  536. /* Assume a failed state */
  537. rval = QLA_FUNCTION_FAILED;
  538. DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
  539. ha->host_no, (u_long)&reg->flash_address));
  540. spin_lock_irqsave(&ha->hardware_lock, flags);
  541. /* Reset ISP chip. */
  542. WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
  543. /*
  544. * We need to have a delay here since the card will not respond while
  545. * in reset causing an MCA on some architectures.
  546. */
  547. udelay(20);
  548. data = qla2x00_debounce_register(&reg->ctrl_status);
  549. for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
  550. udelay(5);
  551. data = RD_REG_WORD(&reg->ctrl_status);
  552. barrier();
  553. }
  554. if (!cnt)
  555. goto chip_diag_failed;
  556. DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
  557. ha->host_no));
  558. /* Reset RISC processor. */
  559. WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
  560. WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
  561. /* Workaround for QLA2312 PCI parity error */
  562. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  563. data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
  564. for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
  565. udelay(5);
  566. data = RD_MAILBOX_REG(ha, reg, 0);
  567. barrier();
  568. }
  569. } else
  570. udelay(10);
  571. if (!cnt)
  572. goto chip_diag_failed;
  573. /* Check product ID of chip */
  574. DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
  575. mb[1] = RD_MAILBOX_REG(ha, reg, 1);
  576. mb[2] = RD_MAILBOX_REG(ha, reg, 2);
  577. mb[3] = RD_MAILBOX_REG(ha, reg, 3);
  578. mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
  579. if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
  580. mb[3] != PROD_ID_3) {
  581. qla_printk(KERN_WARNING, ha,
  582. "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
  583. goto chip_diag_failed;
  584. }
  585. ha->product_id[0] = mb[1];
  586. ha->product_id[1] = mb[2];
  587. ha->product_id[2] = mb[3];
  588. ha->product_id[3] = mb[4];
  589. /* Adjust fw RISC transfer size */
  590. if (ha->request_q_length > 1024)
  591. ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
  592. else
  593. ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
  594. ha->request_q_length;
  595. if (IS_QLA2200(ha) &&
  596. RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
  597. /* Limit firmware transfer size with a 2200A */
  598. DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
  599. ha->host_no));
  600. ha->device_type |= DT_ISP2200A;
  601. ha->fw_transfer_size = 128;
  602. }
  603. /* Wrap Incoming Mailboxes Test. */
  604. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  605. DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
  606. rval = qla2x00_mbx_reg_test(ha);
  607. if (rval) {
  608. DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
  609. ha->host_no));
  610. qla_printk(KERN_WARNING, ha,
  611. "Failed mailbox send register test\n");
  612. }
  613. else {
  614. /* Flag a successful rval */
  615. rval = QLA_SUCCESS;
  616. }
  617. spin_lock_irqsave(&ha->hardware_lock, flags);
  618. chip_diag_failed:
  619. if (rval)
  620. DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
  621. "****\n", ha->host_no));
  622. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  623. return (rval);
  624. }
  625. /**
  626. * qla24xx_chip_diag() - Test ISP24xx for proper operation.
  627. * @ha: HA context
  628. *
  629. * Returns 0 on success.
  630. */
  631. int
  632. qla24xx_chip_diag(scsi_qla_host_t *ha)
  633. {
  634. int rval;
  635. /* Perform RISC reset. */
  636. qla24xx_reset_risc(ha);
  637. ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
  638. rval = qla2x00_mbx_reg_test(ha);
  639. if (rval) {
  640. DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
  641. ha->host_no));
  642. qla_printk(KERN_WARNING, ha,
  643. "Failed mailbox send register test\n");
  644. } else {
  645. /* Flag a successful rval */
  646. rval = QLA_SUCCESS;
  647. }
  648. return rval;
  649. }
  650. void
  651. qla2x00_alloc_fw_dump(scsi_qla_host_t *ha)
  652. {
  653. int rval;
  654. uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
  655. eft_size;
  656. dma_addr_t eft_dma;
  657. void *eft;
  658. if (ha->fw_dump) {
  659. qla_printk(KERN_WARNING, ha,
  660. "Firmware dump previously allocated.\n");
  661. return;
  662. }
  663. ha->fw_dumped = 0;
  664. fixed_size = mem_size = eft_size = 0;
  665. if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
  666. fixed_size = sizeof(struct qla2100_fw_dump);
  667. } else if (IS_QLA23XX(ha)) {
  668. fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
  669. mem_size = (ha->fw_memory_size - 0x11000 + 1) *
  670. sizeof(uint16_t);
  671. } else if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
  672. fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
  673. mem_size = (ha->fw_memory_size - 0x100000 + 1) *
  674. sizeof(uint32_t);
  675. /* Allocate memory for Extended Trace Buffer. */
  676. eft = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &eft_dma,
  677. GFP_KERNEL);
  678. if (!eft) {
  679. qla_printk(KERN_WARNING, ha, "Unable to allocate "
  680. "(%d KB) for EFT.\n", EFT_SIZE / 1024);
  681. goto cont_alloc;
  682. }
  683. rval = qla2x00_trace_control(ha, TC_ENABLE, eft_dma,
  684. EFT_NUM_BUFFERS);
  685. if (rval) {
  686. qla_printk(KERN_WARNING, ha, "Unable to initialize "
  687. "EFT (%d).\n", rval);
  688. dma_free_coherent(&ha->pdev->dev, EFT_SIZE, eft,
  689. eft_dma);
  690. goto cont_alloc;
  691. }
  692. qla_printk(KERN_INFO, ha, "Allocated (%d KB) for EFT...\n",
  693. EFT_SIZE / 1024);
  694. eft_size = EFT_SIZE;
  695. memset(eft, 0, eft_size);
  696. ha->eft_dma = eft_dma;
  697. ha->eft = eft;
  698. }
  699. cont_alloc:
  700. req_q_size = ha->request_q_length * sizeof(request_t);
  701. rsp_q_size = ha->response_q_length * sizeof(response_t);
  702. dump_size = offsetof(struct qla2xxx_fw_dump, isp);
  703. dump_size += fixed_size + mem_size + req_q_size + rsp_q_size +
  704. eft_size;
  705. ha->fw_dump = vmalloc(dump_size);
  706. if (!ha->fw_dump) {
  707. qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
  708. "firmware dump!!!\n", dump_size / 1024);
  709. if (ha->eft) {
  710. dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
  711. ha->eft_dma);
  712. ha->eft = NULL;
  713. ha->eft_dma = 0;
  714. }
  715. return;
  716. }
  717. qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware dump...\n",
  718. dump_size / 1024);
  719. ha->fw_dump_len = dump_size;
  720. ha->fw_dump->signature[0] = 'Q';
  721. ha->fw_dump->signature[1] = 'L';
  722. ha->fw_dump->signature[2] = 'G';
  723. ha->fw_dump->signature[3] = 'C';
  724. ha->fw_dump->version = __constant_htonl(1);
  725. ha->fw_dump->fixed_size = htonl(fixed_size);
  726. ha->fw_dump->mem_size = htonl(mem_size);
  727. ha->fw_dump->req_q_size = htonl(req_q_size);
  728. ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
  729. ha->fw_dump->eft_size = htonl(eft_size);
  730. ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
  731. ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
  732. ha->fw_dump->header_size =
  733. htonl(offsetof(struct qla2xxx_fw_dump, isp));
  734. }
  735. /**
  736. * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
  737. * @ha: HA context
  738. *
  739. * Returns 0 on success.
  740. */
  741. static void
  742. qla2x00_resize_request_q(scsi_qla_host_t *ha)
  743. {
  744. int rval;
  745. uint16_t fw_iocb_cnt = 0;
  746. uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
  747. dma_addr_t request_dma;
  748. request_t *request_ring;
  749. /* Valid only on recent ISPs. */
  750. if (IS_QLA2100(ha) || IS_QLA2200(ha))
  751. return;
  752. /* Retrieve IOCB counts available to the firmware. */
  753. rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt);
  754. if (rval)
  755. return;
  756. /* No point in continuing if current settings are sufficient. */
  757. if (fw_iocb_cnt < 1024)
  758. return;
  759. if (ha->request_q_length >= request_q_length)
  760. return;
  761. /* Attempt to claim larger area for request queue. */
  762. request_ring = dma_alloc_coherent(&ha->pdev->dev,
  763. (request_q_length + 1) * sizeof(request_t), &request_dma,
  764. GFP_KERNEL);
  765. if (request_ring == NULL)
  766. return;
  767. /* Resize successful, report extensions. */
  768. qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
  769. (ha->fw_memory_size + 1) / 1024);
  770. qla_printk(KERN_INFO, ha, "Resizing request queue depth "
  771. "(%d -> %d)...\n", ha->request_q_length, request_q_length);
  772. /* Clear old allocations. */
  773. dma_free_coherent(&ha->pdev->dev,
  774. (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
  775. ha->request_dma);
  776. /* Begin using larger queue. */
  777. ha->request_q_length = request_q_length;
  778. ha->request_ring = request_ring;
  779. ha->request_dma = request_dma;
  780. }
  781. /**
  782. * qla2x00_setup_chip() - Load and start RISC firmware.
  783. * @ha: HA context
  784. *
  785. * Returns 0 on success.
  786. */
  787. static int
  788. qla2x00_setup_chip(scsi_qla_host_t *ha)
  789. {
  790. int rval;
  791. uint32_t srisc_address = 0;
  792. /* Load firmware sequences */
  793. rval = ha->isp_ops.load_risc(ha, &srisc_address);
  794. if (rval == QLA_SUCCESS) {
  795. DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
  796. "code.\n", ha->host_no));
  797. rval = qla2x00_verify_checksum(ha, srisc_address);
  798. if (rval == QLA_SUCCESS) {
  799. /* Start firmware execution. */
  800. DEBUG(printk("scsi(%ld): Checksum OK, start "
  801. "firmware.\n", ha->host_no));
  802. rval = qla2x00_execute_fw(ha, srisc_address);
  803. /* Retrieve firmware information. */
  804. if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
  805. qla2x00_get_fw_version(ha,
  806. &ha->fw_major_version,
  807. &ha->fw_minor_version,
  808. &ha->fw_subminor_version,
  809. &ha->fw_attributes, &ha->fw_memory_size);
  810. qla2x00_resize_request_q(ha);
  811. if (ql2xallocfwdump)
  812. qla2x00_alloc_fw_dump(ha);
  813. }
  814. } else {
  815. DEBUG2(printk(KERN_INFO
  816. "scsi(%ld): ISP Firmware failed checksum.\n",
  817. ha->host_no));
  818. }
  819. }
  820. if (rval) {
  821. DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
  822. ha->host_no));
  823. }
  824. return (rval);
  825. }
  826. /**
  827. * qla2x00_init_response_q_entries() - Initializes response queue entries.
  828. * @ha: HA context
  829. *
  830. * Beginning of request ring has initialization control block already built
  831. * by nvram config routine.
  832. *
  833. * Returns 0 on success.
  834. */
  835. static void
  836. qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
  837. {
  838. uint16_t cnt;
  839. response_t *pkt;
  840. pkt = ha->response_ring_ptr;
  841. for (cnt = 0; cnt < ha->response_q_length; cnt++) {
  842. pkt->signature = RESPONSE_PROCESSED;
  843. pkt++;
  844. }
  845. }
  846. /**
  847. * qla2x00_update_fw_options() - Read and process firmware options.
  848. * @ha: HA context
  849. *
  850. * Returns 0 on success.
  851. */
  852. void
  853. qla2x00_update_fw_options(scsi_qla_host_t *ha)
  854. {
  855. uint16_t swing, emphasis, tx_sens, rx_sens;
  856. memset(ha->fw_options, 0, sizeof(ha->fw_options));
  857. qla2x00_get_fw_options(ha, ha->fw_options);
  858. if (IS_QLA2100(ha) || IS_QLA2200(ha))
  859. return;
  860. /* Serial Link options. */
  861. DEBUG3(printk("scsi(%ld): Serial link options:\n",
  862. ha->host_no));
  863. DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
  864. sizeof(ha->fw_seriallink_options)));
  865. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  866. if (ha->fw_seriallink_options[3] & BIT_2) {
  867. ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
  868. /* 1G settings */
  869. swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
  870. emphasis = (ha->fw_seriallink_options[2] &
  871. (BIT_4 | BIT_3)) >> 3;
  872. tx_sens = ha->fw_seriallink_options[0] &
  873. (BIT_3 | BIT_2 | BIT_1 | BIT_0);
  874. rx_sens = (ha->fw_seriallink_options[0] &
  875. (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
  876. ha->fw_options[10] = (emphasis << 14) | (swing << 8);
  877. if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
  878. if (rx_sens == 0x0)
  879. rx_sens = 0x3;
  880. ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
  881. } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
  882. ha->fw_options[10] |= BIT_5 |
  883. ((rx_sens & (BIT_1 | BIT_0)) << 2) |
  884. (tx_sens & (BIT_1 | BIT_0));
  885. /* 2G settings */
  886. swing = (ha->fw_seriallink_options[2] &
  887. (BIT_7 | BIT_6 | BIT_5)) >> 5;
  888. emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
  889. tx_sens = ha->fw_seriallink_options[1] &
  890. (BIT_3 | BIT_2 | BIT_1 | BIT_0);
  891. rx_sens = (ha->fw_seriallink_options[1] &
  892. (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
  893. ha->fw_options[11] = (emphasis << 14) | (swing << 8);
  894. if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
  895. if (rx_sens == 0x0)
  896. rx_sens = 0x3;
  897. ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
  898. } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
  899. ha->fw_options[11] |= BIT_5 |
  900. ((rx_sens & (BIT_1 | BIT_0)) << 2) |
  901. (tx_sens & (BIT_1 | BIT_0));
  902. }
  903. /* FCP2 options. */
  904. /* Return command IOCBs without waiting for an ABTS to complete. */
  905. ha->fw_options[3] |= BIT_13;
  906. /* LED scheme. */
  907. if (ha->flags.enable_led_scheme)
  908. ha->fw_options[2] |= BIT_12;
  909. /* Detect ISP6312. */
  910. if (IS_QLA6312(ha))
  911. ha->fw_options[2] |= BIT_13;
  912. /* Update firmware options. */
  913. qla2x00_set_fw_options(ha, ha->fw_options);
  914. }
  915. void
  916. qla24xx_update_fw_options(scsi_qla_host_t *ha)
  917. {
  918. int rval;
  919. /* Update Serial Link options. */
  920. if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
  921. return;
  922. rval = qla2x00_set_serdes_params(ha,
  923. le16_to_cpu(ha->fw_seriallink_options24[1]),
  924. le16_to_cpu(ha->fw_seriallink_options24[2]),
  925. le16_to_cpu(ha->fw_seriallink_options24[3]));
  926. if (rval != QLA_SUCCESS) {
  927. qla_printk(KERN_WARNING, ha,
  928. "Unable to update Serial Link options (%x).\n", rval);
  929. }
  930. }
  931. void
  932. qla2x00_config_rings(struct scsi_qla_host *ha)
  933. {
  934. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  935. /* Setup ring parameters in initialization control block. */
  936. ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
  937. ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
  938. ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
  939. ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
  940. ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
  941. ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
  942. ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
  943. ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
  944. WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
  945. WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
  946. WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
  947. WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
  948. RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
  949. }
  950. void
  951. qla24xx_config_rings(struct scsi_qla_host *ha)
  952. {
  953. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  954. struct init_cb_24xx *icb;
  955. /* Setup ring parameters in initialization control block. */
  956. icb = (struct init_cb_24xx *)ha->init_cb;
  957. icb->request_q_outpointer = __constant_cpu_to_le16(0);
  958. icb->response_q_inpointer = __constant_cpu_to_le16(0);
  959. icb->request_q_length = cpu_to_le16(ha->request_q_length);
  960. icb->response_q_length = cpu_to_le16(ha->response_q_length);
  961. icb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
  962. icb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
  963. icb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
  964. icb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
  965. WRT_REG_DWORD(&reg->req_q_in, 0);
  966. WRT_REG_DWORD(&reg->req_q_out, 0);
  967. WRT_REG_DWORD(&reg->rsp_q_in, 0);
  968. WRT_REG_DWORD(&reg->rsp_q_out, 0);
  969. RD_REG_DWORD(&reg->rsp_q_out);
  970. }
  971. /**
  972. * qla2x00_init_rings() - Initializes firmware.
  973. * @ha: HA context
  974. *
  975. * Beginning of request ring has initialization control block already built
  976. * by nvram config routine.
  977. *
  978. * Returns 0 on success.
  979. */
  980. static int
  981. qla2x00_init_rings(scsi_qla_host_t *ha)
  982. {
  983. int rval;
  984. unsigned long flags = 0;
  985. int cnt;
  986. spin_lock_irqsave(&ha->hardware_lock, flags);
  987. /* Clear outstanding commands array. */
  988. for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
  989. ha->outstanding_cmds[cnt] = NULL;
  990. ha->current_outstanding_cmd = 0;
  991. /* Clear RSCN queue. */
  992. ha->rscn_in_ptr = 0;
  993. ha->rscn_out_ptr = 0;
  994. /* Initialize firmware. */
  995. ha->request_ring_ptr = ha->request_ring;
  996. ha->req_ring_index = 0;
  997. ha->req_q_cnt = ha->request_q_length;
  998. ha->response_ring_ptr = ha->response_ring;
  999. ha->rsp_ring_index = 0;
  1000. /* Initialize response queue entries */
  1001. qla2x00_init_response_q_entries(ha);
  1002. ha->isp_ops.config_rings(ha);
  1003. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1004. /* Update any ISP specific firmware options before initialization. */
  1005. ha->isp_ops.update_fw_options(ha);
  1006. DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
  1007. rval = qla2x00_init_firmware(ha, ha->init_cb_size);
  1008. if (rval) {
  1009. DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
  1010. ha->host_no));
  1011. } else {
  1012. DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
  1013. ha->host_no));
  1014. }
  1015. return (rval);
  1016. }
  1017. /**
  1018. * qla2x00_fw_ready() - Waits for firmware ready.
  1019. * @ha: HA context
  1020. *
  1021. * Returns 0 on success.
  1022. */
  1023. static int
  1024. qla2x00_fw_ready(scsi_qla_host_t *ha)
  1025. {
  1026. int rval;
  1027. unsigned long wtime, mtime;
  1028. uint16_t min_wait; /* Minimum wait time if loop is down */
  1029. uint16_t wait_time; /* Wait time if loop is coming ready */
  1030. uint16_t fw_state;
  1031. rval = QLA_SUCCESS;
  1032. /* 20 seconds for loop down. */
  1033. min_wait = 20;
  1034. /*
  1035. * Firmware should take at most one RATOV to login, plus 5 seconds for
  1036. * our own processing.
  1037. */
  1038. if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
  1039. wait_time = min_wait;
  1040. }
  1041. /* Min wait time if loop down */
  1042. mtime = jiffies + (min_wait * HZ);
  1043. /* wait time before firmware ready */
  1044. wtime = jiffies + (wait_time * HZ);
  1045. /* Wait for ISP to finish LIP */
  1046. if (!ha->flags.init_done)
  1047. qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
  1048. DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
  1049. ha->host_no));
  1050. do {
  1051. rval = qla2x00_get_firmware_state(ha, &fw_state);
  1052. if (rval == QLA_SUCCESS) {
  1053. if (fw_state < FSTATE_LOSS_OF_SYNC) {
  1054. ha->device_flags &= ~DFLG_NO_CABLE;
  1055. }
  1056. if (fw_state == FSTATE_READY) {
  1057. DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
  1058. ha->host_no));
  1059. qla2x00_get_retry_cnt(ha, &ha->retry_count,
  1060. &ha->login_timeout, &ha->r_a_tov);
  1061. rval = QLA_SUCCESS;
  1062. break;
  1063. }
  1064. rval = QLA_FUNCTION_FAILED;
  1065. if (atomic_read(&ha->loop_down_timer) &&
  1066. fw_state != FSTATE_READY) {
  1067. /* Loop down. Timeout on min_wait for states
  1068. * other than Wait for Login.
  1069. */
  1070. if (time_after_eq(jiffies, mtime)) {
  1071. qla_printk(KERN_INFO, ha,
  1072. "Cable is unplugged...\n");
  1073. ha->device_flags |= DFLG_NO_CABLE;
  1074. break;
  1075. }
  1076. }
  1077. } else {
  1078. /* Mailbox cmd failed. Timeout on min_wait. */
  1079. if (time_after_eq(jiffies, mtime))
  1080. break;
  1081. }
  1082. if (time_after_eq(jiffies, wtime))
  1083. break;
  1084. /* Delay for a while */
  1085. msleep(500);
  1086. DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
  1087. ha->host_no, fw_state, jiffies));
  1088. } while (1);
  1089. DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
  1090. ha->host_no, fw_state, jiffies));
  1091. if (rval) {
  1092. DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
  1093. ha->host_no));
  1094. }
  1095. return (rval);
  1096. }
  1097. /*
  1098. * qla2x00_configure_hba
  1099. * Setup adapter context.
  1100. *
  1101. * Input:
  1102. * ha = adapter state pointer.
  1103. *
  1104. * Returns:
  1105. * 0 = success
  1106. *
  1107. * Context:
  1108. * Kernel context.
  1109. */
  1110. static int
  1111. qla2x00_configure_hba(scsi_qla_host_t *ha)
  1112. {
  1113. int rval;
  1114. uint16_t loop_id;
  1115. uint16_t topo;
  1116. uint8_t al_pa;
  1117. uint8_t area;
  1118. uint8_t domain;
  1119. char connect_type[22];
  1120. /* Get host addresses. */
  1121. rval = qla2x00_get_adapter_id(ha,
  1122. &loop_id, &al_pa, &area, &domain, &topo);
  1123. if (rval != QLA_SUCCESS) {
  1124. if (LOOP_TRANSITION(ha) || atomic_read(&ha->loop_down_timer) ||
  1125. (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
  1126. DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
  1127. __func__, ha->host_no));
  1128. } else {
  1129. qla_printk(KERN_WARNING, ha,
  1130. "ERROR -- Unable to get host loop ID.\n");
  1131. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  1132. }
  1133. return (rval);
  1134. }
  1135. if (topo == 4) {
  1136. qla_printk(KERN_INFO, ha,
  1137. "Cannot get topology - retrying.\n");
  1138. return (QLA_FUNCTION_FAILED);
  1139. }
  1140. ha->loop_id = loop_id;
  1141. /* initialize */
  1142. ha->min_external_loopid = SNS_FIRST_LOOP_ID;
  1143. ha->operating_mode = LOOP;
  1144. switch (topo) {
  1145. case 0:
  1146. DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
  1147. ha->host_no));
  1148. ha->current_topology = ISP_CFG_NL;
  1149. strcpy(connect_type, "(Loop)");
  1150. break;
  1151. case 1:
  1152. DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
  1153. ha->host_no));
  1154. ha->current_topology = ISP_CFG_FL;
  1155. strcpy(connect_type, "(FL_Port)");
  1156. break;
  1157. case 2:
  1158. DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
  1159. ha->host_no));
  1160. ha->operating_mode = P2P;
  1161. ha->current_topology = ISP_CFG_N;
  1162. strcpy(connect_type, "(N_Port-to-N_Port)");
  1163. break;
  1164. case 3:
  1165. DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
  1166. ha->host_no));
  1167. ha->operating_mode = P2P;
  1168. ha->current_topology = ISP_CFG_F;
  1169. strcpy(connect_type, "(F_Port)");
  1170. break;
  1171. default:
  1172. DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
  1173. "Using NL.\n",
  1174. ha->host_no, topo));
  1175. ha->current_topology = ISP_CFG_NL;
  1176. strcpy(connect_type, "(Loop)");
  1177. break;
  1178. }
  1179. /* Save Host port and loop ID. */
  1180. /* byte order - Big Endian */
  1181. ha->d_id.b.domain = domain;
  1182. ha->d_id.b.area = area;
  1183. ha->d_id.b.al_pa = al_pa;
  1184. if (!ha->flags.init_done)
  1185. qla_printk(KERN_INFO, ha,
  1186. "Topology - %s, Host Loop address 0x%x\n",
  1187. connect_type, ha->loop_id);
  1188. if (rval) {
  1189. DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
  1190. } else {
  1191. DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
  1192. }
  1193. return(rval);
  1194. }
  1195. /*
  1196. * NVRAM configuration for ISP 2xxx
  1197. *
  1198. * Input:
  1199. * ha = adapter block pointer.
  1200. *
  1201. * Output:
  1202. * initialization control block in response_ring
  1203. * host adapters parameters in host adapter block
  1204. *
  1205. * Returns:
  1206. * 0 = success.
  1207. */
  1208. int
  1209. qla2x00_nvram_config(scsi_qla_host_t *ha)
  1210. {
  1211. int rval;
  1212. uint8_t chksum = 0;
  1213. uint16_t cnt;
  1214. uint8_t *dptr1, *dptr2;
  1215. init_cb_t *icb = ha->init_cb;
  1216. nvram_t *nv = (nvram_t *)ha->request_ring;
  1217. uint8_t *ptr = (uint8_t *)ha->request_ring;
  1218. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1219. rval = QLA_SUCCESS;
  1220. /* Determine NVRAM starting address. */
  1221. ha->nvram_size = sizeof(nvram_t);
  1222. ha->nvram_base = 0;
  1223. if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
  1224. if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
  1225. ha->nvram_base = 0x80;
  1226. /* Get NVRAM data and calculate checksum. */
  1227. ha->isp_ops.read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
  1228. for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
  1229. chksum += *ptr++;
  1230. DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
  1231. DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
  1232. ha->nvram_size));
  1233. /* Bad NVRAM data, set defaults parameters. */
  1234. if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
  1235. nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
  1236. /* Reset NVRAM data. */
  1237. qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
  1238. "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
  1239. nv->nvram_version);
  1240. qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
  1241. "invalid -- WWPN) defaults.\n");
  1242. /*
  1243. * Set default initialization control block.
  1244. */
  1245. memset(nv, 0, ha->nvram_size);
  1246. nv->parameter_block_version = ICB_VERSION;
  1247. if (IS_QLA23XX(ha)) {
  1248. nv->firmware_options[0] = BIT_2 | BIT_1;
  1249. nv->firmware_options[1] = BIT_7 | BIT_5;
  1250. nv->add_firmware_options[0] = BIT_5;
  1251. nv->add_firmware_options[1] = BIT_5 | BIT_4;
  1252. nv->frame_payload_size = __constant_cpu_to_le16(2048);
  1253. nv->special_options[1] = BIT_7;
  1254. } else if (IS_QLA2200(ha)) {
  1255. nv->firmware_options[0] = BIT_2 | BIT_1;
  1256. nv->firmware_options[1] = BIT_7 | BIT_5;
  1257. nv->add_firmware_options[0] = BIT_5;
  1258. nv->add_firmware_options[1] = BIT_5 | BIT_4;
  1259. nv->frame_payload_size = __constant_cpu_to_le16(1024);
  1260. } else if (IS_QLA2100(ha)) {
  1261. nv->firmware_options[0] = BIT_3 | BIT_1;
  1262. nv->firmware_options[1] = BIT_5;
  1263. nv->frame_payload_size = __constant_cpu_to_le16(1024);
  1264. }
  1265. nv->max_iocb_allocation = __constant_cpu_to_le16(256);
  1266. nv->execution_throttle = __constant_cpu_to_le16(16);
  1267. nv->retry_count = 8;
  1268. nv->retry_delay = 1;
  1269. nv->port_name[0] = 33;
  1270. nv->port_name[3] = 224;
  1271. nv->port_name[4] = 139;
  1272. nv->login_timeout = 4;
  1273. /*
  1274. * Set default host adapter parameters
  1275. */
  1276. nv->host_p[1] = BIT_2;
  1277. nv->reset_delay = 5;
  1278. nv->port_down_retry_count = 8;
  1279. nv->max_luns_per_target = __constant_cpu_to_le16(8);
  1280. nv->link_down_timeout = 60;
  1281. rval = 1;
  1282. }
  1283. #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
  1284. /*
  1285. * The SN2 does not provide BIOS emulation which means you can't change
  1286. * potentially bogus BIOS settings. Force the use of default settings
  1287. * for link rate and frame size. Hope that the rest of the settings
  1288. * are valid.
  1289. */
  1290. if (ia64_platform_is("sn2")) {
  1291. nv->frame_payload_size = __constant_cpu_to_le16(2048);
  1292. if (IS_QLA23XX(ha))
  1293. nv->special_options[1] = BIT_7;
  1294. }
  1295. #endif
  1296. /* Reset Initialization control block */
  1297. memset(icb, 0, ha->init_cb_size);
  1298. /*
  1299. * Setup driver NVRAM options.
  1300. */
  1301. nv->firmware_options[0] |= (BIT_6 | BIT_1);
  1302. nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
  1303. nv->firmware_options[1] |= (BIT_5 | BIT_0);
  1304. nv->firmware_options[1] &= ~BIT_4;
  1305. if (IS_QLA23XX(ha)) {
  1306. nv->firmware_options[0] |= BIT_2;
  1307. nv->firmware_options[0] &= ~BIT_3;
  1308. nv->add_firmware_options[1] |= BIT_5 | BIT_4;
  1309. if (IS_QLA2300(ha)) {
  1310. if (ha->fb_rev == FPM_2310) {
  1311. strcpy(ha->model_number, "QLA2310");
  1312. } else {
  1313. strcpy(ha->model_number, "QLA2300");
  1314. }
  1315. } else {
  1316. if (rval == 0 &&
  1317. memcmp(nv->model_number, BINZERO,
  1318. sizeof(nv->model_number)) != 0) {
  1319. char *st, *en;
  1320. strncpy(ha->model_number, nv->model_number,
  1321. sizeof(nv->model_number));
  1322. st = en = ha->model_number;
  1323. en += sizeof(nv->model_number) - 1;
  1324. while (en > st) {
  1325. if (*en != 0x20 && *en != 0x00)
  1326. break;
  1327. *en-- = '\0';
  1328. }
  1329. } else {
  1330. uint16_t index;
  1331. index = (ha->pdev->subsystem_device & 0xff);
  1332. if (index < QLA_MODEL_NAMES) {
  1333. strcpy(ha->model_number,
  1334. qla2x00_model_name[index * 2]);
  1335. ha->model_desc =
  1336. qla2x00_model_name[index * 2 + 1];
  1337. } else {
  1338. strcpy(ha->model_number, "QLA23xx");
  1339. }
  1340. }
  1341. }
  1342. } else if (IS_QLA2200(ha)) {
  1343. nv->firmware_options[0] |= BIT_2;
  1344. /*
  1345. * 'Point-to-point preferred, else loop' is not a safe
  1346. * connection mode setting.
  1347. */
  1348. if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
  1349. (BIT_5 | BIT_4)) {
  1350. /* Force 'loop preferred, else point-to-point'. */
  1351. nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
  1352. nv->add_firmware_options[0] |= BIT_5;
  1353. }
  1354. strcpy(ha->model_number, "QLA22xx");
  1355. } else /*if (IS_QLA2100(ha))*/ {
  1356. strcpy(ha->model_number, "QLA2100");
  1357. }
  1358. /*
  1359. * Copy over NVRAM RISC parameter block to initialization control block.
  1360. */
  1361. dptr1 = (uint8_t *)icb;
  1362. dptr2 = (uint8_t *)&nv->parameter_block_version;
  1363. cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
  1364. while (cnt--)
  1365. *dptr1++ = *dptr2++;
  1366. /* Copy 2nd half. */
  1367. dptr1 = (uint8_t *)icb->add_firmware_options;
  1368. cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
  1369. while (cnt--)
  1370. *dptr1++ = *dptr2++;
  1371. /* Use alternate WWN? */
  1372. if (nv->host_p[1] & BIT_7) {
  1373. memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
  1374. memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
  1375. }
  1376. /* Prepare nodename */
  1377. if ((icb->firmware_options[1] & BIT_6) == 0) {
  1378. /*
  1379. * Firmware will apply the following mask if the nodename was
  1380. * not provided.
  1381. */
  1382. memcpy(icb->node_name, icb->port_name, WWN_SIZE);
  1383. icb->node_name[0] &= 0xF0;
  1384. }
  1385. /*
  1386. * Set host adapter parameters.
  1387. */
  1388. if (nv->host_p[0] & BIT_7)
  1389. extended_error_logging = 1;
  1390. ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
  1391. /* Always load RISC code on non ISP2[12]00 chips. */
  1392. if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
  1393. ha->flags.disable_risc_code_load = 0;
  1394. ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
  1395. ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
  1396. ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
  1397. ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
  1398. ha->flags.disable_serdes = 0;
  1399. ha->operating_mode =
  1400. (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
  1401. memcpy(ha->fw_seriallink_options, nv->seriallink_options,
  1402. sizeof(ha->fw_seriallink_options));
  1403. /* save HBA serial number */
  1404. ha->serial0 = icb->port_name[5];
  1405. ha->serial1 = icb->port_name[6];
  1406. ha->serial2 = icb->port_name[7];
  1407. ha->node_name = icb->node_name;
  1408. ha->port_name = icb->port_name;
  1409. icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
  1410. ha->retry_count = nv->retry_count;
  1411. /* Set minimum login_timeout to 4 seconds. */
  1412. if (nv->login_timeout < ql2xlogintimeout)
  1413. nv->login_timeout = ql2xlogintimeout;
  1414. if (nv->login_timeout < 4)
  1415. nv->login_timeout = 4;
  1416. ha->login_timeout = nv->login_timeout;
  1417. icb->login_timeout = nv->login_timeout;
  1418. /* Set minimum RATOV to 200 tenths of a second. */
  1419. ha->r_a_tov = 200;
  1420. ha->loop_reset_delay = nv->reset_delay;
  1421. /* Link Down Timeout = 0:
  1422. *
  1423. * When Port Down timer expires we will start returning
  1424. * I/O's to OS with "DID_NO_CONNECT".
  1425. *
  1426. * Link Down Timeout != 0:
  1427. *
  1428. * The driver waits for the link to come up after link down
  1429. * before returning I/Os to OS with "DID_NO_CONNECT".
  1430. */
  1431. if (nv->link_down_timeout == 0) {
  1432. ha->loop_down_abort_time =
  1433. (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
  1434. } else {
  1435. ha->link_down_timeout = nv->link_down_timeout;
  1436. ha->loop_down_abort_time =
  1437. (LOOP_DOWN_TIME - ha->link_down_timeout);
  1438. }
  1439. /*
  1440. * Need enough time to try and get the port back.
  1441. */
  1442. ha->port_down_retry_count = nv->port_down_retry_count;
  1443. if (qlport_down_retry)
  1444. ha->port_down_retry_count = qlport_down_retry;
  1445. /* Set login_retry_count */
  1446. ha->login_retry_count = nv->retry_count;
  1447. if (ha->port_down_retry_count == nv->port_down_retry_count &&
  1448. ha->port_down_retry_count > 3)
  1449. ha->login_retry_count = ha->port_down_retry_count;
  1450. else if (ha->port_down_retry_count > (int)ha->login_retry_count)
  1451. ha->login_retry_count = ha->port_down_retry_count;
  1452. if (ql2xloginretrycount)
  1453. ha->login_retry_count = ql2xloginretrycount;
  1454. icb->lun_enables = __constant_cpu_to_le16(0);
  1455. icb->command_resource_count = 0;
  1456. icb->immediate_notify_resource_count = 0;
  1457. icb->timeout = __constant_cpu_to_le16(0);
  1458. if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
  1459. /* Enable RIO */
  1460. icb->firmware_options[0] &= ~BIT_3;
  1461. icb->add_firmware_options[0] &=
  1462. ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
  1463. icb->add_firmware_options[0] |= BIT_2;
  1464. icb->response_accumulation_timer = 3;
  1465. icb->interrupt_delay_timer = 5;
  1466. ha->flags.process_response_queue = 1;
  1467. } else {
  1468. /* Enable ZIO. */
  1469. if (!ha->flags.init_done) {
  1470. ha->zio_mode = icb->add_firmware_options[0] &
  1471. (BIT_3 | BIT_2 | BIT_1 | BIT_0);
  1472. ha->zio_timer = icb->interrupt_delay_timer ?
  1473. icb->interrupt_delay_timer: 2;
  1474. }
  1475. icb->add_firmware_options[0] &=
  1476. ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
  1477. ha->flags.process_response_queue = 0;
  1478. if (ha->zio_mode != QLA_ZIO_DISABLED) {
  1479. ha->zio_mode = QLA_ZIO_MODE_6;
  1480. DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
  1481. "delay (%d us).\n", ha->host_no, ha->zio_mode,
  1482. ha->zio_timer * 100));
  1483. qla_printk(KERN_INFO, ha,
  1484. "ZIO mode %d enabled; timer delay (%d us).\n",
  1485. ha->zio_mode, ha->zio_timer * 100);
  1486. icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
  1487. icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
  1488. ha->flags.process_response_queue = 1;
  1489. }
  1490. }
  1491. if (rval) {
  1492. DEBUG2_3(printk(KERN_WARNING
  1493. "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
  1494. }
  1495. return (rval);
  1496. }
  1497. static void
  1498. qla2x00_rport_del(void *data)
  1499. {
  1500. fc_port_t *fcport = data;
  1501. struct fc_rport *rport;
  1502. unsigned long flags;
  1503. spin_lock_irqsave(&fcport->rport_lock, flags);
  1504. rport = fcport->drport;
  1505. fcport->drport = NULL;
  1506. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1507. if (rport)
  1508. fc_remote_port_delete(rport);
  1509. }
  1510. /**
  1511. * qla2x00_alloc_fcport() - Allocate a generic fcport.
  1512. * @ha: HA context
  1513. * @flags: allocation flags
  1514. *
  1515. * Returns a pointer to the allocated fcport, or NULL, if none available.
  1516. */
  1517. fc_port_t *
  1518. qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
  1519. {
  1520. fc_port_t *fcport;
  1521. fcport = kmalloc(sizeof(fc_port_t), flags);
  1522. if (fcport == NULL)
  1523. return (fcport);
  1524. /* Setup fcport template structure. */
  1525. memset(fcport, 0, sizeof (fc_port_t));
  1526. fcport->ha = ha;
  1527. fcport->port_type = FCT_UNKNOWN;
  1528. fcport->loop_id = FC_NO_LOOP_ID;
  1529. atomic_set(&fcport->state, FCS_UNCONFIGURED);
  1530. fcport->flags = FCF_RLC_SUPPORT;
  1531. fcport->supported_classes = FC_COS_UNSPECIFIED;
  1532. spin_lock_init(&fcport->rport_lock);
  1533. return (fcport);
  1534. }
  1535. /*
  1536. * qla2x00_configure_loop
  1537. * Updates Fibre Channel Device Database with what is actually on loop.
  1538. *
  1539. * Input:
  1540. * ha = adapter block pointer.
  1541. *
  1542. * Returns:
  1543. * 0 = success.
  1544. * 1 = error.
  1545. * 2 = database was full and device was not configured.
  1546. */
  1547. static int
  1548. qla2x00_configure_loop(scsi_qla_host_t *ha)
  1549. {
  1550. int rval;
  1551. unsigned long flags, save_flags;
  1552. rval = QLA_SUCCESS;
  1553. /* Get Initiator ID */
  1554. if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
  1555. rval = qla2x00_configure_hba(ha);
  1556. if (rval != QLA_SUCCESS) {
  1557. DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
  1558. ha->host_no));
  1559. return (rval);
  1560. }
  1561. }
  1562. save_flags = flags = ha->dpc_flags;
  1563. DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
  1564. ha->host_no, flags));
  1565. /*
  1566. * If we have both an RSCN and PORT UPDATE pending then handle them
  1567. * both at the same time.
  1568. */
  1569. clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
  1570. clear_bit(RSCN_UPDATE, &ha->dpc_flags);
  1571. /* Determine what we need to do */
  1572. if (ha->current_topology == ISP_CFG_FL &&
  1573. (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
  1574. ha->flags.rscn_queue_overflow = 1;
  1575. set_bit(RSCN_UPDATE, &flags);
  1576. } else if (ha->current_topology == ISP_CFG_F &&
  1577. (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
  1578. ha->flags.rscn_queue_overflow = 1;
  1579. set_bit(RSCN_UPDATE, &flags);
  1580. clear_bit(LOCAL_LOOP_UPDATE, &flags);
  1581. } else if (ha->current_topology == ISP_CFG_N) {
  1582. clear_bit(RSCN_UPDATE, &flags);
  1583. } else if (!ha->flags.online ||
  1584. (test_bit(ABORT_ISP_ACTIVE, &flags))) {
  1585. ha->flags.rscn_queue_overflow = 1;
  1586. set_bit(RSCN_UPDATE, &flags);
  1587. set_bit(LOCAL_LOOP_UPDATE, &flags);
  1588. }
  1589. if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
  1590. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1591. rval = QLA_FUNCTION_FAILED;
  1592. } else {
  1593. rval = qla2x00_configure_local_loop(ha);
  1594. }
  1595. }
  1596. if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
  1597. if (LOOP_TRANSITION(ha)) {
  1598. rval = QLA_FUNCTION_FAILED;
  1599. } else {
  1600. rval = qla2x00_configure_fabric(ha);
  1601. }
  1602. }
  1603. if (rval == QLA_SUCCESS) {
  1604. if (atomic_read(&ha->loop_down_timer) ||
  1605. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1606. rval = QLA_FUNCTION_FAILED;
  1607. } else {
  1608. atomic_set(&ha->loop_state, LOOP_READY);
  1609. DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
  1610. }
  1611. }
  1612. if (rval) {
  1613. DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
  1614. __func__, ha->host_no));
  1615. } else {
  1616. DEBUG3(printk("%s: exiting normally\n", __func__));
  1617. }
  1618. /* Restore state if a resync event occured during processing */
  1619. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1620. if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
  1621. set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
  1622. if (test_bit(RSCN_UPDATE, &save_flags))
  1623. set_bit(RSCN_UPDATE, &ha->dpc_flags);
  1624. }
  1625. return (rval);
  1626. }
  1627. /*
  1628. * qla2x00_configure_local_loop
  1629. * Updates Fibre Channel Device Database with local loop devices.
  1630. *
  1631. * Input:
  1632. * ha = adapter block pointer.
  1633. *
  1634. * Returns:
  1635. * 0 = success.
  1636. */
  1637. static int
  1638. qla2x00_configure_local_loop(scsi_qla_host_t *ha)
  1639. {
  1640. int rval, rval2;
  1641. int found_devs;
  1642. int found;
  1643. fc_port_t *fcport, *new_fcport;
  1644. uint16_t index;
  1645. uint16_t entries;
  1646. char *id_iter;
  1647. uint16_t loop_id;
  1648. uint8_t domain, area, al_pa;
  1649. found_devs = 0;
  1650. new_fcport = NULL;
  1651. entries = MAX_FIBRE_DEVICES;
  1652. DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
  1653. DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
  1654. /* Get list of logged in devices. */
  1655. memset(ha->gid_list, 0, GID_LIST_SIZE);
  1656. rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
  1657. &entries);
  1658. if (rval != QLA_SUCCESS)
  1659. goto cleanup_allocation;
  1660. DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
  1661. ha->host_no, entries));
  1662. DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
  1663. entries * sizeof(struct gid_list_info)));
  1664. /* Allocate temporary fcport for any new fcports discovered. */
  1665. new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
  1666. if (new_fcport == NULL) {
  1667. rval = QLA_MEMORY_ALLOC_FAILED;
  1668. goto cleanup_allocation;
  1669. }
  1670. new_fcport->flags &= ~FCF_FABRIC_DEVICE;
  1671. /*
  1672. * Mark local devices that were present with FCF_DEVICE_LOST for now.
  1673. */
  1674. list_for_each_entry(fcport, &ha->fcports, list) {
  1675. if (atomic_read(&fcport->state) == FCS_ONLINE &&
  1676. fcport->port_type != FCT_BROADCAST &&
  1677. (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
  1678. DEBUG(printk("scsi(%ld): Marking port lost, "
  1679. "loop_id=0x%04x\n",
  1680. ha->host_no, fcport->loop_id));
  1681. atomic_set(&fcport->state, FCS_DEVICE_LOST);
  1682. fcport->flags &= ~FCF_FARP_DONE;
  1683. }
  1684. }
  1685. /* Add devices to port list. */
  1686. id_iter = (char *)ha->gid_list;
  1687. for (index = 0; index < entries; index++) {
  1688. domain = ((struct gid_list_info *)id_iter)->domain;
  1689. area = ((struct gid_list_info *)id_iter)->area;
  1690. al_pa = ((struct gid_list_info *)id_iter)->al_pa;
  1691. if (IS_QLA2100(ha) || IS_QLA2200(ha))
  1692. loop_id = (uint16_t)
  1693. ((struct gid_list_info *)id_iter)->loop_id_2100;
  1694. else
  1695. loop_id = le16_to_cpu(
  1696. ((struct gid_list_info *)id_iter)->loop_id);
  1697. id_iter += ha->gid_list_info_size;
  1698. /* Bypass reserved domain fields. */
  1699. if ((domain & 0xf0) == 0xf0)
  1700. continue;
  1701. /* Bypass if not same domain and area of adapter. */
  1702. if (area && domain &&
  1703. (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
  1704. continue;
  1705. /* Bypass invalid local loop ID. */
  1706. if (loop_id > LAST_LOCAL_LOOP_ID)
  1707. continue;
  1708. /* Fill in member data. */
  1709. new_fcport->d_id.b.domain = domain;
  1710. new_fcport->d_id.b.area = area;
  1711. new_fcport->d_id.b.al_pa = al_pa;
  1712. new_fcport->loop_id = loop_id;
  1713. rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
  1714. if (rval2 != QLA_SUCCESS) {
  1715. DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
  1716. "information -- get_port_database=%x, "
  1717. "loop_id=0x%04x\n",
  1718. ha->host_no, rval2, new_fcport->loop_id));
  1719. DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
  1720. ha->host_no));
  1721. set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  1722. continue;
  1723. }
  1724. /* Check for matching device in port list. */
  1725. found = 0;
  1726. fcport = NULL;
  1727. list_for_each_entry(fcport, &ha->fcports, list) {
  1728. if (memcmp(new_fcport->port_name, fcport->port_name,
  1729. WWN_SIZE))
  1730. continue;
  1731. fcport->flags &= ~(FCF_FABRIC_DEVICE |
  1732. FCF_PERSISTENT_BOUND);
  1733. fcport->loop_id = new_fcport->loop_id;
  1734. fcport->port_type = new_fcport->port_type;
  1735. fcport->d_id.b24 = new_fcport->d_id.b24;
  1736. memcpy(fcport->node_name, new_fcport->node_name,
  1737. WWN_SIZE);
  1738. found++;
  1739. break;
  1740. }
  1741. if (!found) {
  1742. /* New device, add to fcports list. */
  1743. new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
  1744. list_add_tail(&new_fcport->list, &ha->fcports);
  1745. /* Allocate a new replacement fcport. */
  1746. fcport = new_fcport;
  1747. new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
  1748. if (new_fcport == NULL) {
  1749. rval = QLA_MEMORY_ALLOC_FAILED;
  1750. goto cleanup_allocation;
  1751. }
  1752. new_fcport->flags &= ~FCF_FABRIC_DEVICE;
  1753. }
  1754. qla2x00_update_fcport(ha, fcport);
  1755. found_devs++;
  1756. }
  1757. cleanup_allocation:
  1758. kfree(new_fcport);
  1759. if (rval != QLA_SUCCESS) {
  1760. DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
  1761. "rval=%x\n", ha->host_no, rval));
  1762. }
  1763. if (found_devs) {
  1764. ha->device_flags |= DFLG_LOCAL_DEVICES;
  1765. ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
  1766. }
  1767. return (rval);
  1768. }
  1769. static void
  1770. qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
  1771. {
  1772. fc_port_t *fcport;
  1773. qla2x00_mark_all_devices_lost(ha, 0);
  1774. list_for_each_entry(fcport, &ha->fcports, list) {
  1775. if (fcport->port_type != FCT_TARGET)
  1776. continue;
  1777. qla2x00_update_fcport(ha, fcport);
  1778. }
  1779. }
  1780. /*
  1781. * qla2x00_update_fcport
  1782. * Updates device on list.
  1783. *
  1784. * Input:
  1785. * ha = adapter block pointer.
  1786. * fcport = port structure pointer.
  1787. *
  1788. * Return:
  1789. * 0 - Success
  1790. * BIT_0 - error
  1791. *
  1792. * Context:
  1793. * Kernel context.
  1794. */
  1795. void
  1796. qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
  1797. {
  1798. fcport->ha = ha;
  1799. fcport->login_retry = 0;
  1800. fcport->port_login_retry_count = ha->port_down_retry_count *
  1801. PORT_RETRY_TIME;
  1802. atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
  1803. PORT_RETRY_TIME);
  1804. fcport->flags &= ~FCF_LOGIN_NEEDED;
  1805. atomic_set(&fcport->state, FCS_ONLINE);
  1806. if (ha->flags.init_done)
  1807. qla2x00_reg_remote_port(ha, fcport);
  1808. }
  1809. void
  1810. qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
  1811. {
  1812. struct fc_rport_identifiers rport_ids;
  1813. struct fc_rport *rport;
  1814. unsigned long flags;
  1815. if (fcport->drport)
  1816. qla2x00_rport_del(fcport);
  1817. if (fcport->rport)
  1818. return;
  1819. rport_ids.node_name = wwn_to_u64(fcport->node_name);
  1820. rport_ids.port_name = wwn_to_u64(fcport->port_name);
  1821. rport_ids.port_id = fcport->d_id.b.domain << 16 |
  1822. fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
  1823. rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
  1824. rport = fc_remote_port_add(ha->host, 0, &rport_ids);
  1825. if (!rport) {
  1826. qla_printk(KERN_WARNING, ha,
  1827. "Unable to allocate fc remote port!\n");
  1828. return;
  1829. }
  1830. spin_lock_irqsave(&fcport->rport_lock, flags);
  1831. fcport->rport = rport;
  1832. *((fc_port_t **)rport->dd_data) = fcport;
  1833. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1834. rport->supported_classes = fcport->supported_classes;
  1835. rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
  1836. if (fcport->port_type == FCT_INITIATOR)
  1837. rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  1838. if (fcport->port_type == FCT_TARGET)
  1839. rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
  1840. fc_remote_port_rolechg(rport, rport_ids.roles);
  1841. if (rport->scsi_target_id != -1 &&
  1842. rport->scsi_target_id < ha->host->max_id)
  1843. fcport->os_target_id = rport->scsi_target_id;
  1844. }
  1845. /*
  1846. * qla2x00_configure_fabric
  1847. * Setup SNS devices with loop ID's.
  1848. *
  1849. * Input:
  1850. * ha = adapter block pointer.
  1851. *
  1852. * Returns:
  1853. * 0 = success.
  1854. * BIT_0 = error
  1855. */
  1856. static int
  1857. qla2x00_configure_fabric(scsi_qla_host_t *ha)
  1858. {
  1859. int rval, rval2;
  1860. fc_port_t *fcport, *fcptemp;
  1861. uint16_t next_loopid;
  1862. uint16_t mb[MAILBOX_REGISTER_COUNT];
  1863. uint16_t loop_id;
  1864. LIST_HEAD(new_fcports);
  1865. /* If FL port exists, then SNS is present */
  1866. if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
  1867. loop_id = NPH_F_PORT;
  1868. else
  1869. loop_id = SNS_FL_PORT;
  1870. rval = qla2x00_get_port_name(ha, loop_id, NULL, 0);
  1871. if (rval != QLA_SUCCESS) {
  1872. DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
  1873. "Port\n", ha->host_no));
  1874. ha->device_flags &= ~SWITCH_FOUND;
  1875. return (QLA_SUCCESS);
  1876. }
  1877. /* Mark devices that need re-synchronization. */
  1878. rval2 = qla2x00_device_resync(ha);
  1879. if (rval2 == QLA_RSCNS_HANDLED) {
  1880. /* No point doing the scan, just continue. */
  1881. return (QLA_SUCCESS);
  1882. }
  1883. do {
  1884. /* FDMI support. */
  1885. if (ql2xfdmienable &&
  1886. test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
  1887. qla2x00_fdmi_register(ha);
  1888. /* Ensure we are logged into the SNS. */
  1889. if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
  1890. loop_id = NPH_SNS;
  1891. else
  1892. loop_id = SIMPLE_NAME_SERVER;
  1893. ha->isp_ops.fabric_login(ha, loop_id, 0xff, 0xff,
  1894. 0xfc, mb, BIT_1 | BIT_0);
  1895. if (mb[0] != MBS_COMMAND_COMPLETE) {
  1896. DEBUG2(qla_printk(KERN_INFO, ha,
  1897. "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
  1898. "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
  1899. mb[0], mb[1], mb[2], mb[6], mb[7]));
  1900. return (QLA_SUCCESS);
  1901. }
  1902. if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
  1903. if (qla2x00_rft_id(ha)) {
  1904. /* EMPTY */
  1905. DEBUG2(printk("scsi(%ld): Register FC-4 "
  1906. "TYPE failed.\n", ha->host_no));
  1907. }
  1908. if (qla2x00_rff_id(ha)) {
  1909. /* EMPTY */
  1910. DEBUG2(printk("scsi(%ld): Register FC-4 "
  1911. "Features failed.\n", ha->host_no));
  1912. }
  1913. if (qla2x00_rnn_id(ha)) {
  1914. /* EMPTY */
  1915. DEBUG2(printk("scsi(%ld): Register Node Name "
  1916. "failed.\n", ha->host_no));
  1917. } else if (qla2x00_rsnn_nn(ha)) {
  1918. /* EMPTY */
  1919. DEBUG2(printk("scsi(%ld): Register Symbolic "
  1920. "Node Name failed.\n", ha->host_no));
  1921. }
  1922. }
  1923. rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
  1924. if (rval != QLA_SUCCESS)
  1925. break;
  1926. /*
  1927. * Logout all previous fabric devices marked lost, except
  1928. * tape devices.
  1929. */
  1930. list_for_each_entry(fcport, &ha->fcports, list) {
  1931. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  1932. break;
  1933. if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
  1934. continue;
  1935. if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
  1936. qla2x00_mark_device_lost(ha, fcport,
  1937. ql2xplogiabsentdevice, 0);
  1938. if (fcport->loop_id != FC_NO_LOOP_ID &&
  1939. (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
  1940. fcport->port_type != FCT_INITIATOR &&
  1941. fcport->port_type != FCT_BROADCAST) {
  1942. ha->isp_ops.fabric_logout(ha,
  1943. fcport->loop_id,
  1944. fcport->d_id.b.domain,
  1945. fcport->d_id.b.area,
  1946. fcport->d_id.b.al_pa);
  1947. fcport->loop_id = FC_NO_LOOP_ID;
  1948. }
  1949. }
  1950. }
  1951. /* Starting free loop ID. */
  1952. next_loopid = ha->min_external_loopid;
  1953. /*
  1954. * Scan through our port list and login entries that need to be
  1955. * logged in.
  1956. */
  1957. list_for_each_entry(fcport, &ha->fcports, list) {
  1958. if (atomic_read(&ha->loop_down_timer) ||
  1959. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  1960. break;
  1961. if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
  1962. (fcport->flags & FCF_LOGIN_NEEDED) == 0)
  1963. continue;
  1964. if (fcport->loop_id == FC_NO_LOOP_ID) {
  1965. fcport->loop_id = next_loopid;
  1966. rval = qla2x00_find_new_loop_id(ha, fcport);
  1967. if (rval != QLA_SUCCESS) {
  1968. /* Ran out of IDs to use */
  1969. break;
  1970. }
  1971. }
  1972. /* Login and update database */
  1973. qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
  1974. }
  1975. /* Exit if out of loop IDs. */
  1976. if (rval != QLA_SUCCESS) {
  1977. break;
  1978. }
  1979. /*
  1980. * Login and add the new devices to our port list.
  1981. */
  1982. list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
  1983. if (atomic_read(&ha->loop_down_timer) ||
  1984. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  1985. break;
  1986. /* Find a new loop ID to use. */
  1987. fcport->loop_id = next_loopid;
  1988. rval = qla2x00_find_new_loop_id(ha, fcport);
  1989. if (rval != QLA_SUCCESS) {
  1990. /* Ran out of IDs to use */
  1991. break;
  1992. }
  1993. /* Remove device from the new list and add it to DB */
  1994. list_del(&fcport->list);
  1995. list_add_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. 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->link_down_timeout = __constant_cpu_to_le16(200);
  2893. nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
  2894. nv->login_timeout = __constant_cpu_to_le16(0);
  2895. nv->firmware_options_1 =
  2896. __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
  2897. nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
  2898. nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
  2899. nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
  2900. nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
  2901. nv->efi_parameters = __constant_cpu_to_le32(0);
  2902. nv->reset_delay = 5;
  2903. nv->max_luns_per_target = __constant_cpu_to_le16(128);
  2904. nv->port_down_retry_count = __constant_cpu_to_le16(30);
  2905. nv->link_down_timeout = __constant_cpu_to_le16(30);
  2906. rval = 1;
  2907. }
  2908. /* Reset Initialization control block */
  2909. memset(icb, 0, sizeof(struct init_cb_24xx));
  2910. /* Copy 1st segment. */
  2911. dptr1 = (uint8_t *)icb;
  2912. dptr2 = (uint8_t *)&nv->version;
  2913. cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
  2914. while (cnt--)
  2915. *dptr1++ = *dptr2++;
  2916. icb->login_retry_count = nv->login_retry_count;
  2917. icb->link_down_timeout = nv->link_down_timeout;
  2918. /* Copy 2nd segment. */
  2919. dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
  2920. dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
  2921. cnt = (uint8_t *)&icb->reserved_3 -
  2922. (uint8_t *)&icb->interrupt_delay_timer;
  2923. while (cnt--)
  2924. *dptr1++ = *dptr2++;
  2925. /*
  2926. * Setup driver NVRAM options.
  2927. */
  2928. if (memcmp(nv->model_name, BINZERO, sizeof(nv->model_name)) != 0) {
  2929. char *st, *en;
  2930. uint16_t index;
  2931. strncpy(ha->model_number, nv->model_name,
  2932. sizeof(nv->model_name));
  2933. st = en = ha->model_number;
  2934. en += sizeof(nv->model_name) - 1;
  2935. while (en > st) {
  2936. if (*en != 0x20 && *en != 0x00)
  2937. break;
  2938. *en-- = '\0';
  2939. }
  2940. index = (ha->pdev->subsystem_device & 0xff);
  2941. if (index < QLA_MODEL_NAMES)
  2942. ha->model_desc = qla2x00_model_name[index * 2 + 1];
  2943. } else
  2944. strcpy(ha->model_number, "QLA2462");
  2945. /* Use alternate WWN? */
  2946. if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
  2947. memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
  2948. memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
  2949. }
  2950. /* Prepare nodename */
  2951. if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
  2952. /*
  2953. * Firmware will apply the following mask if the nodename was
  2954. * not provided.
  2955. */
  2956. memcpy(icb->node_name, icb->port_name, WWN_SIZE);
  2957. icb->node_name[0] &= 0xF0;
  2958. }
  2959. /* Set host adapter parameters. */
  2960. ha->flags.disable_risc_code_load = 0;
  2961. ha->flags.enable_lip_reset = 1;
  2962. ha->flags.enable_lip_full_login = 1;
  2963. ha->flags.enable_target_reset = 1;
  2964. ha->flags.enable_led_scheme = 0;
  2965. ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
  2966. ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
  2967. (BIT_6 | BIT_5 | BIT_4)) >> 4;
  2968. memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
  2969. sizeof(ha->fw_seriallink_options24));
  2970. /* save HBA serial number */
  2971. ha->serial0 = icb->port_name[5];
  2972. ha->serial1 = icb->port_name[6];
  2973. ha->serial2 = icb->port_name[7];
  2974. ha->node_name = icb->node_name;
  2975. ha->port_name = icb->port_name;
  2976. icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
  2977. ha->retry_count = le16_to_cpu(nv->login_retry_count);
  2978. /* Set minimum login_timeout to 4 seconds. */
  2979. if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
  2980. nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
  2981. if (le16_to_cpu(nv->login_timeout) < 4)
  2982. nv->login_timeout = __constant_cpu_to_le16(4);
  2983. ha->login_timeout = le16_to_cpu(nv->login_timeout);
  2984. icb->login_timeout = cpu_to_le16(nv->login_timeout);
  2985. /* Set minimum RATOV to 200 tenths of a second. */
  2986. ha->r_a_tov = 200;
  2987. ha->loop_reset_delay = nv->reset_delay;
  2988. /* Link Down Timeout = 0:
  2989. *
  2990. * When Port Down timer expires we will start returning
  2991. * I/O's to OS with "DID_NO_CONNECT".
  2992. *
  2993. * Link Down Timeout != 0:
  2994. *
  2995. * The driver waits for the link to come up after link down
  2996. * before returning I/Os to OS with "DID_NO_CONNECT".
  2997. */
  2998. if (le16_to_cpu(nv->link_down_timeout) == 0) {
  2999. ha->loop_down_abort_time =
  3000. (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
  3001. } else {
  3002. ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
  3003. ha->loop_down_abort_time =
  3004. (LOOP_DOWN_TIME - ha->link_down_timeout);
  3005. }
  3006. /* Need enough time to try and get the port back. */
  3007. ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
  3008. if (qlport_down_retry)
  3009. ha->port_down_retry_count = qlport_down_retry;
  3010. /* Set login_retry_count */
  3011. ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
  3012. if (ha->port_down_retry_count ==
  3013. le16_to_cpu(nv->port_down_retry_count) &&
  3014. ha->port_down_retry_count > 3)
  3015. ha->login_retry_count = ha->port_down_retry_count;
  3016. else if (ha->port_down_retry_count > (int)ha->login_retry_count)
  3017. ha->login_retry_count = ha->port_down_retry_count;
  3018. if (ql2xloginretrycount)
  3019. ha->login_retry_count = ql2xloginretrycount;
  3020. /* Enable ZIO. */
  3021. if (!ha->flags.init_done) {
  3022. ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
  3023. (BIT_3 | BIT_2 | BIT_1 | BIT_0);
  3024. ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
  3025. le16_to_cpu(icb->interrupt_delay_timer): 2;
  3026. }
  3027. icb->firmware_options_2 &= __constant_cpu_to_le32(
  3028. ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
  3029. ha->flags.process_response_queue = 0;
  3030. if (ha->zio_mode != QLA_ZIO_DISABLED) {
  3031. ha->zio_mode = QLA_ZIO_MODE_6;
  3032. DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
  3033. "(%d us).\n", ha->host_no, ha->zio_mode,
  3034. ha->zio_timer * 100));
  3035. qla_printk(KERN_INFO, ha,
  3036. "ZIO mode %d enabled; timer delay (%d us).\n",
  3037. ha->zio_mode, ha->zio_timer * 100);
  3038. icb->firmware_options_2 |= cpu_to_le32(
  3039. (uint32_t)ha->zio_mode);
  3040. icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
  3041. ha->flags.process_response_queue = 1;
  3042. }
  3043. if (rval) {
  3044. DEBUG2_3(printk(KERN_WARNING
  3045. "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
  3046. }
  3047. return (rval);
  3048. }
  3049. int
  3050. qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
  3051. {
  3052. int rval;
  3053. int segments, fragment;
  3054. uint32_t faddr;
  3055. uint32_t *dcode, dlen;
  3056. uint32_t risc_addr;
  3057. uint32_t risc_size;
  3058. uint32_t i;
  3059. rval = QLA_SUCCESS;
  3060. segments = FA_RISC_CODE_SEGMENTS;
  3061. faddr = FA_RISC_CODE_ADDR;
  3062. dcode = (uint32_t *)ha->request_ring;
  3063. *srisc_addr = 0;
  3064. /* Validate firmware image by checking version. */
  3065. qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
  3066. for (i = 0; i < 4; i++)
  3067. dcode[i] = be32_to_cpu(dcode[i]);
  3068. if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
  3069. dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
  3070. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  3071. dcode[3] == 0)) {
  3072. qla_printk(KERN_WARNING, ha,
  3073. "Unable to verify integrity of flash firmware image!\n");
  3074. qla_printk(KERN_WARNING, ha,
  3075. "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
  3076. dcode[1], dcode[2], dcode[3]);
  3077. return QLA_FUNCTION_FAILED;
  3078. }
  3079. while (segments && rval == QLA_SUCCESS) {
  3080. /* Read segment's load information. */
  3081. qla24xx_read_flash_data(ha, dcode, faddr, 4);
  3082. risc_addr = be32_to_cpu(dcode[2]);
  3083. *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
  3084. risc_size = be32_to_cpu(dcode[3]);
  3085. fragment = 0;
  3086. while (risc_size > 0 && rval == QLA_SUCCESS) {
  3087. dlen = (uint32_t)(ha->fw_transfer_size >> 2);
  3088. if (dlen > risc_size)
  3089. dlen = risc_size;
  3090. DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
  3091. "addr %x, number of dwords 0x%x, offset 0x%x.\n",
  3092. ha->host_no, risc_addr, dlen, faddr));
  3093. qla24xx_read_flash_data(ha, dcode, faddr, dlen);
  3094. for (i = 0; i < dlen; i++)
  3095. dcode[i] = swab32(dcode[i]);
  3096. rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
  3097. dlen);
  3098. if (rval) {
  3099. DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
  3100. "segment %d of firmware\n", ha->host_no,
  3101. fragment));
  3102. qla_printk(KERN_WARNING, ha,
  3103. "[ERROR] Failed to load segment %d of "
  3104. "firmware\n", fragment);
  3105. break;
  3106. }
  3107. faddr += dlen;
  3108. risc_addr += dlen;
  3109. risc_size -= dlen;
  3110. fragment++;
  3111. }
  3112. /* Next segment. */
  3113. segments--;
  3114. }
  3115. return rval;
  3116. }
  3117. #define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
  3118. int
  3119. qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
  3120. {
  3121. int rval;
  3122. int i, fragment;
  3123. uint16_t *wcode, *fwcode;
  3124. uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
  3125. struct fw_blob *blob;
  3126. /* Load firmware blob. */
  3127. blob = qla2x00_request_firmware(ha);
  3128. if (!blob) {
  3129. qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
  3130. qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
  3131. "from: " QLA_FW_URL ".\n");
  3132. return QLA_FUNCTION_FAILED;
  3133. }
  3134. rval = QLA_SUCCESS;
  3135. wcode = (uint16_t *)ha->request_ring;
  3136. *srisc_addr = 0;
  3137. fwcode = (uint16_t *)blob->fw->data;
  3138. fwclen = 0;
  3139. /* Validate firmware image by checking version. */
  3140. if (blob->fw->size < 8 * sizeof(uint16_t)) {
  3141. qla_printk(KERN_WARNING, ha,
  3142. "Unable to verify integrity of firmware image (%Zd)!\n",
  3143. blob->fw->size);
  3144. goto fail_fw_integrity;
  3145. }
  3146. for (i = 0; i < 4; i++)
  3147. wcode[i] = be16_to_cpu(fwcode[i + 4]);
  3148. if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
  3149. wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
  3150. wcode[2] == 0 && wcode[3] == 0)) {
  3151. qla_printk(KERN_WARNING, ha,
  3152. "Unable to verify integrity of firmware image!\n");
  3153. qla_printk(KERN_WARNING, ha,
  3154. "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
  3155. wcode[1], wcode[2], wcode[3]);
  3156. goto fail_fw_integrity;
  3157. }
  3158. seg = blob->segs;
  3159. while (*seg && rval == QLA_SUCCESS) {
  3160. risc_addr = *seg;
  3161. *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
  3162. risc_size = be16_to_cpu(fwcode[3]);
  3163. /* Validate firmware image size. */
  3164. fwclen += risc_size * sizeof(uint16_t);
  3165. if (blob->fw->size < fwclen) {
  3166. qla_printk(KERN_WARNING, ha,
  3167. "Unable to verify integrity of firmware image "
  3168. "(%Zd)!\n", blob->fw->size);
  3169. goto fail_fw_integrity;
  3170. }
  3171. fragment = 0;
  3172. while (risc_size > 0 && rval == QLA_SUCCESS) {
  3173. wlen = (uint16_t)(ha->fw_transfer_size >> 1);
  3174. if (wlen > risc_size)
  3175. wlen = risc_size;
  3176. DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
  3177. "addr %x, number of words 0x%x.\n", ha->host_no,
  3178. risc_addr, wlen));
  3179. for (i = 0; i < wlen; i++)
  3180. wcode[i] = swab16(fwcode[i]);
  3181. rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
  3182. wlen);
  3183. if (rval) {
  3184. DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
  3185. "segment %d of firmware\n", ha->host_no,
  3186. fragment));
  3187. qla_printk(KERN_WARNING, ha,
  3188. "[ERROR] Failed to load segment %d of "
  3189. "firmware\n", fragment);
  3190. break;
  3191. }
  3192. fwcode += wlen;
  3193. risc_addr += wlen;
  3194. risc_size -= wlen;
  3195. fragment++;
  3196. }
  3197. /* Next segment. */
  3198. seg++;
  3199. }
  3200. return rval;
  3201. fail_fw_integrity:
  3202. return QLA_FUNCTION_FAILED;
  3203. }
  3204. int
  3205. qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
  3206. {
  3207. int rval;
  3208. int segments, fragment;
  3209. uint32_t *dcode, dlen;
  3210. uint32_t risc_addr;
  3211. uint32_t risc_size;
  3212. uint32_t i;
  3213. struct fw_blob *blob;
  3214. uint32_t *fwcode, fwclen;
  3215. /* Load firmware blob. */
  3216. blob = qla2x00_request_firmware(ha);
  3217. if (!blob) {
  3218. qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
  3219. qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
  3220. "from: " QLA_FW_URL ".\n");
  3221. /* Try to load RISC code from flash. */
  3222. qla_printk(KERN_ERR, ha, "Attempting to load (potentially "
  3223. "outdated) firmware from flash.\n");
  3224. return qla24xx_load_risc_flash(ha, srisc_addr);
  3225. }
  3226. rval = QLA_SUCCESS;
  3227. segments = FA_RISC_CODE_SEGMENTS;
  3228. dcode = (uint32_t *)ha->request_ring;
  3229. *srisc_addr = 0;
  3230. fwcode = (uint32_t *)blob->fw->data;
  3231. fwclen = 0;
  3232. /* Validate firmware image by checking version. */
  3233. if (blob->fw->size < 8 * sizeof(uint32_t)) {
  3234. qla_printk(KERN_WARNING, ha,
  3235. "Unable to verify integrity of firmware image (%Zd)!\n",
  3236. blob->fw->size);
  3237. goto fail_fw_integrity;
  3238. }
  3239. for (i = 0; i < 4; i++)
  3240. dcode[i] = be32_to_cpu(fwcode[i + 4]);
  3241. if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
  3242. dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
  3243. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  3244. dcode[3] == 0)) {
  3245. qla_printk(KERN_WARNING, ha,
  3246. "Unable to verify integrity of firmware image!\n");
  3247. qla_printk(KERN_WARNING, ha,
  3248. "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
  3249. dcode[1], dcode[2], dcode[3]);
  3250. goto fail_fw_integrity;
  3251. }
  3252. while (segments && rval == QLA_SUCCESS) {
  3253. risc_addr = be32_to_cpu(fwcode[2]);
  3254. *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
  3255. risc_size = be32_to_cpu(fwcode[3]);
  3256. /* Validate firmware image size. */
  3257. fwclen += risc_size * sizeof(uint32_t);
  3258. if (blob->fw->size < fwclen) {
  3259. qla_printk(KERN_WARNING, ha,
  3260. "Unable to verify integrity of firmware image "
  3261. "(%Zd)!\n", blob->fw->size);
  3262. goto fail_fw_integrity;
  3263. }
  3264. fragment = 0;
  3265. while (risc_size > 0 && rval == QLA_SUCCESS) {
  3266. dlen = (uint32_t)(ha->fw_transfer_size >> 2);
  3267. if (dlen > risc_size)
  3268. dlen = risc_size;
  3269. DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
  3270. "addr %x, number of dwords 0x%x.\n", ha->host_no,
  3271. risc_addr, dlen));
  3272. for (i = 0; i < dlen; i++)
  3273. dcode[i] = swab32(fwcode[i]);
  3274. rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
  3275. dlen);
  3276. if (rval) {
  3277. DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
  3278. "segment %d of firmware\n", ha->host_no,
  3279. fragment));
  3280. qla_printk(KERN_WARNING, ha,
  3281. "[ERROR] Failed to load segment %d of "
  3282. "firmware\n", fragment);
  3283. break;
  3284. }
  3285. fwcode += dlen;
  3286. risc_addr += dlen;
  3287. risc_size -= dlen;
  3288. fragment++;
  3289. }
  3290. /* Next segment. */
  3291. segments--;
  3292. }
  3293. return rval;
  3294. fail_fw_integrity:
  3295. return QLA_FUNCTION_FAILED;
  3296. }