libiscsi.c 73 KB

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