ql4_os.c 73 KB

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