libiscsi.c 78 KB

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