qla_init.c 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087
  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 = ha->nvram;
  1226. uint8_t *ptr = ha->nvram;
  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 *)nv, ha->nvram_size));
  1241. /* Bad NVRAM data, set defaults parameters. */
  1242. if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
  1243. nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
  1244. /* Reset NVRAM data. */
  1245. qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
  1246. "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
  1247. nv->nvram_version);
  1248. qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
  1249. "invalid -- WWPN) defaults.\n");
  1250. /*
  1251. * Set default initialization control block.
  1252. */
  1253. memset(nv, 0, ha->nvram_size);
  1254. nv->parameter_block_version = ICB_VERSION;
  1255. if (IS_QLA23XX(ha)) {
  1256. nv->firmware_options[0] = BIT_2 | BIT_1;
  1257. nv->firmware_options[1] = BIT_7 | BIT_5;
  1258. nv->add_firmware_options[0] = BIT_5;
  1259. nv->add_firmware_options[1] = BIT_5 | BIT_4;
  1260. nv->frame_payload_size = __constant_cpu_to_le16(2048);
  1261. nv->special_options[1] = BIT_7;
  1262. } else if (IS_QLA2200(ha)) {
  1263. nv->firmware_options[0] = BIT_2 | BIT_1;
  1264. nv->firmware_options[1] = BIT_7 | BIT_5;
  1265. nv->add_firmware_options[0] = BIT_5;
  1266. nv->add_firmware_options[1] = BIT_5 | BIT_4;
  1267. nv->frame_payload_size = __constant_cpu_to_le16(1024);
  1268. } else if (IS_QLA2100(ha)) {
  1269. nv->firmware_options[0] = BIT_3 | BIT_1;
  1270. nv->firmware_options[1] = BIT_5;
  1271. nv->frame_payload_size = __constant_cpu_to_le16(1024);
  1272. }
  1273. nv->max_iocb_allocation = __constant_cpu_to_le16(256);
  1274. nv->execution_throttle = __constant_cpu_to_le16(16);
  1275. nv->retry_count = 8;
  1276. nv->retry_delay = 1;
  1277. nv->port_name[0] = 33;
  1278. nv->port_name[3] = 224;
  1279. nv->port_name[4] = 139;
  1280. qla2xxx_nvram_wwn_from_ofw(ha, nv);
  1281. nv->login_timeout = 4;
  1282. /*
  1283. * Set default host adapter parameters
  1284. */
  1285. nv->host_p[1] = BIT_2;
  1286. nv->reset_delay = 5;
  1287. nv->port_down_retry_count = 8;
  1288. nv->max_luns_per_target = __constant_cpu_to_le16(8);
  1289. nv->link_down_timeout = 60;
  1290. rval = 1;
  1291. }
  1292. #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
  1293. /*
  1294. * The SN2 does not provide BIOS emulation which means you can't change
  1295. * potentially bogus BIOS settings. Force the use of default settings
  1296. * for link rate and frame size. Hope that the rest of the settings
  1297. * are valid.
  1298. */
  1299. if (ia64_platform_is("sn2")) {
  1300. nv->frame_payload_size = __constant_cpu_to_le16(2048);
  1301. if (IS_QLA23XX(ha))
  1302. nv->special_options[1] = BIT_7;
  1303. }
  1304. #endif
  1305. /* Reset Initialization control block */
  1306. memset(icb, 0, ha->init_cb_size);
  1307. /*
  1308. * Setup driver NVRAM options.
  1309. */
  1310. nv->firmware_options[0] |= (BIT_6 | BIT_1);
  1311. nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
  1312. nv->firmware_options[1] |= (BIT_5 | BIT_0);
  1313. nv->firmware_options[1] &= ~BIT_4;
  1314. if (IS_QLA23XX(ha)) {
  1315. nv->firmware_options[0] |= BIT_2;
  1316. nv->firmware_options[0] &= ~BIT_3;
  1317. nv->add_firmware_options[1] |= BIT_5 | BIT_4;
  1318. if (IS_QLA2300(ha)) {
  1319. if (ha->fb_rev == FPM_2310) {
  1320. strcpy(ha->model_number, "QLA2310");
  1321. } else {
  1322. strcpy(ha->model_number, "QLA2300");
  1323. }
  1324. } else {
  1325. qla2x00_set_model_info(ha, nv->model_number,
  1326. sizeof(nv->model_number), "QLA23xx");
  1327. }
  1328. } else if (IS_QLA2200(ha)) {
  1329. nv->firmware_options[0] |= BIT_2;
  1330. /*
  1331. * 'Point-to-point preferred, else loop' is not a safe
  1332. * connection mode setting.
  1333. */
  1334. if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
  1335. (BIT_5 | BIT_4)) {
  1336. /* Force 'loop preferred, else point-to-point'. */
  1337. nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
  1338. nv->add_firmware_options[0] |= BIT_5;
  1339. }
  1340. strcpy(ha->model_number, "QLA22xx");
  1341. } else /*if (IS_QLA2100(ha))*/ {
  1342. strcpy(ha->model_number, "QLA2100");
  1343. }
  1344. /*
  1345. * Copy over NVRAM RISC parameter block to initialization control block.
  1346. */
  1347. dptr1 = (uint8_t *)icb;
  1348. dptr2 = (uint8_t *)&nv->parameter_block_version;
  1349. cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
  1350. while (cnt--)
  1351. *dptr1++ = *dptr2++;
  1352. /* Copy 2nd half. */
  1353. dptr1 = (uint8_t *)icb->add_firmware_options;
  1354. cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
  1355. while (cnt--)
  1356. *dptr1++ = *dptr2++;
  1357. /* Use alternate WWN? */
  1358. if (nv->host_p[1] & BIT_7) {
  1359. memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
  1360. memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
  1361. }
  1362. /* Prepare nodename */
  1363. if ((icb->firmware_options[1] & BIT_6) == 0) {
  1364. /*
  1365. * Firmware will apply the following mask if the nodename was
  1366. * not provided.
  1367. */
  1368. memcpy(icb->node_name, icb->port_name, WWN_SIZE);
  1369. icb->node_name[0] &= 0xF0;
  1370. }
  1371. /*
  1372. * Set host adapter parameters.
  1373. */
  1374. if (nv->host_p[0] & BIT_7)
  1375. ql2xextended_error_logging = 1;
  1376. ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
  1377. /* Always load RISC code on non ISP2[12]00 chips. */
  1378. if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
  1379. ha->flags.disable_risc_code_load = 0;
  1380. ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
  1381. ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
  1382. ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
  1383. ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
  1384. ha->flags.disable_serdes = 0;
  1385. ha->operating_mode =
  1386. (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
  1387. memcpy(ha->fw_seriallink_options, nv->seriallink_options,
  1388. sizeof(ha->fw_seriallink_options));
  1389. /* save HBA serial number */
  1390. ha->serial0 = icb->port_name[5];
  1391. ha->serial1 = icb->port_name[6];
  1392. ha->serial2 = icb->port_name[7];
  1393. ha->node_name = icb->node_name;
  1394. ha->port_name = icb->port_name;
  1395. icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
  1396. ha->retry_count = nv->retry_count;
  1397. /* Set minimum login_timeout to 4 seconds. */
  1398. if (nv->login_timeout < ql2xlogintimeout)
  1399. nv->login_timeout = ql2xlogintimeout;
  1400. if (nv->login_timeout < 4)
  1401. nv->login_timeout = 4;
  1402. ha->login_timeout = nv->login_timeout;
  1403. icb->login_timeout = nv->login_timeout;
  1404. /* Set minimum RATOV to 200 tenths of a second. */
  1405. ha->r_a_tov = 200;
  1406. ha->loop_reset_delay = nv->reset_delay;
  1407. /* Link Down Timeout = 0:
  1408. *
  1409. * When Port Down timer expires we will start returning
  1410. * I/O's to OS with "DID_NO_CONNECT".
  1411. *
  1412. * Link Down Timeout != 0:
  1413. *
  1414. * The driver waits for the link to come up after link down
  1415. * before returning I/Os to OS with "DID_NO_CONNECT".
  1416. */
  1417. if (nv->link_down_timeout == 0) {
  1418. ha->loop_down_abort_time =
  1419. (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
  1420. } else {
  1421. ha->link_down_timeout = nv->link_down_timeout;
  1422. ha->loop_down_abort_time =
  1423. (LOOP_DOWN_TIME - ha->link_down_timeout);
  1424. }
  1425. /*
  1426. * Need enough time to try and get the port back.
  1427. */
  1428. ha->port_down_retry_count = nv->port_down_retry_count;
  1429. if (qlport_down_retry)
  1430. ha->port_down_retry_count = qlport_down_retry;
  1431. /* Set login_retry_count */
  1432. ha->login_retry_count = nv->retry_count;
  1433. if (ha->port_down_retry_count == nv->port_down_retry_count &&
  1434. ha->port_down_retry_count > 3)
  1435. ha->login_retry_count = ha->port_down_retry_count;
  1436. else if (ha->port_down_retry_count > (int)ha->login_retry_count)
  1437. ha->login_retry_count = ha->port_down_retry_count;
  1438. if (ql2xloginretrycount)
  1439. ha->login_retry_count = ql2xloginretrycount;
  1440. icb->lun_enables = __constant_cpu_to_le16(0);
  1441. icb->command_resource_count = 0;
  1442. icb->immediate_notify_resource_count = 0;
  1443. icb->timeout = __constant_cpu_to_le16(0);
  1444. if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
  1445. /* Enable RIO */
  1446. icb->firmware_options[0] &= ~BIT_3;
  1447. icb->add_firmware_options[0] &=
  1448. ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
  1449. icb->add_firmware_options[0] |= BIT_2;
  1450. icb->response_accumulation_timer = 3;
  1451. icb->interrupt_delay_timer = 5;
  1452. ha->flags.process_response_queue = 1;
  1453. } else {
  1454. /* Enable ZIO. */
  1455. if (!ha->flags.init_done) {
  1456. ha->zio_mode = icb->add_firmware_options[0] &
  1457. (BIT_3 | BIT_2 | BIT_1 | BIT_0);
  1458. ha->zio_timer = icb->interrupt_delay_timer ?
  1459. icb->interrupt_delay_timer: 2;
  1460. }
  1461. icb->add_firmware_options[0] &=
  1462. ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
  1463. ha->flags.process_response_queue = 0;
  1464. if (ha->zio_mode != QLA_ZIO_DISABLED) {
  1465. ha->zio_mode = QLA_ZIO_MODE_6;
  1466. DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
  1467. "delay (%d us).\n", ha->host_no, ha->zio_mode,
  1468. ha->zio_timer * 100));
  1469. qla_printk(KERN_INFO, ha,
  1470. "ZIO mode %d enabled; timer delay (%d us).\n",
  1471. ha->zio_mode, ha->zio_timer * 100);
  1472. icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
  1473. icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
  1474. ha->flags.process_response_queue = 1;
  1475. }
  1476. }
  1477. if (rval) {
  1478. DEBUG2_3(printk(KERN_WARNING
  1479. "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
  1480. }
  1481. return (rval);
  1482. }
  1483. static void
  1484. qla2x00_rport_del(void *data)
  1485. {
  1486. fc_port_t *fcport = data;
  1487. struct fc_rport *rport;
  1488. unsigned long flags;
  1489. spin_lock_irqsave(&fcport->rport_lock, flags);
  1490. rport = fcport->drport;
  1491. fcport->drport = NULL;
  1492. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1493. if (rport)
  1494. fc_remote_port_delete(rport);
  1495. }
  1496. /**
  1497. * qla2x00_alloc_fcport() - Allocate a generic fcport.
  1498. * @ha: HA context
  1499. * @flags: allocation flags
  1500. *
  1501. * Returns a pointer to the allocated fcport, or NULL, if none available.
  1502. */
  1503. static fc_port_t *
  1504. qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
  1505. {
  1506. fc_port_t *fcport;
  1507. fcport = kmalloc(sizeof(fc_port_t), flags);
  1508. if (fcport == NULL)
  1509. return (fcport);
  1510. /* Setup fcport template structure. */
  1511. memset(fcport, 0, sizeof (fc_port_t));
  1512. fcport->ha = ha;
  1513. fcport->vp_idx = ha->vp_idx;
  1514. fcport->port_type = FCT_UNKNOWN;
  1515. fcport->loop_id = FC_NO_LOOP_ID;
  1516. atomic_set(&fcport->state, FCS_UNCONFIGURED);
  1517. fcport->flags = FCF_RLC_SUPPORT;
  1518. fcport->supported_classes = FC_COS_UNSPECIFIED;
  1519. spin_lock_init(&fcport->rport_lock);
  1520. return (fcport);
  1521. }
  1522. /*
  1523. * qla2x00_configure_loop
  1524. * Updates Fibre Channel Device Database with what is actually on loop.
  1525. *
  1526. * Input:
  1527. * ha = adapter block pointer.
  1528. *
  1529. * Returns:
  1530. * 0 = success.
  1531. * 1 = error.
  1532. * 2 = database was full and device was not configured.
  1533. */
  1534. static int
  1535. qla2x00_configure_loop(scsi_qla_host_t *ha)
  1536. {
  1537. int rval;
  1538. unsigned long flags, save_flags;
  1539. rval = QLA_SUCCESS;
  1540. /* Get Initiator ID */
  1541. if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
  1542. rval = qla2x00_configure_hba(ha);
  1543. if (rval != QLA_SUCCESS) {
  1544. DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
  1545. ha->host_no));
  1546. return (rval);
  1547. }
  1548. }
  1549. save_flags = flags = ha->dpc_flags;
  1550. DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
  1551. ha->host_no, flags));
  1552. /*
  1553. * If we have both an RSCN and PORT UPDATE pending then handle them
  1554. * both at the same time.
  1555. */
  1556. clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
  1557. clear_bit(RSCN_UPDATE, &ha->dpc_flags);
  1558. /* Determine what we need to do */
  1559. if (ha->current_topology == ISP_CFG_FL &&
  1560. (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
  1561. ha->flags.rscn_queue_overflow = 1;
  1562. set_bit(RSCN_UPDATE, &flags);
  1563. } else if (ha->current_topology == ISP_CFG_F &&
  1564. (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
  1565. ha->flags.rscn_queue_overflow = 1;
  1566. set_bit(RSCN_UPDATE, &flags);
  1567. clear_bit(LOCAL_LOOP_UPDATE, &flags);
  1568. } else if (ha->current_topology == ISP_CFG_N) {
  1569. clear_bit(RSCN_UPDATE, &flags);
  1570. } else if (!ha->flags.online ||
  1571. (test_bit(ABORT_ISP_ACTIVE, &flags))) {
  1572. ha->flags.rscn_queue_overflow = 1;
  1573. set_bit(RSCN_UPDATE, &flags);
  1574. set_bit(LOCAL_LOOP_UPDATE, &flags);
  1575. }
  1576. if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
  1577. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1578. rval = QLA_FUNCTION_FAILED;
  1579. } else {
  1580. rval = qla2x00_configure_local_loop(ha);
  1581. }
  1582. }
  1583. if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
  1584. if (LOOP_TRANSITION(ha)) {
  1585. rval = QLA_FUNCTION_FAILED;
  1586. } else {
  1587. rval = qla2x00_configure_fabric(ha);
  1588. }
  1589. }
  1590. if (rval == QLA_SUCCESS) {
  1591. if (atomic_read(&ha->loop_down_timer) ||
  1592. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1593. rval = QLA_FUNCTION_FAILED;
  1594. } else {
  1595. atomic_set(&ha->loop_state, LOOP_READY);
  1596. DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
  1597. }
  1598. }
  1599. if (rval) {
  1600. DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
  1601. __func__, ha->host_no));
  1602. } else {
  1603. DEBUG3(printk("%s: exiting normally\n", __func__));
  1604. }
  1605. /* Restore state if a resync event occured during processing */
  1606. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1607. if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
  1608. set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
  1609. if (test_bit(RSCN_UPDATE, &save_flags))
  1610. set_bit(RSCN_UPDATE, &ha->dpc_flags);
  1611. }
  1612. return (rval);
  1613. }
  1614. /*
  1615. * qla2x00_configure_local_loop
  1616. * Updates Fibre Channel Device Database with local loop devices.
  1617. *
  1618. * Input:
  1619. * ha = adapter block pointer.
  1620. *
  1621. * Returns:
  1622. * 0 = success.
  1623. */
  1624. static int
  1625. qla2x00_configure_local_loop(scsi_qla_host_t *ha)
  1626. {
  1627. int rval, rval2;
  1628. int found_devs;
  1629. int found;
  1630. fc_port_t *fcport, *new_fcport;
  1631. uint16_t index;
  1632. uint16_t entries;
  1633. char *id_iter;
  1634. uint16_t loop_id;
  1635. uint8_t domain, area, al_pa;
  1636. scsi_qla_host_t *pha = to_qla_parent(ha);
  1637. found_devs = 0;
  1638. new_fcport = NULL;
  1639. entries = MAX_FIBRE_DEVICES;
  1640. DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
  1641. DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
  1642. /* Get list of logged in devices. */
  1643. memset(ha->gid_list, 0, GID_LIST_SIZE);
  1644. rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
  1645. &entries);
  1646. if (rval != QLA_SUCCESS)
  1647. goto cleanup_allocation;
  1648. DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
  1649. ha->host_no, entries));
  1650. DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
  1651. entries * sizeof(struct gid_list_info)));
  1652. /* Allocate temporary fcport for any new fcports discovered. */
  1653. new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
  1654. if (new_fcport == NULL) {
  1655. rval = QLA_MEMORY_ALLOC_FAILED;
  1656. goto cleanup_allocation;
  1657. }
  1658. new_fcport->flags &= ~FCF_FABRIC_DEVICE;
  1659. /*
  1660. * Mark local devices that were present with FCF_DEVICE_LOST for now.
  1661. */
  1662. list_for_each_entry(fcport, &pha->fcports, list) {
  1663. if (fcport->vp_idx != ha->vp_idx)
  1664. continue;
  1665. if (atomic_read(&fcport->state) == FCS_ONLINE &&
  1666. fcport->port_type != FCT_BROADCAST &&
  1667. (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
  1668. DEBUG(printk("scsi(%ld): Marking port lost, "
  1669. "loop_id=0x%04x\n",
  1670. ha->host_no, fcport->loop_id));
  1671. atomic_set(&fcport->state, FCS_DEVICE_LOST);
  1672. fcport->flags &= ~FCF_FARP_DONE;
  1673. }
  1674. }
  1675. /* Add devices to port list. */
  1676. id_iter = (char *)ha->gid_list;
  1677. for (index = 0; index < entries; index++) {
  1678. domain = ((struct gid_list_info *)id_iter)->domain;
  1679. area = ((struct gid_list_info *)id_iter)->area;
  1680. al_pa = ((struct gid_list_info *)id_iter)->al_pa;
  1681. if (IS_QLA2100(ha) || IS_QLA2200(ha))
  1682. loop_id = (uint16_t)
  1683. ((struct gid_list_info *)id_iter)->loop_id_2100;
  1684. else
  1685. loop_id = le16_to_cpu(
  1686. ((struct gid_list_info *)id_iter)->loop_id);
  1687. id_iter += ha->gid_list_info_size;
  1688. /* Bypass reserved domain fields. */
  1689. if ((domain & 0xf0) == 0xf0)
  1690. continue;
  1691. /* Bypass if not same domain and area of adapter. */
  1692. if (area && domain &&
  1693. (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
  1694. continue;
  1695. /* Bypass invalid local loop ID. */
  1696. if (loop_id > LAST_LOCAL_LOOP_ID)
  1697. continue;
  1698. /* Fill in member data. */
  1699. new_fcport->d_id.b.domain = domain;
  1700. new_fcport->d_id.b.area = area;
  1701. new_fcport->d_id.b.al_pa = al_pa;
  1702. new_fcport->loop_id = loop_id;
  1703. new_fcport->vp_idx = ha->vp_idx;
  1704. rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
  1705. if (rval2 != QLA_SUCCESS) {
  1706. DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
  1707. "information -- get_port_database=%x, "
  1708. "loop_id=0x%04x\n",
  1709. ha->host_no, rval2, new_fcport->loop_id));
  1710. DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
  1711. ha->host_no));
  1712. set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  1713. continue;
  1714. }
  1715. /* Check for matching device in port list. */
  1716. found = 0;
  1717. fcport = NULL;
  1718. list_for_each_entry(fcport, &pha->fcports, list) {
  1719. if (fcport->vp_idx != ha->vp_idx)
  1720. continue;
  1721. if (memcmp(new_fcport->port_name, fcport->port_name,
  1722. WWN_SIZE))
  1723. continue;
  1724. fcport->flags &= ~(FCF_FABRIC_DEVICE |
  1725. FCF_PERSISTENT_BOUND);
  1726. fcport->loop_id = new_fcport->loop_id;
  1727. fcport->port_type = new_fcport->port_type;
  1728. fcport->d_id.b24 = new_fcport->d_id.b24;
  1729. memcpy(fcport->node_name, new_fcport->node_name,
  1730. WWN_SIZE);
  1731. found++;
  1732. break;
  1733. }
  1734. if (!found) {
  1735. /* New device, add to fcports list. */
  1736. new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
  1737. if (ha->parent) {
  1738. new_fcport->ha = ha;
  1739. new_fcport->vp_idx = ha->vp_idx;
  1740. list_add_tail(&new_fcport->vp_fcport,
  1741. &ha->vp_fcports);
  1742. }
  1743. list_add_tail(&new_fcport->list, &pha->fcports);
  1744. /* Allocate a new replacement fcport. */
  1745. fcport = new_fcport;
  1746. new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
  1747. if (new_fcport == NULL) {
  1748. rval = QLA_MEMORY_ALLOC_FAILED;
  1749. goto cleanup_allocation;
  1750. }
  1751. new_fcport->flags &= ~FCF_FABRIC_DEVICE;
  1752. }
  1753. /* Base iIDMA settings on HBA port speed. */
  1754. switch (ha->link_data_rate) {
  1755. case PORT_SPEED_1GB:
  1756. fcport->fp_speed = cpu_to_be16(BIT_15);
  1757. break;
  1758. case PORT_SPEED_2GB:
  1759. fcport->fp_speed = cpu_to_be16(BIT_14);
  1760. break;
  1761. case PORT_SPEED_4GB:
  1762. fcport->fp_speed = cpu_to_be16(BIT_13);
  1763. break;
  1764. }
  1765. qla2x00_update_fcport(ha, fcport);
  1766. found_devs++;
  1767. }
  1768. cleanup_allocation:
  1769. kfree(new_fcport);
  1770. if (rval != QLA_SUCCESS) {
  1771. DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
  1772. "rval=%x\n", ha->host_no, rval));
  1773. }
  1774. if (found_devs) {
  1775. ha->device_flags |= DFLG_LOCAL_DEVICES;
  1776. ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
  1777. }
  1778. return (rval);
  1779. }
  1780. static void
  1781. qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
  1782. {
  1783. fc_port_t *fcport;
  1784. qla2x00_mark_all_devices_lost(ha, 0);
  1785. list_for_each_entry(fcport, &ha->fcports, list) {
  1786. if (fcport->port_type != FCT_TARGET)
  1787. continue;
  1788. qla2x00_update_fcport(ha, fcport);
  1789. }
  1790. }
  1791. static void
  1792. qla2x00_iidma_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
  1793. {
  1794. #define LS_UNKNOWN 2
  1795. static char *link_speeds[5] = { "1", "2", "?", "4" };
  1796. int rval;
  1797. uint16_t port_speed, mb[6];
  1798. if (!IS_IIDMA_CAPABLE(ha))
  1799. return;
  1800. switch (be16_to_cpu(fcport->fp_speed)) {
  1801. case BIT_15:
  1802. port_speed = PORT_SPEED_1GB;
  1803. break;
  1804. case BIT_14:
  1805. port_speed = PORT_SPEED_2GB;
  1806. break;
  1807. case BIT_13:
  1808. port_speed = PORT_SPEED_4GB;
  1809. break;
  1810. default:
  1811. DEBUG2(printk("scsi(%ld): %02x%02x%02x%02x%02x%02x%02x%02x -- "
  1812. "unsupported FM port operating speed (%04x).\n",
  1813. ha->host_no, fcport->port_name[0], fcport->port_name[1],
  1814. fcport->port_name[2], fcport->port_name[3],
  1815. fcport->port_name[4], fcport->port_name[5],
  1816. fcport->port_name[6], fcport->port_name[7],
  1817. be16_to_cpu(fcport->fp_speed)));
  1818. port_speed = PORT_SPEED_UNKNOWN;
  1819. break;
  1820. }
  1821. if (port_speed == PORT_SPEED_UNKNOWN)
  1822. return;
  1823. rval = qla2x00_set_idma_speed(ha, fcport->loop_id, port_speed, mb);
  1824. if (rval != QLA_SUCCESS) {
  1825. DEBUG2(printk("scsi(%ld): Unable to adjust iIDMA "
  1826. "%02x%02x%02x%02x%02x%02x%02x%02x -- %04x %x %04x %04x.\n",
  1827. ha->host_no, fcport->port_name[0], fcport->port_name[1],
  1828. fcport->port_name[2], fcport->port_name[3],
  1829. fcport->port_name[4], fcport->port_name[5],
  1830. fcport->port_name[6], fcport->port_name[7], rval,
  1831. port_speed, mb[0], mb[1]));
  1832. } else {
  1833. DEBUG2(qla_printk(KERN_INFO, ha,
  1834. "iIDMA adjusted to %s GB/s on "
  1835. "%02x%02x%02x%02x%02x%02x%02x%02x.\n",
  1836. link_speeds[port_speed], fcport->port_name[0],
  1837. fcport->port_name[1], fcport->port_name[2],
  1838. fcport->port_name[3], fcport->port_name[4],
  1839. fcport->port_name[5], fcport->port_name[6],
  1840. fcport->port_name[7]));
  1841. }
  1842. }
  1843. static void
  1844. qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
  1845. {
  1846. struct fc_rport_identifiers rport_ids;
  1847. struct fc_rport *rport;
  1848. unsigned long flags;
  1849. if (fcport->drport)
  1850. qla2x00_rport_del(fcport);
  1851. if (fcport->rport)
  1852. return;
  1853. rport_ids.node_name = wwn_to_u64(fcport->node_name);
  1854. rport_ids.port_name = wwn_to_u64(fcport->port_name);
  1855. rport_ids.port_id = fcport->d_id.b.domain << 16 |
  1856. fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
  1857. rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
  1858. rport = fc_remote_port_add(ha->host, 0, &rport_ids);
  1859. if (!rport) {
  1860. qla_printk(KERN_WARNING, ha,
  1861. "Unable to allocate fc remote port!\n");
  1862. return;
  1863. }
  1864. spin_lock_irqsave(&fcport->rport_lock, flags);
  1865. fcport->rport = rport;
  1866. *((fc_port_t **)rport->dd_data) = fcport;
  1867. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1868. rport->supported_classes = fcport->supported_classes;
  1869. rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
  1870. if (fcport->port_type == FCT_INITIATOR)
  1871. rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  1872. if (fcport->port_type == FCT_TARGET)
  1873. rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
  1874. fc_remote_port_rolechg(rport, rport_ids.roles);
  1875. if (rport->scsi_target_id != -1 &&
  1876. rport->scsi_target_id < ha->host->max_id)
  1877. fcport->os_target_id = rport->scsi_target_id;
  1878. }
  1879. /*
  1880. * qla2x00_update_fcport
  1881. * Updates device on list.
  1882. *
  1883. * Input:
  1884. * ha = adapter block pointer.
  1885. * fcport = port structure pointer.
  1886. *
  1887. * Return:
  1888. * 0 - Success
  1889. * BIT_0 - error
  1890. *
  1891. * Context:
  1892. * Kernel context.
  1893. */
  1894. void
  1895. qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
  1896. {
  1897. scsi_qla_host_t *pha = to_qla_parent(ha);
  1898. fcport->ha = ha;
  1899. fcport->login_retry = 0;
  1900. fcport->port_login_retry_count = pha->port_down_retry_count *
  1901. PORT_RETRY_TIME;
  1902. atomic_set(&fcport->port_down_timer, pha->port_down_retry_count *
  1903. PORT_RETRY_TIME);
  1904. fcport->flags &= ~FCF_LOGIN_NEEDED;
  1905. qla2x00_iidma_fcport(ha, fcport);
  1906. atomic_set(&fcport->state, FCS_ONLINE);
  1907. qla2x00_reg_remote_port(ha, fcport);
  1908. }
  1909. /*
  1910. * qla2x00_configure_fabric
  1911. * Setup SNS devices with loop ID's.
  1912. *
  1913. * Input:
  1914. * ha = adapter block pointer.
  1915. *
  1916. * Returns:
  1917. * 0 = success.
  1918. * BIT_0 = error
  1919. */
  1920. static int
  1921. qla2x00_configure_fabric(scsi_qla_host_t *ha)
  1922. {
  1923. int rval, rval2;
  1924. fc_port_t *fcport, *fcptemp;
  1925. uint16_t next_loopid;
  1926. uint16_t mb[MAILBOX_REGISTER_COUNT];
  1927. uint16_t loop_id;
  1928. LIST_HEAD(new_fcports);
  1929. scsi_qla_host_t *pha = to_qla_parent(ha);
  1930. /* If FL port exists, then SNS is present */
  1931. if (IS_FWI2_CAPABLE(ha))
  1932. loop_id = NPH_F_PORT;
  1933. else
  1934. loop_id = SNS_FL_PORT;
  1935. rval = qla2x00_get_port_name(ha, loop_id, ha->fabric_node_name, 1);
  1936. if (rval != QLA_SUCCESS) {
  1937. DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
  1938. "Port\n", ha->host_no));
  1939. ha->device_flags &= ~SWITCH_FOUND;
  1940. return (QLA_SUCCESS);
  1941. }
  1942. ha->device_flags |= SWITCH_FOUND;
  1943. /* Mark devices that need re-synchronization. */
  1944. rval2 = qla2x00_device_resync(ha);
  1945. if (rval2 == QLA_RSCNS_HANDLED) {
  1946. /* No point doing the scan, just continue. */
  1947. return (QLA_SUCCESS);
  1948. }
  1949. do {
  1950. /* FDMI support. */
  1951. if (ql2xfdmienable &&
  1952. test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
  1953. qla2x00_fdmi_register(ha);
  1954. /* Ensure we are logged into the SNS. */
  1955. if (IS_FWI2_CAPABLE(ha))
  1956. loop_id = NPH_SNS;
  1957. else
  1958. loop_id = SIMPLE_NAME_SERVER;
  1959. ha->isp_ops->fabric_login(ha, loop_id, 0xff, 0xff,
  1960. 0xfc, mb, BIT_1 | BIT_0);
  1961. if (mb[0] != MBS_COMMAND_COMPLETE) {
  1962. DEBUG2(qla_printk(KERN_INFO, ha,
  1963. "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
  1964. "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
  1965. mb[0], mb[1], mb[2], mb[6], mb[7]));
  1966. return (QLA_SUCCESS);
  1967. }
  1968. if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
  1969. if (qla2x00_rft_id(ha)) {
  1970. /* EMPTY */
  1971. DEBUG2(printk("scsi(%ld): Register FC-4 "
  1972. "TYPE failed.\n", ha->host_no));
  1973. }
  1974. if (qla2x00_rff_id(ha)) {
  1975. /* EMPTY */
  1976. DEBUG2(printk("scsi(%ld): Register FC-4 "
  1977. "Features failed.\n", ha->host_no));
  1978. }
  1979. if (qla2x00_rnn_id(ha)) {
  1980. /* EMPTY */
  1981. DEBUG2(printk("scsi(%ld): Register Node Name "
  1982. "failed.\n", ha->host_no));
  1983. } else if (qla2x00_rsnn_nn(ha)) {
  1984. /* EMPTY */
  1985. DEBUG2(printk("scsi(%ld): Register Symbolic "
  1986. "Node Name failed.\n", ha->host_no));
  1987. }
  1988. }
  1989. rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
  1990. if (rval != QLA_SUCCESS)
  1991. break;
  1992. /*
  1993. * Logout all previous fabric devices marked lost, except
  1994. * tape devices.
  1995. */
  1996. list_for_each_entry(fcport, &pha->fcports, list) {
  1997. if (fcport->vp_idx !=ha->vp_idx)
  1998. continue;
  1999. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  2000. break;
  2001. if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
  2002. continue;
  2003. if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
  2004. qla2x00_mark_device_lost(ha, fcport,
  2005. ql2xplogiabsentdevice, 0);
  2006. if (fcport->loop_id != FC_NO_LOOP_ID &&
  2007. (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
  2008. fcport->port_type != FCT_INITIATOR &&
  2009. fcport->port_type != FCT_BROADCAST) {
  2010. ha->isp_ops->fabric_logout(ha,
  2011. fcport->loop_id,
  2012. fcport->d_id.b.domain,
  2013. fcport->d_id.b.area,
  2014. fcport->d_id.b.al_pa);
  2015. fcport->loop_id = FC_NO_LOOP_ID;
  2016. }
  2017. }
  2018. }
  2019. /* Starting free loop ID. */
  2020. next_loopid = pha->min_external_loopid;
  2021. /*
  2022. * Scan through our port list and login entries that need to be
  2023. * logged in.
  2024. */
  2025. list_for_each_entry(fcport, &pha->fcports, list) {
  2026. if (fcport->vp_idx != ha->vp_idx)
  2027. continue;
  2028. if (atomic_read(&ha->loop_down_timer) ||
  2029. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  2030. break;
  2031. if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
  2032. (fcport->flags & FCF_LOGIN_NEEDED) == 0)
  2033. continue;
  2034. if (fcport->loop_id == FC_NO_LOOP_ID) {
  2035. fcport->loop_id = next_loopid;
  2036. rval = qla2x00_find_new_loop_id(ha, fcport);
  2037. if (rval != QLA_SUCCESS) {
  2038. /* Ran out of IDs to use */
  2039. break;
  2040. }
  2041. }
  2042. /* Login and update database */
  2043. qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
  2044. }
  2045. /* Exit if out of loop IDs. */
  2046. if (rval != QLA_SUCCESS) {
  2047. break;
  2048. }
  2049. /*
  2050. * Login and add the new devices to our port list.
  2051. */
  2052. list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
  2053. if (atomic_read(&ha->loop_down_timer) ||
  2054. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  2055. break;
  2056. /* Find a new loop ID to use. */
  2057. fcport->loop_id = next_loopid;
  2058. rval = qla2x00_find_new_loop_id(ha, fcport);
  2059. if (rval != QLA_SUCCESS) {
  2060. /* Ran out of IDs to use */
  2061. break;
  2062. }
  2063. /* Login and update database */
  2064. qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
  2065. if (ha->parent) {
  2066. fcport->ha = ha;
  2067. fcport->vp_idx = ha->vp_idx;
  2068. list_add_tail(&fcport->vp_fcport,
  2069. &ha->vp_fcports);
  2070. list_move_tail(&fcport->list,
  2071. &ha->parent->fcports);
  2072. } else
  2073. list_move_tail(&fcport->list, &ha->fcports);
  2074. }
  2075. } while (0);
  2076. /* Free all new device structures not processed. */
  2077. list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
  2078. list_del(&fcport->list);
  2079. kfree(fcport);
  2080. }
  2081. if (rval) {
  2082. DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
  2083. "rval=%d\n", ha->host_no, rval));
  2084. }
  2085. return (rval);
  2086. }
  2087. /*
  2088. * qla2x00_find_all_fabric_devs
  2089. *
  2090. * Input:
  2091. * ha = adapter block pointer.
  2092. * dev = database device entry pointer.
  2093. *
  2094. * Returns:
  2095. * 0 = success.
  2096. *
  2097. * Context:
  2098. * Kernel context.
  2099. */
  2100. static int
  2101. qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
  2102. {
  2103. int rval;
  2104. uint16_t loop_id;
  2105. fc_port_t *fcport, *new_fcport, *fcptemp;
  2106. int found;
  2107. sw_info_t *swl;
  2108. int swl_idx;
  2109. int first_dev, last_dev;
  2110. port_id_t wrap, nxt_d_id;
  2111. int vp_index;
  2112. int empty_vp_index;
  2113. int found_vp;
  2114. scsi_qla_host_t *vha;
  2115. scsi_qla_host_t *pha = to_qla_parent(ha);
  2116. rval = QLA_SUCCESS;
  2117. /* Try GID_PT to get device list, else GAN. */
  2118. swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
  2119. if (swl == NULL) {
  2120. /*EMPTY*/
  2121. DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
  2122. "on GA_NXT\n", ha->host_no));
  2123. } else {
  2124. memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
  2125. if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
  2126. kfree(swl);
  2127. swl = NULL;
  2128. } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
  2129. kfree(swl);
  2130. swl = NULL;
  2131. } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
  2132. kfree(swl);
  2133. swl = NULL;
  2134. } else if (qla2x00_gfpn_id(ha, swl) == QLA_SUCCESS) {
  2135. qla2x00_gpsc(ha, swl);
  2136. }
  2137. }
  2138. swl_idx = 0;
  2139. /* Allocate temporary fcport for any new fcports discovered. */
  2140. new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
  2141. if (new_fcport == NULL) {
  2142. kfree(swl);
  2143. return (QLA_MEMORY_ALLOC_FAILED);
  2144. }
  2145. new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
  2146. new_fcport->vp_idx = ha->vp_idx;
  2147. /* Set start port ID scan at adapter ID. */
  2148. first_dev = 1;
  2149. last_dev = 0;
  2150. /* Starting free loop ID. */
  2151. loop_id = pha->min_external_loopid;
  2152. for (; loop_id <= ha->last_loop_id; loop_id++) {
  2153. if (qla2x00_is_reserved_id(ha, loop_id))
  2154. continue;
  2155. if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
  2156. break;
  2157. if (swl != NULL) {
  2158. if (last_dev) {
  2159. wrap.b24 = new_fcport->d_id.b24;
  2160. } else {
  2161. new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
  2162. memcpy(new_fcport->node_name,
  2163. swl[swl_idx].node_name, WWN_SIZE);
  2164. memcpy(new_fcport->port_name,
  2165. swl[swl_idx].port_name, WWN_SIZE);
  2166. memcpy(new_fcport->fabric_port_name,
  2167. swl[swl_idx].fabric_port_name, WWN_SIZE);
  2168. new_fcport->fp_speed = swl[swl_idx].fp_speed;
  2169. if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
  2170. last_dev = 1;
  2171. }
  2172. swl_idx++;
  2173. }
  2174. } else {
  2175. /* Send GA_NXT to the switch */
  2176. rval = qla2x00_ga_nxt(ha, new_fcport);
  2177. if (rval != QLA_SUCCESS) {
  2178. qla_printk(KERN_WARNING, ha,
  2179. "SNS scan failed -- assuming zero-entry "
  2180. "result...\n");
  2181. list_for_each_entry_safe(fcport, fcptemp,
  2182. new_fcports, list) {
  2183. list_del(&fcport->list);
  2184. kfree(fcport);
  2185. }
  2186. rval = QLA_SUCCESS;
  2187. break;
  2188. }
  2189. }
  2190. /* If wrap on switch device list, exit. */
  2191. if (first_dev) {
  2192. wrap.b24 = new_fcport->d_id.b24;
  2193. first_dev = 0;
  2194. } else if (new_fcport->d_id.b24 == wrap.b24) {
  2195. DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
  2196. ha->host_no, new_fcport->d_id.b.domain,
  2197. new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
  2198. break;
  2199. }
  2200. /* Bypass if same physical adapter. */
  2201. if (new_fcport->d_id.b24 == pha->d_id.b24)
  2202. continue;
  2203. /* Bypass virtual ports of the same host. */
  2204. if (pha->num_vhosts) {
  2205. vp_index = find_next_bit(
  2206. (unsigned long *)pha->vp_idx_map,
  2207. MAX_MULTI_ID_FABRIC + 1, 1);
  2208. for (;vp_index <= MAX_MULTI_ID_FABRIC;
  2209. vp_index = find_next_bit(
  2210. (unsigned long *)pha->vp_idx_map,
  2211. MAX_MULTI_ID_FABRIC + 1, vp_index + 1)) {
  2212. empty_vp_index = 1;
  2213. found_vp = 0;
  2214. list_for_each_entry(vha, &pha->vp_list,
  2215. vp_list) {
  2216. if (vp_index == vha->vp_idx) {
  2217. empty_vp_index = 0;
  2218. found_vp = 1;
  2219. break;
  2220. }
  2221. }
  2222. if (empty_vp_index)
  2223. continue;
  2224. if (found_vp &&
  2225. new_fcport->d_id.b24 == vha->d_id.b24)
  2226. break;
  2227. }
  2228. if (vp_index <= MAX_MULTI_ID_FABRIC)
  2229. continue;
  2230. }
  2231. /* Bypass if same domain and area of adapter. */
  2232. if (((new_fcport->d_id.b24 & 0xffff00) ==
  2233. (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
  2234. ISP_CFG_FL)
  2235. continue;
  2236. /* Bypass reserved domain fields. */
  2237. if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
  2238. continue;
  2239. /* Locate matching device in database. */
  2240. found = 0;
  2241. list_for_each_entry(fcport, &pha->fcports, list) {
  2242. if (new_fcport->vp_idx != fcport->vp_idx)
  2243. continue;
  2244. if (memcmp(new_fcport->port_name, fcport->port_name,
  2245. WWN_SIZE))
  2246. continue;
  2247. found++;
  2248. /* Update port state. */
  2249. memcpy(fcport->fabric_port_name,
  2250. new_fcport->fabric_port_name, WWN_SIZE);
  2251. fcport->fp_speed = new_fcport->fp_speed;
  2252. /*
  2253. * If address the same and state FCS_ONLINE, nothing
  2254. * changed.
  2255. */
  2256. if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
  2257. atomic_read(&fcport->state) == FCS_ONLINE) {
  2258. break;
  2259. }
  2260. /*
  2261. * If device was not a fabric device before.
  2262. */
  2263. if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
  2264. fcport->d_id.b24 = new_fcport->d_id.b24;
  2265. fcport->loop_id = FC_NO_LOOP_ID;
  2266. fcport->flags |= (FCF_FABRIC_DEVICE |
  2267. FCF_LOGIN_NEEDED);
  2268. fcport->flags &= ~FCF_PERSISTENT_BOUND;
  2269. break;
  2270. }
  2271. /*
  2272. * Port ID changed or device was marked to be updated;
  2273. * Log it out if still logged in and mark it for
  2274. * relogin later.
  2275. */
  2276. fcport->d_id.b24 = new_fcport->d_id.b24;
  2277. fcport->flags |= FCF_LOGIN_NEEDED;
  2278. if (fcport->loop_id != FC_NO_LOOP_ID &&
  2279. (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
  2280. fcport->port_type != FCT_INITIATOR &&
  2281. fcport->port_type != FCT_BROADCAST) {
  2282. ha->isp_ops->fabric_logout(ha, fcport->loop_id,
  2283. fcport->d_id.b.domain, fcport->d_id.b.area,
  2284. fcport->d_id.b.al_pa);
  2285. fcport->loop_id = FC_NO_LOOP_ID;
  2286. }
  2287. break;
  2288. }
  2289. if (found)
  2290. continue;
  2291. /* If device was not in our fcports list, then add it. */
  2292. list_add_tail(&new_fcport->list, new_fcports);
  2293. /* Allocate a new replacement fcport. */
  2294. nxt_d_id.b24 = new_fcport->d_id.b24;
  2295. new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
  2296. if (new_fcport == NULL) {
  2297. kfree(swl);
  2298. return (QLA_MEMORY_ALLOC_FAILED);
  2299. }
  2300. new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
  2301. new_fcport->d_id.b24 = nxt_d_id.b24;
  2302. new_fcport->vp_idx = ha->vp_idx;
  2303. }
  2304. kfree(swl);
  2305. kfree(new_fcport);
  2306. if (!list_empty(new_fcports))
  2307. ha->device_flags |= DFLG_FABRIC_DEVICES;
  2308. return (rval);
  2309. }
  2310. /*
  2311. * qla2x00_find_new_loop_id
  2312. * Scan through our port list and find a new usable loop ID.
  2313. *
  2314. * Input:
  2315. * ha: adapter state pointer.
  2316. * dev: port structure pointer.
  2317. *
  2318. * Returns:
  2319. * qla2x00 local function return status code.
  2320. *
  2321. * Context:
  2322. * Kernel context.
  2323. */
  2324. static int
  2325. qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
  2326. {
  2327. int rval;
  2328. int found;
  2329. fc_port_t *fcport;
  2330. uint16_t first_loop_id;
  2331. scsi_qla_host_t *pha = to_qla_parent(ha);
  2332. rval = QLA_SUCCESS;
  2333. /* Save starting loop ID. */
  2334. first_loop_id = dev->loop_id;
  2335. for (;;) {
  2336. /* Skip loop ID if already used by adapter. */
  2337. if (dev->loop_id == ha->loop_id) {
  2338. dev->loop_id++;
  2339. }
  2340. /* Skip reserved loop IDs. */
  2341. while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
  2342. dev->loop_id++;
  2343. }
  2344. /* Reset loop ID if passed the end. */
  2345. if (dev->loop_id > ha->last_loop_id) {
  2346. /* first loop ID. */
  2347. dev->loop_id = ha->min_external_loopid;
  2348. }
  2349. /* Check for loop ID being already in use. */
  2350. found = 0;
  2351. fcport = NULL;
  2352. list_for_each_entry(fcport, &pha->fcports, list) {
  2353. if (fcport->loop_id == dev->loop_id && fcport != dev) {
  2354. /* ID possibly in use */
  2355. found++;
  2356. break;
  2357. }
  2358. }
  2359. /* If not in use then it is free to use. */
  2360. if (!found) {
  2361. break;
  2362. }
  2363. /* ID in use. Try next value. */
  2364. dev->loop_id++;
  2365. /* If wrap around. No free ID to use. */
  2366. if (dev->loop_id == first_loop_id) {
  2367. dev->loop_id = FC_NO_LOOP_ID;
  2368. rval = QLA_FUNCTION_FAILED;
  2369. break;
  2370. }
  2371. }
  2372. return (rval);
  2373. }
  2374. /*
  2375. * qla2x00_device_resync
  2376. * Marks devices in the database that needs resynchronization.
  2377. *
  2378. * Input:
  2379. * ha = adapter block pointer.
  2380. *
  2381. * Context:
  2382. * Kernel context.
  2383. */
  2384. static int
  2385. qla2x00_device_resync(scsi_qla_host_t *ha)
  2386. {
  2387. int rval;
  2388. uint32_t mask;
  2389. fc_port_t *fcport;
  2390. uint32_t rscn_entry;
  2391. uint8_t rscn_out_iter;
  2392. uint8_t format;
  2393. port_id_t d_id;
  2394. scsi_qla_host_t *pha = to_qla_parent(ha);
  2395. rval = QLA_RSCNS_HANDLED;
  2396. while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
  2397. ha->flags.rscn_queue_overflow) {
  2398. rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
  2399. format = MSB(MSW(rscn_entry));
  2400. d_id.b.domain = LSB(MSW(rscn_entry));
  2401. d_id.b.area = MSB(LSW(rscn_entry));
  2402. d_id.b.al_pa = LSB(LSW(rscn_entry));
  2403. DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
  2404. "[%02x/%02x%02x%02x].\n",
  2405. ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
  2406. d_id.b.area, d_id.b.al_pa));
  2407. ha->rscn_out_ptr++;
  2408. if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
  2409. ha->rscn_out_ptr = 0;
  2410. /* Skip duplicate entries. */
  2411. for (rscn_out_iter = ha->rscn_out_ptr;
  2412. !ha->flags.rscn_queue_overflow &&
  2413. rscn_out_iter != ha->rscn_in_ptr;
  2414. rscn_out_iter = (rscn_out_iter ==
  2415. (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
  2416. if (rscn_entry != ha->rscn_queue[rscn_out_iter])
  2417. break;
  2418. DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
  2419. "entry found at [%d].\n", ha->host_no,
  2420. rscn_out_iter));
  2421. ha->rscn_out_ptr = rscn_out_iter;
  2422. }
  2423. /* Queue overflow, set switch default case. */
  2424. if (ha->flags.rscn_queue_overflow) {
  2425. DEBUG(printk("scsi(%ld): device_resync: rscn "
  2426. "overflow.\n", ha->host_no));
  2427. format = 3;
  2428. ha->flags.rscn_queue_overflow = 0;
  2429. }
  2430. switch (format) {
  2431. case 0:
  2432. mask = 0xffffff;
  2433. break;
  2434. case 1:
  2435. mask = 0xffff00;
  2436. break;
  2437. case 2:
  2438. mask = 0xff0000;
  2439. break;
  2440. default:
  2441. mask = 0x0;
  2442. d_id.b24 = 0;
  2443. ha->rscn_out_ptr = ha->rscn_in_ptr;
  2444. break;
  2445. }
  2446. rval = QLA_SUCCESS;
  2447. list_for_each_entry(fcport, &pha->fcports, list) {
  2448. if (fcport->vp_idx != ha->vp_idx)
  2449. continue;
  2450. if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
  2451. (fcport->d_id.b24 & mask) != d_id.b24 ||
  2452. fcport->port_type == FCT_BROADCAST)
  2453. continue;
  2454. if (atomic_read(&fcport->state) == FCS_ONLINE) {
  2455. if (format != 3 ||
  2456. fcport->port_type != FCT_INITIATOR) {
  2457. qla2x00_mark_device_lost(ha, fcport,
  2458. 0, 0);
  2459. }
  2460. }
  2461. fcport->flags &= ~FCF_FARP_DONE;
  2462. }
  2463. }
  2464. return (rval);
  2465. }
  2466. /*
  2467. * qla2x00_fabric_dev_login
  2468. * Login fabric target device and update FC port database.
  2469. *
  2470. * Input:
  2471. * ha: adapter state pointer.
  2472. * fcport: port structure list pointer.
  2473. * next_loopid: contains value of a new loop ID that can be used
  2474. * by the next login attempt.
  2475. *
  2476. * Returns:
  2477. * qla2x00 local function return status code.
  2478. *
  2479. * Context:
  2480. * Kernel context.
  2481. */
  2482. static int
  2483. qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
  2484. uint16_t *next_loopid)
  2485. {
  2486. int rval;
  2487. int retry;
  2488. uint8_t opts;
  2489. rval = QLA_SUCCESS;
  2490. retry = 0;
  2491. rval = qla2x00_fabric_login(ha, fcport, next_loopid);
  2492. if (rval == QLA_SUCCESS) {
  2493. /* Send an ADISC to tape devices.*/
  2494. opts = 0;
  2495. if (fcport->flags & FCF_TAPE_PRESENT)
  2496. opts |= BIT_1;
  2497. rval = qla2x00_get_port_database(ha, fcport, opts);
  2498. if (rval != QLA_SUCCESS) {
  2499. ha->isp_ops->fabric_logout(ha, fcport->loop_id,
  2500. fcport->d_id.b.domain, fcport->d_id.b.area,
  2501. fcport->d_id.b.al_pa);
  2502. qla2x00_mark_device_lost(ha, fcport, 1, 0);
  2503. } else {
  2504. qla2x00_update_fcport(ha, fcport);
  2505. }
  2506. }
  2507. return (rval);
  2508. }
  2509. /*
  2510. * qla2x00_fabric_login
  2511. * Issue fabric login command.
  2512. *
  2513. * Input:
  2514. * ha = adapter block pointer.
  2515. * device = pointer to FC device type structure.
  2516. *
  2517. * Returns:
  2518. * 0 - Login successfully
  2519. * 1 - Login failed
  2520. * 2 - Initiator device
  2521. * 3 - Fatal error
  2522. */
  2523. int
  2524. qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
  2525. uint16_t *next_loopid)
  2526. {
  2527. int rval;
  2528. int retry;
  2529. uint16_t tmp_loopid;
  2530. uint16_t mb[MAILBOX_REGISTER_COUNT];
  2531. retry = 0;
  2532. tmp_loopid = 0;
  2533. for (;;) {
  2534. DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
  2535. "for port %02x%02x%02x.\n",
  2536. ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
  2537. fcport->d_id.b.area, fcport->d_id.b.al_pa));
  2538. /* Login fcport on switch. */
  2539. ha->isp_ops->fabric_login(ha, fcport->loop_id,
  2540. fcport->d_id.b.domain, fcport->d_id.b.area,
  2541. fcport->d_id.b.al_pa, mb, BIT_0);
  2542. if (mb[0] == MBS_PORT_ID_USED) {
  2543. /*
  2544. * Device has another loop ID. The firmware team
  2545. * recommends the driver perform an implicit login with
  2546. * the specified ID again. The ID we just used is save
  2547. * here so we return with an ID that can be tried by
  2548. * the next login.
  2549. */
  2550. retry++;
  2551. tmp_loopid = fcport->loop_id;
  2552. fcport->loop_id = mb[1];
  2553. DEBUG(printk("Fabric Login: port in use - next "
  2554. "loop id=0x%04x, port Id=%02x%02x%02x.\n",
  2555. fcport->loop_id, fcport->d_id.b.domain,
  2556. fcport->d_id.b.area, fcport->d_id.b.al_pa));
  2557. } else if (mb[0] == MBS_COMMAND_COMPLETE) {
  2558. /*
  2559. * Login succeeded.
  2560. */
  2561. if (retry) {
  2562. /* A retry occurred before. */
  2563. *next_loopid = tmp_loopid;
  2564. } else {
  2565. /*
  2566. * No retry occurred before. Just increment the
  2567. * ID value for next login.
  2568. */
  2569. *next_loopid = (fcport->loop_id + 1);
  2570. }
  2571. if (mb[1] & BIT_0) {
  2572. fcport->port_type = FCT_INITIATOR;
  2573. } else {
  2574. fcport->port_type = FCT_TARGET;
  2575. if (mb[1] & BIT_1) {
  2576. fcport->flags |= FCF_TAPE_PRESENT;
  2577. }
  2578. }
  2579. if (mb[10] & BIT_0)
  2580. fcport->supported_classes |= FC_COS_CLASS2;
  2581. if (mb[10] & BIT_1)
  2582. fcport->supported_classes |= FC_COS_CLASS3;
  2583. rval = QLA_SUCCESS;
  2584. break;
  2585. } else if (mb[0] == MBS_LOOP_ID_USED) {
  2586. /*
  2587. * Loop ID already used, try next loop ID.
  2588. */
  2589. fcport->loop_id++;
  2590. rval = qla2x00_find_new_loop_id(ha, fcport);
  2591. if (rval != QLA_SUCCESS) {
  2592. /* Ran out of loop IDs to use */
  2593. break;
  2594. }
  2595. } else if (mb[0] == MBS_COMMAND_ERROR) {
  2596. /*
  2597. * Firmware possibly timed out during login. If NO
  2598. * retries are left to do then the device is declared
  2599. * dead.
  2600. */
  2601. *next_loopid = fcport->loop_id;
  2602. ha->isp_ops->fabric_logout(ha, fcport->loop_id,
  2603. fcport->d_id.b.domain, fcport->d_id.b.area,
  2604. fcport->d_id.b.al_pa);
  2605. qla2x00_mark_device_lost(ha, fcport, 1, 0);
  2606. rval = 1;
  2607. break;
  2608. } else {
  2609. /*
  2610. * unrecoverable / not handled error
  2611. */
  2612. DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
  2613. "loop_id=%x jiffies=%lx.\n",
  2614. __func__, ha->host_no, mb[0],
  2615. fcport->d_id.b.domain, fcport->d_id.b.area,
  2616. fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
  2617. *next_loopid = fcport->loop_id;
  2618. ha->isp_ops->fabric_logout(ha, fcport->loop_id,
  2619. fcport->d_id.b.domain, fcport->d_id.b.area,
  2620. fcport->d_id.b.al_pa);
  2621. fcport->loop_id = FC_NO_LOOP_ID;
  2622. fcport->login_retry = 0;
  2623. rval = 3;
  2624. break;
  2625. }
  2626. }
  2627. return (rval);
  2628. }
  2629. /*
  2630. * qla2x00_local_device_login
  2631. * Issue local device login command.
  2632. *
  2633. * Input:
  2634. * ha = adapter block pointer.
  2635. * loop_id = loop id of device to login to.
  2636. *
  2637. * Returns (Where's the #define!!!!):
  2638. * 0 - Login successfully
  2639. * 1 - Login failed
  2640. * 3 - Fatal error
  2641. */
  2642. int
  2643. qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
  2644. {
  2645. int rval;
  2646. uint16_t mb[MAILBOX_REGISTER_COUNT];
  2647. memset(mb, 0, sizeof(mb));
  2648. rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
  2649. if (rval == QLA_SUCCESS) {
  2650. /* Interrogate mailbox registers for any errors */
  2651. if (mb[0] == MBS_COMMAND_ERROR)
  2652. rval = 1;
  2653. else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
  2654. /* device not in PCB table */
  2655. rval = 3;
  2656. }
  2657. return (rval);
  2658. }
  2659. /*
  2660. * qla2x00_loop_resync
  2661. * Resync with fibre channel devices.
  2662. *
  2663. * Input:
  2664. * ha = adapter block pointer.
  2665. *
  2666. * Returns:
  2667. * 0 = success
  2668. */
  2669. int
  2670. qla2x00_loop_resync(scsi_qla_host_t *ha)
  2671. {
  2672. int rval;
  2673. uint32_t wait_time;
  2674. rval = QLA_SUCCESS;
  2675. atomic_set(&ha->loop_state, LOOP_UPDATE);
  2676. clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
  2677. if (ha->flags.online) {
  2678. if (!(rval = qla2x00_fw_ready(ha))) {
  2679. /* Wait at most MAX_TARGET RSCNs for a stable link. */
  2680. wait_time = 256;
  2681. do {
  2682. atomic_set(&ha->loop_state, LOOP_UPDATE);
  2683. /* Issue a marker after FW becomes ready. */
  2684. qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
  2685. ha->marker_needed = 0;
  2686. /* Remap devices on Loop. */
  2687. clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  2688. qla2x00_configure_loop(ha);
  2689. wait_time--;
  2690. } while (!atomic_read(&ha->loop_down_timer) &&
  2691. !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
  2692. wait_time &&
  2693. (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
  2694. }
  2695. }
  2696. if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
  2697. return (QLA_FUNCTION_FAILED);
  2698. }
  2699. if (rval) {
  2700. DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
  2701. }
  2702. return (rval);
  2703. }
  2704. void
  2705. qla2x00_rescan_fcports(scsi_qla_host_t *ha)
  2706. {
  2707. int rescan_done;
  2708. fc_port_t *fcport;
  2709. rescan_done = 0;
  2710. list_for_each_entry(fcport, &ha->fcports, list) {
  2711. if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
  2712. continue;
  2713. qla2x00_update_fcport(ha, fcport);
  2714. fcport->flags &= ~FCF_RESCAN_NEEDED;
  2715. rescan_done = 1;
  2716. }
  2717. qla2x00_probe_for_all_luns(ha);
  2718. }
  2719. void
  2720. qla2x00_update_fcports(scsi_qla_host_t *ha)
  2721. {
  2722. fc_port_t *fcport;
  2723. /* Go with deferred removal of rport references. */
  2724. list_for_each_entry(fcport, &ha->fcports, list)
  2725. if (fcport->drport)
  2726. qla2x00_rport_del(fcport);
  2727. }
  2728. /*
  2729. * qla2x00_abort_isp
  2730. * Resets ISP and aborts all outstanding commands.
  2731. *
  2732. * Input:
  2733. * ha = adapter block pointer.
  2734. *
  2735. * Returns:
  2736. * 0 = success
  2737. */
  2738. int
  2739. qla2x00_abort_isp(scsi_qla_host_t *ha)
  2740. {
  2741. int rval;
  2742. unsigned long flags = 0;
  2743. uint16_t cnt;
  2744. srb_t *sp;
  2745. uint8_t status = 0;
  2746. if (ha->flags.online) {
  2747. ha->flags.online = 0;
  2748. clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  2749. qla_printk(KERN_INFO, ha,
  2750. "Performing ISP error recovery - ha= %p.\n", ha);
  2751. ha->isp_ops->reset_chip(ha);
  2752. atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
  2753. if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
  2754. atomic_set(&ha->loop_state, LOOP_DOWN);
  2755. qla2x00_mark_all_devices_lost(ha, 0);
  2756. } else {
  2757. if (!atomic_read(&ha->loop_down_timer))
  2758. atomic_set(&ha->loop_down_timer,
  2759. LOOP_DOWN_TIME);
  2760. }
  2761. spin_lock_irqsave(&ha->hardware_lock, flags);
  2762. /* Requeue all commands in outstanding command list. */
  2763. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  2764. sp = ha->outstanding_cmds[cnt];
  2765. if (sp) {
  2766. ha->outstanding_cmds[cnt] = NULL;
  2767. sp->flags = 0;
  2768. sp->cmd->result = DID_RESET << 16;
  2769. sp->cmd->host_scribble = (unsigned char *)NULL;
  2770. qla2x00_sp_compl(ha, sp);
  2771. }
  2772. }
  2773. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2774. ha->isp_ops->get_flash_version(ha, ha->request_ring);
  2775. ha->isp_ops->nvram_config(ha);
  2776. if (!qla2x00_restart_isp(ha)) {
  2777. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  2778. if (!atomic_read(&ha->loop_down_timer)) {
  2779. /*
  2780. * Issue marker command only when we are going
  2781. * to start the I/O .
  2782. */
  2783. ha->marker_needed = 1;
  2784. }
  2785. ha->flags.online = 1;
  2786. ha->isp_ops->enable_intrs(ha);
  2787. ha->isp_abort_cnt = 0;
  2788. clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
  2789. if (ha->eft) {
  2790. rval = qla2x00_trace_control(ha, TC_ENABLE,
  2791. ha->eft_dma, EFT_NUM_BUFFERS);
  2792. if (rval) {
  2793. qla_printk(KERN_WARNING, ha,
  2794. "Unable to reinitialize EFT "
  2795. "(%d).\n", rval);
  2796. }
  2797. }
  2798. } else { /* failed the ISP abort */
  2799. ha->flags.online = 1;
  2800. if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
  2801. if (ha->isp_abort_cnt == 0) {
  2802. qla_printk(KERN_WARNING, ha,
  2803. "ISP error recovery failed - "
  2804. "board disabled\n");
  2805. /*
  2806. * The next call disables the board
  2807. * completely.
  2808. */
  2809. ha->isp_ops->reset_adapter(ha);
  2810. ha->flags.online = 0;
  2811. clear_bit(ISP_ABORT_RETRY,
  2812. &ha->dpc_flags);
  2813. status = 0;
  2814. } else { /* schedule another ISP abort */
  2815. ha->isp_abort_cnt--;
  2816. DEBUG(printk("qla%ld: ISP abort - "
  2817. "retry remaining %d\n",
  2818. ha->host_no, ha->isp_abort_cnt));
  2819. status = 1;
  2820. }
  2821. } else {
  2822. ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
  2823. DEBUG(printk("qla2x00(%ld): ISP error recovery "
  2824. "- retrying (%d) more times\n",
  2825. ha->host_no, ha->isp_abort_cnt));
  2826. set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
  2827. status = 1;
  2828. }
  2829. }
  2830. }
  2831. if (status) {
  2832. qla_printk(KERN_INFO, ha,
  2833. "qla2x00_abort_isp: **** FAILED ****\n");
  2834. } else {
  2835. DEBUG(printk(KERN_INFO
  2836. "qla2x00_abort_isp(%ld): exiting.\n",
  2837. ha->host_no));
  2838. }
  2839. return(status);
  2840. }
  2841. /*
  2842. * qla2x00_restart_isp
  2843. * restarts the ISP after a reset
  2844. *
  2845. * Input:
  2846. * ha = adapter block pointer.
  2847. *
  2848. * Returns:
  2849. * 0 = success
  2850. */
  2851. static int
  2852. qla2x00_restart_isp(scsi_qla_host_t *ha)
  2853. {
  2854. uint8_t status = 0;
  2855. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  2856. unsigned long flags = 0;
  2857. uint32_t wait_time;
  2858. /* If firmware needs to be loaded */
  2859. if (qla2x00_isp_firmware(ha)) {
  2860. ha->flags.online = 0;
  2861. if (!(status = ha->isp_ops->chip_diag(ha))) {
  2862. if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
  2863. status = qla2x00_setup_chip(ha);
  2864. goto done;
  2865. }
  2866. spin_lock_irqsave(&ha->hardware_lock, flags);
  2867. if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
  2868. /*
  2869. * Disable SRAM, Instruction RAM and GP RAM
  2870. * parity.
  2871. */
  2872. WRT_REG_WORD(&reg->hccr,
  2873. (HCCR_ENABLE_PARITY + 0x0));
  2874. RD_REG_WORD(&reg->hccr);
  2875. }
  2876. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2877. status = qla2x00_setup_chip(ha);
  2878. spin_lock_irqsave(&ha->hardware_lock, flags);
  2879. if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
  2880. /* Enable proper parity */
  2881. if (IS_QLA2300(ha))
  2882. /* SRAM parity */
  2883. WRT_REG_WORD(&reg->hccr,
  2884. (HCCR_ENABLE_PARITY + 0x1));
  2885. else
  2886. /*
  2887. * SRAM, Instruction RAM and GP RAM
  2888. * parity.
  2889. */
  2890. WRT_REG_WORD(&reg->hccr,
  2891. (HCCR_ENABLE_PARITY + 0x7));
  2892. RD_REG_WORD(&reg->hccr);
  2893. }
  2894. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2895. }
  2896. }
  2897. done:
  2898. if (!status && !(status = qla2x00_init_rings(ha))) {
  2899. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  2900. if (!(status = qla2x00_fw_ready(ha))) {
  2901. DEBUG(printk("%s(): Start configure loop, "
  2902. "status = %d\n", __func__, status));
  2903. /* Issue a marker after FW becomes ready. */
  2904. qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
  2905. ha->flags.online = 1;
  2906. /* Wait at most MAX_TARGET RSCNs for a stable link. */
  2907. wait_time = 256;
  2908. do {
  2909. clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  2910. qla2x00_configure_loop(ha);
  2911. wait_time--;
  2912. } while (!atomic_read(&ha->loop_down_timer) &&
  2913. !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
  2914. wait_time &&
  2915. (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
  2916. }
  2917. /* if no cable then assume it's good */
  2918. if ((ha->device_flags & DFLG_NO_CABLE))
  2919. status = 0;
  2920. DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
  2921. __func__,
  2922. status));
  2923. }
  2924. return (status);
  2925. }
  2926. /*
  2927. * qla2x00_reset_adapter
  2928. * Reset adapter.
  2929. *
  2930. * Input:
  2931. * ha = adapter block pointer.
  2932. */
  2933. void
  2934. qla2x00_reset_adapter(scsi_qla_host_t *ha)
  2935. {
  2936. unsigned long flags = 0;
  2937. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  2938. ha->flags.online = 0;
  2939. ha->isp_ops->disable_intrs(ha);
  2940. spin_lock_irqsave(&ha->hardware_lock, flags);
  2941. WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
  2942. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  2943. WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
  2944. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  2945. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2946. }
  2947. void
  2948. qla24xx_reset_adapter(scsi_qla_host_t *ha)
  2949. {
  2950. unsigned long flags = 0;
  2951. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  2952. ha->flags.online = 0;
  2953. ha->isp_ops->disable_intrs(ha);
  2954. spin_lock_irqsave(&ha->hardware_lock, flags);
  2955. WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
  2956. RD_REG_DWORD(&reg->hccr);
  2957. WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
  2958. RD_REG_DWORD(&reg->hccr);
  2959. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2960. }
  2961. /* On sparc systems, obtain port and node WWN from firmware
  2962. * properties.
  2963. */
  2964. static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *ha, struct nvram_24xx *nv)
  2965. {
  2966. #ifdef CONFIG_SPARC
  2967. struct pci_dev *pdev = ha->pdev;
  2968. struct device_node *dp = pci_device_to_OF_node(pdev);
  2969. const u8 *val;
  2970. int len;
  2971. val = of_get_property(dp, "port-wwn", &len);
  2972. if (val && len >= WWN_SIZE)
  2973. memcpy(nv->port_name, val, WWN_SIZE);
  2974. val = of_get_property(dp, "node-wwn", &len);
  2975. if (val && len >= WWN_SIZE)
  2976. memcpy(nv->node_name, val, WWN_SIZE);
  2977. #endif
  2978. }
  2979. int
  2980. qla24xx_nvram_config(scsi_qla_host_t *ha)
  2981. {
  2982. int rval;
  2983. struct init_cb_24xx *icb;
  2984. struct nvram_24xx *nv;
  2985. uint32_t *dptr;
  2986. uint8_t *dptr1, *dptr2;
  2987. uint32_t chksum;
  2988. uint16_t cnt;
  2989. rval = QLA_SUCCESS;
  2990. icb = (struct init_cb_24xx *)ha->init_cb;
  2991. nv = ha->nvram;
  2992. /* Determine NVRAM starting address. */
  2993. ha->nvram_size = sizeof(struct nvram_24xx);
  2994. ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
  2995. ha->vpd_size = FA_NVRAM_VPD_SIZE;
  2996. ha->vpd_base = FA_NVRAM_VPD0_ADDR;
  2997. if (PCI_FUNC(ha->pdev->devfn)) {
  2998. ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
  2999. ha->vpd_base = FA_NVRAM_VPD1_ADDR;
  3000. }
  3001. /* Get VPD data into cache */
  3002. ha->vpd = ha->nvram + VPD_OFFSET;
  3003. ha->isp_ops->read_nvram(ha, (uint8_t *)ha->vpd,
  3004. ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
  3005. /* Get NVRAM data into cache and calculate checksum. */
  3006. dptr = (uint32_t *)nv;
  3007. ha->isp_ops->read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
  3008. ha->nvram_size);
  3009. for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
  3010. chksum += le32_to_cpu(*dptr++);
  3011. DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
  3012. DEBUG5(qla2x00_dump_buffer((uint8_t *)nv, ha->nvram_size));
  3013. /* Bad NVRAM data, set defaults parameters. */
  3014. if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
  3015. || nv->id[3] != ' ' ||
  3016. nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
  3017. /* Reset NVRAM data. */
  3018. qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
  3019. "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
  3020. le16_to_cpu(nv->nvram_version));
  3021. qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
  3022. "invalid -- WWPN) defaults.\n");
  3023. /*
  3024. * Set default initialization control block.
  3025. */
  3026. memset(nv, 0, ha->nvram_size);
  3027. nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
  3028. nv->version = __constant_cpu_to_le16(ICB_VERSION);
  3029. nv->frame_payload_size = __constant_cpu_to_le16(2048);
  3030. nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
  3031. nv->exchange_count = __constant_cpu_to_le16(0);
  3032. nv->hard_address = __constant_cpu_to_le16(124);
  3033. nv->port_name[0] = 0x21;
  3034. nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
  3035. nv->port_name[2] = 0x00;
  3036. nv->port_name[3] = 0xe0;
  3037. nv->port_name[4] = 0x8b;
  3038. nv->port_name[5] = 0x1c;
  3039. nv->port_name[6] = 0x55;
  3040. nv->port_name[7] = 0x86;
  3041. nv->node_name[0] = 0x20;
  3042. nv->node_name[1] = 0x00;
  3043. nv->node_name[2] = 0x00;
  3044. nv->node_name[3] = 0xe0;
  3045. nv->node_name[4] = 0x8b;
  3046. nv->node_name[5] = 0x1c;
  3047. nv->node_name[6] = 0x55;
  3048. nv->node_name[7] = 0x86;
  3049. qla24xx_nvram_wwn_from_ofw(ha, nv);
  3050. nv->login_retry_count = __constant_cpu_to_le16(8);
  3051. nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
  3052. nv->login_timeout = __constant_cpu_to_le16(0);
  3053. nv->firmware_options_1 =
  3054. __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
  3055. nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
  3056. nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
  3057. nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
  3058. nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
  3059. nv->efi_parameters = __constant_cpu_to_le32(0);
  3060. nv->reset_delay = 5;
  3061. nv->max_luns_per_target = __constant_cpu_to_le16(128);
  3062. nv->port_down_retry_count = __constant_cpu_to_le16(30);
  3063. nv->link_down_timeout = __constant_cpu_to_le16(30);
  3064. rval = 1;
  3065. }
  3066. /* Reset Initialization control block */
  3067. memset(icb, 0, sizeof(struct init_cb_24xx));
  3068. /* Copy 1st segment. */
  3069. dptr1 = (uint8_t *)icb;
  3070. dptr2 = (uint8_t *)&nv->version;
  3071. cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
  3072. while (cnt--)
  3073. *dptr1++ = *dptr2++;
  3074. icb->login_retry_count = nv->login_retry_count;
  3075. icb->link_down_on_nos = nv->link_down_on_nos;
  3076. /* Copy 2nd segment. */
  3077. dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
  3078. dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
  3079. cnt = (uint8_t *)&icb->reserved_3 -
  3080. (uint8_t *)&icb->interrupt_delay_timer;
  3081. while (cnt--)
  3082. *dptr1++ = *dptr2++;
  3083. /*
  3084. * Setup driver NVRAM options.
  3085. */
  3086. qla2x00_set_model_info(ha, nv->model_name, sizeof(nv->model_name),
  3087. "QLA2462");
  3088. /* Use alternate WWN? */
  3089. if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
  3090. memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
  3091. memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
  3092. }
  3093. /* Prepare nodename */
  3094. if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
  3095. /*
  3096. * Firmware will apply the following mask if the nodename was
  3097. * not provided.
  3098. */
  3099. memcpy(icb->node_name, icb->port_name, WWN_SIZE);
  3100. icb->node_name[0] &= 0xF0;
  3101. }
  3102. /* Set host adapter parameters. */
  3103. ha->flags.disable_risc_code_load = 0;
  3104. ha->flags.enable_lip_reset = 0;
  3105. ha->flags.enable_lip_full_login =
  3106. le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
  3107. ha->flags.enable_target_reset =
  3108. le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
  3109. ha->flags.enable_led_scheme = 0;
  3110. ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
  3111. ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
  3112. (BIT_6 | BIT_5 | BIT_4)) >> 4;
  3113. memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
  3114. sizeof(ha->fw_seriallink_options24));
  3115. /* save HBA serial number */
  3116. ha->serial0 = icb->port_name[5];
  3117. ha->serial1 = icb->port_name[6];
  3118. ha->serial2 = icb->port_name[7];
  3119. ha->node_name = icb->node_name;
  3120. ha->port_name = icb->port_name;
  3121. icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
  3122. ha->retry_count = le16_to_cpu(nv->login_retry_count);
  3123. /* Set minimum login_timeout to 4 seconds. */
  3124. if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
  3125. nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
  3126. if (le16_to_cpu(nv->login_timeout) < 4)
  3127. nv->login_timeout = __constant_cpu_to_le16(4);
  3128. ha->login_timeout = le16_to_cpu(nv->login_timeout);
  3129. icb->login_timeout = cpu_to_le16(nv->login_timeout);
  3130. /* Set minimum RATOV to 200 tenths of a second. */
  3131. ha->r_a_tov = 200;
  3132. ha->loop_reset_delay = nv->reset_delay;
  3133. /* Link Down Timeout = 0:
  3134. *
  3135. * When Port Down timer expires we will start returning
  3136. * I/O's to OS with "DID_NO_CONNECT".
  3137. *
  3138. * Link Down Timeout != 0:
  3139. *
  3140. * The driver waits for the link to come up after link down
  3141. * before returning I/Os to OS with "DID_NO_CONNECT".
  3142. */
  3143. if (le16_to_cpu(nv->link_down_timeout) == 0) {
  3144. ha->loop_down_abort_time =
  3145. (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
  3146. } else {
  3147. ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
  3148. ha->loop_down_abort_time =
  3149. (LOOP_DOWN_TIME - ha->link_down_timeout);
  3150. }
  3151. /* Need enough time to try and get the port back. */
  3152. ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
  3153. if (qlport_down_retry)
  3154. ha->port_down_retry_count = qlport_down_retry;
  3155. /* Set login_retry_count */
  3156. ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
  3157. if (ha->port_down_retry_count ==
  3158. le16_to_cpu(nv->port_down_retry_count) &&
  3159. ha->port_down_retry_count > 3)
  3160. ha->login_retry_count = ha->port_down_retry_count;
  3161. else if (ha->port_down_retry_count > (int)ha->login_retry_count)
  3162. ha->login_retry_count = ha->port_down_retry_count;
  3163. if (ql2xloginretrycount)
  3164. ha->login_retry_count = ql2xloginretrycount;
  3165. /* Enable ZIO. */
  3166. if (!ha->flags.init_done) {
  3167. ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
  3168. (BIT_3 | BIT_2 | BIT_1 | BIT_0);
  3169. ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
  3170. le16_to_cpu(icb->interrupt_delay_timer): 2;
  3171. }
  3172. icb->firmware_options_2 &= __constant_cpu_to_le32(
  3173. ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
  3174. ha->flags.process_response_queue = 0;
  3175. if (ha->zio_mode != QLA_ZIO_DISABLED) {
  3176. ha->zio_mode = QLA_ZIO_MODE_6;
  3177. DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
  3178. "(%d us).\n", ha->host_no, ha->zio_mode,
  3179. ha->zio_timer * 100));
  3180. qla_printk(KERN_INFO, ha,
  3181. "ZIO mode %d enabled; timer delay (%d us).\n",
  3182. ha->zio_mode, ha->zio_timer * 100);
  3183. icb->firmware_options_2 |= cpu_to_le32(
  3184. (uint32_t)ha->zio_mode);
  3185. icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
  3186. ha->flags.process_response_queue = 1;
  3187. }
  3188. if (rval) {
  3189. DEBUG2_3(printk(KERN_WARNING
  3190. "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
  3191. }
  3192. return (rval);
  3193. }
  3194. static int
  3195. qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
  3196. {
  3197. int rval;
  3198. int segments, fragment;
  3199. uint32_t faddr;
  3200. uint32_t *dcode, dlen;
  3201. uint32_t risc_addr;
  3202. uint32_t risc_size;
  3203. uint32_t i;
  3204. rval = QLA_SUCCESS;
  3205. segments = FA_RISC_CODE_SEGMENTS;
  3206. faddr = FA_RISC_CODE_ADDR;
  3207. dcode = (uint32_t *)ha->request_ring;
  3208. *srisc_addr = 0;
  3209. /* Validate firmware image by checking version. */
  3210. qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
  3211. for (i = 0; i < 4; i++)
  3212. dcode[i] = be32_to_cpu(dcode[i]);
  3213. if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
  3214. dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
  3215. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  3216. dcode[3] == 0)) {
  3217. qla_printk(KERN_WARNING, ha,
  3218. "Unable to verify integrity of flash firmware image!\n");
  3219. qla_printk(KERN_WARNING, ha,
  3220. "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
  3221. dcode[1], dcode[2], dcode[3]);
  3222. return QLA_FUNCTION_FAILED;
  3223. }
  3224. while (segments && rval == QLA_SUCCESS) {
  3225. /* Read segment's load information. */
  3226. qla24xx_read_flash_data(ha, dcode, faddr, 4);
  3227. risc_addr = be32_to_cpu(dcode[2]);
  3228. *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
  3229. risc_size = be32_to_cpu(dcode[3]);
  3230. fragment = 0;
  3231. while (risc_size > 0 && rval == QLA_SUCCESS) {
  3232. dlen = (uint32_t)(ha->fw_transfer_size >> 2);
  3233. if (dlen > risc_size)
  3234. dlen = risc_size;
  3235. DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
  3236. "addr %x, number of dwords 0x%x, offset 0x%x.\n",
  3237. ha->host_no, risc_addr, dlen, faddr));
  3238. qla24xx_read_flash_data(ha, dcode, faddr, dlen);
  3239. for (i = 0; i < dlen; i++)
  3240. dcode[i] = swab32(dcode[i]);
  3241. rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
  3242. dlen);
  3243. if (rval) {
  3244. DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
  3245. "segment %d of firmware\n", ha->host_no,
  3246. fragment));
  3247. qla_printk(KERN_WARNING, ha,
  3248. "[ERROR] Failed to load segment %d of "
  3249. "firmware\n", fragment);
  3250. break;
  3251. }
  3252. faddr += dlen;
  3253. risc_addr += dlen;
  3254. risc_size -= dlen;
  3255. fragment++;
  3256. }
  3257. /* Next segment. */
  3258. segments--;
  3259. }
  3260. return rval;
  3261. }
  3262. #define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
  3263. int
  3264. qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
  3265. {
  3266. int rval;
  3267. int i, fragment;
  3268. uint16_t *wcode, *fwcode;
  3269. uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
  3270. struct fw_blob *blob;
  3271. /* Load firmware blob. */
  3272. blob = qla2x00_request_firmware(ha);
  3273. if (!blob) {
  3274. qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
  3275. qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
  3276. "from: " QLA_FW_URL ".\n");
  3277. return QLA_FUNCTION_FAILED;
  3278. }
  3279. rval = QLA_SUCCESS;
  3280. wcode = (uint16_t *)ha->request_ring;
  3281. *srisc_addr = 0;
  3282. fwcode = (uint16_t *)blob->fw->data;
  3283. fwclen = 0;
  3284. /* Validate firmware image by checking version. */
  3285. if (blob->fw->size < 8 * sizeof(uint16_t)) {
  3286. qla_printk(KERN_WARNING, ha,
  3287. "Unable to verify integrity of firmware image (%Zd)!\n",
  3288. blob->fw->size);
  3289. goto fail_fw_integrity;
  3290. }
  3291. for (i = 0; i < 4; i++)
  3292. wcode[i] = be16_to_cpu(fwcode[i + 4]);
  3293. if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
  3294. wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
  3295. wcode[2] == 0 && wcode[3] == 0)) {
  3296. qla_printk(KERN_WARNING, ha,
  3297. "Unable to verify integrity of firmware image!\n");
  3298. qla_printk(KERN_WARNING, ha,
  3299. "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
  3300. wcode[1], wcode[2], wcode[3]);
  3301. goto fail_fw_integrity;
  3302. }
  3303. seg = blob->segs;
  3304. while (*seg && rval == QLA_SUCCESS) {
  3305. risc_addr = *seg;
  3306. *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
  3307. risc_size = be16_to_cpu(fwcode[3]);
  3308. /* Validate firmware image size. */
  3309. fwclen += risc_size * sizeof(uint16_t);
  3310. if (blob->fw->size < fwclen) {
  3311. qla_printk(KERN_WARNING, ha,
  3312. "Unable to verify integrity of firmware image "
  3313. "(%Zd)!\n", blob->fw->size);
  3314. goto fail_fw_integrity;
  3315. }
  3316. fragment = 0;
  3317. while (risc_size > 0 && rval == QLA_SUCCESS) {
  3318. wlen = (uint16_t)(ha->fw_transfer_size >> 1);
  3319. if (wlen > risc_size)
  3320. wlen = risc_size;
  3321. DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
  3322. "addr %x, number of words 0x%x.\n", ha->host_no,
  3323. risc_addr, wlen));
  3324. for (i = 0; i < wlen; i++)
  3325. wcode[i] = swab16(fwcode[i]);
  3326. rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
  3327. wlen);
  3328. if (rval) {
  3329. DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
  3330. "segment %d of firmware\n", ha->host_no,
  3331. fragment));
  3332. qla_printk(KERN_WARNING, ha,
  3333. "[ERROR] Failed to load segment %d of "
  3334. "firmware\n", fragment);
  3335. break;
  3336. }
  3337. fwcode += wlen;
  3338. risc_addr += wlen;
  3339. risc_size -= wlen;
  3340. fragment++;
  3341. }
  3342. /* Next segment. */
  3343. seg++;
  3344. }
  3345. return rval;
  3346. fail_fw_integrity:
  3347. return QLA_FUNCTION_FAILED;
  3348. }
  3349. int
  3350. qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
  3351. {
  3352. int rval;
  3353. int segments, fragment;
  3354. uint32_t *dcode, dlen;
  3355. uint32_t risc_addr;
  3356. uint32_t risc_size;
  3357. uint32_t i;
  3358. struct fw_blob *blob;
  3359. uint32_t *fwcode, fwclen;
  3360. /* Load firmware blob. */
  3361. blob = qla2x00_request_firmware(ha);
  3362. if (!blob) {
  3363. qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
  3364. qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
  3365. "from: " QLA_FW_URL ".\n");
  3366. /* Try to load RISC code from flash. */
  3367. qla_printk(KERN_ERR, ha, "Attempting to load (potentially "
  3368. "outdated) firmware from flash.\n");
  3369. return qla24xx_load_risc_flash(ha, srisc_addr);
  3370. }
  3371. rval = QLA_SUCCESS;
  3372. segments = FA_RISC_CODE_SEGMENTS;
  3373. dcode = (uint32_t *)ha->request_ring;
  3374. *srisc_addr = 0;
  3375. fwcode = (uint32_t *)blob->fw->data;
  3376. fwclen = 0;
  3377. /* Validate firmware image by checking version. */
  3378. if (blob->fw->size < 8 * sizeof(uint32_t)) {
  3379. qla_printk(KERN_WARNING, ha,
  3380. "Unable to verify integrity of firmware image (%Zd)!\n",
  3381. blob->fw->size);
  3382. goto fail_fw_integrity;
  3383. }
  3384. for (i = 0; i < 4; i++)
  3385. dcode[i] = be32_to_cpu(fwcode[i + 4]);
  3386. if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
  3387. dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
  3388. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  3389. dcode[3] == 0)) {
  3390. qla_printk(KERN_WARNING, ha,
  3391. "Unable to verify integrity of firmware image!\n");
  3392. qla_printk(KERN_WARNING, ha,
  3393. "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
  3394. dcode[1], dcode[2], dcode[3]);
  3395. goto fail_fw_integrity;
  3396. }
  3397. while (segments && rval == QLA_SUCCESS) {
  3398. risc_addr = be32_to_cpu(fwcode[2]);
  3399. *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
  3400. risc_size = be32_to_cpu(fwcode[3]);
  3401. /* Validate firmware image size. */
  3402. fwclen += risc_size * sizeof(uint32_t);
  3403. if (blob->fw->size < fwclen) {
  3404. qla_printk(KERN_WARNING, ha,
  3405. "Unable to verify integrity of firmware image "
  3406. "(%Zd)!\n", blob->fw->size);
  3407. goto fail_fw_integrity;
  3408. }
  3409. fragment = 0;
  3410. while (risc_size > 0 && rval == QLA_SUCCESS) {
  3411. dlen = (uint32_t)(ha->fw_transfer_size >> 2);
  3412. if (dlen > risc_size)
  3413. dlen = risc_size;
  3414. DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
  3415. "addr %x, number of dwords 0x%x.\n", ha->host_no,
  3416. risc_addr, dlen));
  3417. for (i = 0; i < dlen; i++)
  3418. dcode[i] = swab32(fwcode[i]);
  3419. rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
  3420. dlen);
  3421. if (rval) {
  3422. DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
  3423. "segment %d of firmware\n", ha->host_no,
  3424. fragment));
  3425. qla_printk(KERN_WARNING, ha,
  3426. "[ERROR] Failed to load segment %d of "
  3427. "firmware\n", fragment);
  3428. break;
  3429. }
  3430. fwcode += dlen;
  3431. risc_addr += dlen;
  3432. risc_size -= dlen;
  3433. fragment++;
  3434. }
  3435. /* Next segment. */
  3436. segments--;
  3437. }
  3438. return rval;
  3439. fail_fw_integrity:
  3440. return QLA_FUNCTION_FAILED;
  3441. }
  3442. void
  3443. qla2x00_try_to_stop_firmware(scsi_qla_host_t *ha)
  3444. {
  3445. int ret, retries;
  3446. if (!IS_FWI2_CAPABLE(ha))
  3447. return;
  3448. if (!ha->fw_major_version)
  3449. return;
  3450. ret = qla2x00_stop_firmware(ha);
  3451. for (retries = 5; ret != QLA_SUCCESS && retries ; retries--) {
  3452. qla2x00_reset_chip(ha);
  3453. if (qla2x00_chip_diag(ha) != QLA_SUCCESS)
  3454. continue;
  3455. if (qla2x00_setup_chip(ha) != QLA_SUCCESS)
  3456. continue;
  3457. qla_printk(KERN_INFO, ha,
  3458. "Attempting retry of stop-firmware command...\n");
  3459. ret = qla2x00_stop_firmware(ha);
  3460. }
  3461. }
  3462. int
  3463. qla24xx_configure_vhba(scsi_qla_host_t *ha)
  3464. {
  3465. int rval = QLA_SUCCESS;
  3466. uint16_t mb[MAILBOX_REGISTER_COUNT];
  3467. if (!ha->parent)
  3468. return -EINVAL;
  3469. rval = qla2x00_fw_ready(ha);
  3470. if (rval == QLA_SUCCESS) {
  3471. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  3472. qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
  3473. }
  3474. ha->flags.management_server_logged_in = 0;
  3475. /* Login to SNS first */
  3476. qla24xx_login_fabric(ha, NPH_SNS, 0xff, 0xff, 0xfc,
  3477. mb, BIT_1);
  3478. if (mb[0] != MBS_COMMAND_COMPLETE) {
  3479. DEBUG15(qla_printk(KERN_INFO, ha,
  3480. "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
  3481. "mb[2]=%x mb[6]=%x mb[7]=%x\n", NPH_SNS,
  3482. mb[0], mb[1], mb[2], mb[6], mb[7]));
  3483. return (QLA_FUNCTION_FAILED);
  3484. }
  3485. atomic_set(&ha->loop_down_timer, 0);
  3486. atomic_set(&ha->loop_state, LOOP_UP);
  3487. set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  3488. set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
  3489. rval = qla2x00_loop_resync(ha);
  3490. return rval;
  3491. }