libiscsi.c 91 KB

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