qla_init.c 96 KB

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