libiscsi.c 86 KB

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