qla_init.c 105 KB

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