libiscsi.c 69 KB

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