libiscsi.c 91 KB

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