qla_init.c 96 KB

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