libiscsi.c 98 KB

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