libiscsi.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562
  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. if (hdr->itt != RESERVED_ITT)
  558. itt = get_itt(hdr->itt);
  559. else
  560. itt = ~0U;
  561. if (itt < session->cmds_max) {
  562. ctask = session->cmds[itt];
  563. debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  564. opcode, conn->id, ctask->itt, datalen);
  565. switch(opcode) {
  566. case ISCSI_OP_SCSI_CMD_RSP:
  567. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  568. iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
  569. datalen);
  570. break;
  571. case ISCSI_OP_SCSI_DATA_IN:
  572. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  573. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  574. conn->scsirsp_pdus_cnt++;
  575. __iscsi_put_ctask(ctask);
  576. }
  577. break;
  578. case ISCSI_OP_R2T:
  579. /* LLD handles this for now */
  580. break;
  581. default:
  582. rc = ISCSI_ERR_BAD_OPCODE;
  583. break;
  584. }
  585. } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
  586. itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
  587. mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
  588. debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  589. opcode, conn->id, mtask->itt, datalen);
  590. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  591. switch(opcode) {
  592. case ISCSI_OP_LOGOUT_RSP:
  593. if (datalen) {
  594. rc = ISCSI_ERR_PROTO;
  595. break;
  596. }
  597. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  598. /* fall through */
  599. case ISCSI_OP_LOGIN_RSP:
  600. case ISCSI_OP_TEXT_RSP:
  601. /*
  602. * login related PDU's exp_statsn is handled in
  603. * userspace
  604. */
  605. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  606. rc = ISCSI_ERR_CONN_FAILED;
  607. iscsi_free_mgmt_task(conn, mtask);
  608. break;
  609. case ISCSI_OP_SCSI_TMFUNC_RSP:
  610. if (datalen) {
  611. rc = ISCSI_ERR_PROTO;
  612. break;
  613. }
  614. iscsi_tmf_rsp(conn, hdr);
  615. iscsi_free_mgmt_task(conn, mtask);
  616. break;
  617. case ISCSI_OP_NOOP_IN:
  618. if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) ||
  619. datalen) {
  620. rc = ISCSI_ERR_PROTO;
  621. break;
  622. }
  623. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  624. if (conn->ping_mtask != mtask) {
  625. /*
  626. * If this is not in response to one of our
  627. * nops then it must be from userspace.
  628. */
  629. if (iscsi_recv_pdu(conn->cls_conn, hdr, data,
  630. datalen))
  631. rc = ISCSI_ERR_CONN_FAILED;
  632. } else
  633. mod_timer(&conn->transport_timer,
  634. jiffies + conn->recv_timeout);
  635. iscsi_free_mgmt_task(conn, mtask);
  636. break;
  637. default:
  638. rc = ISCSI_ERR_BAD_OPCODE;
  639. break;
  640. }
  641. } else if (itt == ~0U) {
  642. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  643. switch(opcode) {
  644. case ISCSI_OP_NOOP_IN:
  645. if (datalen) {
  646. rc = ISCSI_ERR_PROTO;
  647. break;
  648. }
  649. if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
  650. break;
  651. iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
  652. break;
  653. case ISCSI_OP_REJECT:
  654. rc = iscsi_handle_reject(conn, hdr, data, datalen);
  655. break;
  656. case ISCSI_OP_ASYNC_EVENT:
  657. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  658. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  659. rc = ISCSI_ERR_CONN_FAILED;
  660. break;
  661. default:
  662. rc = ISCSI_ERR_BAD_OPCODE;
  663. break;
  664. }
  665. } else
  666. rc = ISCSI_ERR_BAD_ITT;
  667. return rc;
  668. }
  669. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  670. char *data, int datalen)
  671. {
  672. int rc;
  673. spin_lock(&conn->session->lock);
  674. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  675. spin_unlock(&conn->session->lock);
  676. return rc;
  677. }
  678. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  679. /* verify itt (itt encoding: age+cid+itt) */
  680. int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  681. uint32_t *ret_itt)
  682. {
  683. struct iscsi_session *session = conn->session;
  684. struct iscsi_cmd_task *ctask;
  685. uint32_t itt;
  686. if (hdr->itt != RESERVED_ITT) {
  687. if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
  688. (session->age << ISCSI_AGE_SHIFT)) {
  689. iscsi_conn_printk(KERN_ERR, conn,
  690. "received itt %x expected session "
  691. "age (%x)\n", (__force u32)hdr->itt,
  692. session->age & ISCSI_AGE_MASK);
  693. return ISCSI_ERR_BAD_ITT;
  694. }
  695. itt = get_itt(hdr->itt);
  696. } else
  697. itt = ~0U;
  698. if (itt < session->cmds_max) {
  699. ctask = session->cmds[itt];
  700. if (!ctask->sc) {
  701. iscsi_conn_printk(KERN_INFO, conn, "dropping ctask "
  702. "with itt 0x%x\n", ctask->itt);
  703. /* force drop */
  704. return ISCSI_ERR_NO_SCSI_CMD;
  705. }
  706. if (ctask->sc->SCp.phase != session->age) {
  707. iscsi_conn_printk(KERN_ERR, conn,
  708. "iscsi: ctask's session age %d, "
  709. "expected %d\n", ctask->sc->SCp.phase,
  710. session->age);
  711. return ISCSI_ERR_SESSION_FAILED;
  712. }
  713. }
  714. *ret_itt = itt;
  715. return 0;
  716. }
  717. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  718. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  719. {
  720. struct iscsi_session *session = conn->session;
  721. unsigned long flags;
  722. spin_lock_irqsave(&session->lock, flags);
  723. if (session->state == ISCSI_STATE_FAILED) {
  724. spin_unlock_irqrestore(&session->lock, flags);
  725. return;
  726. }
  727. if (conn->stop_stage == 0)
  728. session->state = ISCSI_STATE_FAILED;
  729. spin_unlock_irqrestore(&session->lock, flags);
  730. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  731. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  732. iscsi_conn_error(conn->cls_conn, err);
  733. }
  734. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  735. static void iscsi_prep_mtask(struct iscsi_conn *conn,
  736. struct iscsi_mgmt_task *mtask)
  737. {
  738. struct iscsi_session *session = conn->session;
  739. struct iscsi_hdr *hdr = mtask->hdr;
  740. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  741. if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
  742. hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  743. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  744. /*
  745. * pre-format CmdSN for outgoing PDU.
  746. */
  747. nop->cmdsn = cpu_to_be32(session->cmdsn);
  748. if (hdr->itt != RESERVED_ITT) {
  749. hdr->itt = build_itt(mtask->itt, session->age);
  750. /*
  751. * TODO: We always use immediate, so we never hit this.
  752. * If we start to send tmfs or nops as non-immediate then
  753. * we should start checking the cmdsn numbers for mgmt tasks.
  754. */
  755. if (conn->c_stage == ISCSI_CONN_STARTED &&
  756. !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
  757. session->queued_cmdsn++;
  758. session->cmdsn++;
  759. }
  760. }
  761. if (session->tt->init_mgmt_task)
  762. session->tt->init_mgmt_task(conn, mtask);
  763. debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
  764. hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
  765. mtask->data_count);
  766. }
  767. static int iscsi_xmit_mtask(struct iscsi_conn *conn)
  768. {
  769. struct iscsi_hdr *hdr = conn->mtask->hdr;
  770. int rc;
  771. if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
  772. conn->session->state = ISCSI_STATE_LOGGING_OUT;
  773. spin_unlock_bh(&conn->session->lock);
  774. rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
  775. spin_lock_bh(&conn->session->lock);
  776. if (rc)
  777. return rc;
  778. /* done with this in-progress mtask */
  779. conn->mtask = NULL;
  780. return 0;
  781. }
  782. static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
  783. {
  784. struct iscsi_session *session = conn->session;
  785. /*
  786. * Check for iSCSI window and take care of CmdSN wrap-around
  787. */
  788. if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
  789. debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
  790. "CmdSN %u/%u\n", session->exp_cmdsn,
  791. session->max_cmdsn, session->cmdsn,
  792. session->queued_cmdsn);
  793. return -ENOSPC;
  794. }
  795. return 0;
  796. }
  797. static int iscsi_xmit_ctask(struct iscsi_conn *conn)
  798. {
  799. struct iscsi_cmd_task *ctask = conn->ctask;
  800. int rc;
  801. __iscsi_get_ctask(ctask);
  802. spin_unlock_bh(&conn->session->lock);
  803. rc = conn->session->tt->xmit_cmd_task(conn, ctask);
  804. spin_lock_bh(&conn->session->lock);
  805. __iscsi_put_ctask(ctask);
  806. if (!rc)
  807. /* done with this ctask */
  808. conn->ctask = NULL;
  809. return rc;
  810. }
  811. /**
  812. * iscsi_requeue_ctask - requeue ctask to run from session workqueue
  813. * @ctask: ctask to requeue
  814. *
  815. * LLDs that need to run a ctask from the session workqueue should call
  816. * this. The session lock must be held.
  817. */
  818. void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask)
  819. {
  820. struct iscsi_conn *conn = ctask->conn;
  821. list_move_tail(&ctask->running, &conn->requeue);
  822. scsi_queue_work(conn->session->host, &conn->xmitwork);
  823. }
  824. EXPORT_SYMBOL_GPL(iscsi_requeue_ctask);
  825. /**
  826. * iscsi_data_xmit - xmit any command into the scheduled connection
  827. * @conn: iscsi connection
  828. *
  829. * Notes:
  830. * The function can return -EAGAIN in which case the caller must
  831. * re-schedule it again later or recover. '0' return code means
  832. * successful xmit.
  833. **/
  834. static int iscsi_data_xmit(struct iscsi_conn *conn)
  835. {
  836. int rc = 0;
  837. spin_lock_bh(&conn->session->lock);
  838. if (unlikely(conn->suspend_tx)) {
  839. debug_scsi("conn %d Tx suspended!\n", conn->id);
  840. spin_unlock_bh(&conn->session->lock);
  841. return -ENODATA;
  842. }
  843. if (conn->ctask) {
  844. rc = iscsi_xmit_ctask(conn);
  845. if (rc)
  846. goto again;
  847. }
  848. if (conn->mtask) {
  849. rc = iscsi_xmit_mtask(conn);
  850. if (rc)
  851. goto again;
  852. }
  853. /*
  854. * process mgmt pdus like nops before commands since we should
  855. * only have one nop-out as a ping from us and targets should not
  856. * overflow us with nop-ins
  857. */
  858. check_mgmt:
  859. while (!list_empty(&conn->mgmtqueue)) {
  860. conn->mtask = list_entry(conn->mgmtqueue.next,
  861. struct iscsi_mgmt_task, running);
  862. if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
  863. iscsi_free_mgmt_task(conn, conn->mtask);
  864. conn->mtask = NULL;
  865. continue;
  866. }
  867. iscsi_prep_mtask(conn, conn->mtask);
  868. list_move_tail(conn->mgmtqueue.next, &conn->mgmt_run_list);
  869. rc = iscsi_xmit_mtask(conn);
  870. if (rc)
  871. goto again;
  872. }
  873. /* process pending command queue */
  874. while (!list_empty(&conn->xmitqueue)) {
  875. if (conn->tmf_state == TMF_QUEUED)
  876. break;
  877. conn->ctask = list_entry(conn->xmitqueue.next,
  878. struct iscsi_cmd_task, running);
  879. if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
  880. fail_command(conn, conn->ctask, DID_IMM_RETRY << 16);
  881. continue;
  882. }
  883. if (iscsi_prep_scsi_cmd_pdu(conn->ctask)) {
  884. fail_command(conn, conn->ctask, DID_ABORT << 16);
  885. continue;
  886. }
  887. conn->ctask->state = ISCSI_TASK_RUNNING;
  888. list_move_tail(conn->xmitqueue.next, &conn->run_list);
  889. rc = iscsi_xmit_ctask(conn);
  890. if (rc)
  891. goto again;
  892. /*
  893. * we could continuously get new ctask requests so
  894. * we need to check the mgmt queue for nops that need to
  895. * be sent to aviod starvation
  896. */
  897. if (!list_empty(&conn->mgmtqueue))
  898. goto check_mgmt;
  899. }
  900. while (!list_empty(&conn->requeue)) {
  901. if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
  902. break;
  903. /*
  904. * we always do fastlogout - conn stop code will clean up.
  905. */
  906. if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
  907. break;
  908. conn->ctask = list_entry(conn->requeue.next,
  909. struct iscsi_cmd_task, running);
  910. conn->ctask->state = ISCSI_TASK_RUNNING;
  911. list_move_tail(conn->requeue.next, &conn->run_list);
  912. rc = iscsi_xmit_ctask(conn);
  913. if (rc)
  914. goto again;
  915. if (!list_empty(&conn->mgmtqueue))
  916. goto check_mgmt;
  917. }
  918. spin_unlock_bh(&conn->session->lock);
  919. return -ENODATA;
  920. again:
  921. if (unlikely(conn->suspend_tx))
  922. rc = -ENODATA;
  923. spin_unlock_bh(&conn->session->lock);
  924. return rc;
  925. }
  926. static void iscsi_xmitworker(struct work_struct *work)
  927. {
  928. struct iscsi_conn *conn =
  929. container_of(work, struct iscsi_conn, xmitwork);
  930. int rc;
  931. /*
  932. * serialize Xmit worker on a per-connection basis.
  933. */
  934. do {
  935. rc = iscsi_data_xmit(conn);
  936. } while (rc >= 0 || rc == -EAGAIN);
  937. }
  938. enum {
  939. FAILURE_BAD_HOST = 1,
  940. FAILURE_SESSION_FAILED,
  941. FAILURE_SESSION_FREED,
  942. FAILURE_WINDOW_CLOSED,
  943. FAILURE_OOM,
  944. FAILURE_SESSION_TERMINATE,
  945. FAILURE_SESSION_IN_RECOVERY,
  946. FAILURE_SESSION_RECOVERY_TIMEOUT,
  947. FAILURE_SESSION_LOGGING_OUT,
  948. FAILURE_SESSION_NOT_READY,
  949. };
  950. int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  951. {
  952. struct iscsi_cls_session *cls_session;
  953. struct Scsi_Host *host;
  954. int reason = 0;
  955. struct iscsi_session *session;
  956. struct iscsi_conn *conn;
  957. struct iscsi_cmd_task *ctask = NULL;
  958. sc->scsi_done = done;
  959. sc->result = 0;
  960. sc->SCp.ptr = NULL;
  961. host = sc->device->host;
  962. spin_unlock(host->host_lock);
  963. cls_session = starget_to_session(scsi_target(sc->device));
  964. session = cls_session->dd_data;
  965. spin_lock(&session->lock);
  966. reason = iscsi_session_chkready(cls_session);
  967. if (reason) {
  968. sc->result = reason;
  969. goto fault;
  970. }
  971. /*
  972. * ISCSI_STATE_FAILED is a temp. state. The recovery
  973. * code will decide what is best to do with command queued
  974. * during this time
  975. */
  976. if (session->state != ISCSI_STATE_LOGGED_IN &&
  977. session->state != ISCSI_STATE_FAILED) {
  978. /*
  979. * to handle the race between when we set the recovery state
  980. * and block the session we requeue here (commands could
  981. * be entering our queuecommand while a block is starting
  982. * up because the block code is not locked)
  983. */
  984. switch (session->state) {
  985. case ISCSI_STATE_IN_RECOVERY:
  986. reason = FAILURE_SESSION_IN_RECOVERY;
  987. sc->result = DID_IMM_RETRY << 16;
  988. break;
  989. case ISCSI_STATE_LOGGING_OUT:
  990. reason = FAILURE_SESSION_LOGGING_OUT;
  991. sc->result = DID_IMM_RETRY << 16;
  992. break;
  993. case ISCSI_STATE_RECOVERY_FAILED:
  994. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  995. sc->result = DID_NO_CONNECT << 16;
  996. break;
  997. case ISCSI_STATE_TERMINATE:
  998. reason = FAILURE_SESSION_TERMINATE;
  999. sc->result = DID_NO_CONNECT << 16;
  1000. break;
  1001. default:
  1002. reason = FAILURE_SESSION_FREED;
  1003. sc->result = DID_NO_CONNECT << 16;
  1004. }
  1005. goto fault;
  1006. }
  1007. conn = session->leadconn;
  1008. if (!conn) {
  1009. reason = FAILURE_SESSION_FREED;
  1010. sc->result = DID_NO_CONNECT << 16;
  1011. goto fault;
  1012. }
  1013. if (iscsi_check_cmdsn_window_closed(conn)) {
  1014. reason = FAILURE_WINDOW_CLOSED;
  1015. goto reject;
  1016. }
  1017. if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
  1018. sizeof(void*))) {
  1019. reason = FAILURE_OOM;
  1020. goto reject;
  1021. }
  1022. session->queued_cmdsn++;
  1023. sc->SCp.phase = session->age;
  1024. sc->SCp.ptr = (char *)ctask;
  1025. atomic_set(&ctask->refcount, 1);
  1026. ctask->state = ISCSI_TASK_PENDING;
  1027. ctask->conn = conn;
  1028. ctask->sc = sc;
  1029. INIT_LIST_HEAD(&ctask->running);
  1030. list_add_tail(&ctask->running, &conn->xmitqueue);
  1031. spin_unlock(&session->lock);
  1032. scsi_queue_work(host, &conn->xmitwork);
  1033. spin_lock(host->host_lock);
  1034. return 0;
  1035. reject:
  1036. spin_unlock(&session->lock);
  1037. debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
  1038. spin_lock(host->host_lock);
  1039. return SCSI_MLQUEUE_HOST_BUSY;
  1040. fault:
  1041. spin_unlock(&session->lock);
  1042. debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
  1043. if (!scsi_bidi_cmnd(sc))
  1044. scsi_set_resid(sc, scsi_bufflen(sc));
  1045. else {
  1046. scsi_out(sc)->resid = scsi_out(sc)->length;
  1047. scsi_in(sc)->resid = scsi_in(sc)->length;
  1048. }
  1049. sc->scsi_done(sc);
  1050. spin_lock(host->host_lock);
  1051. return 0;
  1052. }
  1053. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  1054. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
  1055. {
  1056. if (depth > ISCSI_MAX_CMD_PER_LUN)
  1057. depth = ISCSI_MAX_CMD_PER_LUN;
  1058. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  1059. return sdev->queue_depth;
  1060. }
  1061. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  1062. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  1063. {
  1064. struct iscsi_session *session = cls_session->dd_data;
  1065. spin_lock_bh(&session->lock);
  1066. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1067. session->state = ISCSI_STATE_RECOVERY_FAILED;
  1068. if (session->leadconn)
  1069. wake_up(&session->leadconn->ehwait);
  1070. }
  1071. spin_unlock_bh(&session->lock);
  1072. }
  1073. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  1074. int iscsi_eh_host_reset(struct scsi_cmnd *sc)
  1075. {
  1076. struct iscsi_cls_session *cls_session;
  1077. struct iscsi_session *session;
  1078. struct iscsi_conn *conn;
  1079. cls_session = starget_to_session(scsi_target(sc->device));
  1080. session = cls_session->dd_data;
  1081. conn = session->leadconn;
  1082. mutex_lock(&session->eh_mutex);
  1083. spin_lock_bh(&session->lock);
  1084. if (session->state == ISCSI_STATE_TERMINATE) {
  1085. failed:
  1086. debug_scsi("failing host reset: session terminated "
  1087. "[CID %d age %d]\n", conn->id, session->age);
  1088. spin_unlock_bh(&session->lock);
  1089. mutex_unlock(&session->eh_mutex);
  1090. return FAILED;
  1091. }
  1092. spin_unlock_bh(&session->lock);
  1093. mutex_unlock(&session->eh_mutex);
  1094. /*
  1095. * we drop the lock here but the leadconn cannot be destoyed while
  1096. * we are in the scsi eh
  1097. */
  1098. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1099. debug_scsi("iscsi_eh_host_reset wait for relogin\n");
  1100. wait_event_interruptible(conn->ehwait,
  1101. session->state == ISCSI_STATE_TERMINATE ||
  1102. session->state == ISCSI_STATE_LOGGED_IN ||
  1103. session->state == ISCSI_STATE_RECOVERY_FAILED);
  1104. if (signal_pending(current))
  1105. flush_signals(current);
  1106. mutex_lock(&session->eh_mutex);
  1107. spin_lock_bh(&session->lock);
  1108. if (session->state == ISCSI_STATE_LOGGED_IN)
  1109. iscsi_session_printk(KERN_INFO, session,
  1110. "host reset succeeded\n");
  1111. else
  1112. goto failed;
  1113. spin_unlock_bh(&session->lock);
  1114. mutex_unlock(&session->eh_mutex);
  1115. return SUCCESS;
  1116. }
  1117. EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
  1118. static void iscsi_tmf_timedout(unsigned long data)
  1119. {
  1120. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1121. struct iscsi_session *session = conn->session;
  1122. spin_lock(&session->lock);
  1123. if (conn->tmf_state == TMF_QUEUED) {
  1124. conn->tmf_state = TMF_TIMEDOUT;
  1125. debug_scsi("tmf timedout\n");
  1126. /* unblock eh_abort() */
  1127. wake_up(&conn->ehwait);
  1128. }
  1129. spin_unlock(&session->lock);
  1130. }
  1131. static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
  1132. struct iscsi_tm *hdr, int age,
  1133. int timeout)
  1134. {
  1135. struct iscsi_session *session = conn->session;
  1136. struct iscsi_mgmt_task *mtask;
  1137. mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
  1138. NULL, 0);
  1139. if (!mtask) {
  1140. spin_unlock_bh(&session->lock);
  1141. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1142. spin_lock_bh(&session->lock);
  1143. debug_scsi("tmf exec failure\n");
  1144. return -EPERM;
  1145. }
  1146. conn->tmfcmd_pdus_cnt++;
  1147. conn->tmf_timer.expires = timeout * HZ + jiffies;
  1148. conn->tmf_timer.function = iscsi_tmf_timedout;
  1149. conn->tmf_timer.data = (unsigned long)conn;
  1150. add_timer(&conn->tmf_timer);
  1151. debug_scsi("tmf set timeout\n");
  1152. spin_unlock_bh(&session->lock);
  1153. mutex_unlock(&session->eh_mutex);
  1154. scsi_queue_work(session->host, &conn->xmitwork);
  1155. /*
  1156. * block eh thread until:
  1157. *
  1158. * 1) tmf response
  1159. * 2) tmf timeout
  1160. * 3) session is terminated or restarted or userspace has
  1161. * given up on recovery
  1162. */
  1163. wait_event_interruptible(conn->ehwait, age != session->age ||
  1164. session->state != ISCSI_STATE_LOGGED_IN ||
  1165. conn->tmf_state != TMF_QUEUED);
  1166. if (signal_pending(current))
  1167. flush_signals(current);
  1168. del_timer_sync(&conn->tmf_timer);
  1169. mutex_lock(&session->eh_mutex);
  1170. spin_lock_bh(&session->lock);
  1171. /* if the session drops it will clean up the mtask */
  1172. if (age != session->age ||
  1173. session->state != ISCSI_STATE_LOGGED_IN)
  1174. return -ENOTCONN;
  1175. return 0;
  1176. }
  1177. /*
  1178. * Fail commands. session lock held and recv side suspended and xmit
  1179. * thread flushed
  1180. */
  1181. static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
  1182. int error)
  1183. {
  1184. struct iscsi_cmd_task *ctask, *tmp;
  1185. if (conn->ctask && (conn->ctask->sc->device->lun == lun || lun == -1))
  1186. conn->ctask = NULL;
  1187. /* flush pending */
  1188. list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
  1189. if (lun == ctask->sc->device->lun || lun == -1) {
  1190. debug_scsi("failing pending sc %p itt 0x%x\n",
  1191. ctask->sc, ctask->itt);
  1192. fail_command(conn, ctask, error << 16);
  1193. }
  1194. }
  1195. list_for_each_entry_safe(ctask, tmp, &conn->requeue, running) {
  1196. if (lun == ctask->sc->device->lun || lun == -1) {
  1197. debug_scsi("failing requeued sc %p itt 0x%x\n",
  1198. ctask->sc, ctask->itt);
  1199. fail_command(conn, ctask, error << 16);
  1200. }
  1201. }
  1202. /* fail all other running */
  1203. list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
  1204. if (lun == ctask->sc->device->lun || lun == -1) {
  1205. debug_scsi("failing in progress sc %p itt 0x%x\n",
  1206. ctask->sc, ctask->itt);
  1207. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1208. }
  1209. }
  1210. }
  1211. static void iscsi_suspend_tx(struct iscsi_conn *conn)
  1212. {
  1213. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1214. scsi_flush_work(conn->session->host);
  1215. }
  1216. static void iscsi_start_tx(struct iscsi_conn *conn)
  1217. {
  1218. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1219. scsi_queue_work(conn->session->host, &conn->xmitwork);
  1220. }
  1221. static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
  1222. {
  1223. struct iscsi_cls_session *cls_session;
  1224. struct iscsi_session *session;
  1225. struct iscsi_conn *conn;
  1226. enum scsi_eh_timer_return rc = EH_NOT_HANDLED;
  1227. cls_session = starget_to_session(scsi_target(scmd->device));
  1228. session = cls_session->dd_data;
  1229. debug_scsi("scsi cmd %p timedout\n", scmd);
  1230. spin_lock(&session->lock);
  1231. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1232. /*
  1233. * We are probably in the middle of iscsi recovery so let
  1234. * that complete and handle the error.
  1235. */
  1236. rc = EH_RESET_TIMER;
  1237. goto done;
  1238. }
  1239. conn = session->leadconn;
  1240. if (!conn) {
  1241. /* In the middle of shuting down */
  1242. rc = EH_RESET_TIMER;
  1243. goto done;
  1244. }
  1245. if (!conn->recv_timeout && !conn->ping_timeout)
  1246. goto done;
  1247. /*
  1248. * if the ping timedout then we are in the middle of cleaning up
  1249. * and can let the iscsi eh handle it
  1250. */
  1251. if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
  1252. (conn->ping_timeout * HZ), jiffies))
  1253. rc = EH_RESET_TIMER;
  1254. /*
  1255. * if we are about to check the transport then give the command
  1256. * more time
  1257. */
  1258. if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
  1259. jiffies))
  1260. rc = EH_RESET_TIMER;
  1261. /* if in the middle of checking the transport then give us more time */
  1262. if (conn->ping_mtask)
  1263. rc = EH_RESET_TIMER;
  1264. done:
  1265. spin_unlock(&session->lock);
  1266. debug_scsi("return %s\n", rc == EH_RESET_TIMER ? "timer reset" : "nh");
  1267. return rc;
  1268. }
  1269. static void iscsi_check_transport_timeouts(unsigned long data)
  1270. {
  1271. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1272. struct iscsi_session *session = conn->session;
  1273. unsigned long recv_timeout, next_timeout = 0, last_recv;
  1274. spin_lock(&session->lock);
  1275. if (session->state != ISCSI_STATE_LOGGED_IN)
  1276. goto done;
  1277. recv_timeout = conn->recv_timeout;
  1278. if (!recv_timeout)
  1279. goto done;
  1280. recv_timeout *= HZ;
  1281. last_recv = conn->last_recv;
  1282. if (conn->ping_mtask &&
  1283. time_before_eq(conn->last_ping + (conn->ping_timeout * HZ),
  1284. jiffies)) {
  1285. iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
  1286. "expired, last rx %lu, last ping %lu, "
  1287. "now %lu\n", conn->ping_timeout, last_recv,
  1288. conn->last_ping, jiffies);
  1289. spin_unlock(&session->lock);
  1290. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1291. return;
  1292. }
  1293. if (time_before_eq(last_recv + recv_timeout, jiffies)) {
  1294. /* send a ping to try to provoke some traffic */
  1295. debug_scsi("Sending nopout as ping on conn %p\n", conn);
  1296. iscsi_send_nopout(conn, NULL);
  1297. next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
  1298. } else
  1299. next_timeout = last_recv + recv_timeout;
  1300. debug_scsi("Setting next tmo %lu\n", next_timeout);
  1301. mod_timer(&conn->transport_timer, next_timeout);
  1302. done:
  1303. spin_unlock(&session->lock);
  1304. }
  1305. static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task *ctask,
  1306. struct iscsi_tm *hdr)
  1307. {
  1308. memset(hdr, 0, sizeof(*hdr));
  1309. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1310. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
  1311. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1312. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  1313. hdr->rtt = ctask->hdr->itt;
  1314. hdr->refcmdsn = ctask->hdr->cmdsn;
  1315. }
  1316. int iscsi_eh_abort(struct scsi_cmnd *sc)
  1317. {
  1318. struct iscsi_cls_session *cls_session;
  1319. struct iscsi_session *session;
  1320. struct iscsi_conn *conn;
  1321. struct iscsi_cmd_task *ctask;
  1322. struct iscsi_tm *hdr;
  1323. int rc, age;
  1324. cls_session = starget_to_session(scsi_target(sc->device));
  1325. session = cls_session->dd_data;
  1326. mutex_lock(&session->eh_mutex);
  1327. spin_lock_bh(&session->lock);
  1328. /*
  1329. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1330. * got the command.
  1331. */
  1332. if (!sc->SCp.ptr) {
  1333. debug_scsi("sc never reached iscsi layer or it completed.\n");
  1334. spin_unlock_bh(&session->lock);
  1335. mutex_unlock(&session->eh_mutex);
  1336. return SUCCESS;
  1337. }
  1338. /*
  1339. * If we are not logged in or we have started a new session
  1340. * then let the host reset code handle this
  1341. */
  1342. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
  1343. sc->SCp.phase != session->age) {
  1344. spin_unlock_bh(&session->lock);
  1345. mutex_unlock(&session->eh_mutex);
  1346. return FAILED;
  1347. }
  1348. conn = session->leadconn;
  1349. conn->eh_abort_cnt++;
  1350. age = session->age;
  1351. ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
  1352. debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
  1353. /* ctask completed before time out */
  1354. if (!ctask->sc) {
  1355. debug_scsi("sc completed while abort in progress\n");
  1356. goto success;
  1357. }
  1358. if (ctask->state == ISCSI_TASK_PENDING) {
  1359. fail_command(conn, ctask, DID_ABORT << 16);
  1360. goto success;
  1361. }
  1362. /* only have one tmf outstanding at a time */
  1363. if (conn->tmf_state != TMF_INITIAL)
  1364. goto failed;
  1365. conn->tmf_state = TMF_QUEUED;
  1366. hdr = &conn->tmhdr;
  1367. iscsi_prep_abort_task_pdu(ctask, hdr);
  1368. if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
  1369. rc = FAILED;
  1370. goto failed;
  1371. }
  1372. switch (conn->tmf_state) {
  1373. case TMF_SUCCESS:
  1374. spin_unlock_bh(&session->lock);
  1375. iscsi_suspend_tx(conn);
  1376. /*
  1377. * clean up task if aborted. grab the recv lock as a writer
  1378. */
  1379. write_lock_bh(conn->recv_lock);
  1380. spin_lock(&session->lock);
  1381. fail_command(conn, ctask, DID_ABORT << 16);
  1382. conn->tmf_state = TMF_INITIAL;
  1383. spin_unlock(&session->lock);
  1384. write_unlock_bh(conn->recv_lock);
  1385. iscsi_start_tx(conn);
  1386. goto success_unlocked;
  1387. case TMF_TIMEDOUT:
  1388. spin_unlock_bh(&session->lock);
  1389. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1390. goto failed_unlocked;
  1391. case TMF_NOT_FOUND:
  1392. if (!sc->SCp.ptr) {
  1393. conn->tmf_state = TMF_INITIAL;
  1394. /* ctask completed before tmf abort response */
  1395. debug_scsi("sc completed while abort in progress\n");
  1396. goto success;
  1397. }
  1398. /* fall through */
  1399. default:
  1400. conn->tmf_state = TMF_INITIAL;
  1401. goto failed;
  1402. }
  1403. success:
  1404. spin_unlock_bh(&session->lock);
  1405. success_unlocked:
  1406. debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1407. mutex_unlock(&session->eh_mutex);
  1408. return SUCCESS;
  1409. failed:
  1410. spin_unlock_bh(&session->lock);
  1411. failed_unlocked:
  1412. debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
  1413. ctask ? ctask->itt : 0);
  1414. mutex_unlock(&session->eh_mutex);
  1415. return FAILED;
  1416. }
  1417. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1418. static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
  1419. {
  1420. memset(hdr, 0, sizeof(*hdr));
  1421. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1422. hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
  1423. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1424. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  1425. hdr->rtt = RESERVED_ITT;
  1426. }
  1427. int iscsi_eh_device_reset(struct scsi_cmnd *sc)
  1428. {
  1429. struct iscsi_cls_session *cls_session;
  1430. struct iscsi_session *session;
  1431. struct iscsi_conn *conn;
  1432. struct iscsi_tm *hdr;
  1433. int rc = FAILED;
  1434. cls_session = starget_to_session(scsi_target(sc->device));
  1435. session = cls_session->dd_data;
  1436. debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
  1437. mutex_lock(&session->eh_mutex);
  1438. spin_lock_bh(&session->lock);
  1439. /*
  1440. * Just check if we are not logged in. We cannot check for
  1441. * the phase because the reset could come from a ioctl.
  1442. */
  1443. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  1444. goto unlock;
  1445. conn = session->leadconn;
  1446. /* only have one tmf outstanding at a time */
  1447. if (conn->tmf_state != TMF_INITIAL)
  1448. goto unlock;
  1449. conn->tmf_state = TMF_QUEUED;
  1450. hdr = &conn->tmhdr;
  1451. iscsi_prep_lun_reset_pdu(sc, hdr);
  1452. if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
  1453. session->lu_reset_timeout)) {
  1454. rc = FAILED;
  1455. goto unlock;
  1456. }
  1457. switch (conn->tmf_state) {
  1458. case TMF_SUCCESS:
  1459. break;
  1460. case TMF_TIMEDOUT:
  1461. spin_unlock_bh(&session->lock);
  1462. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1463. goto done;
  1464. default:
  1465. conn->tmf_state = TMF_INITIAL;
  1466. goto unlock;
  1467. }
  1468. rc = SUCCESS;
  1469. spin_unlock_bh(&session->lock);
  1470. iscsi_suspend_tx(conn);
  1471. /* need to grab the recv lock then session lock */
  1472. write_lock_bh(conn->recv_lock);
  1473. spin_lock(&session->lock);
  1474. fail_all_commands(conn, sc->device->lun, DID_ERROR);
  1475. conn->tmf_state = TMF_INITIAL;
  1476. spin_unlock(&session->lock);
  1477. write_unlock_bh(conn->recv_lock);
  1478. iscsi_start_tx(conn);
  1479. goto done;
  1480. unlock:
  1481. spin_unlock_bh(&session->lock);
  1482. done:
  1483. debug_scsi("iscsi_eh_device_reset %s\n",
  1484. rc == SUCCESS ? "SUCCESS" : "FAILED");
  1485. mutex_unlock(&session->eh_mutex);
  1486. return rc;
  1487. }
  1488. EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
  1489. /*
  1490. * Pre-allocate a pool of @max items of @item_size. By default, the pool
  1491. * should be accessed via kfifo_{get,put} on q->queue.
  1492. * Optionally, the caller can obtain the array of object pointers
  1493. * by passing in a non-NULL @items pointer
  1494. */
  1495. int
  1496. iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
  1497. {
  1498. int i, num_arrays = 1;
  1499. memset(q, 0, sizeof(*q));
  1500. q->max = max;
  1501. /* If the user passed an items pointer, he wants a copy of
  1502. * the array. */
  1503. if (items)
  1504. num_arrays++;
  1505. q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
  1506. if (q->pool == NULL)
  1507. goto enomem;
  1508. q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
  1509. GFP_KERNEL, NULL);
  1510. if (q->queue == ERR_PTR(-ENOMEM))
  1511. goto enomem;
  1512. for (i = 0; i < max; i++) {
  1513. q->pool[i] = kzalloc(item_size, GFP_KERNEL);
  1514. if (q->pool[i] == NULL) {
  1515. q->max = i;
  1516. goto enomem;
  1517. }
  1518. __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
  1519. }
  1520. if (items) {
  1521. *items = q->pool + max;
  1522. memcpy(*items, q->pool, max * sizeof(void *));
  1523. }
  1524. return 0;
  1525. enomem:
  1526. iscsi_pool_free(q);
  1527. return -ENOMEM;
  1528. }
  1529. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  1530. void iscsi_pool_free(struct iscsi_pool *q)
  1531. {
  1532. int i;
  1533. for (i = 0; i < q->max; i++)
  1534. kfree(q->pool[i]);
  1535. if (q->pool)
  1536. kfree(q->pool);
  1537. }
  1538. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  1539. void iscsi_host_setup(struct Scsi_Host *shost, uint16_t qdepth)
  1540. {
  1541. if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
  1542. if (qdepth != 0)
  1543. printk(KERN_ERR "iscsi: invalid queue depth of %d. "
  1544. "Queue depth must be between 1 and %d.\n",
  1545. qdepth, ISCSI_MAX_CMD_PER_LUN);
  1546. qdepth = ISCSI_DEF_CMD_PER_LUN;
  1547. }
  1548. shost->transportt->create_work_queue = 1;
  1549. shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
  1550. shost->cmd_per_lun = qdepth;
  1551. }
  1552. EXPORT_SYMBOL_GPL(iscsi_host_setup);
  1553. void iscsi_host_teardown(struct Scsi_Host *shost)
  1554. {
  1555. struct iscsi_host *ihost = shost_priv(shost);
  1556. kfree(ihost->netdev);
  1557. kfree(ihost->hwaddress);
  1558. kfree(ihost->initiatorname);
  1559. }
  1560. EXPORT_SYMBOL_GPL(iscsi_host_teardown);
  1561. /**
  1562. * iscsi_session_setup - create iscsi cls session and host and session
  1563. * @iscsit: iscsi transport template
  1564. * @shost: scsi host
  1565. * @cmds_max: session can queue
  1566. * @cmd_task_size: LLD ctask private data size
  1567. * @mgmt_task_size: LLD mtask private data size
  1568. * @initial_cmdsn: initial CmdSN
  1569. *
  1570. * This can be used by software iscsi_transports that allocate
  1571. * a session per scsi host.
  1572. */
  1573. struct iscsi_cls_session *
  1574. iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
  1575. uint16_t cmds_max, int cmd_task_size, int mgmt_task_size,
  1576. uint32_t initial_cmdsn)
  1577. {
  1578. struct iscsi_session *session;
  1579. struct iscsi_cls_session *cls_session;
  1580. int cmd_i;
  1581. if (!is_power_of_2(cmds_max) || cmds_max >= ISCSI_MGMT_ITT_OFFSET ||
  1582. cmds_max < 2) {
  1583. if (cmds_max != 0)
  1584. printk(KERN_ERR "iscsi: invalid can_queue of %d. "
  1585. "can_queue must be a power of 2 and between "
  1586. "2 and %d - setting to %d.\n", cmds_max,
  1587. ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
  1588. cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
  1589. }
  1590. cls_session = iscsi_alloc_session(shost, iscsit);
  1591. if (!cls_session)
  1592. return NULL;
  1593. session = cls_session->dd_data;
  1594. session->cls_session = cls_session;
  1595. session->host = shost;
  1596. session->state = ISCSI_STATE_FREE;
  1597. session->fast_abort = 1;
  1598. session->lu_reset_timeout = 15;
  1599. session->abort_timeout = 10;
  1600. session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
  1601. session->cmds_max = cmds_max;
  1602. session->queued_cmdsn = session->cmdsn = initial_cmdsn;
  1603. session->exp_cmdsn = initial_cmdsn + 1;
  1604. session->max_cmdsn = initial_cmdsn + 1;
  1605. session->max_r2t = 1;
  1606. session->tt = iscsit;
  1607. mutex_init(&session->eh_mutex);
  1608. spin_lock_init(&session->lock);
  1609. /* initialize SCSI PDU commands pool */
  1610. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  1611. (void***)&session->cmds,
  1612. cmd_task_size + sizeof(struct iscsi_cmd_task)))
  1613. goto cmdpool_alloc_fail;
  1614. /* pre-format cmds pool with ITT */
  1615. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1616. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1617. if (cmd_task_size)
  1618. ctask->dd_data = &ctask[1];
  1619. ctask->itt = cmd_i;
  1620. INIT_LIST_HEAD(&ctask->running);
  1621. }
  1622. /* initialize immediate command pool */
  1623. if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
  1624. (void***)&session->mgmt_cmds,
  1625. mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
  1626. goto mgmtpool_alloc_fail;
  1627. /* pre-format immediate cmds pool with ITT */
  1628. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1629. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1630. if (mgmt_task_size)
  1631. mtask->dd_data = &mtask[1];
  1632. mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
  1633. INIT_LIST_HEAD(&mtask->running);
  1634. }
  1635. if (!try_module_get(iscsit->owner))
  1636. goto module_get_fail;
  1637. if (iscsi_add_session(cls_session, 0))
  1638. goto cls_session_fail;
  1639. return cls_session;
  1640. cls_session_fail:
  1641. module_put(iscsit->owner);
  1642. module_get_fail:
  1643. iscsi_pool_free(&session->mgmtpool);
  1644. mgmtpool_alloc_fail:
  1645. iscsi_pool_free(&session->cmdpool);
  1646. cmdpool_alloc_fail:
  1647. iscsi_free_session(cls_session);
  1648. return NULL;
  1649. }
  1650. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  1651. /**
  1652. * iscsi_session_teardown - destroy session, host, and cls_session
  1653. * @cls_session: iscsi session
  1654. *
  1655. * The driver must have called iscsi_remove_session before
  1656. * calling this.
  1657. */
  1658. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  1659. {
  1660. struct iscsi_session *session = cls_session->dd_data;
  1661. struct module *owner = cls_session->transport->owner;
  1662. iscsi_pool_free(&session->mgmtpool);
  1663. iscsi_pool_free(&session->cmdpool);
  1664. kfree(session->password);
  1665. kfree(session->password_in);
  1666. kfree(session->username);
  1667. kfree(session->username_in);
  1668. kfree(session->targetname);
  1669. iscsi_destroy_session(cls_session);
  1670. module_put(owner);
  1671. }
  1672. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  1673. /**
  1674. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  1675. * @cls_session: iscsi_cls_session
  1676. * @conn_idx: cid
  1677. **/
  1678. struct iscsi_cls_conn *
  1679. iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1680. {
  1681. struct iscsi_session *session = cls_session->dd_data;
  1682. struct iscsi_conn *conn;
  1683. struct iscsi_cls_conn *cls_conn;
  1684. char *data;
  1685. cls_conn = iscsi_create_conn(cls_session, conn_idx);
  1686. if (!cls_conn)
  1687. return NULL;
  1688. conn = cls_conn->dd_data;
  1689. memset(conn, 0, sizeof(*conn));
  1690. conn->session = session;
  1691. conn->cls_conn = cls_conn;
  1692. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  1693. conn->id = conn_idx;
  1694. conn->exp_statsn = 0;
  1695. conn->tmf_state = TMF_INITIAL;
  1696. init_timer(&conn->transport_timer);
  1697. conn->transport_timer.data = (unsigned long)conn;
  1698. conn->transport_timer.function = iscsi_check_transport_timeouts;
  1699. INIT_LIST_HEAD(&conn->run_list);
  1700. INIT_LIST_HEAD(&conn->mgmt_run_list);
  1701. INIT_LIST_HEAD(&conn->mgmtqueue);
  1702. INIT_LIST_HEAD(&conn->xmitqueue);
  1703. INIT_LIST_HEAD(&conn->requeue);
  1704. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  1705. /* allocate login_mtask used for the login/text sequences */
  1706. spin_lock_bh(&session->lock);
  1707. if (!__kfifo_get(session->mgmtpool.queue,
  1708. (void*)&conn->login_mtask,
  1709. sizeof(void*))) {
  1710. spin_unlock_bh(&session->lock);
  1711. goto login_mtask_alloc_fail;
  1712. }
  1713. spin_unlock_bh(&session->lock);
  1714. data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
  1715. if (!data)
  1716. goto login_mtask_data_alloc_fail;
  1717. conn->login_mtask->data = conn->data = data;
  1718. init_timer(&conn->tmf_timer);
  1719. init_waitqueue_head(&conn->ehwait);
  1720. return cls_conn;
  1721. login_mtask_data_alloc_fail:
  1722. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1723. sizeof(void*));
  1724. login_mtask_alloc_fail:
  1725. iscsi_destroy_conn(cls_conn);
  1726. return NULL;
  1727. }
  1728. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  1729. /**
  1730. * iscsi_conn_teardown - teardown iscsi connection
  1731. * cls_conn: iscsi class connection
  1732. *
  1733. * TODO: we may need to make this into a two step process
  1734. * like scsi-mls remove + put host
  1735. */
  1736. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  1737. {
  1738. struct iscsi_conn *conn = cls_conn->dd_data;
  1739. struct iscsi_session *session = conn->session;
  1740. unsigned long flags;
  1741. del_timer_sync(&conn->transport_timer);
  1742. spin_lock_bh(&session->lock);
  1743. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  1744. if (session->leadconn == conn) {
  1745. /*
  1746. * leading connection? then give up on recovery.
  1747. */
  1748. session->state = ISCSI_STATE_TERMINATE;
  1749. wake_up(&conn->ehwait);
  1750. }
  1751. spin_unlock_bh(&session->lock);
  1752. /*
  1753. * Block until all in-progress commands for this connection
  1754. * time out or fail.
  1755. */
  1756. for (;;) {
  1757. spin_lock_irqsave(session->host->host_lock, flags);
  1758. if (!session->host->host_busy) { /* OK for ERL == 0 */
  1759. spin_unlock_irqrestore(session->host->host_lock, flags);
  1760. break;
  1761. }
  1762. spin_unlock_irqrestore(session->host->host_lock, flags);
  1763. msleep_interruptible(500);
  1764. iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
  1765. "host_busy %d host_failed %d\n",
  1766. session->host->host_busy,
  1767. session->host->host_failed);
  1768. /*
  1769. * force eh_abort() to unblock
  1770. */
  1771. wake_up(&conn->ehwait);
  1772. }
  1773. /* flush queued up work because we free the connection below */
  1774. iscsi_suspend_tx(conn);
  1775. spin_lock_bh(&session->lock);
  1776. kfree(conn->data);
  1777. kfree(conn->persistent_address);
  1778. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1779. sizeof(void*));
  1780. if (session->leadconn == conn)
  1781. session->leadconn = NULL;
  1782. spin_unlock_bh(&session->lock);
  1783. iscsi_destroy_conn(cls_conn);
  1784. }
  1785. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  1786. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  1787. {
  1788. struct iscsi_conn *conn = cls_conn->dd_data;
  1789. struct iscsi_session *session = conn->session;
  1790. if (!session) {
  1791. iscsi_conn_printk(KERN_ERR, conn,
  1792. "can't start unbound connection\n");
  1793. return -EPERM;
  1794. }
  1795. if ((session->imm_data_en || !session->initial_r2t_en) &&
  1796. session->first_burst > session->max_burst) {
  1797. iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
  1798. "first_burst %d max_burst %d\n",
  1799. session->first_burst, session->max_burst);
  1800. return -EINVAL;
  1801. }
  1802. if (conn->ping_timeout && !conn->recv_timeout) {
  1803. iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
  1804. "zero. Using 5 seconds\n.");
  1805. conn->recv_timeout = 5;
  1806. }
  1807. if (conn->recv_timeout && !conn->ping_timeout) {
  1808. iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
  1809. "zero. Using 5 seconds.\n");
  1810. conn->ping_timeout = 5;
  1811. }
  1812. spin_lock_bh(&session->lock);
  1813. conn->c_stage = ISCSI_CONN_STARTED;
  1814. session->state = ISCSI_STATE_LOGGED_IN;
  1815. session->queued_cmdsn = session->cmdsn;
  1816. conn->last_recv = jiffies;
  1817. conn->last_ping = jiffies;
  1818. if (conn->recv_timeout && conn->ping_timeout)
  1819. mod_timer(&conn->transport_timer,
  1820. jiffies + (conn->recv_timeout * HZ));
  1821. switch(conn->stop_stage) {
  1822. case STOP_CONN_RECOVER:
  1823. /*
  1824. * unblock eh_abort() if it is blocked. re-try all
  1825. * commands after successful recovery
  1826. */
  1827. conn->stop_stage = 0;
  1828. conn->tmf_state = TMF_INITIAL;
  1829. session->age++;
  1830. if (session->age == 16)
  1831. session->age = 0;
  1832. break;
  1833. case STOP_CONN_TERM:
  1834. conn->stop_stage = 0;
  1835. break;
  1836. default:
  1837. break;
  1838. }
  1839. spin_unlock_bh(&session->lock);
  1840. iscsi_unblock_session(session->cls_session);
  1841. wake_up(&conn->ehwait);
  1842. return 0;
  1843. }
  1844. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  1845. static void
  1846. flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
  1847. {
  1848. struct iscsi_mgmt_task *mtask, *tmp;
  1849. /* handle pending */
  1850. list_for_each_entry_safe(mtask, tmp, &conn->mgmtqueue, running) {
  1851. debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
  1852. iscsi_free_mgmt_task(conn, mtask);
  1853. }
  1854. /* handle running */
  1855. list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
  1856. debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
  1857. iscsi_free_mgmt_task(conn, mtask);
  1858. }
  1859. conn->mtask = NULL;
  1860. }
  1861. static void iscsi_start_session_recovery(struct iscsi_session *session,
  1862. struct iscsi_conn *conn, int flag)
  1863. {
  1864. int old_stop_stage;
  1865. del_timer_sync(&conn->transport_timer);
  1866. mutex_lock(&session->eh_mutex);
  1867. spin_lock_bh(&session->lock);
  1868. if (conn->stop_stage == STOP_CONN_TERM) {
  1869. spin_unlock_bh(&session->lock);
  1870. mutex_unlock(&session->eh_mutex);
  1871. return;
  1872. }
  1873. /*
  1874. * The LLD either freed/unset the lock on us, or userspace called
  1875. * stop but did not create a proper connection (connection was never
  1876. * bound or it was unbound then stop was called).
  1877. */
  1878. if (!conn->recv_lock) {
  1879. spin_unlock_bh(&session->lock);
  1880. mutex_unlock(&session->eh_mutex);
  1881. return;
  1882. }
  1883. /*
  1884. * When this is called for the in_login state, we only want to clean
  1885. * up the login task and connection. We do not need to block and set
  1886. * the recovery state again
  1887. */
  1888. if (flag == STOP_CONN_TERM)
  1889. session->state = ISCSI_STATE_TERMINATE;
  1890. else if (conn->stop_stage != STOP_CONN_RECOVER)
  1891. session->state = ISCSI_STATE_IN_RECOVERY;
  1892. old_stop_stage = conn->stop_stage;
  1893. conn->stop_stage = flag;
  1894. conn->c_stage = ISCSI_CONN_STOPPED;
  1895. spin_unlock_bh(&session->lock);
  1896. iscsi_suspend_tx(conn);
  1897. write_lock_bh(conn->recv_lock);
  1898. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1899. write_unlock_bh(conn->recv_lock);
  1900. /*
  1901. * for connection level recovery we should not calculate
  1902. * header digest. conn->hdr_size used for optimization
  1903. * in hdr_extract() and will be re-negotiated at
  1904. * set_param() time.
  1905. */
  1906. if (flag == STOP_CONN_RECOVER) {
  1907. conn->hdrdgst_en = 0;
  1908. conn->datadgst_en = 0;
  1909. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  1910. old_stop_stage != STOP_CONN_RECOVER) {
  1911. debug_scsi("blocking session\n");
  1912. iscsi_block_session(session->cls_session);
  1913. }
  1914. }
  1915. /*
  1916. * flush queues.
  1917. */
  1918. spin_lock_bh(&session->lock);
  1919. fail_all_commands(conn, -1,
  1920. STOP_CONN_RECOVER ? DID_BUS_BUSY : DID_ERROR);
  1921. flush_control_queues(session, conn);
  1922. spin_unlock_bh(&session->lock);
  1923. mutex_unlock(&session->eh_mutex);
  1924. }
  1925. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1926. {
  1927. struct iscsi_conn *conn = cls_conn->dd_data;
  1928. struct iscsi_session *session = conn->session;
  1929. switch (flag) {
  1930. case STOP_CONN_RECOVER:
  1931. case STOP_CONN_TERM:
  1932. iscsi_start_session_recovery(session, conn, flag);
  1933. break;
  1934. default:
  1935. iscsi_conn_printk(KERN_ERR, conn,
  1936. "invalid stop flag %d\n", flag);
  1937. }
  1938. }
  1939. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  1940. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  1941. struct iscsi_cls_conn *cls_conn, int is_leading)
  1942. {
  1943. struct iscsi_session *session = cls_session->dd_data;
  1944. struct iscsi_conn *conn = cls_conn->dd_data;
  1945. spin_lock_bh(&session->lock);
  1946. if (is_leading)
  1947. session->leadconn = conn;
  1948. spin_unlock_bh(&session->lock);
  1949. /*
  1950. * Unblock xmitworker(), Login Phase will pass through.
  1951. */
  1952. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1953. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1954. return 0;
  1955. }
  1956. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  1957. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  1958. enum iscsi_param param, char *buf, int buflen)
  1959. {
  1960. struct iscsi_conn *conn = cls_conn->dd_data;
  1961. struct iscsi_session *session = conn->session;
  1962. uint32_t value;
  1963. switch(param) {
  1964. case ISCSI_PARAM_FAST_ABORT:
  1965. sscanf(buf, "%d", &session->fast_abort);
  1966. break;
  1967. case ISCSI_PARAM_ABORT_TMO:
  1968. sscanf(buf, "%d", &session->abort_timeout);
  1969. break;
  1970. case ISCSI_PARAM_LU_RESET_TMO:
  1971. sscanf(buf, "%d", &session->lu_reset_timeout);
  1972. break;
  1973. case ISCSI_PARAM_PING_TMO:
  1974. sscanf(buf, "%d", &conn->ping_timeout);
  1975. break;
  1976. case ISCSI_PARAM_RECV_TMO:
  1977. sscanf(buf, "%d", &conn->recv_timeout);
  1978. break;
  1979. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1980. sscanf(buf, "%d", &conn->max_recv_dlength);
  1981. break;
  1982. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1983. sscanf(buf, "%d", &conn->max_xmit_dlength);
  1984. break;
  1985. case ISCSI_PARAM_HDRDGST_EN:
  1986. sscanf(buf, "%d", &conn->hdrdgst_en);
  1987. break;
  1988. case ISCSI_PARAM_DATADGST_EN:
  1989. sscanf(buf, "%d", &conn->datadgst_en);
  1990. break;
  1991. case ISCSI_PARAM_INITIAL_R2T_EN:
  1992. sscanf(buf, "%d", &session->initial_r2t_en);
  1993. break;
  1994. case ISCSI_PARAM_MAX_R2T:
  1995. sscanf(buf, "%d", &session->max_r2t);
  1996. break;
  1997. case ISCSI_PARAM_IMM_DATA_EN:
  1998. sscanf(buf, "%d", &session->imm_data_en);
  1999. break;
  2000. case ISCSI_PARAM_FIRST_BURST:
  2001. sscanf(buf, "%d", &session->first_burst);
  2002. break;
  2003. case ISCSI_PARAM_MAX_BURST:
  2004. sscanf(buf, "%d", &session->max_burst);
  2005. break;
  2006. case ISCSI_PARAM_PDU_INORDER_EN:
  2007. sscanf(buf, "%d", &session->pdu_inorder_en);
  2008. break;
  2009. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2010. sscanf(buf, "%d", &session->dataseq_inorder_en);
  2011. break;
  2012. case ISCSI_PARAM_ERL:
  2013. sscanf(buf, "%d", &session->erl);
  2014. break;
  2015. case ISCSI_PARAM_IFMARKER_EN:
  2016. sscanf(buf, "%d", &value);
  2017. BUG_ON(value);
  2018. break;
  2019. case ISCSI_PARAM_OFMARKER_EN:
  2020. sscanf(buf, "%d", &value);
  2021. BUG_ON(value);
  2022. break;
  2023. case ISCSI_PARAM_EXP_STATSN:
  2024. sscanf(buf, "%u", &conn->exp_statsn);
  2025. break;
  2026. case ISCSI_PARAM_USERNAME:
  2027. kfree(session->username);
  2028. session->username = kstrdup(buf, GFP_KERNEL);
  2029. if (!session->username)
  2030. return -ENOMEM;
  2031. break;
  2032. case ISCSI_PARAM_USERNAME_IN:
  2033. kfree(session->username_in);
  2034. session->username_in = kstrdup(buf, GFP_KERNEL);
  2035. if (!session->username_in)
  2036. return -ENOMEM;
  2037. break;
  2038. case ISCSI_PARAM_PASSWORD:
  2039. kfree(session->password);
  2040. session->password = kstrdup(buf, GFP_KERNEL);
  2041. if (!session->password)
  2042. return -ENOMEM;
  2043. break;
  2044. case ISCSI_PARAM_PASSWORD_IN:
  2045. kfree(session->password_in);
  2046. session->password_in = kstrdup(buf, GFP_KERNEL);
  2047. if (!session->password_in)
  2048. return -ENOMEM;
  2049. break;
  2050. case ISCSI_PARAM_TARGET_NAME:
  2051. /* this should not change between logins */
  2052. if (session->targetname)
  2053. break;
  2054. session->targetname = kstrdup(buf, GFP_KERNEL);
  2055. if (!session->targetname)
  2056. return -ENOMEM;
  2057. break;
  2058. case ISCSI_PARAM_TPGT:
  2059. sscanf(buf, "%d", &session->tpgt);
  2060. break;
  2061. case ISCSI_PARAM_PERSISTENT_PORT:
  2062. sscanf(buf, "%d", &conn->persistent_port);
  2063. break;
  2064. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  2065. /*
  2066. * this is the address returned in discovery so it should
  2067. * not change between logins.
  2068. */
  2069. if (conn->persistent_address)
  2070. break;
  2071. conn->persistent_address = kstrdup(buf, GFP_KERNEL);
  2072. if (!conn->persistent_address)
  2073. return -ENOMEM;
  2074. break;
  2075. default:
  2076. return -ENOSYS;
  2077. }
  2078. return 0;
  2079. }
  2080. EXPORT_SYMBOL_GPL(iscsi_set_param);
  2081. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  2082. enum iscsi_param param, char *buf)
  2083. {
  2084. struct iscsi_session *session = cls_session->dd_data;
  2085. int len;
  2086. switch(param) {
  2087. case ISCSI_PARAM_FAST_ABORT:
  2088. len = sprintf(buf, "%d\n", session->fast_abort);
  2089. break;
  2090. case ISCSI_PARAM_ABORT_TMO:
  2091. len = sprintf(buf, "%d\n", session->abort_timeout);
  2092. break;
  2093. case ISCSI_PARAM_LU_RESET_TMO:
  2094. len = sprintf(buf, "%d\n", session->lu_reset_timeout);
  2095. break;
  2096. case ISCSI_PARAM_INITIAL_R2T_EN:
  2097. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  2098. break;
  2099. case ISCSI_PARAM_MAX_R2T:
  2100. len = sprintf(buf, "%hu\n", session->max_r2t);
  2101. break;
  2102. case ISCSI_PARAM_IMM_DATA_EN:
  2103. len = sprintf(buf, "%d\n", session->imm_data_en);
  2104. break;
  2105. case ISCSI_PARAM_FIRST_BURST:
  2106. len = sprintf(buf, "%u\n", session->first_burst);
  2107. break;
  2108. case ISCSI_PARAM_MAX_BURST:
  2109. len = sprintf(buf, "%u\n", session->max_burst);
  2110. break;
  2111. case ISCSI_PARAM_PDU_INORDER_EN:
  2112. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  2113. break;
  2114. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2115. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  2116. break;
  2117. case ISCSI_PARAM_ERL:
  2118. len = sprintf(buf, "%d\n", session->erl);
  2119. break;
  2120. case ISCSI_PARAM_TARGET_NAME:
  2121. len = sprintf(buf, "%s\n", session->targetname);
  2122. break;
  2123. case ISCSI_PARAM_TPGT:
  2124. len = sprintf(buf, "%d\n", session->tpgt);
  2125. break;
  2126. case ISCSI_PARAM_USERNAME:
  2127. len = sprintf(buf, "%s\n", session->username);
  2128. break;
  2129. case ISCSI_PARAM_USERNAME_IN:
  2130. len = sprintf(buf, "%s\n", session->username_in);
  2131. break;
  2132. case ISCSI_PARAM_PASSWORD:
  2133. len = sprintf(buf, "%s\n", session->password);
  2134. break;
  2135. case ISCSI_PARAM_PASSWORD_IN:
  2136. len = sprintf(buf, "%s\n", session->password_in);
  2137. break;
  2138. default:
  2139. return -ENOSYS;
  2140. }
  2141. return len;
  2142. }
  2143. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  2144. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  2145. enum iscsi_param param, char *buf)
  2146. {
  2147. struct iscsi_conn *conn = cls_conn->dd_data;
  2148. int len;
  2149. switch(param) {
  2150. case ISCSI_PARAM_PING_TMO:
  2151. len = sprintf(buf, "%u\n", conn->ping_timeout);
  2152. break;
  2153. case ISCSI_PARAM_RECV_TMO:
  2154. len = sprintf(buf, "%u\n", conn->recv_timeout);
  2155. break;
  2156. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  2157. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  2158. break;
  2159. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  2160. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  2161. break;
  2162. case ISCSI_PARAM_HDRDGST_EN:
  2163. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  2164. break;
  2165. case ISCSI_PARAM_DATADGST_EN:
  2166. len = sprintf(buf, "%d\n", conn->datadgst_en);
  2167. break;
  2168. case ISCSI_PARAM_IFMARKER_EN:
  2169. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  2170. break;
  2171. case ISCSI_PARAM_OFMARKER_EN:
  2172. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  2173. break;
  2174. case ISCSI_PARAM_EXP_STATSN:
  2175. len = sprintf(buf, "%u\n", conn->exp_statsn);
  2176. break;
  2177. case ISCSI_PARAM_PERSISTENT_PORT:
  2178. len = sprintf(buf, "%d\n", conn->persistent_port);
  2179. break;
  2180. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  2181. len = sprintf(buf, "%s\n", conn->persistent_address);
  2182. break;
  2183. default:
  2184. return -ENOSYS;
  2185. }
  2186. return len;
  2187. }
  2188. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  2189. int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  2190. char *buf)
  2191. {
  2192. struct iscsi_host *ihost = shost_priv(shost);
  2193. int len;
  2194. switch (param) {
  2195. case ISCSI_HOST_PARAM_NETDEV_NAME:
  2196. if (!ihost->netdev)
  2197. len = sprintf(buf, "%s\n", "default");
  2198. else
  2199. len = sprintf(buf, "%s\n", ihost->netdev);
  2200. break;
  2201. case ISCSI_HOST_PARAM_HWADDRESS:
  2202. if (!ihost->hwaddress)
  2203. len = sprintf(buf, "%s\n", "default");
  2204. else
  2205. len = sprintf(buf, "%s\n", ihost->hwaddress);
  2206. break;
  2207. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  2208. if (!ihost->initiatorname)
  2209. len = sprintf(buf, "%s\n", "unknown");
  2210. else
  2211. len = sprintf(buf, "%s\n", ihost->initiatorname);
  2212. break;
  2213. case ISCSI_HOST_PARAM_IPADDRESS:
  2214. if (!strlen(ihost->local_address))
  2215. len = sprintf(buf, "%s\n", "unknown");
  2216. else
  2217. len = sprintf(buf, "%s\n",
  2218. ihost->local_address);
  2219. default:
  2220. return -ENOSYS;
  2221. }
  2222. return len;
  2223. }
  2224. EXPORT_SYMBOL_GPL(iscsi_host_get_param);
  2225. int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  2226. char *buf, int buflen)
  2227. {
  2228. struct iscsi_host *ihost = shost_priv(shost);
  2229. switch (param) {
  2230. case ISCSI_HOST_PARAM_NETDEV_NAME:
  2231. if (!ihost->netdev)
  2232. ihost->netdev = kstrdup(buf, GFP_KERNEL);
  2233. break;
  2234. case ISCSI_HOST_PARAM_HWADDRESS:
  2235. if (!ihost->hwaddress)
  2236. ihost->hwaddress = kstrdup(buf, GFP_KERNEL);
  2237. break;
  2238. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  2239. if (!ihost->initiatorname)
  2240. ihost->initiatorname = kstrdup(buf, GFP_KERNEL);
  2241. break;
  2242. default:
  2243. return -ENOSYS;
  2244. }
  2245. return 0;
  2246. }
  2247. EXPORT_SYMBOL_GPL(iscsi_host_set_param);
  2248. MODULE_AUTHOR("Mike Christie");
  2249. MODULE_DESCRIPTION("iSCSI library functions");
  2250. MODULE_LICENSE("GPL");