libiscsi.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586
  1. /*
  2. * iSCSI lib functions
  3. *
  4. * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
  5. * Copyright (C) 2004 - 2006 Mike Christie
  6. * Copyright (C) 2004 - 2005 Dmitry Yusupov
  7. * Copyright (C) 2004 - 2005 Alex Aizman
  8. * maintained by open-iscsi@googlegroups.com
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. #include <linux/types.h>
  25. #include <linux/kfifo.h>
  26. #include <linux/delay.h>
  27. #include <linux/log2.h>
  28. #include <asm/unaligned.h>
  29. #include <net/tcp.h>
  30. #include <scsi/scsi_cmnd.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_eh.h>
  33. #include <scsi/scsi_tcq.h>
  34. #include <scsi/scsi_host.h>
  35. #include <scsi/scsi.h>
  36. #include <scsi/iscsi_proto.h>
  37. #include <scsi/scsi_transport.h>
  38. #include <scsi/scsi_transport_iscsi.h>
  39. #include <scsi/libiscsi.h>
  40. struct iscsi_session *
  41. class_to_transport_session(struct iscsi_cls_session *cls_session)
  42. {
  43. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  44. return iscsi_hostdata(shost->hostdata);
  45. }
  46. EXPORT_SYMBOL_GPL(class_to_transport_session);
  47. /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
  48. #define SNA32_CHECK 2147483648UL
  49. static int iscsi_sna_lt(u32 n1, u32 n2)
  50. {
  51. return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
  52. (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
  53. }
  54. /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
  55. static int iscsi_sna_lte(u32 n1, u32 n2)
  56. {
  57. return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
  58. (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
  59. }
  60. void
  61. iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
  62. {
  63. uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
  64. uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
  65. /*
  66. * standard specifies this check for when to update expected and
  67. * max sequence numbers
  68. */
  69. if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
  70. return;
  71. if (exp_cmdsn != session->exp_cmdsn &&
  72. !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
  73. session->exp_cmdsn = exp_cmdsn;
  74. if (max_cmdsn != session->max_cmdsn &&
  75. !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
  76. session->max_cmdsn = max_cmdsn;
  77. /*
  78. * if the window closed with IO queued, then kick the
  79. * xmit thread
  80. */
  81. if (!list_empty(&session->leadconn->xmitqueue) ||
  82. !list_empty(&session->leadconn->mgmtqueue))
  83. scsi_queue_work(session->host,
  84. &session->leadconn->xmitwork);
  85. }
  86. }
  87. EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
  88. void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
  89. struct iscsi_data *hdr)
  90. {
  91. struct iscsi_conn *conn = ctask->conn;
  92. memset(hdr, 0, sizeof(struct iscsi_data));
  93. hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
  94. hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
  95. ctask->unsol_datasn++;
  96. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  97. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  98. hdr->itt = ctask->hdr->itt;
  99. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  100. hdr->offset = cpu_to_be32(ctask->unsol_offset);
  101. if (ctask->unsol_count > conn->max_xmit_dlength) {
  102. hton24(hdr->dlength, conn->max_xmit_dlength);
  103. ctask->data_count = conn->max_xmit_dlength;
  104. ctask->unsol_offset += ctask->data_count;
  105. hdr->flags = 0;
  106. } else {
  107. hton24(hdr->dlength, ctask->unsol_count);
  108. ctask->data_count = ctask->unsol_count;
  109. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  110. }
  111. }
  112. EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
  113. static int iscsi_add_hdr(struct iscsi_cmd_task *ctask, unsigned len)
  114. {
  115. unsigned exp_len = ctask->hdr_len + len;
  116. if (exp_len > ctask->hdr_max) {
  117. WARN_ON(1);
  118. return -EINVAL;
  119. }
  120. WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
  121. ctask->hdr_len = exp_len;
  122. return 0;
  123. }
  124. /*
  125. * make an extended cdb AHS
  126. */
  127. static int iscsi_prep_ecdb_ahs(struct iscsi_cmd_task *ctask)
  128. {
  129. struct scsi_cmnd *cmd = ctask->sc;
  130. unsigned rlen, pad_len;
  131. unsigned short ahslength;
  132. struct iscsi_ecdb_ahdr *ecdb_ahdr;
  133. int rc;
  134. ecdb_ahdr = iscsi_next_hdr(ctask);
  135. rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
  136. BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
  137. ahslength = rlen + sizeof(ecdb_ahdr->reserved);
  138. pad_len = iscsi_padding(rlen);
  139. rc = iscsi_add_hdr(ctask, sizeof(ecdb_ahdr->ahslength) +
  140. sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
  141. if (rc)
  142. return rc;
  143. if (pad_len)
  144. memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
  145. ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
  146. ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
  147. ecdb_ahdr->reserved = 0;
  148. memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
  149. debug_scsi("iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
  150. "rlen %d pad_len %d ahs_length %d iscsi_headers_size %u\n",
  151. cmd->cmd_len, rlen, pad_len, ahslength, ctask->hdr_len);
  152. return 0;
  153. }
  154. static int iscsi_prep_bidi_ahs(struct iscsi_cmd_task *ctask)
  155. {
  156. struct scsi_cmnd *sc = ctask->sc;
  157. struct iscsi_rlength_ahdr *rlen_ahdr;
  158. int rc;
  159. rlen_ahdr = iscsi_next_hdr(ctask);
  160. rc = iscsi_add_hdr(ctask, sizeof(*rlen_ahdr));
  161. if (rc)
  162. return rc;
  163. rlen_ahdr->ahslength =
  164. cpu_to_be16(sizeof(rlen_ahdr->read_length) +
  165. sizeof(rlen_ahdr->reserved));
  166. rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
  167. rlen_ahdr->reserved = 0;
  168. rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
  169. debug_scsi("bidi-in rlen_ahdr->read_length(%d) "
  170. "rlen_ahdr->ahslength(%d)\n",
  171. be32_to_cpu(rlen_ahdr->read_length),
  172. be16_to_cpu(rlen_ahdr->ahslength));
  173. return 0;
  174. }
  175. /**
  176. * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
  177. * @ctask: iscsi cmd task
  178. *
  179. * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
  180. * fields like dlength or final based on how much data it sends
  181. */
  182. static int iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
  183. {
  184. struct iscsi_conn *conn = ctask->conn;
  185. struct iscsi_session *session = conn->session;
  186. struct iscsi_cmd *hdr = ctask->hdr;
  187. struct scsi_cmnd *sc = ctask->sc;
  188. unsigned hdrlength, cmd_len;
  189. int rc;
  190. ctask->hdr_len = 0;
  191. rc = iscsi_add_hdr(ctask, sizeof(*hdr));
  192. if (rc)
  193. return rc;
  194. hdr->opcode = ISCSI_OP_SCSI_CMD;
  195. hdr->flags = ISCSI_ATTR_SIMPLE;
  196. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  197. hdr->itt = build_itt(ctask->itt, session->age);
  198. hdr->cmdsn = cpu_to_be32(session->cmdsn);
  199. session->cmdsn++;
  200. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  201. cmd_len = sc->cmd_len;
  202. if (cmd_len < ISCSI_CDB_SIZE)
  203. memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
  204. else if (cmd_len > ISCSI_CDB_SIZE) {
  205. rc = iscsi_prep_ecdb_ahs(ctask);
  206. if (rc)
  207. return rc;
  208. cmd_len = ISCSI_CDB_SIZE;
  209. }
  210. memcpy(hdr->cdb, sc->cmnd, cmd_len);
  211. ctask->imm_count = 0;
  212. if (scsi_bidi_cmnd(sc)) {
  213. hdr->flags |= ISCSI_FLAG_CMD_READ;
  214. rc = iscsi_prep_bidi_ahs(ctask);
  215. if (rc)
  216. return rc;
  217. }
  218. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  219. unsigned out_len = scsi_out(sc)->length;
  220. hdr->data_length = cpu_to_be32(out_len);
  221. hdr->flags |= ISCSI_FLAG_CMD_WRITE;
  222. /*
  223. * Write counters:
  224. *
  225. * imm_count bytes to be sent right after
  226. * SCSI PDU Header
  227. *
  228. * unsol_count bytes(as Data-Out) to be sent
  229. * without R2T ack right after
  230. * immediate data
  231. *
  232. * r2t_data_count bytes to be sent via R2T ack's
  233. *
  234. * pad_count bytes to be sent as zero-padding
  235. */
  236. ctask->unsol_count = 0;
  237. ctask->unsol_offset = 0;
  238. ctask->unsol_datasn = 0;
  239. if (session->imm_data_en) {
  240. if (out_len >= session->first_burst)
  241. ctask->imm_count = min(session->first_burst,
  242. conn->max_xmit_dlength);
  243. else
  244. ctask->imm_count = min(out_len,
  245. conn->max_xmit_dlength);
  246. hton24(hdr->dlength, ctask->imm_count);
  247. } else
  248. zero_data(hdr->dlength);
  249. if (!session->initial_r2t_en) {
  250. ctask->unsol_count = min(session->first_burst, out_len)
  251. - ctask->imm_count;
  252. ctask->unsol_offset = ctask->imm_count;
  253. }
  254. if (!ctask->unsol_count)
  255. /* No unsolicit Data-Out's */
  256. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  257. } else {
  258. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  259. zero_data(hdr->dlength);
  260. hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
  261. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  262. hdr->flags |= ISCSI_FLAG_CMD_READ;
  263. }
  264. /* calculate size of additional header segments (AHSs) */
  265. hdrlength = ctask->hdr_len - sizeof(*hdr);
  266. WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
  267. hdrlength /= ISCSI_PAD_LEN;
  268. WARN_ON(hdrlength >= 256);
  269. hdr->hlength = hdrlength & 0xFF;
  270. if (conn->session->tt->init_cmd_task(conn->ctask))
  271. return EIO;
  272. conn->scsicmd_pdus_cnt++;
  273. debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x "
  274. "len %d bidi_len %d cmdsn %d win %d]\n",
  275. scsi_bidi_cmnd(sc) ? "bidirectional" :
  276. sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
  277. conn->id, sc, sc->cmnd[0], ctask->itt,
  278. scsi_bufflen(sc), scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
  279. session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
  280. return 0;
  281. }
  282. /**
  283. * iscsi_complete_command - return command back to scsi-ml
  284. * @ctask: iscsi cmd task
  285. *
  286. * Must be called with session lock.
  287. * This function returns the scsi command to scsi-ml and returns
  288. * the cmd task to the pool of available cmd tasks.
  289. */
  290. static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
  291. {
  292. struct iscsi_conn *conn = ctask->conn;
  293. struct iscsi_session *session = conn->session;
  294. struct scsi_cmnd *sc = ctask->sc;
  295. ctask->state = ISCSI_TASK_COMPLETED;
  296. ctask->sc = NULL;
  297. /* SCSI eh reuses commands to verify us */
  298. sc->SCp.ptr = NULL;
  299. if (conn->ctask == ctask)
  300. conn->ctask = NULL;
  301. list_del_init(&ctask->running);
  302. __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
  303. sc->scsi_done(sc);
  304. }
  305. static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
  306. {
  307. atomic_inc(&ctask->refcount);
  308. }
  309. static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
  310. {
  311. if (atomic_dec_and_test(&ctask->refcount))
  312. iscsi_complete_command(ctask);
  313. }
  314. /*
  315. * session lock must be held
  316. */
  317. static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  318. int err)
  319. {
  320. struct scsi_cmnd *sc;
  321. sc = ctask->sc;
  322. if (!sc)
  323. return;
  324. if (ctask->state == ISCSI_TASK_PENDING)
  325. /*
  326. * cmd never made it to the xmit thread, so we should not count
  327. * the cmd in the sequencing
  328. */
  329. conn->session->queued_cmdsn--;
  330. else
  331. conn->session->tt->cleanup_cmd_task(conn, ctask);
  332. sc->result = err;
  333. if (!scsi_bidi_cmnd(sc))
  334. scsi_set_resid(sc, scsi_bufflen(sc));
  335. else {
  336. scsi_out(sc)->resid = scsi_out(sc)->length;
  337. scsi_in(sc)->resid = scsi_in(sc)->length;
  338. }
  339. if (conn->ctask == ctask)
  340. conn->ctask = NULL;
  341. /* release ref from queuecommand */
  342. __iscsi_put_ctask(ctask);
  343. }
  344. /**
  345. * iscsi_free_mgmt_task - return mgmt task back to pool
  346. * @conn: iscsi connection
  347. * @mtask: mtask
  348. *
  349. * Must be called with session lock.
  350. */
  351. void iscsi_free_mgmt_task(struct iscsi_conn *conn,
  352. struct iscsi_mgmt_task *mtask)
  353. {
  354. list_del_init(&mtask->running);
  355. if (conn->login_mtask == mtask)
  356. return;
  357. if (conn->ping_mtask == mtask)
  358. conn->ping_mtask = NULL;
  359. __kfifo_put(conn->session->mgmtpool.queue,
  360. (void*)&mtask, sizeof(void*));
  361. }
  362. EXPORT_SYMBOL_GPL(iscsi_free_mgmt_task);
  363. static struct iscsi_mgmt_task *
  364. __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  365. char *data, uint32_t data_size)
  366. {
  367. struct iscsi_session *session = conn->session;
  368. struct iscsi_mgmt_task *mtask;
  369. if (session->state == ISCSI_STATE_TERMINATE)
  370. return NULL;
  371. if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
  372. hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  373. /*
  374. * Login and Text are sent serially, in
  375. * request-followed-by-response sequence.
  376. * Same mtask can be used. Same ITT must be used.
  377. * Note that login_mtask is preallocated at conn_create().
  378. */
  379. mtask = conn->login_mtask;
  380. else {
  381. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  382. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  383. if (!__kfifo_get(session->mgmtpool.queue,
  384. (void*)&mtask, sizeof(void*)))
  385. return NULL;
  386. }
  387. if (data_size) {
  388. memcpy(mtask->data, data, data_size);
  389. mtask->data_count = data_size;
  390. } else
  391. mtask->data_count = 0;
  392. memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
  393. INIT_LIST_HEAD(&mtask->running);
  394. list_add_tail(&mtask->running, &conn->mgmtqueue);
  395. return mtask;
  396. }
  397. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  398. char *data, uint32_t data_size)
  399. {
  400. struct iscsi_conn *conn = cls_conn->dd_data;
  401. struct iscsi_session *session = conn->session;
  402. int err = 0;
  403. spin_lock_bh(&session->lock);
  404. if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
  405. err = -EPERM;
  406. spin_unlock_bh(&session->lock);
  407. scsi_queue_work(session->host, &conn->xmitwork);
  408. return err;
  409. }
  410. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  411. /**
  412. * iscsi_cmd_rsp - SCSI Command Response processing
  413. * @conn: iscsi connection
  414. * @hdr: iscsi header
  415. * @ctask: scsi command task
  416. * @data: cmd data buffer
  417. * @datalen: len of buffer
  418. *
  419. * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
  420. * then completes the command and task.
  421. **/
  422. static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  423. struct iscsi_cmd_task *ctask, char *data,
  424. int datalen)
  425. {
  426. struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
  427. struct iscsi_session *session = conn->session;
  428. struct scsi_cmnd *sc = ctask->sc;
  429. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  430. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  431. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  432. if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
  433. sc->result = DID_ERROR << 16;
  434. goto out;
  435. }
  436. if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
  437. uint16_t senselen;
  438. if (datalen < 2) {
  439. invalid_datalen:
  440. iscsi_conn_printk(KERN_ERR, conn,
  441. "Got CHECK_CONDITION but invalid data "
  442. "buffer size of %d\n", datalen);
  443. sc->result = DID_BAD_TARGET << 16;
  444. goto out;
  445. }
  446. senselen = be16_to_cpu(get_unaligned((__be16 *) data));
  447. if (datalen < senselen)
  448. goto invalid_datalen;
  449. memcpy(sc->sense_buffer, data + 2,
  450. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  451. debug_scsi("copied %d bytes of sense\n",
  452. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  453. }
  454. if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
  455. ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
  456. int res_count = be32_to_cpu(rhdr->bi_residual_count);
  457. if (scsi_bidi_cmnd(sc) && res_count > 0 &&
  458. (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
  459. res_count <= scsi_in(sc)->length))
  460. scsi_in(sc)->resid = res_count;
  461. else
  462. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  463. }
  464. if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
  465. ISCSI_FLAG_CMD_OVERFLOW)) {
  466. int res_count = be32_to_cpu(rhdr->residual_count);
  467. if (res_count > 0 &&
  468. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  469. res_count <= scsi_bufflen(sc)))
  470. /* write side for bidi or uni-io set_resid */
  471. scsi_set_resid(sc, res_count);
  472. else
  473. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  474. }
  475. out:
  476. debug_scsi("done [sc %lx res %d itt 0x%x]\n",
  477. (long)sc, sc->result, ctask->itt);
  478. conn->scsirsp_pdus_cnt++;
  479. __iscsi_put_ctask(ctask);
  480. }
  481. static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  482. {
  483. struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
  484. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  485. conn->tmfrsp_pdus_cnt++;
  486. if (conn->tmf_state != TMF_QUEUED)
  487. return;
  488. if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
  489. conn->tmf_state = TMF_SUCCESS;
  490. else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
  491. conn->tmf_state = TMF_NOT_FOUND;
  492. else
  493. conn->tmf_state = TMF_FAILED;
  494. wake_up(&conn->ehwait);
  495. }
  496. static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
  497. {
  498. struct iscsi_nopout hdr;
  499. struct iscsi_mgmt_task *mtask;
  500. if (!rhdr && conn->ping_mtask)
  501. return;
  502. memset(&hdr, 0, sizeof(struct iscsi_nopout));
  503. hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
  504. hdr.flags = ISCSI_FLAG_CMD_FINAL;
  505. if (rhdr) {
  506. memcpy(hdr.lun, rhdr->lun, 8);
  507. hdr.ttt = rhdr->ttt;
  508. hdr.itt = RESERVED_ITT;
  509. } else
  510. hdr.ttt = RESERVED_ITT;
  511. mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
  512. if (!mtask) {
  513. iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
  514. return;
  515. }
  516. /* only track our nops */
  517. if (!rhdr) {
  518. conn->ping_mtask = mtask;
  519. conn->last_ping = jiffies;
  520. }
  521. scsi_queue_work(conn->session->host, &conn->xmitwork);
  522. }
  523. static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  524. char *data, int datalen)
  525. {
  526. struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
  527. struct iscsi_hdr rejected_pdu;
  528. uint32_t itt;
  529. conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
  530. if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
  531. if (ntoh24(reject->dlength) > datalen)
  532. return ISCSI_ERR_PROTO;
  533. if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
  534. memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
  535. itt = get_itt(rejected_pdu.itt);
  536. iscsi_conn_printk(KERN_ERR, conn,
  537. "itt 0x%x had pdu (op 0x%x) rejected "
  538. "due to DataDigest error.\n", itt,
  539. rejected_pdu.opcode);
  540. }
  541. }
  542. return 0;
  543. }
  544. /**
  545. * __iscsi_complete_pdu - complete pdu
  546. * @conn: iscsi conn
  547. * @hdr: iscsi header
  548. * @data: data buffer
  549. * @datalen: len of data buffer
  550. *
  551. * Completes pdu processing by freeing any resources allocated at
  552. * queuecommand or send generic. session lock must be held and verify
  553. * itt must have been called.
  554. */
  555. static int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  556. char *data, int datalen)
  557. {
  558. struct iscsi_session *session = conn->session;
  559. int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
  560. struct iscsi_cmd_task *ctask;
  561. struct iscsi_mgmt_task *mtask;
  562. uint32_t itt;
  563. conn->last_recv = jiffies;
  564. if (hdr->itt != RESERVED_ITT)
  565. itt = get_itt(hdr->itt);
  566. else
  567. itt = ~0U;
  568. if (itt < session->cmds_max) {
  569. ctask = session->cmds[itt];
  570. debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  571. opcode, conn->id, ctask->itt, datalen);
  572. switch(opcode) {
  573. case ISCSI_OP_SCSI_CMD_RSP:
  574. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  575. iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
  576. datalen);
  577. break;
  578. case ISCSI_OP_SCSI_DATA_IN:
  579. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  580. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  581. conn->scsirsp_pdus_cnt++;
  582. __iscsi_put_ctask(ctask);
  583. }
  584. break;
  585. case ISCSI_OP_R2T:
  586. /* LLD handles this for now */
  587. break;
  588. default:
  589. rc = ISCSI_ERR_BAD_OPCODE;
  590. break;
  591. }
  592. } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
  593. itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
  594. mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
  595. debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  596. opcode, conn->id, mtask->itt, datalen);
  597. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  598. switch(opcode) {
  599. case ISCSI_OP_LOGOUT_RSP:
  600. if (datalen) {
  601. rc = ISCSI_ERR_PROTO;
  602. break;
  603. }
  604. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  605. /* fall through */
  606. case ISCSI_OP_LOGIN_RSP:
  607. case ISCSI_OP_TEXT_RSP:
  608. /*
  609. * login related PDU's exp_statsn is handled in
  610. * userspace
  611. */
  612. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  613. rc = ISCSI_ERR_CONN_FAILED;
  614. iscsi_free_mgmt_task(conn, mtask);
  615. break;
  616. case ISCSI_OP_SCSI_TMFUNC_RSP:
  617. if (datalen) {
  618. rc = ISCSI_ERR_PROTO;
  619. break;
  620. }
  621. iscsi_tmf_rsp(conn, hdr);
  622. iscsi_free_mgmt_task(conn, mtask);
  623. break;
  624. case ISCSI_OP_NOOP_IN:
  625. if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) ||
  626. datalen) {
  627. rc = ISCSI_ERR_PROTO;
  628. break;
  629. }
  630. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  631. if (conn->ping_mtask != mtask) {
  632. /*
  633. * If this is not in response to one of our
  634. * nops then it must be from userspace.
  635. */
  636. if (iscsi_recv_pdu(conn->cls_conn, hdr, data,
  637. datalen))
  638. rc = ISCSI_ERR_CONN_FAILED;
  639. }
  640. iscsi_free_mgmt_task(conn, mtask);
  641. break;
  642. default:
  643. rc = ISCSI_ERR_BAD_OPCODE;
  644. break;
  645. }
  646. } else if (itt == ~0U) {
  647. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  648. switch(opcode) {
  649. case ISCSI_OP_NOOP_IN:
  650. if (datalen) {
  651. rc = ISCSI_ERR_PROTO;
  652. break;
  653. }
  654. if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
  655. break;
  656. iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
  657. break;
  658. case ISCSI_OP_REJECT:
  659. rc = iscsi_handle_reject(conn, hdr, data, datalen);
  660. break;
  661. case ISCSI_OP_ASYNC_EVENT:
  662. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  663. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  664. rc = ISCSI_ERR_CONN_FAILED;
  665. break;
  666. default:
  667. rc = ISCSI_ERR_BAD_OPCODE;
  668. break;
  669. }
  670. } else
  671. rc = ISCSI_ERR_BAD_ITT;
  672. return rc;
  673. }
  674. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  675. char *data, int datalen)
  676. {
  677. int rc;
  678. spin_lock(&conn->session->lock);
  679. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  680. spin_unlock(&conn->session->lock);
  681. return rc;
  682. }
  683. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  684. /* verify itt (itt encoding: age+cid+itt) */
  685. int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  686. uint32_t *ret_itt)
  687. {
  688. struct iscsi_session *session = conn->session;
  689. struct iscsi_cmd_task *ctask;
  690. uint32_t itt;
  691. if (hdr->itt != RESERVED_ITT) {
  692. if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
  693. (session->age << ISCSI_AGE_SHIFT)) {
  694. iscsi_conn_printk(KERN_ERR, conn,
  695. "received itt %x expected session "
  696. "age (%x)\n", (__force u32)hdr->itt,
  697. session->age & ISCSI_AGE_MASK);
  698. return ISCSI_ERR_BAD_ITT;
  699. }
  700. itt = get_itt(hdr->itt);
  701. } else
  702. itt = ~0U;
  703. if (itt < session->cmds_max) {
  704. ctask = session->cmds[itt];
  705. if (!ctask->sc) {
  706. iscsi_conn_printk(KERN_INFO, conn, "dropping ctask "
  707. "with itt 0x%x\n", ctask->itt);
  708. /* force drop */
  709. return ISCSI_ERR_NO_SCSI_CMD;
  710. }
  711. if (ctask->sc->SCp.phase != session->age) {
  712. iscsi_conn_printk(KERN_ERR, conn,
  713. "iscsi: ctask's session age %d, "
  714. "expected %d\n", ctask->sc->SCp.phase,
  715. session->age);
  716. return ISCSI_ERR_SESSION_FAILED;
  717. }
  718. }
  719. *ret_itt = itt;
  720. return 0;
  721. }
  722. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  723. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  724. {
  725. struct iscsi_session *session = conn->session;
  726. unsigned long flags;
  727. spin_lock_irqsave(&session->lock, flags);
  728. if (session->state == ISCSI_STATE_FAILED) {
  729. spin_unlock_irqrestore(&session->lock, flags);
  730. return;
  731. }
  732. if (conn->stop_stage == 0)
  733. session->state = ISCSI_STATE_FAILED;
  734. spin_unlock_irqrestore(&session->lock, flags);
  735. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  736. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  737. iscsi_conn_error(conn->cls_conn, err);
  738. }
  739. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  740. static void iscsi_prep_mtask(struct iscsi_conn *conn,
  741. struct iscsi_mgmt_task *mtask)
  742. {
  743. struct iscsi_session *session = conn->session;
  744. struct iscsi_hdr *hdr = mtask->hdr;
  745. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  746. if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
  747. hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  748. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  749. /*
  750. * pre-format CmdSN for outgoing PDU.
  751. */
  752. nop->cmdsn = cpu_to_be32(session->cmdsn);
  753. if (hdr->itt != RESERVED_ITT) {
  754. hdr->itt = build_itt(mtask->itt, session->age);
  755. /*
  756. * TODO: We always use immediate, so we never hit this.
  757. * If we start to send tmfs or nops as non-immediate then
  758. * we should start checking the cmdsn numbers for mgmt tasks.
  759. */
  760. if (conn->c_stage == ISCSI_CONN_STARTED &&
  761. !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
  762. session->queued_cmdsn++;
  763. session->cmdsn++;
  764. }
  765. }
  766. if (session->tt->init_mgmt_task)
  767. session->tt->init_mgmt_task(conn, mtask);
  768. debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
  769. hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
  770. mtask->data_count);
  771. }
  772. static int iscsi_xmit_mtask(struct iscsi_conn *conn)
  773. {
  774. struct iscsi_hdr *hdr = conn->mtask->hdr;
  775. int rc;
  776. if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
  777. conn->session->state = ISCSI_STATE_LOGGING_OUT;
  778. spin_unlock_bh(&conn->session->lock);
  779. rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
  780. spin_lock_bh(&conn->session->lock);
  781. if (rc)
  782. return rc;
  783. /* done with this in-progress mtask */
  784. conn->mtask = NULL;
  785. return 0;
  786. }
  787. static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
  788. {
  789. struct iscsi_session *session = conn->session;
  790. /*
  791. * Check for iSCSI window and take care of CmdSN wrap-around
  792. */
  793. if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
  794. debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
  795. "CmdSN %u/%u\n", session->exp_cmdsn,
  796. session->max_cmdsn, session->cmdsn,
  797. session->queued_cmdsn);
  798. return -ENOSPC;
  799. }
  800. return 0;
  801. }
  802. static int iscsi_xmit_ctask(struct iscsi_conn *conn)
  803. {
  804. struct iscsi_cmd_task *ctask = conn->ctask;
  805. int rc;
  806. __iscsi_get_ctask(ctask);
  807. spin_unlock_bh(&conn->session->lock);
  808. rc = conn->session->tt->xmit_cmd_task(conn, ctask);
  809. spin_lock_bh(&conn->session->lock);
  810. __iscsi_put_ctask(ctask);
  811. if (!rc)
  812. /* done with this ctask */
  813. conn->ctask = NULL;
  814. return rc;
  815. }
  816. /**
  817. * iscsi_requeue_ctask - requeue ctask to run from session workqueue
  818. * @ctask: ctask to requeue
  819. *
  820. * LLDs that need to run a ctask from the session workqueue should call
  821. * this. The session lock must be held.
  822. */
  823. void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask)
  824. {
  825. struct iscsi_conn *conn = ctask->conn;
  826. list_move_tail(&ctask->running, &conn->requeue);
  827. scsi_queue_work(conn->session->host, &conn->xmitwork);
  828. }
  829. EXPORT_SYMBOL_GPL(iscsi_requeue_ctask);
  830. /**
  831. * iscsi_data_xmit - xmit any command into the scheduled connection
  832. * @conn: iscsi connection
  833. *
  834. * Notes:
  835. * The function can return -EAGAIN in which case the caller must
  836. * re-schedule it again later or recover. '0' return code means
  837. * successful xmit.
  838. **/
  839. static int iscsi_data_xmit(struct iscsi_conn *conn)
  840. {
  841. int rc = 0;
  842. spin_lock_bh(&conn->session->lock);
  843. if (unlikely(conn->suspend_tx)) {
  844. debug_scsi("conn %d Tx suspended!\n", conn->id);
  845. spin_unlock_bh(&conn->session->lock);
  846. return -ENODATA;
  847. }
  848. if (conn->ctask) {
  849. rc = iscsi_xmit_ctask(conn);
  850. if (rc)
  851. goto again;
  852. }
  853. if (conn->mtask) {
  854. rc = iscsi_xmit_mtask(conn);
  855. if (rc)
  856. goto again;
  857. }
  858. /*
  859. * process mgmt pdus like nops before commands since we should
  860. * only have one nop-out as a ping from us and targets should not
  861. * overflow us with nop-ins
  862. */
  863. check_mgmt:
  864. while (!list_empty(&conn->mgmtqueue)) {
  865. conn->mtask = list_entry(conn->mgmtqueue.next,
  866. struct iscsi_mgmt_task, running);
  867. if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
  868. iscsi_free_mgmt_task(conn, conn->mtask);
  869. conn->mtask = NULL;
  870. continue;
  871. }
  872. iscsi_prep_mtask(conn, conn->mtask);
  873. list_move_tail(conn->mgmtqueue.next, &conn->mgmt_run_list);
  874. rc = iscsi_xmit_mtask(conn);
  875. if (rc)
  876. goto again;
  877. }
  878. /* process pending command queue */
  879. while (!list_empty(&conn->xmitqueue)) {
  880. if (conn->tmf_state == TMF_QUEUED)
  881. break;
  882. conn->ctask = list_entry(conn->xmitqueue.next,
  883. struct iscsi_cmd_task, running);
  884. if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
  885. fail_command(conn, conn->ctask, DID_IMM_RETRY << 16);
  886. continue;
  887. }
  888. if (iscsi_prep_scsi_cmd_pdu(conn->ctask)) {
  889. fail_command(conn, conn->ctask, DID_ABORT << 16);
  890. continue;
  891. }
  892. conn->ctask->state = ISCSI_TASK_RUNNING;
  893. list_move_tail(conn->xmitqueue.next, &conn->run_list);
  894. rc = iscsi_xmit_ctask(conn);
  895. if (rc)
  896. goto again;
  897. /*
  898. * we could continuously get new ctask requests so
  899. * we need to check the mgmt queue for nops that need to
  900. * be sent to aviod starvation
  901. */
  902. if (!list_empty(&conn->mgmtqueue))
  903. goto check_mgmt;
  904. }
  905. while (!list_empty(&conn->requeue)) {
  906. if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
  907. break;
  908. /*
  909. * we always do fastlogout - conn stop code will clean up.
  910. */
  911. if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
  912. break;
  913. conn->ctask = list_entry(conn->requeue.next,
  914. struct iscsi_cmd_task, running);
  915. conn->ctask->state = ISCSI_TASK_RUNNING;
  916. list_move_tail(conn->requeue.next, &conn->run_list);
  917. rc = iscsi_xmit_ctask(conn);
  918. if (rc)
  919. goto again;
  920. if (!list_empty(&conn->mgmtqueue))
  921. goto check_mgmt;
  922. }
  923. spin_unlock_bh(&conn->session->lock);
  924. return -ENODATA;
  925. again:
  926. if (unlikely(conn->suspend_tx))
  927. rc = -ENODATA;
  928. spin_unlock_bh(&conn->session->lock);
  929. return rc;
  930. }
  931. static void iscsi_xmitworker(struct work_struct *work)
  932. {
  933. struct iscsi_conn *conn =
  934. container_of(work, struct iscsi_conn, xmitwork);
  935. int rc;
  936. /*
  937. * serialize Xmit worker on a per-connection basis.
  938. */
  939. do {
  940. rc = iscsi_data_xmit(conn);
  941. } while (rc >= 0 || rc == -EAGAIN);
  942. }
  943. enum {
  944. FAILURE_BAD_HOST = 1,
  945. FAILURE_SESSION_FAILED,
  946. FAILURE_SESSION_FREED,
  947. FAILURE_WINDOW_CLOSED,
  948. FAILURE_OOM,
  949. FAILURE_SESSION_TERMINATE,
  950. FAILURE_SESSION_IN_RECOVERY,
  951. FAILURE_SESSION_RECOVERY_TIMEOUT,
  952. FAILURE_SESSION_LOGGING_OUT,
  953. FAILURE_SESSION_NOT_READY,
  954. };
  955. int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  956. {
  957. struct Scsi_Host *host;
  958. int reason = 0;
  959. struct iscsi_session *session;
  960. struct iscsi_conn *conn;
  961. struct iscsi_cmd_task *ctask = NULL;
  962. sc->scsi_done = done;
  963. sc->result = 0;
  964. sc->SCp.ptr = NULL;
  965. host = sc->device->host;
  966. spin_unlock(host->host_lock);
  967. session = iscsi_hostdata(host->hostdata);
  968. spin_lock(&session->lock);
  969. reason = iscsi_session_chkready(session_to_cls(session));
  970. if (reason) {
  971. sc->result = reason;
  972. goto fault;
  973. }
  974. /*
  975. * ISCSI_STATE_FAILED is a temp. state. The recovery
  976. * code will decide what is best to do with command queued
  977. * during this time
  978. */
  979. if (session->state != ISCSI_STATE_LOGGED_IN &&
  980. session->state != ISCSI_STATE_FAILED) {
  981. /*
  982. * to handle the race between when we set the recovery state
  983. * and block the session we requeue here (commands could
  984. * be entering our queuecommand while a block is starting
  985. * up because the block code is not locked)
  986. */
  987. switch (session->state) {
  988. case ISCSI_STATE_IN_RECOVERY:
  989. reason = FAILURE_SESSION_IN_RECOVERY;
  990. sc->result = DID_IMM_RETRY << 16;
  991. break;
  992. case ISCSI_STATE_LOGGING_OUT:
  993. reason = FAILURE_SESSION_LOGGING_OUT;
  994. sc->result = DID_IMM_RETRY << 16;
  995. break;
  996. case ISCSI_STATE_RECOVERY_FAILED:
  997. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  998. sc->result = DID_NO_CONNECT << 16;
  999. break;
  1000. case ISCSI_STATE_TERMINATE:
  1001. reason = FAILURE_SESSION_TERMINATE;
  1002. sc->result = DID_NO_CONNECT << 16;
  1003. break;
  1004. default:
  1005. reason = FAILURE_SESSION_FREED;
  1006. sc->result = DID_NO_CONNECT << 16;
  1007. }
  1008. goto fault;
  1009. }
  1010. conn = session->leadconn;
  1011. if (!conn) {
  1012. reason = FAILURE_SESSION_FREED;
  1013. sc->result = DID_NO_CONNECT << 16;
  1014. goto fault;
  1015. }
  1016. if (iscsi_check_cmdsn_window_closed(conn)) {
  1017. reason = FAILURE_WINDOW_CLOSED;
  1018. goto reject;
  1019. }
  1020. if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
  1021. sizeof(void*))) {
  1022. reason = FAILURE_OOM;
  1023. goto reject;
  1024. }
  1025. session->queued_cmdsn++;
  1026. sc->SCp.phase = session->age;
  1027. sc->SCp.ptr = (char *)ctask;
  1028. atomic_set(&ctask->refcount, 1);
  1029. ctask->state = ISCSI_TASK_PENDING;
  1030. ctask->conn = conn;
  1031. ctask->sc = sc;
  1032. INIT_LIST_HEAD(&ctask->running);
  1033. list_add_tail(&ctask->running, &conn->xmitqueue);
  1034. spin_unlock(&session->lock);
  1035. scsi_queue_work(host, &conn->xmitwork);
  1036. spin_lock(host->host_lock);
  1037. return 0;
  1038. reject:
  1039. spin_unlock(&session->lock);
  1040. debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
  1041. spin_lock(host->host_lock);
  1042. return SCSI_MLQUEUE_HOST_BUSY;
  1043. fault:
  1044. spin_unlock(&session->lock);
  1045. debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
  1046. if (!scsi_bidi_cmnd(sc))
  1047. scsi_set_resid(sc, scsi_bufflen(sc));
  1048. else {
  1049. scsi_out(sc)->resid = scsi_out(sc)->length;
  1050. scsi_in(sc)->resid = scsi_in(sc)->length;
  1051. }
  1052. sc->scsi_done(sc);
  1053. spin_lock(host->host_lock);
  1054. return 0;
  1055. }
  1056. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  1057. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
  1058. {
  1059. if (depth > ISCSI_MAX_CMD_PER_LUN)
  1060. depth = ISCSI_MAX_CMD_PER_LUN;
  1061. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  1062. return sdev->queue_depth;
  1063. }
  1064. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  1065. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  1066. {
  1067. struct iscsi_session *session = class_to_transport_session(cls_session);
  1068. spin_lock_bh(&session->lock);
  1069. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1070. session->state = ISCSI_STATE_RECOVERY_FAILED;
  1071. if (session->leadconn)
  1072. wake_up(&session->leadconn->ehwait);
  1073. }
  1074. spin_unlock_bh(&session->lock);
  1075. }
  1076. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  1077. int iscsi_eh_host_reset(struct scsi_cmnd *sc)
  1078. {
  1079. struct Scsi_Host *host = sc->device->host;
  1080. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  1081. struct iscsi_conn *conn = session->leadconn;
  1082. mutex_lock(&session->eh_mutex);
  1083. spin_lock_bh(&session->lock);
  1084. if (session->state == ISCSI_STATE_TERMINATE) {
  1085. failed:
  1086. debug_scsi("failing host reset: session terminated "
  1087. "[CID %d age %d]\n", conn->id, session->age);
  1088. spin_unlock_bh(&session->lock);
  1089. mutex_unlock(&session->eh_mutex);
  1090. return FAILED;
  1091. }
  1092. spin_unlock_bh(&session->lock);
  1093. mutex_unlock(&session->eh_mutex);
  1094. /*
  1095. * we drop the lock here but the leadconn cannot be destoyed while
  1096. * we are in the scsi eh
  1097. */
  1098. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1099. debug_scsi("iscsi_eh_host_reset wait for relogin\n");
  1100. wait_event_interruptible(conn->ehwait,
  1101. session->state == ISCSI_STATE_TERMINATE ||
  1102. session->state == ISCSI_STATE_LOGGED_IN ||
  1103. session->state == ISCSI_STATE_RECOVERY_FAILED);
  1104. if (signal_pending(current))
  1105. flush_signals(current);
  1106. mutex_lock(&session->eh_mutex);
  1107. spin_lock_bh(&session->lock);
  1108. if (session->state == ISCSI_STATE_LOGGED_IN)
  1109. iscsi_session_printk(KERN_INFO, session,
  1110. "host reset succeeded\n");
  1111. else
  1112. goto failed;
  1113. spin_unlock_bh(&session->lock);
  1114. mutex_unlock(&session->eh_mutex);
  1115. return SUCCESS;
  1116. }
  1117. EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
  1118. static void iscsi_tmf_timedout(unsigned long data)
  1119. {
  1120. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1121. struct iscsi_session *session = conn->session;
  1122. spin_lock(&session->lock);
  1123. if (conn->tmf_state == TMF_QUEUED) {
  1124. conn->tmf_state = TMF_TIMEDOUT;
  1125. debug_scsi("tmf timedout\n");
  1126. /* unblock eh_abort() */
  1127. wake_up(&conn->ehwait);
  1128. }
  1129. spin_unlock(&session->lock);
  1130. }
  1131. static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
  1132. struct iscsi_tm *hdr, int age,
  1133. int timeout)
  1134. {
  1135. struct iscsi_session *session = conn->session;
  1136. struct iscsi_mgmt_task *mtask;
  1137. mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
  1138. NULL, 0);
  1139. if (!mtask) {
  1140. spin_unlock_bh(&session->lock);
  1141. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1142. spin_lock_bh(&session->lock);
  1143. debug_scsi("tmf exec failure\n");
  1144. return -EPERM;
  1145. }
  1146. conn->tmfcmd_pdus_cnt++;
  1147. conn->tmf_timer.expires = timeout * HZ + jiffies;
  1148. conn->tmf_timer.function = iscsi_tmf_timedout;
  1149. conn->tmf_timer.data = (unsigned long)conn;
  1150. add_timer(&conn->tmf_timer);
  1151. debug_scsi("tmf set timeout\n");
  1152. spin_unlock_bh(&session->lock);
  1153. mutex_unlock(&session->eh_mutex);
  1154. scsi_queue_work(session->host, &conn->xmitwork);
  1155. /*
  1156. * block eh thread until:
  1157. *
  1158. * 1) tmf response
  1159. * 2) tmf timeout
  1160. * 3) session is terminated or restarted or userspace has
  1161. * given up on recovery
  1162. */
  1163. wait_event_interruptible(conn->ehwait, age != session->age ||
  1164. session->state != ISCSI_STATE_LOGGED_IN ||
  1165. conn->tmf_state != TMF_QUEUED);
  1166. if (signal_pending(current))
  1167. flush_signals(current);
  1168. del_timer_sync(&conn->tmf_timer);
  1169. mutex_lock(&session->eh_mutex);
  1170. spin_lock_bh(&session->lock);
  1171. /* if the session drops it will clean up the mtask */
  1172. if (age != session->age ||
  1173. session->state != ISCSI_STATE_LOGGED_IN)
  1174. return -ENOTCONN;
  1175. return 0;
  1176. }
  1177. /*
  1178. * Fail commands. session lock held and recv side suspended and xmit
  1179. * thread flushed
  1180. */
  1181. static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
  1182. int error)
  1183. {
  1184. struct iscsi_cmd_task *ctask, *tmp;
  1185. if (conn->ctask && (conn->ctask->sc->device->lun == lun || lun == -1))
  1186. conn->ctask = NULL;
  1187. /* flush pending */
  1188. list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
  1189. if (lun == ctask->sc->device->lun || lun == -1) {
  1190. debug_scsi("failing pending sc %p itt 0x%x\n",
  1191. ctask->sc, ctask->itt);
  1192. fail_command(conn, ctask, error << 16);
  1193. }
  1194. }
  1195. list_for_each_entry_safe(ctask, tmp, &conn->requeue, running) {
  1196. if (lun == ctask->sc->device->lun || lun == -1) {
  1197. debug_scsi("failing requeued sc %p itt 0x%x\n",
  1198. ctask->sc, ctask->itt);
  1199. fail_command(conn, ctask, error << 16);
  1200. }
  1201. }
  1202. /* fail all other running */
  1203. list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
  1204. if (lun == ctask->sc->device->lun || lun == -1) {
  1205. debug_scsi("failing in progress sc %p itt 0x%x\n",
  1206. ctask->sc, ctask->itt);
  1207. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1208. }
  1209. }
  1210. }
  1211. static void iscsi_suspend_tx(struct iscsi_conn *conn)
  1212. {
  1213. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1214. scsi_flush_work(conn->session->host);
  1215. }
  1216. static void iscsi_start_tx(struct iscsi_conn *conn)
  1217. {
  1218. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1219. scsi_queue_work(conn->session->host, &conn->xmitwork);
  1220. }
  1221. static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
  1222. {
  1223. struct iscsi_cls_session *cls_session;
  1224. struct iscsi_session *session;
  1225. struct iscsi_conn *conn;
  1226. enum scsi_eh_timer_return rc = EH_NOT_HANDLED;
  1227. cls_session = starget_to_session(scsi_target(scmd->device));
  1228. session = class_to_transport_session(cls_session);
  1229. debug_scsi("scsi cmd %p timedout\n", scmd);
  1230. spin_lock(&session->lock);
  1231. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1232. /*
  1233. * We are probably in the middle of iscsi recovery so let
  1234. * that complete and handle the error.
  1235. */
  1236. rc = EH_RESET_TIMER;
  1237. goto done;
  1238. }
  1239. conn = session->leadconn;
  1240. if (!conn) {
  1241. /* In the middle of shuting down */
  1242. rc = EH_RESET_TIMER;
  1243. goto done;
  1244. }
  1245. if (!conn->recv_timeout && !conn->ping_timeout)
  1246. goto done;
  1247. /*
  1248. * if the ping timedout then we are in the middle of cleaning up
  1249. * and can let the iscsi eh handle it
  1250. */
  1251. if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
  1252. (conn->ping_timeout * HZ), jiffies))
  1253. rc = EH_RESET_TIMER;
  1254. /*
  1255. * if we are about to check the transport then give the command
  1256. * more time
  1257. */
  1258. if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
  1259. jiffies))
  1260. rc = EH_RESET_TIMER;
  1261. /* if in the middle of checking the transport then give us more time */
  1262. if (conn->ping_mtask)
  1263. rc = EH_RESET_TIMER;
  1264. done:
  1265. spin_unlock(&session->lock);
  1266. debug_scsi("return %s\n", rc == EH_RESET_TIMER ? "timer reset" : "nh");
  1267. return rc;
  1268. }
  1269. static void iscsi_check_transport_timeouts(unsigned long data)
  1270. {
  1271. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1272. struct iscsi_session *session = conn->session;
  1273. unsigned long timeout, next_timeout = 0, last_recv;
  1274. spin_lock(&session->lock);
  1275. if (session->state != ISCSI_STATE_LOGGED_IN)
  1276. goto done;
  1277. timeout = conn->recv_timeout;
  1278. if (!timeout)
  1279. goto done;
  1280. timeout *= HZ;
  1281. last_recv = conn->last_recv;
  1282. if (time_before_eq(last_recv + timeout + (conn->ping_timeout * HZ),
  1283. jiffies)) {
  1284. iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
  1285. "expired, last rx %lu, last ping %lu, "
  1286. "now %lu\n", conn->ping_timeout, last_recv,
  1287. conn->last_ping, jiffies);
  1288. spin_unlock(&session->lock);
  1289. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1290. return;
  1291. }
  1292. if (time_before_eq(last_recv + timeout, jiffies)) {
  1293. if (time_before_eq(conn->last_ping, last_recv)) {
  1294. /* send a ping to try to provoke some traffic */
  1295. debug_scsi("Sending nopout as ping on conn %p\n", conn);
  1296. iscsi_send_nopout(conn, NULL);
  1297. }
  1298. next_timeout = last_recv + timeout + (conn->ping_timeout * HZ);
  1299. } else
  1300. next_timeout = last_recv + timeout;
  1301. debug_scsi("Setting next tmo %lu\n", next_timeout);
  1302. mod_timer(&conn->transport_timer, next_timeout);
  1303. done:
  1304. spin_unlock(&session->lock);
  1305. }
  1306. static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task *ctask,
  1307. struct iscsi_tm *hdr)
  1308. {
  1309. memset(hdr, 0, sizeof(*hdr));
  1310. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1311. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
  1312. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1313. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  1314. hdr->rtt = ctask->hdr->itt;
  1315. hdr->refcmdsn = ctask->hdr->cmdsn;
  1316. }
  1317. int iscsi_eh_abort(struct scsi_cmnd *sc)
  1318. {
  1319. struct Scsi_Host *host = sc->device->host;
  1320. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  1321. struct iscsi_conn *conn;
  1322. struct iscsi_cmd_task *ctask;
  1323. struct iscsi_tm *hdr;
  1324. int rc, age;
  1325. mutex_lock(&session->eh_mutex);
  1326. spin_lock_bh(&session->lock);
  1327. /*
  1328. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1329. * got the command.
  1330. */
  1331. if (!sc->SCp.ptr) {
  1332. debug_scsi("sc never reached iscsi layer or it completed.\n");
  1333. spin_unlock_bh(&session->lock);
  1334. mutex_unlock(&session->eh_mutex);
  1335. return SUCCESS;
  1336. }
  1337. /*
  1338. * If we are not logged in or we have started a new session
  1339. * then let the host reset code handle this
  1340. */
  1341. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
  1342. sc->SCp.phase != session->age) {
  1343. spin_unlock_bh(&session->lock);
  1344. mutex_unlock(&session->eh_mutex);
  1345. return FAILED;
  1346. }
  1347. conn = session->leadconn;
  1348. conn->eh_abort_cnt++;
  1349. age = session->age;
  1350. ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
  1351. debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
  1352. /* ctask completed before time out */
  1353. if (!ctask->sc) {
  1354. debug_scsi("sc completed while abort in progress\n");
  1355. goto success;
  1356. }
  1357. if (ctask->state == ISCSI_TASK_PENDING) {
  1358. fail_command(conn, ctask, DID_ABORT << 16);
  1359. goto success;
  1360. }
  1361. /* only have one tmf outstanding at a time */
  1362. if (conn->tmf_state != TMF_INITIAL)
  1363. goto failed;
  1364. conn->tmf_state = TMF_QUEUED;
  1365. hdr = &conn->tmhdr;
  1366. iscsi_prep_abort_task_pdu(ctask, hdr);
  1367. if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
  1368. rc = FAILED;
  1369. goto failed;
  1370. }
  1371. switch (conn->tmf_state) {
  1372. case TMF_SUCCESS:
  1373. spin_unlock_bh(&session->lock);
  1374. iscsi_suspend_tx(conn);
  1375. /*
  1376. * clean up task if aborted. grab the recv lock as a writer
  1377. */
  1378. write_lock_bh(conn->recv_lock);
  1379. spin_lock(&session->lock);
  1380. fail_command(conn, ctask, DID_ABORT << 16);
  1381. conn->tmf_state = TMF_INITIAL;
  1382. spin_unlock(&session->lock);
  1383. write_unlock_bh(conn->recv_lock);
  1384. iscsi_start_tx(conn);
  1385. goto success_unlocked;
  1386. case TMF_TIMEDOUT:
  1387. spin_unlock_bh(&session->lock);
  1388. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1389. goto failed_unlocked;
  1390. case TMF_NOT_FOUND:
  1391. if (!sc->SCp.ptr) {
  1392. conn->tmf_state = TMF_INITIAL;
  1393. /* ctask completed before tmf abort response */
  1394. debug_scsi("sc completed while abort in progress\n");
  1395. goto success;
  1396. }
  1397. /* fall through */
  1398. default:
  1399. conn->tmf_state = TMF_INITIAL;
  1400. goto failed;
  1401. }
  1402. success:
  1403. spin_unlock_bh(&session->lock);
  1404. success_unlocked:
  1405. debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1406. mutex_unlock(&session->eh_mutex);
  1407. return SUCCESS;
  1408. failed:
  1409. spin_unlock_bh(&session->lock);
  1410. failed_unlocked:
  1411. debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
  1412. ctask ? ctask->itt : 0);
  1413. mutex_unlock(&session->eh_mutex);
  1414. return FAILED;
  1415. }
  1416. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1417. static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
  1418. {
  1419. memset(hdr, 0, sizeof(*hdr));
  1420. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1421. hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
  1422. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1423. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  1424. hdr->rtt = RESERVED_ITT;
  1425. }
  1426. int iscsi_eh_device_reset(struct scsi_cmnd *sc)
  1427. {
  1428. struct Scsi_Host *host = sc->device->host;
  1429. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  1430. struct iscsi_conn *conn;
  1431. struct iscsi_tm *hdr;
  1432. int rc = FAILED;
  1433. debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
  1434. mutex_lock(&session->eh_mutex);
  1435. spin_lock_bh(&session->lock);
  1436. /*
  1437. * Just check if we are not logged in. We cannot check for
  1438. * the phase because the reset could come from a ioctl.
  1439. */
  1440. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  1441. goto unlock;
  1442. conn = session->leadconn;
  1443. /* only have one tmf outstanding at a time */
  1444. if (conn->tmf_state != TMF_INITIAL)
  1445. goto unlock;
  1446. conn->tmf_state = TMF_QUEUED;
  1447. hdr = &conn->tmhdr;
  1448. iscsi_prep_lun_reset_pdu(sc, hdr);
  1449. if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
  1450. session->lu_reset_timeout)) {
  1451. rc = FAILED;
  1452. goto unlock;
  1453. }
  1454. switch (conn->tmf_state) {
  1455. case TMF_SUCCESS:
  1456. break;
  1457. case TMF_TIMEDOUT:
  1458. spin_unlock_bh(&session->lock);
  1459. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1460. goto done;
  1461. default:
  1462. conn->tmf_state = TMF_INITIAL;
  1463. goto unlock;
  1464. }
  1465. rc = SUCCESS;
  1466. spin_unlock_bh(&session->lock);
  1467. iscsi_suspend_tx(conn);
  1468. /* need to grab the recv lock then session lock */
  1469. write_lock_bh(conn->recv_lock);
  1470. spin_lock(&session->lock);
  1471. fail_all_commands(conn, sc->device->lun, DID_ERROR);
  1472. conn->tmf_state = TMF_INITIAL;
  1473. spin_unlock(&session->lock);
  1474. write_unlock_bh(conn->recv_lock);
  1475. iscsi_start_tx(conn);
  1476. goto done;
  1477. unlock:
  1478. spin_unlock_bh(&session->lock);
  1479. done:
  1480. debug_scsi("iscsi_eh_device_reset %s\n",
  1481. rc == SUCCESS ? "SUCCESS" : "FAILED");
  1482. mutex_unlock(&session->eh_mutex);
  1483. return rc;
  1484. }
  1485. EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
  1486. /*
  1487. * Pre-allocate a pool of @max items of @item_size. By default, the pool
  1488. * should be accessed via kfifo_{get,put} on q->queue.
  1489. * Optionally, the caller can obtain the array of object pointers
  1490. * by passing in a non-NULL @items pointer
  1491. */
  1492. int
  1493. iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
  1494. {
  1495. int i, num_arrays = 1;
  1496. memset(q, 0, sizeof(*q));
  1497. q->max = max;
  1498. /* If the user passed an items pointer, he wants a copy of
  1499. * the array. */
  1500. if (items)
  1501. num_arrays++;
  1502. q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
  1503. if (q->pool == NULL)
  1504. goto enomem;
  1505. q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
  1506. GFP_KERNEL, NULL);
  1507. if (q->queue == ERR_PTR(-ENOMEM))
  1508. goto enomem;
  1509. for (i = 0; i < max; i++) {
  1510. q->pool[i] = kzalloc(item_size, GFP_KERNEL);
  1511. if (q->pool[i] == NULL) {
  1512. q->max = i;
  1513. goto enomem;
  1514. }
  1515. __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
  1516. }
  1517. if (items) {
  1518. *items = q->pool + max;
  1519. memcpy(*items, q->pool, max * sizeof(void *));
  1520. }
  1521. return 0;
  1522. enomem:
  1523. iscsi_pool_free(q);
  1524. return -ENOMEM;
  1525. }
  1526. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  1527. void iscsi_pool_free(struct iscsi_pool *q)
  1528. {
  1529. int i;
  1530. for (i = 0; i < q->max; i++)
  1531. kfree(q->pool[i]);
  1532. if (q->pool)
  1533. kfree(q->pool);
  1534. }
  1535. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  1536. /*
  1537. * iSCSI Session's hostdata organization:
  1538. *
  1539. * *------------------* <== hostdata_session(host->hostdata)
  1540. * | ptr to class sess|
  1541. * |------------------| <== iscsi_hostdata(host->hostdata)
  1542. * | iscsi_session |
  1543. * *------------------*
  1544. */
  1545. #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
  1546. _sz % sizeof(unsigned long))
  1547. #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
  1548. /**
  1549. * iscsi_session_setup - create iscsi cls session and host and session
  1550. * @scsit: scsi transport template
  1551. * @iscsit: iscsi transport template
  1552. * @cmds_max: scsi host can queue
  1553. * @qdepth: scsi host cmds per lun
  1554. * @cmd_task_size: LLD ctask private data size
  1555. * @mgmt_task_size: LLD mtask private data size
  1556. * @initial_cmdsn: initial CmdSN
  1557. * @hostno: host no allocated
  1558. *
  1559. * This can be used by software iscsi_transports that allocate
  1560. * a session per scsi host.
  1561. **/
  1562. struct iscsi_cls_session *
  1563. iscsi_session_setup(struct iscsi_transport *iscsit,
  1564. struct scsi_transport_template *scsit,
  1565. uint16_t cmds_max, uint16_t qdepth,
  1566. int cmd_task_size, int mgmt_task_size,
  1567. uint32_t initial_cmdsn, uint32_t *hostno)
  1568. {
  1569. struct Scsi_Host *shost;
  1570. struct iscsi_session *session;
  1571. struct iscsi_cls_session *cls_session;
  1572. int cmd_i;
  1573. if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
  1574. if (qdepth != 0)
  1575. printk(KERN_ERR "iscsi: invalid queue depth of %d. "
  1576. "Queue depth must be between 1 and %d.\n",
  1577. qdepth, ISCSI_MAX_CMD_PER_LUN);
  1578. qdepth = ISCSI_DEF_CMD_PER_LUN;
  1579. }
  1580. if (!is_power_of_2(cmds_max) || cmds_max >= ISCSI_MGMT_ITT_OFFSET ||
  1581. cmds_max < 2) {
  1582. if (cmds_max != 0)
  1583. printk(KERN_ERR "iscsi: invalid can_queue of %d. "
  1584. "can_queue must be a power of 2 and between "
  1585. "2 and %d - setting to %d.\n", cmds_max,
  1586. ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
  1587. cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
  1588. }
  1589. shost = scsi_host_alloc(iscsit->host_template,
  1590. hostdata_privsize(sizeof(*session)));
  1591. if (!shost)
  1592. return NULL;
  1593. /* the iscsi layer takes one task for reserve */
  1594. shost->can_queue = cmds_max - 1;
  1595. shost->cmd_per_lun = qdepth;
  1596. shost->max_id = 1;
  1597. shost->max_channel = 0;
  1598. shost->max_lun = iscsit->max_lun;
  1599. shost->max_cmd_len = iscsit->max_cmd_len;
  1600. shost->transportt = scsit;
  1601. shost->transportt->create_work_queue = 1;
  1602. shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
  1603. *hostno = shost->host_no;
  1604. session = iscsi_hostdata(shost->hostdata);
  1605. memset(session, 0, sizeof(struct iscsi_session));
  1606. session->host = shost;
  1607. session->state = ISCSI_STATE_FREE;
  1608. session->fast_abort = 1;
  1609. session->lu_reset_timeout = 15;
  1610. session->abort_timeout = 10;
  1611. session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
  1612. session->cmds_max = cmds_max;
  1613. session->queued_cmdsn = session->cmdsn = initial_cmdsn;
  1614. session->exp_cmdsn = initial_cmdsn + 1;
  1615. session->max_cmdsn = initial_cmdsn + 1;
  1616. session->max_r2t = 1;
  1617. session->tt = iscsit;
  1618. mutex_init(&session->eh_mutex);
  1619. /* initialize SCSI PDU commands pool */
  1620. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  1621. (void***)&session->cmds,
  1622. cmd_task_size + sizeof(struct iscsi_cmd_task)))
  1623. goto cmdpool_alloc_fail;
  1624. /* pre-format cmds pool with ITT */
  1625. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1626. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1627. if (cmd_task_size)
  1628. ctask->dd_data = &ctask[1];
  1629. ctask->itt = cmd_i;
  1630. INIT_LIST_HEAD(&ctask->running);
  1631. }
  1632. spin_lock_init(&session->lock);
  1633. /* initialize immediate command pool */
  1634. if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
  1635. (void***)&session->mgmt_cmds,
  1636. mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
  1637. goto mgmtpool_alloc_fail;
  1638. /* pre-format immediate cmds pool with ITT */
  1639. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1640. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1641. if (mgmt_task_size)
  1642. mtask->dd_data = &mtask[1];
  1643. mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
  1644. INIT_LIST_HEAD(&mtask->running);
  1645. }
  1646. if (scsi_add_host(shost, NULL))
  1647. goto add_host_fail;
  1648. if (!try_module_get(iscsit->owner))
  1649. goto cls_session_fail;
  1650. cls_session = iscsi_create_session(shost, iscsit, 0);
  1651. if (!cls_session)
  1652. goto module_put;
  1653. *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
  1654. return cls_session;
  1655. module_put:
  1656. module_put(iscsit->owner);
  1657. cls_session_fail:
  1658. scsi_remove_host(shost);
  1659. add_host_fail:
  1660. iscsi_pool_free(&session->mgmtpool);
  1661. mgmtpool_alloc_fail:
  1662. iscsi_pool_free(&session->cmdpool);
  1663. cmdpool_alloc_fail:
  1664. scsi_host_put(shost);
  1665. return NULL;
  1666. }
  1667. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  1668. /**
  1669. * iscsi_session_teardown - destroy session, host, and cls_session
  1670. * shost: scsi host
  1671. *
  1672. * This can be used by software iscsi_transports that allocate
  1673. * a session per scsi host.
  1674. **/
  1675. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  1676. {
  1677. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1678. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1679. struct module *owner = cls_session->transport->owner;
  1680. iscsi_remove_session(cls_session);
  1681. scsi_remove_host(shost);
  1682. iscsi_pool_free(&session->mgmtpool);
  1683. iscsi_pool_free(&session->cmdpool);
  1684. kfree(session->password);
  1685. kfree(session->password_in);
  1686. kfree(session->username);
  1687. kfree(session->username_in);
  1688. kfree(session->targetname);
  1689. kfree(session->netdev);
  1690. kfree(session->hwaddress);
  1691. kfree(session->initiatorname);
  1692. iscsi_free_session(cls_session);
  1693. scsi_host_put(shost);
  1694. module_put(owner);
  1695. }
  1696. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  1697. /**
  1698. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  1699. * @cls_session: iscsi_cls_session
  1700. * @conn_idx: cid
  1701. **/
  1702. struct iscsi_cls_conn *
  1703. iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1704. {
  1705. struct iscsi_session *session = class_to_transport_session(cls_session);
  1706. struct iscsi_conn *conn;
  1707. struct iscsi_cls_conn *cls_conn;
  1708. char *data;
  1709. cls_conn = iscsi_create_conn(cls_session, conn_idx);
  1710. if (!cls_conn)
  1711. return NULL;
  1712. conn = cls_conn->dd_data;
  1713. memset(conn, 0, sizeof(*conn));
  1714. conn->session = session;
  1715. conn->cls_conn = cls_conn;
  1716. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  1717. conn->id = conn_idx;
  1718. conn->exp_statsn = 0;
  1719. conn->tmf_state = TMF_INITIAL;
  1720. init_timer(&conn->transport_timer);
  1721. conn->transport_timer.data = (unsigned long)conn;
  1722. conn->transport_timer.function = iscsi_check_transport_timeouts;
  1723. INIT_LIST_HEAD(&conn->run_list);
  1724. INIT_LIST_HEAD(&conn->mgmt_run_list);
  1725. INIT_LIST_HEAD(&conn->mgmtqueue);
  1726. INIT_LIST_HEAD(&conn->xmitqueue);
  1727. INIT_LIST_HEAD(&conn->requeue);
  1728. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  1729. /* allocate login_mtask used for the login/text sequences */
  1730. spin_lock_bh(&session->lock);
  1731. if (!__kfifo_get(session->mgmtpool.queue,
  1732. (void*)&conn->login_mtask,
  1733. sizeof(void*))) {
  1734. spin_unlock_bh(&session->lock);
  1735. goto login_mtask_alloc_fail;
  1736. }
  1737. spin_unlock_bh(&session->lock);
  1738. data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
  1739. if (!data)
  1740. goto login_mtask_data_alloc_fail;
  1741. conn->login_mtask->data = conn->data = data;
  1742. init_timer(&conn->tmf_timer);
  1743. init_waitqueue_head(&conn->ehwait);
  1744. return cls_conn;
  1745. login_mtask_data_alloc_fail:
  1746. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1747. sizeof(void*));
  1748. login_mtask_alloc_fail:
  1749. iscsi_destroy_conn(cls_conn);
  1750. return NULL;
  1751. }
  1752. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  1753. /**
  1754. * iscsi_conn_teardown - teardown iscsi connection
  1755. * cls_conn: iscsi class connection
  1756. *
  1757. * TODO: we may need to make this into a two step process
  1758. * like scsi-mls remove + put host
  1759. */
  1760. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  1761. {
  1762. struct iscsi_conn *conn = cls_conn->dd_data;
  1763. struct iscsi_session *session = conn->session;
  1764. unsigned long flags;
  1765. del_timer_sync(&conn->transport_timer);
  1766. spin_lock_bh(&session->lock);
  1767. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  1768. if (session->leadconn == conn) {
  1769. /*
  1770. * leading connection? then give up on recovery.
  1771. */
  1772. session->state = ISCSI_STATE_TERMINATE;
  1773. wake_up(&conn->ehwait);
  1774. }
  1775. spin_unlock_bh(&session->lock);
  1776. /*
  1777. * Block until all in-progress commands for this connection
  1778. * time out or fail.
  1779. */
  1780. for (;;) {
  1781. spin_lock_irqsave(session->host->host_lock, flags);
  1782. if (!session->host->host_busy) { /* OK for ERL == 0 */
  1783. spin_unlock_irqrestore(session->host->host_lock, flags);
  1784. break;
  1785. }
  1786. spin_unlock_irqrestore(session->host->host_lock, flags);
  1787. msleep_interruptible(500);
  1788. iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
  1789. "host_busy %d host_failed %d\n",
  1790. session->host->host_busy,
  1791. session->host->host_failed);
  1792. /*
  1793. * force eh_abort() to unblock
  1794. */
  1795. wake_up(&conn->ehwait);
  1796. }
  1797. /* flush queued up work because we free the connection below */
  1798. iscsi_suspend_tx(conn);
  1799. spin_lock_bh(&session->lock);
  1800. kfree(conn->data);
  1801. kfree(conn->persistent_address);
  1802. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1803. sizeof(void*));
  1804. if (session->leadconn == conn)
  1805. session->leadconn = NULL;
  1806. spin_unlock_bh(&session->lock);
  1807. iscsi_destroy_conn(cls_conn);
  1808. }
  1809. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  1810. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  1811. {
  1812. struct iscsi_conn *conn = cls_conn->dd_data;
  1813. struct iscsi_session *session = conn->session;
  1814. if (!session) {
  1815. iscsi_conn_printk(KERN_ERR, conn,
  1816. "can't start unbound connection\n");
  1817. return -EPERM;
  1818. }
  1819. if ((session->imm_data_en || !session->initial_r2t_en) &&
  1820. session->first_burst > session->max_burst) {
  1821. iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
  1822. "first_burst %d max_burst %d\n",
  1823. session->first_burst, session->max_burst);
  1824. return -EINVAL;
  1825. }
  1826. if (conn->ping_timeout && !conn->recv_timeout) {
  1827. iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
  1828. "zero. Using 5 seconds\n.");
  1829. conn->recv_timeout = 5;
  1830. }
  1831. if (conn->recv_timeout && !conn->ping_timeout) {
  1832. iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
  1833. "zero. Using 5 seconds.\n");
  1834. conn->ping_timeout = 5;
  1835. }
  1836. spin_lock_bh(&session->lock);
  1837. conn->c_stage = ISCSI_CONN_STARTED;
  1838. session->state = ISCSI_STATE_LOGGED_IN;
  1839. session->queued_cmdsn = session->cmdsn;
  1840. conn->last_recv = jiffies;
  1841. conn->last_ping = jiffies;
  1842. if (conn->recv_timeout && conn->ping_timeout)
  1843. mod_timer(&conn->transport_timer,
  1844. jiffies + (conn->recv_timeout * HZ));
  1845. switch(conn->stop_stage) {
  1846. case STOP_CONN_RECOVER:
  1847. /*
  1848. * unblock eh_abort() if it is blocked. re-try all
  1849. * commands after successful recovery
  1850. */
  1851. conn->stop_stage = 0;
  1852. conn->tmf_state = TMF_INITIAL;
  1853. session->age++;
  1854. if (session->age == 16)
  1855. session->age = 0;
  1856. break;
  1857. case STOP_CONN_TERM:
  1858. conn->stop_stage = 0;
  1859. break;
  1860. default:
  1861. break;
  1862. }
  1863. spin_unlock_bh(&session->lock);
  1864. iscsi_unblock_session(session_to_cls(session));
  1865. wake_up(&conn->ehwait);
  1866. return 0;
  1867. }
  1868. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  1869. static void
  1870. flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
  1871. {
  1872. struct iscsi_mgmt_task *mtask, *tmp;
  1873. /* handle pending */
  1874. list_for_each_entry_safe(mtask, tmp, &conn->mgmtqueue, running) {
  1875. debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
  1876. iscsi_free_mgmt_task(conn, mtask);
  1877. }
  1878. /* handle running */
  1879. list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
  1880. debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
  1881. iscsi_free_mgmt_task(conn, mtask);
  1882. }
  1883. conn->mtask = NULL;
  1884. }
  1885. static void iscsi_start_session_recovery(struct iscsi_session *session,
  1886. struct iscsi_conn *conn, int flag)
  1887. {
  1888. int old_stop_stage;
  1889. del_timer_sync(&conn->transport_timer);
  1890. mutex_lock(&session->eh_mutex);
  1891. spin_lock_bh(&session->lock);
  1892. if (conn->stop_stage == STOP_CONN_TERM) {
  1893. spin_unlock_bh(&session->lock);
  1894. mutex_unlock(&session->eh_mutex);
  1895. return;
  1896. }
  1897. /*
  1898. * The LLD either freed/unset the lock on us, or userspace called
  1899. * stop but did not create a proper connection (connection was never
  1900. * bound or it was unbound then stop was called).
  1901. */
  1902. if (!conn->recv_lock) {
  1903. spin_unlock_bh(&session->lock);
  1904. mutex_unlock(&session->eh_mutex);
  1905. return;
  1906. }
  1907. /*
  1908. * When this is called for the in_login state, we only want to clean
  1909. * up the login task and connection. We do not need to block and set
  1910. * the recovery state again
  1911. */
  1912. if (flag == STOP_CONN_TERM)
  1913. session->state = ISCSI_STATE_TERMINATE;
  1914. else if (conn->stop_stage != STOP_CONN_RECOVER)
  1915. session->state = ISCSI_STATE_IN_RECOVERY;
  1916. old_stop_stage = conn->stop_stage;
  1917. conn->stop_stage = flag;
  1918. conn->c_stage = ISCSI_CONN_STOPPED;
  1919. spin_unlock_bh(&session->lock);
  1920. iscsi_suspend_tx(conn);
  1921. write_lock_bh(conn->recv_lock);
  1922. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1923. write_unlock_bh(conn->recv_lock);
  1924. /*
  1925. * for connection level recovery we should not calculate
  1926. * header digest. conn->hdr_size used for optimization
  1927. * in hdr_extract() and will be re-negotiated at
  1928. * set_param() time.
  1929. */
  1930. if (flag == STOP_CONN_RECOVER) {
  1931. conn->hdrdgst_en = 0;
  1932. conn->datadgst_en = 0;
  1933. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  1934. old_stop_stage != STOP_CONN_RECOVER) {
  1935. debug_scsi("blocking session\n");
  1936. iscsi_block_session(session_to_cls(session));
  1937. }
  1938. }
  1939. /*
  1940. * flush queues.
  1941. */
  1942. spin_lock_bh(&session->lock);
  1943. fail_all_commands(conn, -1,
  1944. STOP_CONN_RECOVER ? DID_BUS_BUSY : DID_ERROR);
  1945. flush_control_queues(session, conn);
  1946. spin_unlock_bh(&session->lock);
  1947. mutex_unlock(&session->eh_mutex);
  1948. }
  1949. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1950. {
  1951. struct iscsi_conn *conn = cls_conn->dd_data;
  1952. struct iscsi_session *session = conn->session;
  1953. switch (flag) {
  1954. case STOP_CONN_RECOVER:
  1955. case STOP_CONN_TERM:
  1956. iscsi_start_session_recovery(session, conn, flag);
  1957. break;
  1958. default:
  1959. iscsi_conn_printk(KERN_ERR, conn,
  1960. "invalid stop flag %d\n", flag);
  1961. }
  1962. }
  1963. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  1964. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  1965. struct iscsi_cls_conn *cls_conn, int is_leading)
  1966. {
  1967. struct iscsi_session *session = class_to_transport_session(cls_session);
  1968. struct iscsi_conn *conn = cls_conn->dd_data;
  1969. spin_lock_bh(&session->lock);
  1970. if (is_leading)
  1971. session->leadconn = conn;
  1972. spin_unlock_bh(&session->lock);
  1973. /*
  1974. * Unblock xmitworker(), Login Phase will pass through.
  1975. */
  1976. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1977. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1978. return 0;
  1979. }
  1980. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  1981. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  1982. enum iscsi_param param, char *buf, int buflen)
  1983. {
  1984. struct iscsi_conn *conn = cls_conn->dd_data;
  1985. struct iscsi_session *session = conn->session;
  1986. uint32_t value;
  1987. switch(param) {
  1988. case ISCSI_PARAM_FAST_ABORT:
  1989. sscanf(buf, "%d", &session->fast_abort);
  1990. break;
  1991. case ISCSI_PARAM_ABORT_TMO:
  1992. sscanf(buf, "%d", &session->abort_timeout);
  1993. break;
  1994. case ISCSI_PARAM_LU_RESET_TMO:
  1995. sscanf(buf, "%d", &session->lu_reset_timeout);
  1996. break;
  1997. case ISCSI_PARAM_PING_TMO:
  1998. sscanf(buf, "%d", &conn->ping_timeout);
  1999. break;
  2000. case ISCSI_PARAM_RECV_TMO:
  2001. sscanf(buf, "%d", &conn->recv_timeout);
  2002. break;
  2003. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  2004. sscanf(buf, "%d", &conn->max_recv_dlength);
  2005. break;
  2006. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  2007. sscanf(buf, "%d", &conn->max_xmit_dlength);
  2008. break;
  2009. case ISCSI_PARAM_HDRDGST_EN:
  2010. sscanf(buf, "%d", &conn->hdrdgst_en);
  2011. break;
  2012. case ISCSI_PARAM_DATADGST_EN:
  2013. sscanf(buf, "%d", &conn->datadgst_en);
  2014. break;
  2015. case ISCSI_PARAM_INITIAL_R2T_EN:
  2016. sscanf(buf, "%d", &session->initial_r2t_en);
  2017. break;
  2018. case ISCSI_PARAM_MAX_R2T:
  2019. sscanf(buf, "%d", &session->max_r2t);
  2020. break;
  2021. case ISCSI_PARAM_IMM_DATA_EN:
  2022. sscanf(buf, "%d", &session->imm_data_en);
  2023. break;
  2024. case ISCSI_PARAM_FIRST_BURST:
  2025. sscanf(buf, "%d", &session->first_burst);
  2026. break;
  2027. case ISCSI_PARAM_MAX_BURST:
  2028. sscanf(buf, "%d", &session->max_burst);
  2029. break;
  2030. case ISCSI_PARAM_PDU_INORDER_EN:
  2031. sscanf(buf, "%d", &session->pdu_inorder_en);
  2032. break;
  2033. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2034. sscanf(buf, "%d", &session->dataseq_inorder_en);
  2035. break;
  2036. case ISCSI_PARAM_ERL:
  2037. sscanf(buf, "%d", &session->erl);
  2038. break;
  2039. case ISCSI_PARAM_IFMARKER_EN:
  2040. sscanf(buf, "%d", &value);
  2041. BUG_ON(value);
  2042. break;
  2043. case ISCSI_PARAM_OFMARKER_EN:
  2044. sscanf(buf, "%d", &value);
  2045. BUG_ON(value);
  2046. break;
  2047. case ISCSI_PARAM_EXP_STATSN:
  2048. sscanf(buf, "%u", &conn->exp_statsn);
  2049. break;
  2050. case ISCSI_PARAM_USERNAME:
  2051. kfree(session->username);
  2052. session->username = kstrdup(buf, GFP_KERNEL);
  2053. if (!session->username)
  2054. return -ENOMEM;
  2055. break;
  2056. case ISCSI_PARAM_USERNAME_IN:
  2057. kfree(session->username_in);
  2058. session->username_in = kstrdup(buf, GFP_KERNEL);
  2059. if (!session->username_in)
  2060. return -ENOMEM;
  2061. break;
  2062. case ISCSI_PARAM_PASSWORD:
  2063. kfree(session->password);
  2064. session->password = kstrdup(buf, GFP_KERNEL);
  2065. if (!session->password)
  2066. return -ENOMEM;
  2067. break;
  2068. case ISCSI_PARAM_PASSWORD_IN:
  2069. kfree(session->password_in);
  2070. session->password_in = kstrdup(buf, GFP_KERNEL);
  2071. if (!session->password_in)
  2072. return -ENOMEM;
  2073. break;
  2074. case ISCSI_PARAM_TARGET_NAME:
  2075. /* this should not change between logins */
  2076. if (session->targetname)
  2077. break;
  2078. session->targetname = kstrdup(buf, GFP_KERNEL);
  2079. if (!session->targetname)
  2080. return -ENOMEM;
  2081. break;
  2082. case ISCSI_PARAM_TPGT:
  2083. sscanf(buf, "%d", &session->tpgt);
  2084. break;
  2085. case ISCSI_PARAM_PERSISTENT_PORT:
  2086. sscanf(buf, "%d", &conn->persistent_port);
  2087. break;
  2088. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  2089. /*
  2090. * this is the address returned in discovery so it should
  2091. * not change between logins.
  2092. */
  2093. if (conn->persistent_address)
  2094. break;
  2095. conn->persistent_address = kstrdup(buf, GFP_KERNEL);
  2096. if (!conn->persistent_address)
  2097. return -ENOMEM;
  2098. break;
  2099. default:
  2100. return -ENOSYS;
  2101. }
  2102. return 0;
  2103. }
  2104. EXPORT_SYMBOL_GPL(iscsi_set_param);
  2105. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  2106. enum iscsi_param param, char *buf)
  2107. {
  2108. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  2109. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  2110. int len;
  2111. switch(param) {
  2112. case ISCSI_PARAM_FAST_ABORT:
  2113. len = sprintf(buf, "%d\n", session->fast_abort);
  2114. break;
  2115. case ISCSI_PARAM_ABORT_TMO:
  2116. len = sprintf(buf, "%d\n", session->abort_timeout);
  2117. break;
  2118. case ISCSI_PARAM_LU_RESET_TMO:
  2119. len = sprintf(buf, "%d\n", session->lu_reset_timeout);
  2120. break;
  2121. case ISCSI_PARAM_INITIAL_R2T_EN:
  2122. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  2123. break;
  2124. case ISCSI_PARAM_MAX_R2T:
  2125. len = sprintf(buf, "%hu\n", session->max_r2t);
  2126. break;
  2127. case ISCSI_PARAM_IMM_DATA_EN:
  2128. len = sprintf(buf, "%d\n", session->imm_data_en);
  2129. break;
  2130. case ISCSI_PARAM_FIRST_BURST:
  2131. len = sprintf(buf, "%u\n", session->first_burst);
  2132. break;
  2133. case ISCSI_PARAM_MAX_BURST:
  2134. len = sprintf(buf, "%u\n", session->max_burst);
  2135. break;
  2136. case ISCSI_PARAM_PDU_INORDER_EN:
  2137. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  2138. break;
  2139. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2140. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  2141. break;
  2142. case ISCSI_PARAM_ERL:
  2143. len = sprintf(buf, "%d\n", session->erl);
  2144. break;
  2145. case ISCSI_PARAM_TARGET_NAME:
  2146. len = sprintf(buf, "%s\n", session->targetname);
  2147. break;
  2148. case ISCSI_PARAM_TPGT:
  2149. len = sprintf(buf, "%d\n", session->tpgt);
  2150. break;
  2151. case ISCSI_PARAM_USERNAME:
  2152. len = sprintf(buf, "%s\n", session->username);
  2153. break;
  2154. case ISCSI_PARAM_USERNAME_IN:
  2155. len = sprintf(buf, "%s\n", session->username_in);
  2156. break;
  2157. case ISCSI_PARAM_PASSWORD:
  2158. len = sprintf(buf, "%s\n", session->password);
  2159. break;
  2160. case ISCSI_PARAM_PASSWORD_IN:
  2161. len = sprintf(buf, "%s\n", session->password_in);
  2162. break;
  2163. default:
  2164. return -ENOSYS;
  2165. }
  2166. return len;
  2167. }
  2168. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  2169. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  2170. enum iscsi_param param, char *buf)
  2171. {
  2172. struct iscsi_conn *conn = cls_conn->dd_data;
  2173. int len;
  2174. switch(param) {
  2175. case ISCSI_PARAM_PING_TMO:
  2176. len = sprintf(buf, "%u\n", conn->ping_timeout);
  2177. break;
  2178. case ISCSI_PARAM_RECV_TMO:
  2179. len = sprintf(buf, "%u\n", conn->recv_timeout);
  2180. break;
  2181. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  2182. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  2183. break;
  2184. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  2185. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  2186. break;
  2187. case ISCSI_PARAM_HDRDGST_EN:
  2188. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  2189. break;
  2190. case ISCSI_PARAM_DATADGST_EN:
  2191. len = sprintf(buf, "%d\n", conn->datadgst_en);
  2192. break;
  2193. case ISCSI_PARAM_IFMARKER_EN:
  2194. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  2195. break;
  2196. case ISCSI_PARAM_OFMARKER_EN:
  2197. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  2198. break;
  2199. case ISCSI_PARAM_EXP_STATSN:
  2200. len = sprintf(buf, "%u\n", conn->exp_statsn);
  2201. break;
  2202. case ISCSI_PARAM_PERSISTENT_PORT:
  2203. len = sprintf(buf, "%d\n", conn->persistent_port);
  2204. break;
  2205. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  2206. len = sprintf(buf, "%s\n", conn->persistent_address);
  2207. break;
  2208. default:
  2209. return -ENOSYS;
  2210. }
  2211. return len;
  2212. }
  2213. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  2214. int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  2215. char *buf)
  2216. {
  2217. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  2218. int len;
  2219. switch (param) {
  2220. case ISCSI_HOST_PARAM_NETDEV_NAME:
  2221. if (!session->netdev)
  2222. len = sprintf(buf, "%s\n", "default");
  2223. else
  2224. len = sprintf(buf, "%s\n", session->netdev);
  2225. break;
  2226. case ISCSI_HOST_PARAM_HWADDRESS:
  2227. if (!session->hwaddress)
  2228. len = sprintf(buf, "%s\n", "default");
  2229. else
  2230. len = sprintf(buf, "%s\n", session->hwaddress);
  2231. break;
  2232. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  2233. if (!session->initiatorname)
  2234. len = sprintf(buf, "%s\n", "unknown");
  2235. else
  2236. len = sprintf(buf, "%s\n", session->initiatorname);
  2237. break;
  2238. default:
  2239. return -ENOSYS;
  2240. }
  2241. return len;
  2242. }
  2243. EXPORT_SYMBOL_GPL(iscsi_host_get_param);
  2244. int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  2245. char *buf, int buflen)
  2246. {
  2247. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  2248. switch (param) {
  2249. case ISCSI_HOST_PARAM_NETDEV_NAME:
  2250. if (!session->netdev)
  2251. session->netdev = kstrdup(buf, GFP_KERNEL);
  2252. break;
  2253. case ISCSI_HOST_PARAM_HWADDRESS:
  2254. if (!session->hwaddress)
  2255. session->hwaddress = kstrdup(buf, GFP_KERNEL);
  2256. break;
  2257. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  2258. if (!session->initiatorname)
  2259. session->initiatorname = kstrdup(buf, GFP_KERNEL);
  2260. break;
  2261. default:
  2262. return -ENOSYS;
  2263. }
  2264. return 0;
  2265. }
  2266. EXPORT_SYMBOL_GPL(iscsi_host_set_param);
  2267. MODULE_AUTHOR("Mike Christie");
  2268. MODULE_DESCRIPTION("iSCSI library functions");
  2269. MODULE_LICENSE("GPL");