ql4_os.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085
  1. /*
  2. * QLogic iSCSI HBA Driver
  3. * Copyright (c) 2003-2010 QLogic Corporation
  4. *
  5. * See LICENSE.qla4xxx for copyright and licensing details.
  6. */
  7. #include <linux/moduleparam.h>
  8. #include <linux/slab.h>
  9. #include <scsi/scsi_tcq.h>
  10. #include <scsi/scsicam.h>
  11. #include "ql4_def.h"
  12. #include "ql4_version.h"
  13. #include "ql4_glbl.h"
  14. #include "ql4_dbg.h"
  15. #include "ql4_inline.h"
  16. /*
  17. * Driver version
  18. */
  19. static char qla4xxx_version_str[40];
  20. /*
  21. * SRB allocation cache
  22. */
  23. static struct kmem_cache *srb_cachep;
  24. /*
  25. * Module parameter information and variables
  26. */
  27. int ql4xdontresethba = 0;
  28. module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
  29. MODULE_PARM_DESC(ql4xdontresethba,
  30. "Don't reset the HBA for driver recovery \n"
  31. " 0 - It will reset HBA (Default)\n"
  32. " 1 - It will NOT reset HBA");
  33. int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
  34. module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
  35. MODULE_PARM_DESC(ql4xextended_error_logging,
  36. "Option to enable extended error logging, "
  37. "Default is 0 - no logging, 1 - debug logging");
  38. int ql4xenablemsix = 1;
  39. module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
  40. MODULE_PARM_DESC(ql4xenablemsix,
  41. "Set to enable MSI or MSI-X interrupt mechanism.\n"
  42. " 0 = enable INTx interrupt mechanism.\n"
  43. " 1 = enable MSI-X interrupt mechanism (Default).\n"
  44. " 2 = enable MSI interrupt mechanism.");
  45. #define QL4_DEF_QDEPTH 32
  46. static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
  47. module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
  48. MODULE_PARM_DESC(ql4xmaxqdepth,
  49. "Maximum queue depth to report for target devices.\n"
  50. " Default: 32.");
  51. static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
  52. module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
  53. MODULE_PARM_DESC(ql4xsess_recovery_tmo,
  54. "Target Session Recovery Timeout.\n"
  55. " Default: 30 sec.");
  56. /*
  57. * SCSI host template entry points
  58. */
  59. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
  60. /*
  61. * iSCSI template entry points
  62. */
  63. static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
  64. enum iscsi_tgt_dscvr type, uint32_t enable,
  65. struct sockaddr *dst_addr);
  66. static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
  67. enum iscsi_param param, char *buf);
  68. static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
  69. enum iscsi_param param, char *buf);
  70. static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  71. enum iscsi_host_param param, char *buf);
  72. static int qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data,
  73. int count);
  74. static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
  75. enum iscsi_param_type param_type,
  76. int param, char *buf);
  77. static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
  78. static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
  79. /*
  80. * SCSI host template entry points
  81. */
  82. static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
  83. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
  84. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
  85. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
  86. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
  87. static int qla4xxx_slave_alloc(struct scsi_device *device);
  88. static int qla4xxx_slave_configure(struct scsi_device *device);
  89. static void qla4xxx_slave_destroy(struct scsi_device *sdev);
  90. static void qla4xxx_scan_start(struct Scsi_Host *shost);
  91. static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
  92. QLA82XX_LEGACY_INTR_CONFIG;
  93. static struct scsi_host_template qla4xxx_driver_template = {
  94. .module = THIS_MODULE,
  95. .name = DRIVER_NAME,
  96. .proc_name = DRIVER_NAME,
  97. .queuecommand = qla4xxx_queuecommand,
  98. .eh_abort_handler = qla4xxx_eh_abort,
  99. .eh_device_reset_handler = qla4xxx_eh_device_reset,
  100. .eh_target_reset_handler = qla4xxx_eh_target_reset,
  101. .eh_host_reset_handler = qla4xxx_eh_host_reset,
  102. .eh_timed_out = qla4xxx_eh_cmd_timed_out,
  103. .slave_configure = qla4xxx_slave_configure,
  104. .slave_alloc = qla4xxx_slave_alloc,
  105. .slave_destroy = qla4xxx_slave_destroy,
  106. .scan_finished = iscsi_scan_finished,
  107. .scan_start = qla4xxx_scan_start,
  108. .this_id = -1,
  109. .cmd_per_lun = 3,
  110. .use_clustering = ENABLE_CLUSTERING,
  111. .sg_tablesize = SG_ALL,
  112. .max_sectors = 0xFFFF,
  113. .shost_attrs = qla4xxx_host_attrs,
  114. };
  115. static struct iscsi_transport qla4xxx_iscsi_transport = {
  116. .owner = THIS_MODULE,
  117. .name = DRIVER_NAME,
  118. .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
  119. CAP_DATA_PATH_OFFLOAD,
  120. .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
  121. ISCSI_TARGET_NAME | ISCSI_TPGT |
  122. ISCSI_TARGET_ALIAS,
  123. .host_param_mask = ISCSI_HOST_HWADDRESS |
  124. ISCSI_HOST_IPADDRESS |
  125. ISCSI_HOST_INITIATOR_NAME,
  126. .iface_param_mask = ISCSI_NET_IPV4_ADDR |
  127. ISCSI_NET_IPV4_SUBNET |
  128. ISCSI_NET_IPV4_GW |
  129. ISCSI_NET_IPV4_BOOTPROTO |
  130. ISCSI_NET_IFACE_ENABLE |
  131. ISCSI_NET_IPV6_LINKLOCAL |
  132. ISCSI_NET_IPV6_ADDR |
  133. ISCSI_NET_IPV6_ROUTER |
  134. ISCSI_NET_IPV6_ADDR_AUTOCFG |
  135. ISCSI_NET_IPV6_LINKLOCAL_AUTOCFG |
  136. ISCSI_NET_IFACE_ENABLE,
  137. .tgt_dscvr = qla4xxx_tgt_dscvr,
  138. .get_conn_param = qla4xxx_conn_get_param,
  139. .get_session_param = qla4xxx_sess_get_param,
  140. .get_host_param = qla4xxx_host_get_param,
  141. .set_iface_param = qla4xxx_iface_set_param,
  142. .session_recovery_timedout = qla4xxx_recovery_timedout,
  143. .get_iface_param = qla4xxx_get_iface_param,
  144. };
  145. static struct scsi_transport_template *qla4xxx_scsi_transport;
  146. static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
  147. enum iscsi_param_type param_type,
  148. int param, char *buf)
  149. {
  150. struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
  151. struct scsi_qla_host *ha = to_qla_host(shost);
  152. int len = -ENOSYS;
  153. if (param_type != ISCSI_NET_PARAM)
  154. return -ENOSYS;
  155. switch (param) {
  156. case ISCSI_NET_PARAM_IPV4_ADDR:
  157. len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
  158. break;
  159. case ISCSI_NET_PARAM_IPV4_SUBNET:
  160. len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
  161. break;
  162. case ISCSI_NET_PARAM_IPV4_GW:
  163. len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
  164. break;
  165. case ISCSI_NET_PARAM_IFACE_ENABLE:
  166. if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  167. len = sprintf(buf, "%s\n",
  168. (ha->ip_config.ipv4_options &
  169. IPOPT_IPV4_PROTOCOL_ENABLE) ?
  170. "enabled" : "disabled");
  171. else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  172. len = sprintf(buf, "%s\n",
  173. (ha->ip_config.ipv6_options &
  174. IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
  175. "enabled" : "disabled");
  176. break;
  177. case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
  178. len = sprintf(buf, "%s\n",
  179. (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
  180. "dhcp" : "static");
  181. break;
  182. case ISCSI_NET_PARAM_IPV6_ADDR:
  183. if (iface->iface_num == 0)
  184. len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
  185. if (iface->iface_num == 1)
  186. len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
  187. break;
  188. case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
  189. len = sprintf(buf, "%pI6\n",
  190. &ha->ip_config.ipv6_link_local_addr);
  191. break;
  192. case ISCSI_NET_PARAM_IPV6_ROUTER:
  193. len = sprintf(buf, "%pI6\n",
  194. &ha->ip_config.ipv6_default_router_addr);
  195. break;
  196. case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
  197. len = sprintf(buf, "%s\n",
  198. (ha->ip_config.ipv6_addl_options &
  199. IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
  200. "nd" : "static");
  201. break;
  202. case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
  203. len = sprintf(buf, "%s\n",
  204. (ha->ip_config.ipv6_addl_options &
  205. IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
  206. "auto" : "static");
  207. break;
  208. default:
  209. len = -ENOSYS;
  210. }
  211. return len;
  212. }
  213. static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
  214. {
  215. struct iscsi_cls_session *session;
  216. struct ddb_entry *ddb_entry;
  217. session = starget_to_session(scsi_target(sc->device));
  218. ddb_entry = session->dd_data;
  219. /* if we are not logged in then the LLD is going to clean up the cmd */
  220. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
  221. return BLK_EH_RESET_TIMER;
  222. else
  223. return BLK_EH_NOT_HANDLED;
  224. }
  225. static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
  226. {
  227. struct ddb_entry *ddb_entry = session->dd_data;
  228. struct scsi_qla_host *ha = ddb_entry->ha;
  229. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  230. atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
  231. DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
  232. "of (%d) secs exhausted, marking device DEAD.\n",
  233. ha->host_no, __func__, ddb_entry->fw_ddb_index,
  234. ddb_entry->sess->recovery_tmo));
  235. }
  236. }
  237. static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  238. enum iscsi_host_param param, char *buf)
  239. {
  240. struct scsi_qla_host *ha = to_qla_host(shost);
  241. int len;
  242. switch (param) {
  243. case ISCSI_HOST_PARAM_HWADDRESS:
  244. len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
  245. break;
  246. case ISCSI_HOST_PARAM_IPADDRESS:
  247. len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
  248. break;
  249. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  250. len = sprintf(buf, "%s\n", ha->name_string);
  251. break;
  252. default:
  253. return -ENOSYS;
  254. }
  255. return len;
  256. }
  257. static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
  258. {
  259. if (ha->iface_ipv4)
  260. return;
  261. /* IPv4 */
  262. ha->iface_ipv4 = iscsi_create_iface(ha->host,
  263. &qla4xxx_iscsi_transport,
  264. ISCSI_IFACE_TYPE_IPV4, 0, 0);
  265. if (!ha->iface_ipv4)
  266. ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
  267. "iface0.\n");
  268. }
  269. static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
  270. {
  271. if (!ha->iface_ipv6_0)
  272. /* IPv6 iface-0 */
  273. ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
  274. &qla4xxx_iscsi_transport,
  275. ISCSI_IFACE_TYPE_IPV6, 0,
  276. 0);
  277. if (!ha->iface_ipv6_0)
  278. ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
  279. "iface0.\n");
  280. if (!ha->iface_ipv6_1)
  281. /* IPv6 iface-1 */
  282. ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
  283. &qla4xxx_iscsi_transport,
  284. ISCSI_IFACE_TYPE_IPV6, 1,
  285. 0);
  286. if (!ha->iface_ipv6_1)
  287. ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
  288. "iface1.\n");
  289. }
  290. static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
  291. {
  292. if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
  293. qla4xxx_create_ipv4_iface(ha);
  294. if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
  295. qla4xxx_create_ipv6_iface(ha);
  296. }
  297. static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
  298. {
  299. if (ha->iface_ipv4) {
  300. iscsi_destroy_iface(ha->iface_ipv4);
  301. ha->iface_ipv4 = NULL;
  302. }
  303. }
  304. static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
  305. {
  306. if (ha->iface_ipv6_0) {
  307. iscsi_destroy_iface(ha->iface_ipv6_0);
  308. ha->iface_ipv6_0 = NULL;
  309. }
  310. if (ha->iface_ipv6_1) {
  311. iscsi_destroy_iface(ha->iface_ipv6_1);
  312. ha->iface_ipv6_1 = NULL;
  313. }
  314. }
  315. static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
  316. {
  317. qla4xxx_destroy_ipv4_iface(ha);
  318. qla4xxx_destroy_ipv6_iface(ha);
  319. }
  320. static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
  321. struct iscsi_iface_param_info *iface_param,
  322. struct addr_ctrl_blk *init_fw_cb)
  323. {
  324. /*
  325. * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
  326. * iface_num 1 is valid only for IPv6 Addr.
  327. */
  328. switch (iface_param->param) {
  329. case ISCSI_NET_PARAM_IPV6_ADDR:
  330. if (iface_param->iface_num & 0x1)
  331. /* IPv6 Addr 1 */
  332. memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
  333. sizeof(init_fw_cb->ipv6_addr1));
  334. else
  335. /* IPv6 Addr 0 */
  336. memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
  337. sizeof(init_fw_cb->ipv6_addr0));
  338. break;
  339. case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
  340. if (iface_param->iface_num & 0x1)
  341. break;
  342. memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
  343. sizeof(init_fw_cb->ipv6_if_id));
  344. break;
  345. case ISCSI_NET_PARAM_IPV6_ROUTER:
  346. if (iface_param->iface_num & 0x1)
  347. break;
  348. memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
  349. sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
  350. break;
  351. case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
  352. /* Autocfg applies to even interface */
  353. if (iface_param->iface_num & 0x1)
  354. break;
  355. if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
  356. init_fw_cb->ipv6_addtl_opts &=
  357. cpu_to_le16(
  358. ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
  359. else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
  360. init_fw_cb->ipv6_addtl_opts |=
  361. cpu_to_le16(
  362. IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
  363. else
  364. ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
  365. "IPv6 addr\n");
  366. break;
  367. case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
  368. /* Autocfg applies to even interface */
  369. if (iface_param->iface_num & 0x1)
  370. break;
  371. if (iface_param->value[0] ==
  372. ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
  373. init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
  374. IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
  375. else if (iface_param->value[0] ==
  376. ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
  377. init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
  378. ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
  379. else
  380. ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
  381. "IPv6 linklocal addr\n");
  382. break;
  383. case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
  384. /* Autocfg applies to even interface */
  385. if (iface_param->iface_num & 0x1)
  386. break;
  387. if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
  388. memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
  389. sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
  390. break;
  391. case ISCSI_NET_PARAM_IFACE_ENABLE:
  392. if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
  393. init_fw_cb->ipv6_opts |=
  394. cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
  395. qla4xxx_create_ipv6_iface(ha);
  396. } else {
  397. init_fw_cb->ipv6_opts &=
  398. cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
  399. 0xFFFF);
  400. qla4xxx_destroy_ipv6_iface(ha);
  401. }
  402. break;
  403. case ISCSI_NET_PARAM_VLAN_ID:
  404. if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
  405. break;
  406. init_fw_cb->ipv6_vlan_tag = *(uint16_t *)iface_param->value;
  407. break;
  408. default:
  409. ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
  410. iface_param->param);
  411. break;
  412. }
  413. }
  414. static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
  415. struct iscsi_iface_param_info *iface_param,
  416. struct addr_ctrl_blk *init_fw_cb)
  417. {
  418. switch (iface_param->param) {
  419. case ISCSI_NET_PARAM_IPV4_ADDR:
  420. memcpy(init_fw_cb->ipv4_addr, iface_param->value,
  421. sizeof(init_fw_cb->ipv4_addr));
  422. break;
  423. case ISCSI_NET_PARAM_IPV4_SUBNET:
  424. memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
  425. sizeof(init_fw_cb->ipv4_subnet));
  426. break;
  427. case ISCSI_NET_PARAM_IPV4_GW:
  428. memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
  429. sizeof(init_fw_cb->ipv4_gw_addr));
  430. break;
  431. case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
  432. if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
  433. init_fw_cb->ipv4_tcp_opts |=
  434. cpu_to_le16(TCPOPT_DHCP_ENABLE);
  435. else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
  436. init_fw_cb->ipv4_tcp_opts &=
  437. cpu_to_le16(~TCPOPT_DHCP_ENABLE);
  438. else
  439. ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
  440. break;
  441. case ISCSI_NET_PARAM_IFACE_ENABLE:
  442. if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
  443. init_fw_cb->ipv4_ip_opts |=
  444. cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
  445. qla4xxx_create_ipv4_iface(ha);
  446. } else {
  447. init_fw_cb->ipv4_ip_opts &=
  448. cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
  449. 0xFFFF);
  450. qla4xxx_destroy_ipv4_iface(ha);
  451. }
  452. break;
  453. case ISCSI_NET_PARAM_VLAN_ID:
  454. if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
  455. break;
  456. init_fw_cb->ipv4_vlan_tag = *(uint16_t *)iface_param->value;
  457. break;
  458. default:
  459. ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
  460. iface_param->param);
  461. break;
  462. }
  463. }
  464. static void
  465. qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
  466. {
  467. struct addr_ctrl_blk_def *acb;
  468. acb = (struct addr_ctrl_blk_def *)init_fw_cb;
  469. memset(acb->reserved1, 0, sizeof(acb->reserved1));
  470. memset(acb->reserved2, 0, sizeof(acb->reserved2));
  471. memset(acb->reserved3, 0, sizeof(acb->reserved3));
  472. memset(acb->reserved4, 0, sizeof(acb->reserved4));
  473. memset(acb->reserved5, 0, sizeof(acb->reserved5));
  474. memset(acb->reserved6, 0, sizeof(acb->reserved6));
  475. memset(acb->reserved7, 0, sizeof(acb->reserved7));
  476. memset(acb->reserved8, 0, sizeof(acb->reserved8));
  477. memset(acb->reserved9, 0, sizeof(acb->reserved9));
  478. memset(acb->reserved10, 0, sizeof(acb->reserved10));
  479. memset(acb->reserved11, 0, sizeof(acb->reserved11));
  480. memset(acb->reserved12, 0, sizeof(acb->reserved12));
  481. memset(acb->reserved13, 0, sizeof(acb->reserved13));
  482. memset(acb->reserved14, 0, sizeof(acb->reserved14));
  483. memset(acb->reserved15, 0, sizeof(acb->reserved15));
  484. }
  485. static int
  486. qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data, int count)
  487. {
  488. struct scsi_qla_host *ha = to_qla_host(shost);
  489. int rval = 0;
  490. struct iscsi_iface_param_info *iface_param = NULL;
  491. struct addr_ctrl_blk *init_fw_cb = NULL;
  492. dma_addr_t init_fw_cb_dma;
  493. uint32_t mbox_cmd[MBOX_REG_COUNT];
  494. uint32_t mbox_sts[MBOX_REG_COUNT];
  495. uint32_t total_param_count;
  496. uint32_t length;
  497. init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
  498. sizeof(struct addr_ctrl_blk),
  499. &init_fw_cb_dma, GFP_KERNEL);
  500. if (!init_fw_cb) {
  501. ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
  502. __func__);
  503. return -ENOMEM;
  504. }
  505. memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
  506. memset(&mbox_cmd, 0, sizeof(mbox_cmd));
  507. memset(&mbox_sts, 0, sizeof(mbox_sts));
  508. if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
  509. ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
  510. rval = -EIO;
  511. goto exit_init_fw_cb;
  512. }
  513. total_param_count = count;
  514. iface_param = (struct iscsi_iface_param_info *)data;
  515. for ( ; total_param_count != 0; total_param_count--) {
  516. length = iface_param->len;
  517. if (iface_param->param_type != ISCSI_NET_PARAM)
  518. continue;
  519. switch (iface_param->iface_type) {
  520. case ISCSI_IFACE_TYPE_IPV4:
  521. switch (iface_param->iface_num) {
  522. case 0:
  523. qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
  524. break;
  525. default:
  526. /* Cannot have more than one IPv4 interface */
  527. ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
  528. "number = %d\n",
  529. iface_param->iface_num);
  530. break;
  531. }
  532. break;
  533. case ISCSI_IFACE_TYPE_IPV6:
  534. switch (iface_param->iface_num) {
  535. case 0:
  536. case 1:
  537. qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
  538. break;
  539. default:
  540. /* Cannot have more than two IPv6 interface */
  541. ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
  542. "number = %d\n",
  543. iface_param->iface_num);
  544. break;
  545. }
  546. break;
  547. default:
  548. ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
  549. break;
  550. }
  551. iface_param = (struct iscsi_iface_param_info *)
  552. ((uint8_t *)iface_param +
  553. sizeof(struct iscsi_iface_param_info) + length);
  554. }
  555. init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
  556. rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
  557. sizeof(struct addr_ctrl_blk),
  558. FLASH_OPT_RMW_COMMIT);
  559. if (rval != QLA_SUCCESS) {
  560. ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
  561. __func__);
  562. rval = -EIO;
  563. goto exit_init_fw_cb;
  564. }
  565. qla4xxx_disable_acb(ha);
  566. qla4xxx_initcb_to_acb(init_fw_cb);
  567. rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
  568. if (rval != QLA_SUCCESS) {
  569. ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
  570. __func__);
  571. rval = -EIO;
  572. goto exit_init_fw_cb;
  573. }
  574. memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
  575. qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
  576. init_fw_cb_dma);
  577. exit_init_fw_cb:
  578. dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
  579. init_fw_cb, init_fw_cb_dma);
  580. return rval;
  581. }
  582. static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
  583. enum iscsi_param param, char *buf)
  584. {
  585. struct ddb_entry *ddb_entry = sess->dd_data;
  586. int len;
  587. switch (param) {
  588. case ISCSI_PARAM_TARGET_NAME:
  589. len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
  590. ddb_entry->iscsi_name);
  591. break;
  592. case ISCSI_PARAM_TPGT:
  593. len = sprintf(buf, "%u\n", ddb_entry->tpgt);
  594. break;
  595. case ISCSI_PARAM_TARGET_ALIAS:
  596. len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
  597. ddb_entry->iscsi_alias);
  598. break;
  599. default:
  600. return -ENOSYS;
  601. }
  602. return len;
  603. }
  604. static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
  605. enum iscsi_param param, char *buf)
  606. {
  607. struct iscsi_cls_session *session;
  608. struct ddb_entry *ddb_entry;
  609. int len;
  610. session = iscsi_dev_to_session(conn->dev.parent);
  611. ddb_entry = session->dd_data;
  612. switch (param) {
  613. case ISCSI_PARAM_CONN_PORT:
  614. len = sprintf(buf, "%hu\n", ddb_entry->port);
  615. break;
  616. case ISCSI_PARAM_CONN_ADDRESS:
  617. /* TODO: what are the ipv6 bits */
  618. len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
  619. break;
  620. default:
  621. return -ENOSYS;
  622. }
  623. return len;
  624. }
  625. static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
  626. enum iscsi_tgt_dscvr type, uint32_t enable,
  627. struct sockaddr *dst_addr)
  628. {
  629. struct scsi_qla_host *ha;
  630. struct sockaddr_in *addr;
  631. struct sockaddr_in6 *addr6;
  632. int ret = 0;
  633. ha = (struct scsi_qla_host *) shost->hostdata;
  634. switch (type) {
  635. case ISCSI_TGT_DSCVR_SEND_TARGETS:
  636. if (dst_addr->sa_family == AF_INET) {
  637. addr = (struct sockaddr_in *)dst_addr;
  638. if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
  639. addr->sin_port) != QLA_SUCCESS)
  640. ret = -EIO;
  641. } else if (dst_addr->sa_family == AF_INET6) {
  642. /*
  643. * TODO: fix qla4xxx_send_tgts
  644. */
  645. addr6 = (struct sockaddr_in6 *)dst_addr;
  646. if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
  647. addr6->sin6_port) != QLA_SUCCESS)
  648. ret = -EIO;
  649. } else
  650. ret = -ENOSYS;
  651. break;
  652. default:
  653. ret = -ENOSYS;
  654. }
  655. return ret;
  656. }
  657. void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
  658. {
  659. if (!ddb_entry->sess)
  660. return;
  661. if (ddb_entry->conn) {
  662. atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
  663. iscsi_remove_session(ddb_entry->sess);
  664. }
  665. iscsi_free_session(ddb_entry->sess);
  666. }
  667. int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
  668. {
  669. int err;
  670. ddb_entry->sess->recovery_tmo = ql4xsess_recovery_tmo;
  671. err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
  672. if (err) {
  673. DEBUG2(printk(KERN_ERR "Could not add session.\n"));
  674. return err;
  675. }
  676. ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
  677. if (!ddb_entry->conn) {
  678. iscsi_remove_session(ddb_entry->sess);
  679. DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
  680. return -ENOMEM;
  681. }
  682. /* finally ready to go */
  683. iscsi_unblock_session(ddb_entry->sess);
  684. return 0;
  685. }
  686. struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
  687. {
  688. struct ddb_entry *ddb_entry;
  689. struct iscsi_cls_session *sess;
  690. sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
  691. sizeof(struct ddb_entry));
  692. if (!sess)
  693. return NULL;
  694. ddb_entry = sess->dd_data;
  695. memset(ddb_entry, 0, sizeof(*ddb_entry));
  696. ddb_entry->ha = ha;
  697. ddb_entry->sess = sess;
  698. return ddb_entry;
  699. }
  700. static void qla4xxx_scan_start(struct Scsi_Host *shost)
  701. {
  702. struct scsi_qla_host *ha = shost_priv(shost);
  703. struct ddb_entry *ddb_entry, *ddbtemp;
  704. /* finish setup of sessions that were already setup in firmware */
  705. list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
  706. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
  707. qla4xxx_add_sess(ddb_entry);
  708. }
  709. }
  710. /*
  711. * Timer routines
  712. */
  713. static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
  714. unsigned long interval)
  715. {
  716. DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
  717. __func__, ha->host->host_no));
  718. init_timer(&ha->timer);
  719. ha->timer.expires = jiffies + interval * HZ;
  720. ha->timer.data = (unsigned long)ha;
  721. ha->timer.function = (void (*)(unsigned long))func;
  722. add_timer(&ha->timer);
  723. ha->timer_active = 1;
  724. }
  725. static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
  726. {
  727. del_timer_sync(&ha->timer);
  728. ha->timer_active = 0;
  729. }
  730. /***
  731. * qla4xxx_mark_device_missing - mark a device as missing.
  732. * @ha: Pointer to host adapter structure.
  733. * @ddb_entry: Pointer to device database entry
  734. *
  735. * This routine marks a device missing and close connection.
  736. **/
  737. void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
  738. struct ddb_entry *ddb_entry)
  739. {
  740. if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
  741. atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
  742. DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
  743. ha->host_no, ddb_entry->fw_ddb_index));
  744. } else
  745. DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
  746. ddb_entry->fw_ddb_index))
  747. iscsi_block_session(ddb_entry->sess);
  748. iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
  749. }
  750. /**
  751. * qla4xxx_mark_all_devices_missing - mark all devices as missing.
  752. * @ha: Pointer to host adapter structure.
  753. *
  754. * This routine marks a device missing and resets the relogin retry count.
  755. **/
  756. void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
  757. {
  758. struct ddb_entry *ddb_entry, *ddbtemp;
  759. list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
  760. qla4xxx_mark_device_missing(ha, ddb_entry);
  761. }
  762. }
  763. static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
  764. struct ddb_entry *ddb_entry,
  765. struct scsi_cmnd *cmd)
  766. {
  767. struct srb *srb;
  768. srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
  769. if (!srb)
  770. return srb;
  771. kref_init(&srb->srb_ref);
  772. srb->ha = ha;
  773. srb->ddb = ddb_entry;
  774. srb->cmd = cmd;
  775. srb->flags = 0;
  776. CMD_SP(cmd) = (void *)srb;
  777. return srb;
  778. }
  779. static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
  780. {
  781. struct scsi_cmnd *cmd = srb->cmd;
  782. if (srb->flags & SRB_DMA_VALID) {
  783. scsi_dma_unmap(cmd);
  784. srb->flags &= ~SRB_DMA_VALID;
  785. }
  786. CMD_SP(cmd) = NULL;
  787. }
  788. void qla4xxx_srb_compl(struct kref *ref)
  789. {
  790. struct srb *srb = container_of(ref, struct srb, srb_ref);
  791. struct scsi_cmnd *cmd = srb->cmd;
  792. struct scsi_qla_host *ha = srb->ha;
  793. qla4xxx_srb_free_dma(ha, srb);
  794. mempool_free(srb, ha->srb_mempool);
  795. cmd->scsi_done(cmd);
  796. }
  797. /**
  798. * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
  799. * @host: scsi host
  800. * @cmd: Pointer to Linux's SCSI command structure
  801. *
  802. * Remarks:
  803. * This routine is invoked by Linux to send a SCSI command to the driver.
  804. * The mid-level driver tries to ensure that queuecommand never gets
  805. * invoked concurrently with itself or the interrupt handler (although
  806. * the interrupt handler may call this routine as part of request-
  807. * completion handling). Unfortunely, it sometimes calls the scheduler
  808. * in interrupt context which is a big NO! NO!.
  809. **/
  810. static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
  811. {
  812. struct scsi_qla_host *ha = to_qla_host(host);
  813. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  814. struct iscsi_cls_session *sess = ddb_entry->sess;
  815. struct srb *srb;
  816. int rval;
  817. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  818. if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
  819. cmd->result = DID_NO_CONNECT << 16;
  820. else
  821. cmd->result = DID_REQUEUE << 16;
  822. goto qc_fail_command;
  823. }
  824. if (!sess) {
  825. cmd->result = DID_IMM_RETRY << 16;
  826. goto qc_fail_command;
  827. }
  828. rval = iscsi_session_chkready(sess);
  829. if (rval) {
  830. cmd->result = rval;
  831. goto qc_fail_command;
  832. }
  833. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  834. if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
  835. cmd->result = DID_NO_CONNECT << 16;
  836. goto qc_fail_command;
  837. }
  838. return SCSI_MLQUEUE_TARGET_BUSY;
  839. }
  840. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  841. test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  842. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  843. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  844. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  845. !test_bit(AF_ONLINE, &ha->flags) ||
  846. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
  847. goto qc_host_busy;
  848. srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
  849. if (!srb)
  850. goto qc_host_busy;
  851. rval = qla4xxx_send_command_to_isp(ha, srb);
  852. if (rval != QLA_SUCCESS)
  853. goto qc_host_busy_free_sp;
  854. return 0;
  855. qc_host_busy_free_sp:
  856. qla4xxx_srb_free_dma(ha, srb);
  857. mempool_free(srb, ha->srb_mempool);
  858. qc_host_busy:
  859. return SCSI_MLQUEUE_HOST_BUSY;
  860. qc_fail_command:
  861. cmd->scsi_done(cmd);
  862. return 0;
  863. }
  864. /**
  865. * qla4xxx_mem_free - frees memory allocated to adapter
  866. * @ha: Pointer to host adapter structure.
  867. *
  868. * Frees memory previously allocated by qla4xxx_mem_alloc
  869. **/
  870. static void qla4xxx_mem_free(struct scsi_qla_host *ha)
  871. {
  872. if (ha->queues)
  873. dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
  874. ha->queues_dma);
  875. ha->queues_len = 0;
  876. ha->queues = NULL;
  877. ha->queues_dma = 0;
  878. ha->request_ring = NULL;
  879. ha->request_dma = 0;
  880. ha->response_ring = NULL;
  881. ha->response_dma = 0;
  882. ha->shadow_regs = NULL;
  883. ha->shadow_regs_dma = 0;
  884. /* Free srb pool. */
  885. if (ha->srb_mempool)
  886. mempool_destroy(ha->srb_mempool);
  887. ha->srb_mempool = NULL;
  888. /* release io space registers */
  889. if (is_qla8022(ha)) {
  890. if (ha->nx_pcibase)
  891. iounmap(
  892. (struct device_reg_82xx __iomem *)ha->nx_pcibase);
  893. } else if (ha->reg)
  894. iounmap(ha->reg);
  895. pci_release_regions(ha->pdev);
  896. }
  897. /**
  898. * qla4xxx_mem_alloc - allocates memory for use by adapter.
  899. * @ha: Pointer to host adapter structure
  900. *
  901. * Allocates DMA memory for request and response queues. Also allocates memory
  902. * for srbs.
  903. **/
  904. static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
  905. {
  906. unsigned long align;
  907. /* Allocate contiguous block of DMA memory for queues. */
  908. ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  909. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
  910. sizeof(struct shadow_regs) +
  911. MEM_ALIGN_VALUE +
  912. (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
  913. ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
  914. &ha->queues_dma, GFP_KERNEL);
  915. if (ha->queues == NULL) {
  916. ql4_printk(KERN_WARNING, ha,
  917. "Memory Allocation failed - queues.\n");
  918. goto mem_alloc_error_exit;
  919. }
  920. memset(ha->queues, 0, ha->queues_len);
  921. /*
  922. * As per RISC alignment requirements -- the bus-address must be a
  923. * multiple of the request-ring size (in bytes).
  924. */
  925. align = 0;
  926. if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
  927. align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
  928. (MEM_ALIGN_VALUE - 1));
  929. /* Update request and response queue pointers. */
  930. ha->request_dma = ha->queues_dma + align;
  931. ha->request_ring = (struct queue_entry *) (ha->queues + align);
  932. ha->response_dma = ha->queues_dma + align +
  933. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
  934. ha->response_ring = (struct queue_entry *) (ha->queues + align +
  935. (REQUEST_QUEUE_DEPTH *
  936. QUEUE_SIZE));
  937. ha->shadow_regs_dma = ha->queues_dma + align +
  938. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  939. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
  940. ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
  941. (REQUEST_QUEUE_DEPTH *
  942. QUEUE_SIZE) +
  943. (RESPONSE_QUEUE_DEPTH *
  944. QUEUE_SIZE));
  945. /* Allocate memory for srb pool. */
  946. ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
  947. mempool_free_slab, srb_cachep);
  948. if (ha->srb_mempool == NULL) {
  949. ql4_printk(KERN_WARNING, ha,
  950. "Memory Allocation failed - SRB Pool.\n");
  951. goto mem_alloc_error_exit;
  952. }
  953. return QLA_SUCCESS;
  954. mem_alloc_error_exit:
  955. qla4xxx_mem_free(ha);
  956. return QLA_ERROR;
  957. }
  958. /**
  959. * qla4_8xxx_check_fw_alive - Check firmware health
  960. * @ha: Pointer to host adapter structure.
  961. *
  962. * Context: Interrupt
  963. **/
  964. static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
  965. {
  966. uint32_t fw_heartbeat_counter, halt_status;
  967. fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
  968. /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
  969. if (fw_heartbeat_counter == 0xffffffff) {
  970. DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
  971. "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
  972. ha->host_no, __func__));
  973. return;
  974. }
  975. if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
  976. ha->seconds_since_last_heartbeat++;
  977. /* FW not alive after 2 seconds */
  978. if (ha->seconds_since_last_heartbeat == 2) {
  979. ha->seconds_since_last_heartbeat = 0;
  980. halt_status = qla4_8xxx_rd_32(ha,
  981. QLA82XX_PEG_HALT_STATUS1);
  982. ql4_printk(KERN_INFO, ha,
  983. "scsi(%ld): %s, Dumping hw/fw registers:\n "
  984. " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
  985. " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
  986. " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
  987. " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
  988. ha->host_no, __func__, halt_status,
  989. qla4_8xxx_rd_32(ha,
  990. QLA82XX_PEG_HALT_STATUS2),
  991. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
  992. 0x3c),
  993. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
  994. 0x3c),
  995. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
  996. 0x3c),
  997. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
  998. 0x3c),
  999. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
  1000. 0x3c));
  1001. /* Since we cannot change dev_state in interrupt
  1002. * context, set appropriate DPC flag then wakeup
  1003. * DPC */
  1004. if (halt_status & HALT_STATUS_UNRECOVERABLE)
  1005. set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
  1006. else {
  1007. printk("scsi%ld: %s: detect abort needed!\n",
  1008. ha->host_no, __func__);
  1009. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  1010. }
  1011. qla4xxx_wake_dpc(ha);
  1012. qla4xxx_mailbox_premature_completion(ha);
  1013. }
  1014. } else
  1015. ha->seconds_since_last_heartbeat = 0;
  1016. ha->fw_heartbeat_counter = fw_heartbeat_counter;
  1017. }
  1018. /**
  1019. * qla4_8xxx_watchdog - Poll dev state
  1020. * @ha: Pointer to host adapter structure.
  1021. *
  1022. * Context: Interrupt
  1023. **/
  1024. void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
  1025. {
  1026. uint32_t dev_state;
  1027. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  1028. /* don't poll if reset is going on */
  1029. if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  1030. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  1031. test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
  1032. if (dev_state == QLA82XX_DEV_NEED_RESET &&
  1033. !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  1034. if (!ql4xdontresethba) {
  1035. ql4_printk(KERN_INFO, ha, "%s: HW State: "
  1036. "NEED RESET!\n", __func__);
  1037. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  1038. qla4xxx_wake_dpc(ha);
  1039. qla4xxx_mailbox_premature_completion(ha);
  1040. }
  1041. } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
  1042. !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  1043. ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
  1044. __func__);
  1045. set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
  1046. qla4xxx_wake_dpc(ha);
  1047. } else {
  1048. /* Check firmware health */
  1049. qla4_8xxx_check_fw_alive(ha);
  1050. }
  1051. }
  1052. }
  1053. /**
  1054. * qla4xxx_timer - checks every second for work to do.
  1055. * @ha: Pointer to host adapter structure.
  1056. **/
  1057. static void qla4xxx_timer(struct scsi_qla_host *ha)
  1058. {
  1059. struct ddb_entry *ddb_entry, *dtemp;
  1060. int start_dpc = 0;
  1061. uint16_t w;
  1062. /* If we are in the middle of AER/EEH processing
  1063. * skip any processing and reschedule the timer
  1064. */
  1065. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  1066. mod_timer(&ha->timer, jiffies + HZ);
  1067. return;
  1068. }
  1069. /* Hardware read to trigger an EEH error during mailbox waits. */
  1070. if (!pci_channel_offline(ha->pdev))
  1071. pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
  1072. if (is_qla8022(ha)) {
  1073. qla4_8xxx_watchdog(ha);
  1074. }
  1075. /* Search for relogin's to time-out and port down retry. */
  1076. list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
  1077. /* Count down time between sending relogins */
  1078. if (adapter_up(ha) &&
  1079. !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
  1080. atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  1081. if (atomic_read(&ddb_entry->retry_relogin_timer) !=
  1082. INVALID_ENTRY) {
  1083. if (atomic_read(&ddb_entry->retry_relogin_timer)
  1084. == 0) {
  1085. atomic_set(&ddb_entry->
  1086. retry_relogin_timer,
  1087. INVALID_ENTRY);
  1088. set_bit(DPC_RELOGIN_DEVICE,
  1089. &ha->dpc_flags);
  1090. set_bit(DF_RELOGIN, &ddb_entry->flags);
  1091. DEBUG2(printk("scsi%ld: %s: ddb [%d]"
  1092. " login device\n",
  1093. ha->host_no, __func__,
  1094. ddb_entry->fw_ddb_index));
  1095. } else
  1096. atomic_dec(&ddb_entry->
  1097. retry_relogin_timer);
  1098. }
  1099. }
  1100. /* Wait for relogin to timeout */
  1101. if (atomic_read(&ddb_entry->relogin_timer) &&
  1102. (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
  1103. /*
  1104. * If the relogin times out and the device is
  1105. * still NOT ONLINE then try and relogin again.
  1106. */
  1107. if (atomic_read(&ddb_entry->state) !=
  1108. DDB_STATE_ONLINE &&
  1109. ddb_entry->fw_ddb_device_state ==
  1110. DDB_DS_SESSION_FAILED) {
  1111. /* Reset retry relogin timer */
  1112. atomic_inc(&ddb_entry->relogin_retry_count);
  1113. DEBUG2(printk("scsi%ld: ddb [%d] relogin"
  1114. " timed out-retrying"
  1115. " relogin (%d)\n",
  1116. ha->host_no,
  1117. ddb_entry->fw_ddb_index,
  1118. atomic_read(&ddb_entry->
  1119. relogin_retry_count))
  1120. );
  1121. start_dpc++;
  1122. DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
  1123. "initiate relogin after"
  1124. " %d seconds\n",
  1125. ha->host_no, ddb_entry->bus,
  1126. ddb_entry->target,
  1127. ddb_entry->fw_ddb_index,
  1128. ddb_entry->default_time2wait + 4)
  1129. );
  1130. atomic_set(&ddb_entry->retry_relogin_timer,
  1131. ddb_entry->default_time2wait + 4);
  1132. }
  1133. }
  1134. }
  1135. if (!is_qla8022(ha)) {
  1136. /* Check for heartbeat interval. */
  1137. if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
  1138. ha->heartbeat_interval != 0) {
  1139. ha->seconds_since_last_heartbeat++;
  1140. if (ha->seconds_since_last_heartbeat >
  1141. ha->heartbeat_interval + 2)
  1142. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  1143. }
  1144. }
  1145. /* Wakeup the dpc routine for this adapter, if needed. */
  1146. if (start_dpc ||
  1147. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  1148. test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
  1149. test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
  1150. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  1151. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  1152. test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
  1153. test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
  1154. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  1155. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  1156. test_bit(DPC_AEN, &ha->dpc_flags)) {
  1157. DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
  1158. " - dpc flags = 0x%lx\n",
  1159. ha->host_no, __func__, ha->dpc_flags));
  1160. qla4xxx_wake_dpc(ha);
  1161. }
  1162. /* Reschedule timer thread to call us back in one second */
  1163. mod_timer(&ha->timer, jiffies + HZ);
  1164. DEBUG2(ha->seconds_since_last_intr++);
  1165. }
  1166. /**
  1167. * qla4xxx_cmd_wait - waits for all outstanding commands to complete
  1168. * @ha: Pointer to host adapter structure.
  1169. *
  1170. * This routine stalls the driver until all outstanding commands are returned.
  1171. * Caller must release the Hardware Lock prior to calling this routine.
  1172. **/
  1173. static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
  1174. {
  1175. uint32_t index = 0;
  1176. unsigned long flags;
  1177. struct scsi_cmnd *cmd;
  1178. unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
  1179. DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
  1180. "complete\n", WAIT_CMD_TOV));
  1181. while (!time_after_eq(jiffies, wtime)) {
  1182. spin_lock_irqsave(&ha->hardware_lock, flags);
  1183. /* Find a command that hasn't completed. */
  1184. for (index = 0; index < ha->host->can_queue; index++) {
  1185. cmd = scsi_host_find_tag(ha->host, index);
  1186. /*
  1187. * We cannot just check if the index is valid,
  1188. * becase if we are run from the scsi eh, then
  1189. * the scsi/block layer is going to prevent
  1190. * the tag from being released.
  1191. */
  1192. if (cmd != NULL && CMD_SP(cmd))
  1193. break;
  1194. }
  1195. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1196. /* If No Commands are pending, wait is complete */
  1197. if (index == ha->host->can_queue)
  1198. return QLA_SUCCESS;
  1199. msleep(1000);
  1200. }
  1201. /* If we timed out on waiting for commands to come back
  1202. * return ERROR. */
  1203. return QLA_ERROR;
  1204. }
  1205. int qla4xxx_hw_reset(struct scsi_qla_host *ha)
  1206. {
  1207. uint32_t ctrl_status;
  1208. unsigned long flags = 0;
  1209. DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
  1210. if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  1211. return QLA_ERROR;
  1212. spin_lock_irqsave(&ha->hardware_lock, flags);
  1213. /*
  1214. * If the SCSI Reset Interrupt bit is set, clear it.
  1215. * Otherwise, the Soft Reset won't work.
  1216. */
  1217. ctrl_status = readw(&ha->reg->ctrl_status);
  1218. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
  1219. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  1220. /* Issue Soft Reset */
  1221. writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
  1222. readl(&ha->reg->ctrl_status);
  1223. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1224. return QLA_SUCCESS;
  1225. }
  1226. /**
  1227. * qla4xxx_soft_reset - performs soft reset.
  1228. * @ha: Pointer to host adapter structure.
  1229. **/
  1230. int qla4xxx_soft_reset(struct scsi_qla_host *ha)
  1231. {
  1232. uint32_t max_wait_time;
  1233. unsigned long flags = 0;
  1234. int status;
  1235. uint32_t ctrl_status;
  1236. status = qla4xxx_hw_reset(ha);
  1237. if (status != QLA_SUCCESS)
  1238. return status;
  1239. status = QLA_ERROR;
  1240. /* Wait until the Network Reset Intr bit is cleared */
  1241. max_wait_time = RESET_INTR_TOV;
  1242. do {
  1243. spin_lock_irqsave(&ha->hardware_lock, flags);
  1244. ctrl_status = readw(&ha->reg->ctrl_status);
  1245. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1246. if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
  1247. break;
  1248. msleep(1000);
  1249. } while ((--max_wait_time));
  1250. if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
  1251. DEBUG2(printk(KERN_WARNING
  1252. "scsi%ld: Network Reset Intr not cleared by "
  1253. "Network function, clearing it now!\n",
  1254. ha->host_no));
  1255. spin_lock_irqsave(&ha->hardware_lock, flags);
  1256. writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
  1257. readl(&ha->reg->ctrl_status);
  1258. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1259. }
  1260. /* Wait until the firmware tells us the Soft Reset is done */
  1261. max_wait_time = SOFT_RESET_TOV;
  1262. do {
  1263. spin_lock_irqsave(&ha->hardware_lock, flags);
  1264. ctrl_status = readw(&ha->reg->ctrl_status);
  1265. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1266. if ((ctrl_status & CSR_SOFT_RESET) == 0) {
  1267. status = QLA_SUCCESS;
  1268. break;
  1269. }
  1270. msleep(1000);
  1271. } while ((--max_wait_time));
  1272. /*
  1273. * Also, make sure that the SCSI Reset Interrupt bit has been cleared
  1274. * after the soft reset has taken place.
  1275. */
  1276. spin_lock_irqsave(&ha->hardware_lock, flags);
  1277. ctrl_status = readw(&ha->reg->ctrl_status);
  1278. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
  1279. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  1280. readl(&ha->reg->ctrl_status);
  1281. }
  1282. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1283. /* If soft reset fails then most probably the bios on other
  1284. * function is also enabled.
  1285. * Since the initialization is sequential the other fn
  1286. * wont be able to acknowledge the soft reset.
  1287. * Issue a force soft reset to workaround this scenario.
  1288. */
  1289. if (max_wait_time == 0) {
  1290. /* Issue Force Soft Reset */
  1291. spin_lock_irqsave(&ha->hardware_lock, flags);
  1292. writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
  1293. readl(&ha->reg->ctrl_status);
  1294. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1295. /* Wait until the firmware tells us the Soft Reset is done */
  1296. max_wait_time = SOFT_RESET_TOV;
  1297. do {
  1298. spin_lock_irqsave(&ha->hardware_lock, flags);
  1299. ctrl_status = readw(&ha->reg->ctrl_status);
  1300. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1301. if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
  1302. status = QLA_SUCCESS;
  1303. break;
  1304. }
  1305. msleep(1000);
  1306. } while ((--max_wait_time));
  1307. }
  1308. return status;
  1309. }
  1310. /**
  1311. * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
  1312. * @ha: Pointer to host adapter structure.
  1313. * @res: returned scsi status
  1314. *
  1315. * This routine is called just prior to a HARD RESET to return all
  1316. * outstanding commands back to the Operating System.
  1317. * Caller should make sure that the following locks are released
  1318. * before this calling routine: Hardware lock, and io_request_lock.
  1319. **/
  1320. static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
  1321. {
  1322. struct srb *srb;
  1323. int i;
  1324. unsigned long flags;
  1325. spin_lock_irqsave(&ha->hardware_lock, flags);
  1326. for (i = 0; i < ha->host->can_queue; i++) {
  1327. srb = qla4xxx_del_from_active_array(ha, i);
  1328. if (srb != NULL) {
  1329. srb->cmd->result = res;
  1330. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  1331. }
  1332. }
  1333. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1334. }
  1335. void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
  1336. {
  1337. clear_bit(AF_ONLINE, &ha->flags);
  1338. /* Disable the board */
  1339. ql4_printk(KERN_INFO, ha, "Disabling the board\n");
  1340. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  1341. qla4xxx_mark_all_devices_missing(ha);
  1342. clear_bit(AF_INIT_DONE, &ha->flags);
  1343. }
  1344. /**
  1345. * qla4xxx_recover_adapter - recovers adapter after a fatal error
  1346. * @ha: Pointer to host adapter structure.
  1347. **/
  1348. static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
  1349. {
  1350. int status = QLA_ERROR;
  1351. uint8_t reset_chip = 0;
  1352. /* Stall incoming I/O until we are done */
  1353. scsi_block_requests(ha->host);
  1354. clear_bit(AF_ONLINE, &ha->flags);
  1355. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
  1356. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  1357. if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
  1358. reset_chip = 1;
  1359. /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
  1360. * do not reset adapter, jump to initialize_adapter */
  1361. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  1362. status = QLA_SUCCESS;
  1363. goto recover_ha_init_adapter;
  1364. }
  1365. /* For the ISP-82xx adapter, issue a stop_firmware if invoked
  1366. * from eh_host_reset or ioctl module */
  1367. if (is_qla8022(ha) && !reset_chip &&
  1368. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
  1369. DEBUG2(ql4_printk(KERN_INFO, ha,
  1370. "scsi%ld: %s - Performing stop_firmware...\n",
  1371. ha->host_no, __func__));
  1372. status = ha->isp_ops->reset_firmware(ha);
  1373. if (status == QLA_SUCCESS) {
  1374. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  1375. qla4xxx_cmd_wait(ha);
  1376. ha->isp_ops->disable_intrs(ha);
  1377. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  1378. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  1379. } else {
  1380. /* If the stop_firmware fails then
  1381. * reset the entire chip */
  1382. reset_chip = 1;
  1383. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1384. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  1385. }
  1386. }
  1387. /* Issue full chip reset if recovering from a catastrophic error,
  1388. * or if stop_firmware fails for ISP-82xx.
  1389. * This is the default case for ISP-4xxx */
  1390. if (!is_qla8022(ha) || reset_chip) {
  1391. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  1392. qla4xxx_cmd_wait(ha);
  1393. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  1394. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  1395. DEBUG2(ql4_printk(KERN_INFO, ha,
  1396. "scsi%ld: %s - Performing chip reset..\n",
  1397. ha->host_no, __func__));
  1398. status = ha->isp_ops->reset_chip(ha);
  1399. }
  1400. /* Flush any pending ddb changed AENs */
  1401. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  1402. recover_ha_init_adapter:
  1403. /* Upon successful firmware/chip reset, re-initialize the adapter */
  1404. if (status == QLA_SUCCESS) {
  1405. /* For ISP-4xxx, force function 1 to always initialize
  1406. * before function 3 to prevent both funcions from
  1407. * stepping on top of the other */
  1408. if (!is_qla8022(ha) && (ha->mac_index == 3))
  1409. ssleep(6);
  1410. /* NOTE: AF_ONLINE flag set upon successful completion of
  1411. * qla4xxx_initialize_adapter */
  1412. status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
  1413. }
  1414. /* Retry failed adapter initialization, if necessary
  1415. * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
  1416. * case to prevent ping-pong resets between functions */
  1417. if (!test_bit(AF_ONLINE, &ha->flags) &&
  1418. !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  1419. /* Adapter initialization failed, see if we can retry
  1420. * resetting the ha.
  1421. * Since we don't want to block the DPC for too long
  1422. * with multiple resets in the same thread,
  1423. * utilize DPC to retry */
  1424. if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
  1425. ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
  1426. DEBUG2(printk("scsi%ld: recover adapter - retrying "
  1427. "(%d) more times\n", ha->host_no,
  1428. ha->retry_reset_ha_cnt));
  1429. set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1430. status = QLA_ERROR;
  1431. } else {
  1432. if (ha->retry_reset_ha_cnt > 0) {
  1433. /* Schedule another Reset HA--DPC will retry */
  1434. ha->retry_reset_ha_cnt--;
  1435. DEBUG2(printk("scsi%ld: recover adapter - "
  1436. "retry remaining %d\n",
  1437. ha->host_no,
  1438. ha->retry_reset_ha_cnt));
  1439. status = QLA_ERROR;
  1440. }
  1441. if (ha->retry_reset_ha_cnt == 0) {
  1442. /* Recover adapter retries have been exhausted.
  1443. * Adapter DEAD */
  1444. DEBUG2(printk("scsi%ld: recover adapter "
  1445. "failed - board disabled\n",
  1446. ha->host_no));
  1447. qla4xxx_dead_adapter_cleanup(ha);
  1448. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1449. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1450. clear_bit(DPC_RESET_HA_FW_CONTEXT,
  1451. &ha->dpc_flags);
  1452. status = QLA_ERROR;
  1453. }
  1454. }
  1455. } else {
  1456. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1457. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1458. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1459. }
  1460. ha->adapter_error_count++;
  1461. if (test_bit(AF_ONLINE, &ha->flags))
  1462. ha->isp_ops->enable_intrs(ha);
  1463. scsi_unblock_requests(ha->host);
  1464. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  1465. DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
  1466. status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
  1467. return status;
  1468. }
  1469. static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
  1470. {
  1471. struct ddb_entry *ddb_entry, *dtemp;
  1472. list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
  1473. if ((atomic_read(&ddb_entry->state) == DDB_STATE_MISSING) ||
  1474. (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD)) {
  1475. if (ddb_entry->fw_ddb_device_state ==
  1476. DDB_DS_SESSION_ACTIVE) {
  1477. atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
  1478. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  1479. " marked ONLINE\n", ha->host_no, __func__,
  1480. ddb_entry->fw_ddb_index);
  1481. iscsi_unblock_session(ddb_entry->sess);
  1482. } else
  1483. qla4xxx_relogin_device(ha, ddb_entry);
  1484. }
  1485. }
  1486. }
  1487. void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
  1488. {
  1489. if (ha->dpc_thread)
  1490. queue_work(ha->dpc_thread, &ha->dpc_work);
  1491. }
  1492. /**
  1493. * qla4xxx_do_dpc - dpc routine
  1494. * @data: in our case pointer to adapter structure
  1495. *
  1496. * This routine is a task that is schedule by the interrupt handler
  1497. * to perform the background processing for interrupts. We put it
  1498. * on a task queue that is consumed whenever the scheduler runs; that's
  1499. * so you can do anything (i.e. put the process to sleep etc). In fact,
  1500. * the mid-level tries to sleep when it reaches the driver threshold
  1501. * "host->can_queue". This can cause a panic if we were in our interrupt code.
  1502. **/
  1503. static void qla4xxx_do_dpc(struct work_struct *work)
  1504. {
  1505. struct scsi_qla_host *ha =
  1506. container_of(work, struct scsi_qla_host, dpc_work);
  1507. struct ddb_entry *ddb_entry, *dtemp;
  1508. int status = QLA_ERROR;
  1509. DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
  1510. "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
  1511. ha->host_no, __func__, ha->flags, ha->dpc_flags))
  1512. /* Initialization not yet finished. Don't do anything yet. */
  1513. if (!test_bit(AF_INIT_DONE, &ha->flags))
  1514. return;
  1515. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  1516. DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
  1517. ha->host_no, __func__, ha->flags));
  1518. return;
  1519. }
  1520. if (is_qla8022(ha)) {
  1521. if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
  1522. qla4_8xxx_idc_lock(ha);
  1523. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  1524. QLA82XX_DEV_FAILED);
  1525. qla4_8xxx_idc_unlock(ha);
  1526. ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
  1527. qla4_8xxx_device_state_handler(ha);
  1528. }
  1529. if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  1530. qla4_8xxx_need_qsnt_handler(ha);
  1531. }
  1532. }
  1533. if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
  1534. (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  1535. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  1536. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
  1537. if (ql4xdontresethba) {
  1538. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  1539. ha->host_no, __func__));
  1540. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1541. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  1542. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1543. goto dpc_post_reset_ha;
  1544. }
  1545. if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  1546. test_bit(DPC_RESET_HA, &ha->dpc_flags))
  1547. qla4xxx_recover_adapter(ha);
  1548. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  1549. uint8_t wait_time = RESET_INTR_TOV;
  1550. while ((readw(&ha->reg->ctrl_status) &
  1551. (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
  1552. if (--wait_time == 0)
  1553. break;
  1554. msleep(1000);
  1555. }
  1556. if (wait_time == 0)
  1557. DEBUG2(printk("scsi%ld: %s: SR|FSR "
  1558. "bit not cleared-- resetting\n",
  1559. ha->host_no, __func__));
  1560. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  1561. if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
  1562. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  1563. status = qla4xxx_recover_adapter(ha);
  1564. }
  1565. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  1566. if (status == QLA_SUCCESS)
  1567. ha->isp_ops->enable_intrs(ha);
  1568. }
  1569. }
  1570. dpc_post_reset_ha:
  1571. /* ---- process AEN? --- */
  1572. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
  1573. qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
  1574. /* ---- Get DHCP IP Address? --- */
  1575. if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
  1576. qla4xxx_get_dhcp_ip_address(ha);
  1577. /* ---- link change? --- */
  1578. if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
  1579. if (!test_bit(AF_LINK_UP, &ha->flags)) {
  1580. /* ---- link down? --- */
  1581. qla4xxx_mark_all_devices_missing(ha);
  1582. } else {
  1583. /* ---- link up? --- *
  1584. * F/W will auto login to all devices ONLY ONCE after
  1585. * link up during driver initialization and runtime
  1586. * fatal error recovery. Therefore, the driver must
  1587. * manually relogin to devices when recovering from
  1588. * connection failures, logouts, expired KATO, etc. */
  1589. qla4xxx_relogin_all_devices(ha);
  1590. }
  1591. }
  1592. /* ---- relogin device? --- */
  1593. if (adapter_up(ha) &&
  1594. test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
  1595. list_for_each_entry_safe(ddb_entry, dtemp,
  1596. &ha->ddb_list, list) {
  1597. if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
  1598. atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
  1599. qla4xxx_relogin_device(ha, ddb_entry);
  1600. /*
  1601. * If mbx cmd times out there is no point
  1602. * in continuing further.
  1603. * With large no of targets this can hang
  1604. * the system.
  1605. */
  1606. if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  1607. printk(KERN_WARNING "scsi%ld: %s: "
  1608. "need to reset hba\n",
  1609. ha->host_no, __func__);
  1610. break;
  1611. }
  1612. }
  1613. }
  1614. }
  1615. /**
  1616. * qla4xxx_free_adapter - release the adapter
  1617. * @ha: pointer to adapter structure
  1618. **/
  1619. static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
  1620. {
  1621. if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
  1622. /* Turn-off interrupts on the card. */
  1623. ha->isp_ops->disable_intrs(ha);
  1624. }
  1625. /* Remove timer thread, if present */
  1626. if (ha->timer_active)
  1627. qla4xxx_stop_timer(ha);
  1628. /* Kill the kernel thread for this host */
  1629. if (ha->dpc_thread)
  1630. destroy_workqueue(ha->dpc_thread);
  1631. /* Put firmware in known state */
  1632. ha->isp_ops->reset_firmware(ha);
  1633. if (is_qla8022(ha)) {
  1634. qla4_8xxx_idc_lock(ha);
  1635. qla4_8xxx_clear_drv_active(ha);
  1636. qla4_8xxx_idc_unlock(ha);
  1637. }
  1638. /* Detach interrupts */
  1639. if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
  1640. qla4xxx_free_irqs(ha);
  1641. /* free extra memory */
  1642. qla4xxx_mem_free(ha);
  1643. }
  1644. int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
  1645. {
  1646. int status = 0;
  1647. uint8_t revision_id;
  1648. unsigned long mem_base, mem_len, db_base, db_len;
  1649. struct pci_dev *pdev = ha->pdev;
  1650. status = pci_request_regions(pdev, DRIVER_NAME);
  1651. if (status) {
  1652. printk(KERN_WARNING
  1653. "scsi(%ld) Failed to reserve PIO regions (%s) "
  1654. "status=%d\n", ha->host_no, pci_name(pdev), status);
  1655. goto iospace_error_exit;
  1656. }
  1657. pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
  1658. DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
  1659. __func__, revision_id));
  1660. ha->revision_id = revision_id;
  1661. /* remap phys address */
  1662. mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
  1663. mem_len = pci_resource_len(pdev, 0);
  1664. DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
  1665. __func__, mem_base, mem_len));
  1666. /* mapping of pcibase pointer */
  1667. ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
  1668. if (!ha->nx_pcibase) {
  1669. printk(KERN_ERR
  1670. "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
  1671. pci_release_regions(ha->pdev);
  1672. goto iospace_error_exit;
  1673. }
  1674. /* Mapping of IO base pointer, door bell read and write pointer */
  1675. /* mapping of IO base pointer */
  1676. ha->qla4_8xxx_reg =
  1677. (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
  1678. 0xbc000 + (ha->pdev->devfn << 11));
  1679. db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
  1680. db_len = pci_resource_len(pdev, 4);
  1681. ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
  1682. QLA82XX_CAM_RAM_DB2);
  1683. return 0;
  1684. iospace_error_exit:
  1685. return -ENOMEM;
  1686. }
  1687. /***
  1688. * qla4xxx_iospace_config - maps registers
  1689. * @ha: pointer to adapter structure
  1690. *
  1691. * This routines maps HBA's registers from the pci address space
  1692. * into the kernel virtual address space for memory mapped i/o.
  1693. **/
  1694. int qla4xxx_iospace_config(struct scsi_qla_host *ha)
  1695. {
  1696. unsigned long pio, pio_len, pio_flags;
  1697. unsigned long mmio, mmio_len, mmio_flags;
  1698. pio = pci_resource_start(ha->pdev, 0);
  1699. pio_len = pci_resource_len(ha->pdev, 0);
  1700. pio_flags = pci_resource_flags(ha->pdev, 0);
  1701. if (pio_flags & IORESOURCE_IO) {
  1702. if (pio_len < MIN_IOBASE_LEN) {
  1703. ql4_printk(KERN_WARNING, ha,
  1704. "Invalid PCI I/O region size\n");
  1705. pio = 0;
  1706. }
  1707. } else {
  1708. ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
  1709. pio = 0;
  1710. }
  1711. /* Use MMIO operations for all accesses. */
  1712. mmio = pci_resource_start(ha->pdev, 1);
  1713. mmio_len = pci_resource_len(ha->pdev, 1);
  1714. mmio_flags = pci_resource_flags(ha->pdev, 1);
  1715. if (!(mmio_flags & IORESOURCE_MEM)) {
  1716. ql4_printk(KERN_ERR, ha,
  1717. "region #0 not an MMIO resource, aborting\n");
  1718. goto iospace_error_exit;
  1719. }
  1720. if (mmio_len < MIN_IOBASE_LEN) {
  1721. ql4_printk(KERN_ERR, ha,
  1722. "Invalid PCI mem region size, aborting\n");
  1723. goto iospace_error_exit;
  1724. }
  1725. if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
  1726. ql4_printk(KERN_WARNING, ha,
  1727. "Failed to reserve PIO/MMIO regions\n");
  1728. goto iospace_error_exit;
  1729. }
  1730. ha->pio_address = pio;
  1731. ha->pio_length = pio_len;
  1732. ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
  1733. if (!ha->reg) {
  1734. ql4_printk(KERN_ERR, ha,
  1735. "cannot remap MMIO, aborting\n");
  1736. goto iospace_error_exit;
  1737. }
  1738. return 0;
  1739. iospace_error_exit:
  1740. return -ENOMEM;
  1741. }
  1742. static struct isp_operations qla4xxx_isp_ops = {
  1743. .iospace_config = qla4xxx_iospace_config,
  1744. .pci_config = qla4xxx_pci_config,
  1745. .disable_intrs = qla4xxx_disable_intrs,
  1746. .enable_intrs = qla4xxx_enable_intrs,
  1747. .start_firmware = qla4xxx_start_firmware,
  1748. .intr_handler = qla4xxx_intr_handler,
  1749. .interrupt_service_routine = qla4xxx_interrupt_service_routine,
  1750. .reset_chip = qla4xxx_soft_reset,
  1751. .reset_firmware = qla4xxx_hw_reset,
  1752. .queue_iocb = qla4xxx_queue_iocb,
  1753. .complete_iocb = qla4xxx_complete_iocb,
  1754. .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
  1755. .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
  1756. .get_sys_info = qla4xxx_get_sys_info,
  1757. };
  1758. static struct isp_operations qla4_8xxx_isp_ops = {
  1759. .iospace_config = qla4_8xxx_iospace_config,
  1760. .pci_config = qla4_8xxx_pci_config,
  1761. .disable_intrs = qla4_8xxx_disable_intrs,
  1762. .enable_intrs = qla4_8xxx_enable_intrs,
  1763. .start_firmware = qla4_8xxx_load_risc,
  1764. .intr_handler = qla4_8xxx_intr_handler,
  1765. .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
  1766. .reset_chip = qla4_8xxx_isp_reset,
  1767. .reset_firmware = qla4_8xxx_stop_firmware,
  1768. .queue_iocb = qla4_8xxx_queue_iocb,
  1769. .complete_iocb = qla4_8xxx_complete_iocb,
  1770. .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
  1771. .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
  1772. .get_sys_info = qla4_8xxx_get_sys_info,
  1773. };
  1774. uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  1775. {
  1776. return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
  1777. }
  1778. uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  1779. {
  1780. return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
  1781. }
  1782. uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  1783. {
  1784. return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
  1785. }
  1786. uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  1787. {
  1788. return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
  1789. }
  1790. /**
  1791. * qla4xxx_probe_adapter - callback function to probe HBA
  1792. * @pdev: pointer to pci_dev structure
  1793. * @pci_device_id: pointer to pci_device entry
  1794. *
  1795. * This routine will probe for Qlogic 4xxx iSCSI host adapters.
  1796. * It returns zero if successful. It also initializes all data necessary for
  1797. * the driver.
  1798. **/
  1799. static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
  1800. const struct pci_device_id *ent)
  1801. {
  1802. int ret = -ENODEV, status;
  1803. struct Scsi_Host *host;
  1804. struct scsi_qla_host *ha;
  1805. uint8_t init_retry_count = 0;
  1806. char buf[34];
  1807. struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
  1808. uint32_t dev_state;
  1809. if (pci_enable_device(pdev))
  1810. return -1;
  1811. host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
  1812. if (host == NULL) {
  1813. printk(KERN_WARNING
  1814. "qla4xxx: Couldn't allocate host from scsi layer!\n");
  1815. goto probe_disable_device;
  1816. }
  1817. /* Clear our data area */
  1818. ha = (struct scsi_qla_host *) host->hostdata;
  1819. memset(ha, 0, sizeof(*ha));
  1820. /* Save the information from PCI BIOS. */
  1821. ha->pdev = pdev;
  1822. ha->host = host;
  1823. ha->host_no = host->host_no;
  1824. pci_enable_pcie_error_reporting(pdev);
  1825. /* Setup Runtime configurable options */
  1826. if (is_qla8022(ha)) {
  1827. ha->isp_ops = &qla4_8xxx_isp_ops;
  1828. rwlock_init(&ha->hw_lock);
  1829. ha->qdr_sn_window = -1;
  1830. ha->ddr_mn_window = -1;
  1831. ha->curr_window = 255;
  1832. ha->func_num = PCI_FUNC(ha->pdev->devfn);
  1833. nx_legacy_intr = &legacy_intr[ha->func_num];
  1834. ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
  1835. ha->nx_legacy_intr.tgt_status_reg =
  1836. nx_legacy_intr->tgt_status_reg;
  1837. ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
  1838. ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
  1839. } else {
  1840. ha->isp_ops = &qla4xxx_isp_ops;
  1841. }
  1842. /* Set EEH reset type to fundamental if required by hba */
  1843. if (is_qla8022(ha))
  1844. pdev->needs_freset = 1;
  1845. /* Configure PCI I/O space. */
  1846. ret = ha->isp_ops->iospace_config(ha);
  1847. if (ret)
  1848. goto probe_failed_ioconfig;
  1849. ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
  1850. pdev->device, pdev->irq, ha->reg);
  1851. qla4xxx_config_dma_addressing(ha);
  1852. /* Initialize lists and spinlocks. */
  1853. INIT_LIST_HEAD(&ha->ddb_list);
  1854. INIT_LIST_HEAD(&ha->free_srb_q);
  1855. mutex_init(&ha->mbox_sem);
  1856. init_completion(&ha->mbx_intr_comp);
  1857. spin_lock_init(&ha->hardware_lock);
  1858. /* Allocate dma buffers */
  1859. if (qla4xxx_mem_alloc(ha)) {
  1860. ql4_printk(KERN_WARNING, ha,
  1861. "[ERROR] Failed to allocate memory for adapter\n");
  1862. ret = -ENOMEM;
  1863. goto probe_failed;
  1864. }
  1865. if (is_qla8022(ha))
  1866. (void) qla4_8xxx_get_flash_info(ha);
  1867. /*
  1868. * Initialize the Host adapter request/response queues and
  1869. * firmware
  1870. * NOTE: interrupts enabled upon successful completion
  1871. */
  1872. status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
  1873. while ((!test_bit(AF_ONLINE, &ha->flags)) &&
  1874. init_retry_count++ < MAX_INIT_RETRIES) {
  1875. if (is_qla8022(ha)) {
  1876. qla4_8xxx_idc_lock(ha);
  1877. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  1878. qla4_8xxx_idc_unlock(ha);
  1879. if (dev_state == QLA82XX_DEV_FAILED) {
  1880. ql4_printk(KERN_WARNING, ha, "%s: don't retry "
  1881. "initialize adapter. H/W is in failed state\n",
  1882. __func__);
  1883. break;
  1884. }
  1885. }
  1886. DEBUG2(printk("scsi: %s: retrying adapter initialization "
  1887. "(%d)\n", __func__, init_retry_count));
  1888. if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
  1889. continue;
  1890. status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
  1891. }
  1892. if (!test_bit(AF_ONLINE, &ha->flags)) {
  1893. ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
  1894. if (is_qla8022(ha) && ql4xdontresethba) {
  1895. /* Put the device in failed state. */
  1896. DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
  1897. qla4_8xxx_idc_lock(ha);
  1898. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  1899. QLA82XX_DEV_FAILED);
  1900. qla4_8xxx_idc_unlock(ha);
  1901. }
  1902. ret = -ENODEV;
  1903. goto probe_failed;
  1904. }
  1905. host->cmd_per_lun = 3;
  1906. host->max_channel = 0;
  1907. host->max_lun = MAX_LUNS - 1;
  1908. host->max_id = MAX_TARGETS;
  1909. host->max_cmd_len = IOCB_MAX_CDB_LEN;
  1910. host->can_queue = MAX_SRBS ;
  1911. host->transportt = qla4xxx_scsi_transport;
  1912. ret = scsi_init_shared_tag_map(host, MAX_SRBS);
  1913. if (ret) {
  1914. ql4_printk(KERN_WARNING, ha,
  1915. "scsi_init_shared_tag_map failed\n");
  1916. goto probe_failed;
  1917. }
  1918. /* Startup the kernel thread for this host adapter. */
  1919. DEBUG2(printk("scsi: %s: Starting kernel thread for "
  1920. "qla4xxx_dpc\n", __func__));
  1921. sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
  1922. ha->dpc_thread = create_singlethread_workqueue(buf);
  1923. if (!ha->dpc_thread) {
  1924. ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
  1925. ret = -ENODEV;
  1926. goto probe_failed;
  1927. }
  1928. INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
  1929. /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
  1930. * (which is called indirectly by qla4xxx_initialize_adapter),
  1931. * so that irqs will be registered after crbinit but before
  1932. * mbx_intr_enable.
  1933. */
  1934. if (!is_qla8022(ha)) {
  1935. ret = qla4xxx_request_irqs(ha);
  1936. if (ret) {
  1937. ql4_printk(KERN_WARNING, ha, "Failed to reserve "
  1938. "interrupt %d already in use.\n", pdev->irq);
  1939. goto probe_failed;
  1940. }
  1941. }
  1942. pci_save_state(ha->pdev);
  1943. ha->isp_ops->enable_intrs(ha);
  1944. /* Start timer thread. */
  1945. qla4xxx_start_timer(ha, qla4xxx_timer, 1);
  1946. set_bit(AF_INIT_DONE, &ha->flags);
  1947. pci_set_drvdata(pdev, ha);
  1948. ret = scsi_add_host(host, &pdev->dev);
  1949. if (ret)
  1950. goto probe_failed;
  1951. printk(KERN_INFO
  1952. " QLogic iSCSI HBA Driver version: %s\n"
  1953. " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
  1954. qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
  1955. ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
  1956. ha->patch_number, ha->build_number);
  1957. scsi_scan_host(host);
  1958. qla4xxx_create_ifaces(ha);
  1959. return 0;
  1960. probe_failed:
  1961. qla4xxx_free_adapter(ha);
  1962. probe_failed_ioconfig:
  1963. pci_disable_pcie_error_reporting(pdev);
  1964. scsi_host_put(ha->host);
  1965. probe_disable_device:
  1966. pci_disable_device(pdev);
  1967. return ret;
  1968. }
  1969. /**
  1970. * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
  1971. * @ha: pointer to adapter structure
  1972. *
  1973. * Mark the other ISP-4xxx port to indicate that the driver is being removed,
  1974. * so that the other port will not re-initialize while in the process of
  1975. * removing the ha due to driver unload or hba hotplug.
  1976. **/
  1977. static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
  1978. {
  1979. struct scsi_qla_host *other_ha = NULL;
  1980. struct pci_dev *other_pdev = NULL;
  1981. int fn = ISP4XXX_PCI_FN_2;
  1982. /*iscsi function numbers for ISP4xxx is 1 and 3*/
  1983. if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
  1984. fn = ISP4XXX_PCI_FN_1;
  1985. other_pdev =
  1986. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  1987. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  1988. fn));
  1989. /* Get other_ha if other_pdev is valid and state is enable*/
  1990. if (other_pdev) {
  1991. if (atomic_read(&other_pdev->enable_cnt)) {
  1992. other_ha = pci_get_drvdata(other_pdev);
  1993. if (other_ha) {
  1994. set_bit(AF_HA_REMOVAL, &other_ha->flags);
  1995. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
  1996. "Prevent %s reinit\n", __func__,
  1997. dev_name(&other_ha->pdev->dev)));
  1998. }
  1999. }
  2000. pci_dev_put(other_pdev);
  2001. }
  2002. }
  2003. /**
  2004. * qla4xxx_remove_adapter - calback function to remove adapter.
  2005. * @pci_dev: PCI device pointer
  2006. **/
  2007. static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
  2008. {
  2009. struct scsi_qla_host *ha;
  2010. ha = pci_get_drvdata(pdev);
  2011. if (!is_qla8022(ha))
  2012. qla4xxx_prevent_other_port_reinit(ha);
  2013. /* remove devs from iscsi_sessions to scsi_devices */
  2014. qla4xxx_free_ddb_list(ha);
  2015. /* destroy iface from sysfs */
  2016. qla4xxx_destroy_ifaces(ha);
  2017. scsi_remove_host(ha->host);
  2018. qla4xxx_free_adapter(ha);
  2019. scsi_host_put(ha->host);
  2020. pci_disable_pcie_error_reporting(pdev);
  2021. pci_disable_device(pdev);
  2022. pci_set_drvdata(pdev, NULL);
  2023. }
  2024. /**
  2025. * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
  2026. * @ha: HA context
  2027. *
  2028. * At exit, the @ha's flags.enable_64bit_addressing set to indicated
  2029. * supported addressing method.
  2030. */
  2031. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
  2032. {
  2033. int retval;
  2034. /* Update our PCI device dma_mask for full 64 bit mask */
  2035. if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
  2036. if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
  2037. dev_dbg(&ha->pdev->dev,
  2038. "Failed to set 64 bit PCI consistent mask; "
  2039. "using 32 bit.\n");
  2040. retval = pci_set_consistent_dma_mask(ha->pdev,
  2041. DMA_BIT_MASK(32));
  2042. }
  2043. } else
  2044. retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
  2045. }
  2046. static int qla4xxx_slave_alloc(struct scsi_device *sdev)
  2047. {
  2048. struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
  2049. struct ddb_entry *ddb = sess->dd_data;
  2050. int queue_depth = QL4_DEF_QDEPTH;
  2051. sdev->hostdata = ddb;
  2052. sdev->tagged_supported = 1;
  2053. if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
  2054. queue_depth = ql4xmaxqdepth;
  2055. scsi_activate_tcq(sdev, queue_depth);
  2056. return 0;
  2057. }
  2058. static int qla4xxx_slave_configure(struct scsi_device *sdev)
  2059. {
  2060. sdev->tagged_supported = 1;
  2061. return 0;
  2062. }
  2063. static void qla4xxx_slave_destroy(struct scsi_device *sdev)
  2064. {
  2065. scsi_deactivate_tcq(sdev, 1);
  2066. }
  2067. /**
  2068. * qla4xxx_del_from_active_array - returns an active srb
  2069. * @ha: Pointer to host adapter structure.
  2070. * @index: index into the active_array
  2071. *
  2072. * This routine removes and returns the srb at the specified index
  2073. **/
  2074. struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
  2075. uint32_t index)
  2076. {
  2077. struct srb *srb = NULL;
  2078. struct scsi_cmnd *cmd = NULL;
  2079. cmd = scsi_host_find_tag(ha->host, index);
  2080. if (!cmd)
  2081. return srb;
  2082. srb = (struct srb *)CMD_SP(cmd);
  2083. if (!srb)
  2084. return srb;
  2085. /* update counters */
  2086. if (srb->flags & SRB_DMA_VALID) {
  2087. ha->req_q_count += srb->iocb_cnt;
  2088. ha->iocb_cnt -= srb->iocb_cnt;
  2089. if (srb->cmd)
  2090. srb->cmd->host_scribble =
  2091. (unsigned char *)(unsigned long) MAX_SRBS;
  2092. }
  2093. return srb;
  2094. }
  2095. /**
  2096. * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
  2097. * @ha: Pointer to host adapter structure.
  2098. * @cmd: Scsi Command to wait on.
  2099. *
  2100. * This routine waits for the command to be returned by the Firmware
  2101. * for some max time.
  2102. **/
  2103. static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
  2104. struct scsi_cmnd *cmd)
  2105. {
  2106. int done = 0;
  2107. struct srb *rp;
  2108. uint32_t max_wait_time = EH_WAIT_CMD_TOV;
  2109. int ret = SUCCESS;
  2110. /* Dont wait on command if PCI error is being handled
  2111. * by PCI AER driver
  2112. */
  2113. if (unlikely(pci_channel_offline(ha->pdev)) ||
  2114. (test_bit(AF_EEH_BUSY, &ha->flags))) {
  2115. ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
  2116. ha->host_no, __func__);
  2117. return ret;
  2118. }
  2119. do {
  2120. /* Checking to see if its returned to OS */
  2121. rp = (struct srb *) CMD_SP(cmd);
  2122. if (rp == NULL) {
  2123. done++;
  2124. break;
  2125. }
  2126. msleep(2000);
  2127. } while (max_wait_time--);
  2128. return done;
  2129. }
  2130. /**
  2131. * qla4xxx_wait_for_hba_online - waits for HBA to come online
  2132. * @ha: Pointer to host adapter structure
  2133. **/
  2134. static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
  2135. {
  2136. unsigned long wait_online;
  2137. wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
  2138. while (time_before(jiffies, wait_online)) {
  2139. if (adapter_up(ha))
  2140. return QLA_SUCCESS;
  2141. msleep(2000);
  2142. }
  2143. return QLA_ERROR;
  2144. }
  2145. /**
  2146. * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
  2147. * @ha: pointer to HBA
  2148. * @t: target id
  2149. * @l: lun id
  2150. *
  2151. * This function waits for all outstanding commands to a lun to complete. It
  2152. * returns 0 if all pending commands are returned and 1 otherwise.
  2153. **/
  2154. static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
  2155. struct scsi_target *stgt,
  2156. struct scsi_device *sdev)
  2157. {
  2158. int cnt;
  2159. int status = 0;
  2160. struct scsi_cmnd *cmd;
  2161. /*
  2162. * Waiting for all commands for the designated target or dev
  2163. * in the active array
  2164. */
  2165. for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
  2166. cmd = scsi_host_find_tag(ha->host, cnt);
  2167. if (cmd && stgt == scsi_target(cmd->device) &&
  2168. (!sdev || sdev == cmd->device)) {
  2169. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  2170. status++;
  2171. break;
  2172. }
  2173. }
  2174. }
  2175. return status;
  2176. }
  2177. /**
  2178. * qla4xxx_eh_abort - callback for abort task.
  2179. * @cmd: Pointer to Linux's SCSI command structure
  2180. *
  2181. * This routine is called by the Linux OS to abort the specified
  2182. * command.
  2183. **/
  2184. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
  2185. {
  2186. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  2187. unsigned int id = cmd->device->id;
  2188. unsigned int lun = cmd->device->lun;
  2189. unsigned long flags;
  2190. struct srb *srb = NULL;
  2191. int ret = SUCCESS;
  2192. int wait = 0;
  2193. ql4_printk(KERN_INFO, ha,
  2194. "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
  2195. ha->host_no, id, lun, cmd);
  2196. spin_lock_irqsave(&ha->hardware_lock, flags);
  2197. srb = (struct srb *) CMD_SP(cmd);
  2198. if (!srb) {
  2199. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2200. return SUCCESS;
  2201. }
  2202. kref_get(&srb->srb_ref);
  2203. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2204. if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
  2205. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
  2206. ha->host_no, id, lun));
  2207. ret = FAILED;
  2208. } else {
  2209. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
  2210. ha->host_no, id, lun));
  2211. wait = 1;
  2212. }
  2213. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  2214. /* Wait for command to complete */
  2215. if (wait) {
  2216. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  2217. DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
  2218. ha->host_no, id, lun));
  2219. ret = FAILED;
  2220. }
  2221. }
  2222. ql4_printk(KERN_INFO, ha,
  2223. "scsi%ld:%d:%d: Abort command - %s\n",
  2224. ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
  2225. return ret;
  2226. }
  2227. /**
  2228. * qla4xxx_eh_device_reset - callback for target reset.
  2229. * @cmd: Pointer to Linux's SCSI command structure
  2230. *
  2231. * This routine is called by the Linux OS to reset all luns on the
  2232. * specified target.
  2233. **/
  2234. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
  2235. {
  2236. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  2237. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  2238. int ret = FAILED, stat;
  2239. if (!ddb_entry)
  2240. return ret;
  2241. ret = iscsi_block_scsi_eh(cmd);
  2242. if (ret)
  2243. return ret;
  2244. ret = FAILED;
  2245. ql4_printk(KERN_INFO, ha,
  2246. "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
  2247. cmd->device->channel, cmd->device->id, cmd->device->lun);
  2248. DEBUG2(printk(KERN_INFO
  2249. "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
  2250. "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
  2251. cmd, jiffies, cmd->request->timeout / HZ,
  2252. ha->dpc_flags, cmd->result, cmd->allowed));
  2253. /* FIXME: wait for hba to go online */
  2254. stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
  2255. if (stat != QLA_SUCCESS) {
  2256. ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
  2257. goto eh_dev_reset_done;
  2258. }
  2259. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  2260. cmd->device)) {
  2261. ql4_printk(KERN_INFO, ha,
  2262. "DEVICE RESET FAILED - waiting for "
  2263. "commands.\n");
  2264. goto eh_dev_reset_done;
  2265. }
  2266. /* Send marker. */
  2267. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  2268. MM_LUN_RESET) != QLA_SUCCESS)
  2269. goto eh_dev_reset_done;
  2270. ql4_printk(KERN_INFO, ha,
  2271. "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
  2272. ha->host_no, cmd->device->channel, cmd->device->id,
  2273. cmd->device->lun);
  2274. ret = SUCCESS;
  2275. eh_dev_reset_done:
  2276. return ret;
  2277. }
  2278. /**
  2279. * qla4xxx_eh_target_reset - callback for target reset.
  2280. * @cmd: Pointer to Linux's SCSI command structure
  2281. *
  2282. * This routine is called by the Linux OS to reset the target.
  2283. **/
  2284. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
  2285. {
  2286. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  2287. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  2288. int stat, ret;
  2289. if (!ddb_entry)
  2290. return FAILED;
  2291. ret = iscsi_block_scsi_eh(cmd);
  2292. if (ret)
  2293. return ret;
  2294. starget_printk(KERN_INFO, scsi_target(cmd->device),
  2295. "WARM TARGET RESET ISSUED.\n");
  2296. DEBUG2(printk(KERN_INFO
  2297. "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
  2298. "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
  2299. ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
  2300. ha->dpc_flags, cmd->result, cmd->allowed));
  2301. stat = qla4xxx_reset_target(ha, ddb_entry);
  2302. if (stat != QLA_SUCCESS) {
  2303. starget_printk(KERN_INFO, scsi_target(cmd->device),
  2304. "WARM TARGET RESET FAILED.\n");
  2305. return FAILED;
  2306. }
  2307. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  2308. NULL)) {
  2309. starget_printk(KERN_INFO, scsi_target(cmd->device),
  2310. "WARM TARGET DEVICE RESET FAILED - "
  2311. "waiting for commands.\n");
  2312. return FAILED;
  2313. }
  2314. /* Send marker. */
  2315. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  2316. MM_TGT_WARM_RESET) != QLA_SUCCESS) {
  2317. starget_printk(KERN_INFO, scsi_target(cmd->device),
  2318. "WARM TARGET DEVICE RESET FAILED - "
  2319. "marker iocb failed.\n");
  2320. return FAILED;
  2321. }
  2322. starget_printk(KERN_INFO, scsi_target(cmd->device),
  2323. "WARM TARGET RESET SUCCEEDED.\n");
  2324. return SUCCESS;
  2325. }
  2326. /**
  2327. * qla4xxx_eh_host_reset - kernel callback
  2328. * @cmd: Pointer to Linux's SCSI command structure
  2329. *
  2330. * This routine is invoked by the Linux kernel to perform fatal error
  2331. * recovery on the specified adapter.
  2332. **/
  2333. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
  2334. {
  2335. int return_status = FAILED;
  2336. struct scsi_qla_host *ha;
  2337. ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
  2338. if (ql4xdontresethba) {
  2339. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  2340. ha->host_no, __func__));
  2341. return FAILED;
  2342. }
  2343. ql4_printk(KERN_INFO, ha,
  2344. "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
  2345. cmd->device->channel, cmd->device->id, cmd->device->lun);
  2346. if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
  2347. DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
  2348. "DEAD.\n", ha->host_no, cmd->device->channel,
  2349. __func__));
  2350. return FAILED;
  2351. }
  2352. if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  2353. if (is_qla8022(ha))
  2354. set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  2355. else
  2356. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  2357. }
  2358. if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
  2359. return_status = SUCCESS;
  2360. ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
  2361. return_status == FAILED ? "FAILED" : "SUCCEEDED");
  2362. return return_status;
  2363. }
  2364. /* PCI AER driver recovers from all correctable errors w/o
  2365. * driver intervention. For uncorrectable errors PCI AER
  2366. * driver calls the following device driver's callbacks
  2367. *
  2368. * - Fatal Errors - link_reset
  2369. * - Non-Fatal Errors - driver's pci_error_detected() which
  2370. * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
  2371. *
  2372. * PCI AER driver calls
  2373. * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
  2374. * returns RECOVERED or NEED_RESET if fw_hung
  2375. * NEED_RESET - driver's slot_reset()
  2376. * DISCONNECT - device is dead & cannot recover
  2377. * RECOVERED - driver's pci_resume()
  2378. */
  2379. static pci_ers_result_t
  2380. qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  2381. {
  2382. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  2383. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
  2384. ha->host_no, __func__, state);
  2385. if (!is_aer_supported(ha))
  2386. return PCI_ERS_RESULT_NONE;
  2387. switch (state) {
  2388. case pci_channel_io_normal:
  2389. clear_bit(AF_EEH_BUSY, &ha->flags);
  2390. return PCI_ERS_RESULT_CAN_RECOVER;
  2391. case pci_channel_io_frozen:
  2392. set_bit(AF_EEH_BUSY, &ha->flags);
  2393. qla4xxx_mailbox_premature_completion(ha);
  2394. qla4xxx_free_irqs(ha);
  2395. pci_disable_device(pdev);
  2396. /* Return back all IOs */
  2397. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  2398. return PCI_ERS_RESULT_NEED_RESET;
  2399. case pci_channel_io_perm_failure:
  2400. set_bit(AF_EEH_BUSY, &ha->flags);
  2401. set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
  2402. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  2403. return PCI_ERS_RESULT_DISCONNECT;
  2404. }
  2405. return PCI_ERS_RESULT_NEED_RESET;
  2406. }
  2407. /**
  2408. * qla4xxx_pci_mmio_enabled() gets called if
  2409. * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
  2410. * and read/write to the device still works.
  2411. **/
  2412. static pci_ers_result_t
  2413. qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
  2414. {
  2415. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  2416. if (!is_aer_supported(ha))
  2417. return PCI_ERS_RESULT_NONE;
  2418. return PCI_ERS_RESULT_RECOVERED;
  2419. }
  2420. static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
  2421. {
  2422. uint32_t rval = QLA_ERROR;
  2423. uint32_t ret = 0;
  2424. int fn;
  2425. struct pci_dev *other_pdev = NULL;
  2426. ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
  2427. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  2428. if (test_bit(AF_ONLINE, &ha->flags)) {
  2429. clear_bit(AF_ONLINE, &ha->flags);
  2430. qla4xxx_mark_all_devices_missing(ha);
  2431. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2432. }
  2433. fn = PCI_FUNC(ha->pdev->devfn);
  2434. while (fn > 0) {
  2435. fn--;
  2436. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
  2437. "func %x\n", ha->host_no, __func__, fn);
  2438. /* Get the pci device given the domain, bus,
  2439. * slot/function number */
  2440. other_pdev =
  2441. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  2442. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  2443. fn));
  2444. if (!other_pdev)
  2445. continue;
  2446. if (atomic_read(&other_pdev->enable_cnt)) {
  2447. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
  2448. "func in enabled state%x\n", ha->host_no,
  2449. __func__, fn);
  2450. pci_dev_put(other_pdev);
  2451. break;
  2452. }
  2453. pci_dev_put(other_pdev);
  2454. }
  2455. /* The first function on the card, the reset owner will
  2456. * start & initialize the firmware. The other functions
  2457. * on the card will reset the firmware context
  2458. */
  2459. if (!fn) {
  2460. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
  2461. "0x%x is the owner\n", ha->host_no, __func__,
  2462. ha->pdev->devfn);
  2463. qla4_8xxx_idc_lock(ha);
  2464. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2465. QLA82XX_DEV_COLD);
  2466. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
  2467. QLA82XX_IDC_VERSION);
  2468. qla4_8xxx_idc_unlock(ha);
  2469. clear_bit(AF_FW_RECOVERY, &ha->flags);
  2470. rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
  2471. qla4_8xxx_idc_lock(ha);
  2472. if (rval != QLA_SUCCESS) {
  2473. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  2474. "FAILED\n", ha->host_no, __func__);
  2475. qla4_8xxx_clear_drv_active(ha);
  2476. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2477. QLA82XX_DEV_FAILED);
  2478. } else {
  2479. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  2480. "READY\n", ha->host_no, __func__);
  2481. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2482. QLA82XX_DEV_READY);
  2483. /* Clear driver state register */
  2484. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
  2485. qla4_8xxx_set_drv_active(ha);
  2486. ret = qla4xxx_request_irqs(ha);
  2487. if (ret) {
  2488. ql4_printk(KERN_WARNING, ha, "Failed to "
  2489. "reserve interrupt %d already in use.\n",
  2490. ha->pdev->irq);
  2491. rval = QLA_ERROR;
  2492. } else {
  2493. ha->isp_ops->enable_intrs(ha);
  2494. rval = QLA_SUCCESS;
  2495. }
  2496. }
  2497. qla4_8xxx_idc_unlock(ha);
  2498. } else {
  2499. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
  2500. "the reset owner\n", ha->host_no, __func__,
  2501. ha->pdev->devfn);
  2502. if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
  2503. QLA82XX_DEV_READY)) {
  2504. clear_bit(AF_FW_RECOVERY, &ha->flags);
  2505. rval = qla4xxx_initialize_adapter(ha,
  2506. PRESERVE_DDB_LIST);
  2507. if (rval == QLA_SUCCESS) {
  2508. ret = qla4xxx_request_irqs(ha);
  2509. if (ret) {
  2510. ql4_printk(KERN_WARNING, ha, "Failed to"
  2511. " reserve interrupt %d already in"
  2512. " use.\n", ha->pdev->irq);
  2513. rval = QLA_ERROR;
  2514. } else {
  2515. ha->isp_ops->enable_intrs(ha);
  2516. rval = QLA_SUCCESS;
  2517. }
  2518. }
  2519. qla4_8xxx_idc_lock(ha);
  2520. qla4_8xxx_set_drv_active(ha);
  2521. qla4_8xxx_idc_unlock(ha);
  2522. }
  2523. }
  2524. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  2525. return rval;
  2526. }
  2527. static pci_ers_result_t
  2528. qla4xxx_pci_slot_reset(struct pci_dev *pdev)
  2529. {
  2530. pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
  2531. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  2532. int rc;
  2533. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
  2534. ha->host_no, __func__);
  2535. if (!is_aer_supported(ha))
  2536. return PCI_ERS_RESULT_NONE;
  2537. /* Restore the saved state of PCIe device -
  2538. * BAR registers, PCI Config space, PCIX, MSI,
  2539. * IOV states
  2540. */
  2541. pci_restore_state(pdev);
  2542. /* pci_restore_state() clears the saved_state flag of the device
  2543. * save restored state which resets saved_state flag
  2544. */
  2545. pci_save_state(pdev);
  2546. /* Initialize device or resume if in suspended state */
  2547. rc = pci_enable_device(pdev);
  2548. if (rc) {
  2549. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
  2550. "device after reset\n", ha->host_no, __func__);
  2551. goto exit_slot_reset;
  2552. }
  2553. ha->isp_ops->disable_intrs(ha);
  2554. if (is_qla8022(ha)) {
  2555. if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
  2556. ret = PCI_ERS_RESULT_RECOVERED;
  2557. goto exit_slot_reset;
  2558. } else
  2559. goto exit_slot_reset;
  2560. }
  2561. exit_slot_reset:
  2562. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
  2563. "device after reset\n", ha->host_no, __func__, ret);
  2564. return ret;
  2565. }
  2566. static void
  2567. qla4xxx_pci_resume(struct pci_dev *pdev)
  2568. {
  2569. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  2570. int ret;
  2571. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
  2572. ha->host_no, __func__);
  2573. ret = qla4xxx_wait_for_hba_online(ha);
  2574. if (ret != QLA_SUCCESS) {
  2575. ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
  2576. "resume I/O from slot/link_reset\n", ha->host_no,
  2577. __func__);
  2578. }
  2579. pci_cleanup_aer_uncorrect_error_status(pdev);
  2580. clear_bit(AF_EEH_BUSY, &ha->flags);
  2581. }
  2582. static struct pci_error_handlers qla4xxx_err_handler = {
  2583. .error_detected = qla4xxx_pci_error_detected,
  2584. .mmio_enabled = qla4xxx_pci_mmio_enabled,
  2585. .slot_reset = qla4xxx_pci_slot_reset,
  2586. .resume = qla4xxx_pci_resume,
  2587. };
  2588. static struct pci_device_id qla4xxx_pci_tbl[] = {
  2589. {
  2590. .vendor = PCI_VENDOR_ID_QLOGIC,
  2591. .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
  2592. .subvendor = PCI_ANY_ID,
  2593. .subdevice = PCI_ANY_ID,
  2594. },
  2595. {
  2596. .vendor = PCI_VENDOR_ID_QLOGIC,
  2597. .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
  2598. .subvendor = PCI_ANY_ID,
  2599. .subdevice = PCI_ANY_ID,
  2600. },
  2601. {
  2602. .vendor = PCI_VENDOR_ID_QLOGIC,
  2603. .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
  2604. .subvendor = PCI_ANY_ID,
  2605. .subdevice = PCI_ANY_ID,
  2606. },
  2607. {
  2608. .vendor = PCI_VENDOR_ID_QLOGIC,
  2609. .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
  2610. .subvendor = PCI_ANY_ID,
  2611. .subdevice = PCI_ANY_ID,
  2612. },
  2613. {0, 0},
  2614. };
  2615. MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
  2616. static struct pci_driver qla4xxx_pci_driver = {
  2617. .name = DRIVER_NAME,
  2618. .id_table = qla4xxx_pci_tbl,
  2619. .probe = qla4xxx_probe_adapter,
  2620. .remove = qla4xxx_remove_adapter,
  2621. .err_handler = &qla4xxx_err_handler,
  2622. };
  2623. static int __init qla4xxx_module_init(void)
  2624. {
  2625. int ret;
  2626. /* Allocate cache for SRBs. */
  2627. srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
  2628. SLAB_HWCACHE_ALIGN, NULL);
  2629. if (srb_cachep == NULL) {
  2630. printk(KERN_ERR
  2631. "%s: Unable to allocate SRB cache..."
  2632. "Failing load!\n", DRIVER_NAME);
  2633. ret = -ENOMEM;
  2634. goto no_srp_cache;
  2635. }
  2636. /* Derive version string. */
  2637. strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
  2638. if (ql4xextended_error_logging)
  2639. strcat(qla4xxx_version_str, "-debug");
  2640. qla4xxx_scsi_transport =
  2641. iscsi_register_transport(&qla4xxx_iscsi_transport);
  2642. if (!qla4xxx_scsi_transport){
  2643. ret = -ENODEV;
  2644. goto release_srb_cache;
  2645. }
  2646. ret = pci_register_driver(&qla4xxx_pci_driver);
  2647. if (ret)
  2648. goto unregister_transport;
  2649. printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
  2650. return 0;
  2651. unregister_transport:
  2652. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  2653. release_srb_cache:
  2654. kmem_cache_destroy(srb_cachep);
  2655. no_srp_cache:
  2656. return ret;
  2657. }
  2658. static void __exit qla4xxx_module_exit(void)
  2659. {
  2660. pci_unregister_driver(&qla4xxx_pci_driver);
  2661. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  2662. kmem_cache_destroy(srb_cachep);
  2663. }
  2664. module_init(qla4xxx_module_init);
  2665. module_exit(qla4xxx_module_exit);
  2666. MODULE_AUTHOR("QLogic Corporation");
  2667. MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
  2668. MODULE_LICENSE("GPL");
  2669. MODULE_VERSION(QLA4XXX_DRIVER_VERSION);