libiscsi.c 83 KB

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