ql4_os.c 71 KB

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