scsi_transport_spi.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  1. /*
  2. * Parallel SCSI (SPI) transport specific attributes exported to sysfs.
  3. *
  4. * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
  5. * Copyright (c) 2004, 2005 James Bottomley <James.Bottomley@SteelEye.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/ctype.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/mutex.h>
  27. #include <linux/sysfs.h>
  28. #include <linux/slab.h>
  29. #include <linux/suspend.h>
  30. #include <scsi/scsi.h>
  31. #include "scsi_priv.h"
  32. #include <scsi/scsi_device.h>
  33. #include <scsi/scsi_host.h>
  34. #include <scsi/scsi_cmnd.h>
  35. #include <scsi/scsi_eh.h>
  36. #include <scsi/scsi_tcq.h>
  37. #include <scsi/scsi_transport.h>
  38. #include <scsi/scsi_transport_spi.h>
  39. #define SPI_NUM_ATTRS 14 /* increase this if you add attributes */
  40. #define SPI_OTHER_ATTRS 1 /* Increase this if you add "always
  41. * on" attributes */
  42. #define SPI_HOST_ATTRS 1
  43. #define SPI_MAX_ECHO_BUFFER_SIZE 4096
  44. #define DV_LOOPS 3
  45. #define DV_TIMEOUT (10*HZ)
  46. #define DV_RETRIES 3 /* should only need at most
  47. * two cc/ua clears */
  48. /* Our blacklist flags */
  49. enum {
  50. SPI_BLIST_NOIUS = (__force blist_flags_t)0x1,
  51. };
  52. /* blacklist table, modelled on scsi_devinfo.c */
  53. static struct {
  54. char *vendor;
  55. char *model;
  56. blist_flags_t flags;
  57. } spi_static_device_list[] __initdata = {
  58. {"HP", "Ultrium 3-SCSI", SPI_BLIST_NOIUS },
  59. {"IBM", "ULTRIUM-TD3", SPI_BLIST_NOIUS },
  60. {NULL, NULL, 0}
  61. };
  62. /* Private data accessors (keep these out of the header file) */
  63. #define spi_dv_in_progress(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_in_progress)
  64. #define spi_dv_mutex(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_mutex)
  65. struct spi_internal {
  66. struct scsi_transport_template t;
  67. struct spi_function_template *f;
  68. };
  69. #define to_spi_internal(tmpl) container_of(tmpl, struct spi_internal, t)
  70. static const int ppr_to_ps[] = {
  71. /* The PPR values 0-6 are reserved, fill them in when
  72. * the committee defines them */
  73. -1, /* 0x00 */
  74. -1, /* 0x01 */
  75. -1, /* 0x02 */
  76. -1, /* 0x03 */
  77. -1, /* 0x04 */
  78. -1, /* 0x05 */
  79. -1, /* 0x06 */
  80. 3125, /* 0x07 */
  81. 6250, /* 0x08 */
  82. 12500, /* 0x09 */
  83. 25000, /* 0x0a */
  84. 30300, /* 0x0b */
  85. 50000, /* 0x0c */
  86. };
  87. /* The PPR values at which you calculate the period in ns by multiplying
  88. * by 4 */
  89. #define SPI_STATIC_PPR 0x0c
  90. static int sprint_frac(char *dest, int value, int denom)
  91. {
  92. int frac = value % denom;
  93. int result = sprintf(dest, "%d", value / denom);
  94. if (frac == 0)
  95. return result;
  96. dest[result++] = '.';
  97. do {
  98. denom /= 10;
  99. sprintf(dest + result, "%d", frac / denom);
  100. result++;
  101. frac %= denom;
  102. } while (frac);
  103. dest[result++] = '\0';
  104. return result;
  105. }
  106. static int spi_execute(struct scsi_device *sdev, const void *cmd,
  107. enum dma_data_direction dir,
  108. void *buffer, unsigned bufflen,
  109. struct scsi_sense_hdr *sshdr)
  110. {
  111. int i, result;
  112. unsigned char sense[SCSI_SENSE_BUFFERSIZE];
  113. struct scsi_sense_hdr sshdr_tmp;
  114. if (!sshdr)
  115. sshdr = &sshdr_tmp;
  116. for(i = 0; i < DV_RETRIES; i++) {
  117. /*
  118. * The purpose of the RQF_PM flag below is to bypass the
  119. * SDEV_QUIESCE state.
  120. */
  121. result = scsi_execute(sdev, cmd, dir, buffer, bufflen, sense,
  122. sshdr, DV_TIMEOUT, /* retries */ 1,
  123. REQ_FAILFAST_DEV |
  124. REQ_FAILFAST_TRANSPORT |
  125. REQ_FAILFAST_DRIVER,
  126. RQF_PM, NULL);
  127. if (driver_byte(result) != DRIVER_SENSE ||
  128. sshdr->sense_key != UNIT_ATTENTION)
  129. break;
  130. }
  131. return result;
  132. }
  133. static struct {
  134. enum spi_signal_type value;
  135. char *name;
  136. } signal_types[] = {
  137. { SPI_SIGNAL_UNKNOWN, "unknown" },
  138. { SPI_SIGNAL_SE, "SE" },
  139. { SPI_SIGNAL_LVD, "LVD" },
  140. { SPI_SIGNAL_HVD, "HVD" },
  141. };
  142. static inline const char *spi_signal_to_string(enum spi_signal_type type)
  143. {
  144. int i;
  145. for (i = 0; i < ARRAY_SIZE(signal_types); i++) {
  146. if (type == signal_types[i].value)
  147. return signal_types[i].name;
  148. }
  149. return NULL;
  150. }
  151. static inline enum spi_signal_type spi_signal_to_value(const char *name)
  152. {
  153. int i, len;
  154. for (i = 0; i < ARRAY_SIZE(signal_types); i++) {
  155. len = strlen(signal_types[i].name);
  156. if (strncmp(name, signal_types[i].name, len) == 0 &&
  157. (name[len] == '\n' || name[len] == '\0'))
  158. return signal_types[i].value;
  159. }
  160. return SPI_SIGNAL_UNKNOWN;
  161. }
  162. static int spi_host_setup(struct transport_container *tc, struct device *dev,
  163. struct device *cdev)
  164. {
  165. struct Scsi_Host *shost = dev_to_shost(dev);
  166. spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
  167. return 0;
  168. }
  169. static int spi_host_configure(struct transport_container *tc,
  170. struct device *dev,
  171. struct device *cdev);
  172. static DECLARE_TRANSPORT_CLASS(spi_host_class,
  173. "spi_host",
  174. spi_host_setup,
  175. NULL,
  176. spi_host_configure);
  177. static int spi_host_match(struct attribute_container *cont,
  178. struct device *dev)
  179. {
  180. struct Scsi_Host *shost;
  181. if (!scsi_is_host_device(dev))
  182. return 0;
  183. shost = dev_to_shost(dev);
  184. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  185. != &spi_host_class.class)
  186. return 0;
  187. return &shost->transportt->host_attrs.ac == cont;
  188. }
  189. static int spi_target_configure(struct transport_container *tc,
  190. struct device *dev,
  191. struct device *cdev);
  192. static int spi_device_configure(struct transport_container *tc,
  193. struct device *dev,
  194. struct device *cdev)
  195. {
  196. struct scsi_device *sdev = to_scsi_device(dev);
  197. struct scsi_target *starget = sdev->sdev_target;
  198. blist_flags_t bflags;
  199. bflags = scsi_get_device_flags_keyed(sdev, &sdev->inquiry[8],
  200. &sdev->inquiry[16],
  201. SCSI_DEVINFO_SPI);
  202. /* Populate the target capability fields with the values
  203. * gleaned from the device inquiry */
  204. spi_support_sync(starget) = scsi_device_sync(sdev);
  205. spi_support_wide(starget) = scsi_device_wide(sdev);
  206. spi_support_dt(starget) = scsi_device_dt(sdev);
  207. spi_support_dt_only(starget) = scsi_device_dt_only(sdev);
  208. spi_support_ius(starget) = scsi_device_ius(sdev);
  209. if (bflags & SPI_BLIST_NOIUS) {
  210. dev_info(dev, "Information Units disabled by blacklist\n");
  211. spi_support_ius(starget) = 0;
  212. }
  213. spi_support_qas(starget) = scsi_device_qas(sdev);
  214. return 0;
  215. }
  216. static int spi_setup_transport_attrs(struct transport_container *tc,
  217. struct device *dev,
  218. struct device *cdev)
  219. {
  220. struct scsi_target *starget = to_scsi_target(dev);
  221. spi_period(starget) = -1; /* illegal value */
  222. spi_min_period(starget) = 0;
  223. spi_offset(starget) = 0; /* async */
  224. spi_max_offset(starget) = 255;
  225. spi_width(starget) = 0; /* narrow */
  226. spi_max_width(starget) = 1;
  227. spi_iu(starget) = 0; /* no IU */
  228. spi_max_iu(starget) = 1;
  229. spi_dt(starget) = 0; /* ST */
  230. spi_qas(starget) = 0;
  231. spi_max_qas(starget) = 1;
  232. spi_wr_flow(starget) = 0;
  233. spi_rd_strm(starget) = 0;
  234. spi_rti(starget) = 0;
  235. spi_pcomp_en(starget) = 0;
  236. spi_hold_mcs(starget) = 0;
  237. spi_dv_pending(starget) = 0;
  238. spi_dv_in_progress(starget) = 0;
  239. spi_initial_dv(starget) = 0;
  240. mutex_init(&spi_dv_mutex(starget));
  241. return 0;
  242. }
  243. #define spi_transport_show_simple(field, format_string) \
  244. \
  245. static ssize_t \
  246. show_spi_transport_##field(struct device *dev, \
  247. struct device_attribute *attr, char *buf) \
  248. { \
  249. struct scsi_target *starget = transport_class_to_starget(dev); \
  250. struct spi_transport_attrs *tp; \
  251. \
  252. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  253. return snprintf(buf, 20, format_string, tp->field); \
  254. }
  255. #define spi_transport_store_simple(field, format_string) \
  256. \
  257. static ssize_t \
  258. store_spi_transport_##field(struct device *dev, \
  259. struct device_attribute *attr, \
  260. const char *buf, size_t count) \
  261. { \
  262. int val; \
  263. struct scsi_target *starget = transport_class_to_starget(dev); \
  264. struct spi_transport_attrs *tp; \
  265. \
  266. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  267. val = simple_strtoul(buf, NULL, 0); \
  268. tp->field = val; \
  269. return count; \
  270. }
  271. #define spi_transport_show_function(field, format_string) \
  272. \
  273. static ssize_t \
  274. show_spi_transport_##field(struct device *dev, \
  275. struct device_attribute *attr, char *buf) \
  276. { \
  277. struct scsi_target *starget = transport_class_to_starget(dev); \
  278. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  279. struct spi_transport_attrs *tp; \
  280. struct spi_internal *i = to_spi_internal(shost->transportt); \
  281. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  282. if (i->f->get_##field) \
  283. i->f->get_##field(starget); \
  284. return snprintf(buf, 20, format_string, tp->field); \
  285. }
  286. #define spi_transport_store_function(field, format_string) \
  287. static ssize_t \
  288. store_spi_transport_##field(struct device *dev, \
  289. struct device_attribute *attr, \
  290. const char *buf, size_t count) \
  291. { \
  292. int val; \
  293. struct scsi_target *starget = transport_class_to_starget(dev); \
  294. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  295. struct spi_internal *i = to_spi_internal(shost->transportt); \
  296. \
  297. if (!i->f->set_##field) \
  298. return -EINVAL; \
  299. val = simple_strtoul(buf, NULL, 0); \
  300. i->f->set_##field(starget, val); \
  301. return count; \
  302. }
  303. #define spi_transport_store_max(field, format_string) \
  304. static ssize_t \
  305. store_spi_transport_##field(struct device *dev, \
  306. struct device_attribute *attr, \
  307. const char *buf, size_t count) \
  308. { \
  309. int val; \
  310. struct scsi_target *starget = transport_class_to_starget(dev); \
  311. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  312. struct spi_internal *i = to_spi_internal(shost->transportt); \
  313. struct spi_transport_attrs *tp \
  314. = (struct spi_transport_attrs *)&starget->starget_data; \
  315. \
  316. if (!i->f->set_##field) \
  317. return -EINVAL; \
  318. val = simple_strtoul(buf, NULL, 0); \
  319. if (val > tp->max_##field) \
  320. val = tp->max_##field; \
  321. i->f->set_##field(starget, val); \
  322. return count; \
  323. }
  324. #define spi_transport_rd_attr(field, format_string) \
  325. spi_transport_show_function(field, format_string) \
  326. spi_transport_store_function(field, format_string) \
  327. static DEVICE_ATTR(field, S_IRUGO, \
  328. show_spi_transport_##field, \
  329. store_spi_transport_##field);
  330. #define spi_transport_simple_attr(field, format_string) \
  331. spi_transport_show_simple(field, format_string) \
  332. spi_transport_store_simple(field, format_string) \
  333. static DEVICE_ATTR(field, S_IRUGO, \
  334. show_spi_transport_##field, \
  335. store_spi_transport_##field);
  336. #define spi_transport_max_attr(field, format_string) \
  337. spi_transport_show_function(field, format_string) \
  338. spi_transport_store_max(field, format_string) \
  339. spi_transport_simple_attr(max_##field, format_string) \
  340. static DEVICE_ATTR(field, S_IRUGO, \
  341. show_spi_transport_##field, \
  342. store_spi_transport_##field);
  343. /* The Parallel SCSI Tranport Attributes: */
  344. spi_transport_max_attr(offset, "%d\n");
  345. spi_transport_max_attr(width, "%d\n");
  346. spi_transport_max_attr(iu, "%d\n");
  347. spi_transport_rd_attr(dt, "%d\n");
  348. spi_transport_max_attr(qas, "%d\n");
  349. spi_transport_rd_attr(wr_flow, "%d\n");
  350. spi_transport_rd_attr(rd_strm, "%d\n");
  351. spi_transport_rd_attr(rti, "%d\n");
  352. spi_transport_rd_attr(pcomp_en, "%d\n");
  353. spi_transport_rd_attr(hold_mcs, "%d\n");
  354. /* we only care about the first child device that's a real SCSI device
  355. * so we return 1 to terminate the iteration when we find it */
  356. static int child_iter(struct device *dev, void *data)
  357. {
  358. if (!scsi_is_sdev_device(dev))
  359. return 0;
  360. spi_dv_device(to_scsi_device(dev));
  361. return 1;
  362. }
  363. static ssize_t
  364. store_spi_revalidate(struct device *dev, struct device_attribute *attr,
  365. const char *buf, size_t count)
  366. {
  367. struct scsi_target *starget = transport_class_to_starget(dev);
  368. device_for_each_child(&starget->dev, NULL, child_iter);
  369. return count;
  370. }
  371. static DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
  372. /* Translate the period into ns according to the current spec
  373. * for SDTR/PPR messages */
  374. static int period_to_str(char *buf, int period)
  375. {
  376. int len, picosec;
  377. if (period < 0 || period > 0xff) {
  378. picosec = -1;
  379. } else if (period <= SPI_STATIC_PPR) {
  380. picosec = ppr_to_ps[period];
  381. } else {
  382. picosec = period * 4000;
  383. }
  384. if (picosec == -1) {
  385. len = sprintf(buf, "reserved");
  386. } else {
  387. len = sprint_frac(buf, picosec, 1000);
  388. }
  389. return len;
  390. }
  391. static ssize_t
  392. show_spi_transport_period_helper(char *buf, int period)
  393. {
  394. int len = period_to_str(buf, period);
  395. buf[len++] = '\n';
  396. buf[len] = '\0';
  397. return len;
  398. }
  399. static ssize_t
  400. store_spi_transport_period_helper(struct device *dev, const char *buf,
  401. size_t count, int *periodp)
  402. {
  403. int j, picosec, period = -1;
  404. char *endp;
  405. picosec = simple_strtoul(buf, &endp, 10) * 1000;
  406. if (*endp == '.') {
  407. int mult = 100;
  408. do {
  409. endp++;
  410. if (!isdigit(*endp))
  411. break;
  412. picosec += (*endp - '0') * mult;
  413. mult /= 10;
  414. } while (mult > 0);
  415. }
  416. for (j = 0; j <= SPI_STATIC_PPR; j++) {
  417. if (ppr_to_ps[j] < picosec)
  418. continue;
  419. period = j;
  420. break;
  421. }
  422. if (period == -1)
  423. period = picosec / 4000;
  424. if (period > 0xff)
  425. period = 0xff;
  426. *periodp = period;
  427. return count;
  428. }
  429. static ssize_t
  430. show_spi_transport_period(struct device *dev,
  431. struct device_attribute *attr, char *buf)
  432. {
  433. struct scsi_target *starget = transport_class_to_starget(dev);
  434. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  435. struct spi_internal *i = to_spi_internal(shost->transportt);
  436. struct spi_transport_attrs *tp =
  437. (struct spi_transport_attrs *)&starget->starget_data;
  438. if (i->f->get_period)
  439. i->f->get_period(starget);
  440. return show_spi_transport_period_helper(buf, tp->period);
  441. }
  442. static ssize_t
  443. store_spi_transport_period(struct device *cdev, struct device_attribute *attr,
  444. const char *buf, size_t count)
  445. {
  446. struct scsi_target *starget = transport_class_to_starget(cdev);
  447. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  448. struct spi_internal *i = to_spi_internal(shost->transportt);
  449. struct spi_transport_attrs *tp =
  450. (struct spi_transport_attrs *)&starget->starget_data;
  451. int period, retval;
  452. if (!i->f->set_period)
  453. return -EINVAL;
  454. retval = store_spi_transport_period_helper(cdev, buf, count, &period);
  455. if (period < tp->min_period)
  456. period = tp->min_period;
  457. i->f->set_period(starget, period);
  458. return retval;
  459. }
  460. static DEVICE_ATTR(period, S_IRUGO,
  461. show_spi_transport_period,
  462. store_spi_transport_period);
  463. static ssize_t
  464. show_spi_transport_min_period(struct device *cdev,
  465. struct device_attribute *attr, char *buf)
  466. {
  467. struct scsi_target *starget = transport_class_to_starget(cdev);
  468. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  469. struct spi_internal *i = to_spi_internal(shost->transportt);
  470. struct spi_transport_attrs *tp =
  471. (struct spi_transport_attrs *)&starget->starget_data;
  472. if (!i->f->set_period)
  473. return -EINVAL;
  474. return show_spi_transport_period_helper(buf, tp->min_period);
  475. }
  476. static ssize_t
  477. store_spi_transport_min_period(struct device *cdev,
  478. struct device_attribute *attr,
  479. const char *buf, size_t count)
  480. {
  481. struct scsi_target *starget = transport_class_to_starget(cdev);
  482. struct spi_transport_attrs *tp =
  483. (struct spi_transport_attrs *)&starget->starget_data;
  484. return store_spi_transport_period_helper(cdev, buf, count,
  485. &tp->min_period);
  486. }
  487. static DEVICE_ATTR(min_period, S_IRUGO,
  488. show_spi_transport_min_period,
  489. store_spi_transport_min_period);
  490. static ssize_t show_spi_host_signalling(struct device *cdev,
  491. struct device_attribute *attr,
  492. char *buf)
  493. {
  494. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  495. struct spi_internal *i = to_spi_internal(shost->transportt);
  496. if (i->f->get_signalling)
  497. i->f->get_signalling(shost);
  498. return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost)));
  499. }
  500. static ssize_t store_spi_host_signalling(struct device *dev,
  501. struct device_attribute *attr,
  502. const char *buf, size_t count)
  503. {
  504. struct Scsi_Host *shost = transport_class_to_shost(dev);
  505. struct spi_internal *i = to_spi_internal(shost->transportt);
  506. enum spi_signal_type type = spi_signal_to_value(buf);
  507. if (!i->f->set_signalling)
  508. return -EINVAL;
  509. if (type != SPI_SIGNAL_UNKNOWN)
  510. i->f->set_signalling(shost, type);
  511. return count;
  512. }
  513. static DEVICE_ATTR(signalling, S_IRUGO,
  514. show_spi_host_signalling,
  515. store_spi_host_signalling);
  516. static ssize_t show_spi_host_width(struct device *cdev,
  517. struct device_attribute *attr,
  518. char *buf)
  519. {
  520. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  521. return sprintf(buf, "%s\n", shost->max_id == 16 ? "wide" : "narrow");
  522. }
  523. static DEVICE_ATTR(host_width, S_IRUGO,
  524. show_spi_host_width, NULL);
  525. static ssize_t show_spi_host_hba_id(struct device *cdev,
  526. struct device_attribute *attr,
  527. char *buf)
  528. {
  529. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  530. return sprintf(buf, "%d\n", shost->this_id);
  531. }
  532. static DEVICE_ATTR(hba_id, S_IRUGO,
  533. show_spi_host_hba_id, NULL);
  534. #define DV_SET(x, y) \
  535. if(i->f->set_##x) \
  536. i->f->set_##x(sdev->sdev_target, y)
  537. enum spi_compare_returns {
  538. SPI_COMPARE_SUCCESS,
  539. SPI_COMPARE_FAILURE,
  540. SPI_COMPARE_SKIP_TEST,
  541. };
  542. /* This is for read/write Domain Validation: If the device supports
  543. * an echo buffer, we do read/write tests to it */
  544. static enum spi_compare_returns
  545. spi_dv_device_echo_buffer(struct scsi_device *sdev, u8 *buffer,
  546. u8 *ptr, const int retries)
  547. {
  548. int len = ptr - buffer;
  549. int j, k, r, result;
  550. unsigned int pattern = 0x0000ffff;
  551. struct scsi_sense_hdr sshdr;
  552. const char spi_write_buffer[] = {
  553. WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  554. };
  555. const char spi_read_buffer[] = {
  556. READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  557. };
  558. /* set up the pattern buffer. Doesn't matter if we spill
  559. * slightly beyond since that's where the read buffer is */
  560. for (j = 0; j < len; ) {
  561. /* fill the buffer with counting (test a) */
  562. for ( ; j < min(len, 32); j++)
  563. buffer[j] = j;
  564. k = j;
  565. /* fill the buffer with alternating words of 0x0 and
  566. * 0xffff (test b) */
  567. for ( ; j < min(len, k + 32); j += 2) {
  568. u16 *word = (u16 *)&buffer[j];
  569. *word = (j & 0x02) ? 0x0000 : 0xffff;
  570. }
  571. k = j;
  572. /* fill with crosstalk (alternating 0x5555 0xaaa)
  573. * (test c) */
  574. for ( ; j < min(len, k + 32); j += 2) {
  575. u16 *word = (u16 *)&buffer[j];
  576. *word = (j & 0x02) ? 0x5555 : 0xaaaa;
  577. }
  578. k = j;
  579. /* fill with shifting bits (test d) */
  580. for ( ; j < min(len, k + 32); j += 4) {
  581. u32 *word = (unsigned int *)&buffer[j];
  582. u32 roll = (pattern & 0x80000000) ? 1 : 0;
  583. *word = pattern;
  584. pattern = (pattern << 1) | roll;
  585. }
  586. /* don't bother with random data (test e) */
  587. }
  588. for (r = 0; r < retries; r++) {
  589. result = spi_execute(sdev, spi_write_buffer, DMA_TO_DEVICE,
  590. buffer, len, &sshdr);
  591. if(result || !scsi_device_online(sdev)) {
  592. scsi_device_set_state(sdev, SDEV_QUIESCE);
  593. if (scsi_sense_valid(&sshdr)
  594. && sshdr.sense_key == ILLEGAL_REQUEST
  595. /* INVALID FIELD IN CDB */
  596. && sshdr.asc == 0x24 && sshdr.ascq == 0x00)
  597. /* This would mean that the drive lied
  598. * to us about supporting an echo
  599. * buffer (unfortunately some Western
  600. * Digital drives do precisely this)
  601. */
  602. return SPI_COMPARE_SKIP_TEST;
  603. sdev_printk(KERN_ERR, sdev, "Write Buffer failure %x\n", result);
  604. return SPI_COMPARE_FAILURE;
  605. }
  606. memset(ptr, 0, len);
  607. spi_execute(sdev, spi_read_buffer, DMA_FROM_DEVICE,
  608. ptr, len, NULL);
  609. scsi_device_set_state(sdev, SDEV_QUIESCE);
  610. if (memcmp(buffer, ptr, len) != 0)
  611. return SPI_COMPARE_FAILURE;
  612. }
  613. return SPI_COMPARE_SUCCESS;
  614. }
  615. /* This is for the simplest form of Domain Validation: a read test
  616. * on the inquiry data from the device */
  617. static enum spi_compare_returns
  618. spi_dv_device_compare_inquiry(struct scsi_device *sdev, u8 *buffer,
  619. u8 *ptr, const int retries)
  620. {
  621. int r, result;
  622. const int len = sdev->inquiry_len;
  623. const char spi_inquiry[] = {
  624. INQUIRY, 0, 0, 0, len, 0
  625. };
  626. for (r = 0; r < retries; r++) {
  627. memset(ptr, 0, len);
  628. result = spi_execute(sdev, spi_inquiry, DMA_FROM_DEVICE,
  629. ptr, len, NULL);
  630. if(result || !scsi_device_online(sdev)) {
  631. scsi_device_set_state(sdev, SDEV_QUIESCE);
  632. return SPI_COMPARE_FAILURE;
  633. }
  634. /* If we don't have the inquiry data already, the
  635. * first read gets it */
  636. if (ptr == buffer) {
  637. ptr += len;
  638. --r;
  639. continue;
  640. }
  641. if (memcmp(buffer, ptr, len) != 0)
  642. /* failure */
  643. return SPI_COMPARE_FAILURE;
  644. }
  645. return SPI_COMPARE_SUCCESS;
  646. }
  647. static enum spi_compare_returns
  648. spi_dv_retrain(struct scsi_device *sdev, u8 *buffer, u8 *ptr,
  649. enum spi_compare_returns
  650. (*compare_fn)(struct scsi_device *, u8 *, u8 *, int))
  651. {
  652. struct spi_internal *i = to_spi_internal(sdev->host->transportt);
  653. struct scsi_target *starget = sdev->sdev_target;
  654. int period = 0, prevperiod = 0;
  655. enum spi_compare_returns retval;
  656. for (;;) {
  657. int newperiod;
  658. retval = compare_fn(sdev, buffer, ptr, DV_LOOPS);
  659. if (retval == SPI_COMPARE_SUCCESS
  660. || retval == SPI_COMPARE_SKIP_TEST)
  661. break;
  662. /* OK, retrain, fallback */
  663. if (i->f->get_iu)
  664. i->f->get_iu(starget);
  665. if (i->f->get_qas)
  666. i->f->get_qas(starget);
  667. if (i->f->get_period)
  668. i->f->get_period(sdev->sdev_target);
  669. /* Here's the fallback sequence; first try turning off
  670. * IU, then QAS (if we can control them), then finally
  671. * fall down the periods */
  672. if (i->f->set_iu && spi_iu(starget)) {
  673. starget_printk(KERN_ERR, starget, "Domain Validation Disabling Information Units\n");
  674. DV_SET(iu, 0);
  675. } else if (i->f->set_qas && spi_qas(starget)) {
  676. starget_printk(KERN_ERR, starget, "Domain Validation Disabling Quick Arbitration and Selection\n");
  677. DV_SET(qas, 0);
  678. } else {
  679. newperiod = spi_period(starget);
  680. period = newperiod > period ? newperiod : period;
  681. if (period < 0x0d)
  682. period++;
  683. else
  684. period += period >> 1;
  685. if (unlikely(period > 0xff || period == prevperiod)) {
  686. /* Total failure; set to async and return */
  687. starget_printk(KERN_ERR, starget, "Domain Validation Failure, dropping back to Asynchronous\n");
  688. DV_SET(offset, 0);
  689. return SPI_COMPARE_FAILURE;
  690. }
  691. starget_printk(KERN_ERR, starget, "Domain Validation detected failure, dropping back\n");
  692. DV_SET(period, period);
  693. prevperiod = period;
  694. }
  695. }
  696. return retval;
  697. }
  698. static int
  699. spi_dv_device_get_echo_buffer(struct scsi_device *sdev, u8 *buffer)
  700. {
  701. int l, result;
  702. /* first off do a test unit ready. This can error out
  703. * because of reservations or some other reason. If it
  704. * fails, the device won't let us write to the echo buffer
  705. * so just return failure */
  706. static const char spi_test_unit_ready[] = {
  707. TEST_UNIT_READY, 0, 0, 0, 0, 0
  708. };
  709. static const char spi_read_buffer_descriptor[] = {
  710. READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
  711. };
  712. /* We send a set of three TURs to clear any outstanding
  713. * unit attention conditions if they exist (Otherwise the
  714. * buffer tests won't be happy). If the TUR still fails
  715. * (reservation conflict, device not ready, etc) just
  716. * skip the write tests */
  717. for (l = 0; ; l++) {
  718. result = spi_execute(sdev, spi_test_unit_ready, DMA_NONE,
  719. NULL, 0, NULL);
  720. if(result) {
  721. if(l >= 3)
  722. return 0;
  723. } else {
  724. /* TUR succeeded */
  725. break;
  726. }
  727. }
  728. result = spi_execute(sdev, spi_read_buffer_descriptor,
  729. DMA_FROM_DEVICE, buffer, 4, NULL);
  730. if (result)
  731. /* Device has no echo buffer */
  732. return 0;
  733. return buffer[3] + ((buffer[2] & 0x1f) << 8);
  734. }
  735. static void
  736. spi_dv_device_internal(struct scsi_device *sdev, u8 *buffer)
  737. {
  738. struct spi_internal *i = to_spi_internal(sdev->host->transportt);
  739. struct scsi_target *starget = sdev->sdev_target;
  740. struct Scsi_Host *shost = sdev->host;
  741. int len = sdev->inquiry_len;
  742. int min_period = spi_min_period(starget);
  743. int max_width = spi_max_width(starget);
  744. /* first set us up for narrow async */
  745. DV_SET(offset, 0);
  746. DV_SET(width, 0);
  747. if (spi_dv_device_compare_inquiry(sdev, buffer, buffer, DV_LOOPS)
  748. != SPI_COMPARE_SUCCESS) {
  749. starget_printk(KERN_ERR, starget, "Domain Validation Initial Inquiry Failed\n");
  750. /* FIXME: should probably offline the device here? */
  751. return;
  752. }
  753. if (!spi_support_wide(starget)) {
  754. spi_max_width(starget) = 0;
  755. max_width = 0;
  756. }
  757. /* test width */
  758. if (i->f->set_width && max_width) {
  759. i->f->set_width(starget, 1);
  760. if (spi_dv_device_compare_inquiry(sdev, buffer,
  761. buffer + len,
  762. DV_LOOPS)
  763. != SPI_COMPARE_SUCCESS) {
  764. starget_printk(KERN_ERR, starget, "Wide Transfers Fail\n");
  765. i->f->set_width(starget, 0);
  766. /* Make sure we don't force wide back on by asking
  767. * for a transfer period that requires it */
  768. max_width = 0;
  769. if (min_period < 10)
  770. min_period = 10;
  771. }
  772. }
  773. if (!i->f->set_period)
  774. return;
  775. /* device can't handle synchronous */
  776. if (!spi_support_sync(starget) && !spi_support_dt(starget))
  777. return;
  778. /* len == -1 is the signal that we need to ascertain the
  779. * presence of an echo buffer before trying to use it. len ==
  780. * 0 means we don't have an echo buffer */
  781. len = -1;
  782. retry:
  783. /* now set up to the maximum */
  784. DV_SET(offset, spi_max_offset(starget));
  785. DV_SET(period, min_period);
  786. /* try QAS requests; this should be harmless to set if the
  787. * target supports it */
  788. if (spi_support_qas(starget) && spi_max_qas(starget)) {
  789. DV_SET(qas, 1);
  790. } else {
  791. DV_SET(qas, 0);
  792. }
  793. if (spi_support_ius(starget) && spi_max_iu(starget) &&
  794. min_period < 9) {
  795. /* This u320 (or u640). Set IU transfers */
  796. DV_SET(iu, 1);
  797. /* Then set the optional parameters */
  798. DV_SET(rd_strm, 1);
  799. DV_SET(wr_flow, 1);
  800. DV_SET(rti, 1);
  801. if (min_period == 8)
  802. DV_SET(pcomp_en, 1);
  803. } else {
  804. DV_SET(iu, 0);
  805. }
  806. /* now that we've done all this, actually check the bus
  807. * signal type (if known). Some devices are stupid on
  808. * a SE bus and still claim they can try LVD only settings */
  809. if (i->f->get_signalling)
  810. i->f->get_signalling(shost);
  811. if (spi_signalling(shost) == SPI_SIGNAL_SE ||
  812. spi_signalling(shost) == SPI_SIGNAL_HVD ||
  813. !spi_support_dt(starget)) {
  814. DV_SET(dt, 0);
  815. } else {
  816. DV_SET(dt, 1);
  817. }
  818. /* set width last because it will pull all the other
  819. * parameters down to required values */
  820. DV_SET(width, max_width);
  821. /* Do the read only INQUIRY tests */
  822. spi_dv_retrain(sdev, buffer, buffer + sdev->inquiry_len,
  823. spi_dv_device_compare_inquiry);
  824. /* See if we actually managed to negotiate and sustain DT */
  825. if (i->f->get_dt)
  826. i->f->get_dt(starget);
  827. /* see if the device has an echo buffer. If it does we can do
  828. * the SPI pattern write tests. Because of some broken
  829. * devices, we *only* try this on a device that has actually
  830. * negotiated DT */
  831. if (len == -1 && spi_dt(starget))
  832. len = spi_dv_device_get_echo_buffer(sdev, buffer);
  833. if (len <= 0) {
  834. starget_printk(KERN_INFO, starget, "Domain Validation skipping write tests\n");
  835. return;
  836. }
  837. if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
  838. starget_printk(KERN_WARNING, starget, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE);
  839. len = SPI_MAX_ECHO_BUFFER_SIZE;
  840. }
  841. if (spi_dv_retrain(sdev, buffer, buffer + len,
  842. spi_dv_device_echo_buffer)
  843. == SPI_COMPARE_SKIP_TEST) {
  844. /* OK, the stupid drive can't do a write echo buffer
  845. * test after all, fall back to the read tests */
  846. len = 0;
  847. goto retry;
  848. }
  849. }
  850. /** spi_dv_device - Do Domain Validation on the device
  851. * @sdev: scsi device to validate
  852. *
  853. * Performs the domain validation on the given device in the
  854. * current execution thread. Since DV operations may sleep,
  855. * the current thread must have user context. Also no SCSI
  856. * related locks that would deadlock I/O issued by the DV may
  857. * be held.
  858. */
  859. void
  860. spi_dv_device(struct scsi_device *sdev)
  861. {
  862. struct scsi_target *starget = sdev->sdev_target;
  863. u8 *buffer;
  864. const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
  865. /*
  866. * Because this function and the power management code both call
  867. * scsi_device_quiesce(), it is not safe to perform domain validation
  868. * while suspend or resume is in progress. Hence the
  869. * lock/unlock_system_sleep() calls.
  870. */
  871. lock_system_sleep();
  872. if (scsi_autopm_get_device(sdev))
  873. goto unlock_system_sleep;
  874. if (unlikely(spi_dv_in_progress(starget)))
  875. goto put_autopm;
  876. if (unlikely(scsi_device_get(sdev)))
  877. goto put_autopm;
  878. spi_dv_in_progress(starget) = 1;
  879. buffer = kzalloc(len, GFP_KERNEL);
  880. if (unlikely(!buffer))
  881. goto put_sdev;
  882. /* We need to verify that the actual device will quiesce; the
  883. * later target quiesce is just a nice to have */
  884. if (unlikely(scsi_device_quiesce(sdev)))
  885. goto free_buffer;
  886. scsi_target_quiesce(starget);
  887. spi_dv_pending(starget) = 1;
  888. mutex_lock(&spi_dv_mutex(starget));
  889. starget_printk(KERN_INFO, starget, "Beginning Domain Validation\n");
  890. spi_dv_device_internal(sdev, buffer);
  891. starget_printk(KERN_INFO, starget, "Ending Domain Validation\n");
  892. mutex_unlock(&spi_dv_mutex(starget));
  893. spi_dv_pending(starget) = 0;
  894. scsi_target_resume(starget);
  895. spi_initial_dv(starget) = 1;
  896. free_buffer:
  897. kfree(buffer);
  898. put_sdev:
  899. spi_dv_in_progress(starget) = 0;
  900. scsi_device_put(sdev);
  901. put_autopm:
  902. scsi_autopm_put_device(sdev);
  903. unlock_system_sleep:
  904. unlock_system_sleep();
  905. }
  906. EXPORT_SYMBOL(spi_dv_device);
  907. struct work_queue_wrapper {
  908. struct work_struct work;
  909. struct scsi_device *sdev;
  910. };
  911. static void
  912. spi_dv_device_work_wrapper(struct work_struct *work)
  913. {
  914. struct work_queue_wrapper *wqw =
  915. container_of(work, struct work_queue_wrapper, work);
  916. struct scsi_device *sdev = wqw->sdev;
  917. kfree(wqw);
  918. spi_dv_device(sdev);
  919. spi_dv_pending(sdev->sdev_target) = 0;
  920. scsi_device_put(sdev);
  921. }
  922. /**
  923. * spi_schedule_dv_device - schedule domain validation to occur on the device
  924. * @sdev: The device to validate
  925. *
  926. * Identical to spi_dv_device() above, except that the DV will be
  927. * scheduled to occur in a workqueue later. All memory allocations
  928. * are atomic, so may be called from any context including those holding
  929. * SCSI locks.
  930. */
  931. void
  932. spi_schedule_dv_device(struct scsi_device *sdev)
  933. {
  934. struct work_queue_wrapper *wqw =
  935. kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
  936. if (unlikely(!wqw))
  937. return;
  938. if (unlikely(spi_dv_pending(sdev->sdev_target))) {
  939. kfree(wqw);
  940. return;
  941. }
  942. /* Set pending early (dv_device doesn't check it, only sets it) */
  943. spi_dv_pending(sdev->sdev_target) = 1;
  944. if (unlikely(scsi_device_get(sdev))) {
  945. kfree(wqw);
  946. spi_dv_pending(sdev->sdev_target) = 0;
  947. return;
  948. }
  949. INIT_WORK(&wqw->work, spi_dv_device_work_wrapper);
  950. wqw->sdev = sdev;
  951. schedule_work(&wqw->work);
  952. }
  953. EXPORT_SYMBOL(spi_schedule_dv_device);
  954. /**
  955. * spi_display_xfer_agreement - Print the current target transfer agreement
  956. * @starget: The target for which to display the agreement
  957. *
  958. * Each SPI port is required to maintain a transfer agreement for each
  959. * other port on the bus. This function prints a one-line summary of
  960. * the current agreement; more detailed information is available in sysfs.
  961. */
  962. void spi_display_xfer_agreement(struct scsi_target *starget)
  963. {
  964. struct spi_transport_attrs *tp;
  965. tp = (struct spi_transport_attrs *)&starget->starget_data;
  966. if (tp->offset > 0 && tp->period > 0) {
  967. unsigned int picosec, kb100;
  968. char *scsi = "FAST-?";
  969. char tmp[8];
  970. if (tp->period <= SPI_STATIC_PPR) {
  971. picosec = ppr_to_ps[tp->period];
  972. switch (tp->period) {
  973. case 7: scsi = "FAST-320"; break;
  974. case 8: scsi = "FAST-160"; break;
  975. case 9: scsi = "FAST-80"; break;
  976. case 10:
  977. case 11: scsi = "FAST-40"; break;
  978. case 12: scsi = "FAST-20"; break;
  979. }
  980. } else {
  981. picosec = tp->period * 4000;
  982. if (tp->period < 25)
  983. scsi = "FAST-20";
  984. else if (tp->period < 50)
  985. scsi = "FAST-10";
  986. else
  987. scsi = "FAST-5";
  988. }
  989. kb100 = (10000000 + picosec / 2) / picosec;
  990. if (tp->width)
  991. kb100 *= 2;
  992. sprint_frac(tmp, picosec, 1000);
  993. dev_info(&starget->dev,
  994. "%s %sSCSI %d.%d MB/s %s%s%s%s%s%s%s%s (%s ns, offset %d)\n",
  995. scsi, tp->width ? "WIDE " : "", kb100/10, kb100 % 10,
  996. tp->dt ? "DT" : "ST",
  997. tp->iu ? " IU" : "",
  998. tp->qas ? " QAS" : "",
  999. tp->rd_strm ? " RDSTRM" : "",
  1000. tp->rti ? " RTI" : "",
  1001. tp->wr_flow ? " WRFLOW" : "",
  1002. tp->pcomp_en ? " PCOMP" : "",
  1003. tp->hold_mcs ? " HMCS" : "",
  1004. tmp, tp->offset);
  1005. } else {
  1006. dev_info(&starget->dev, "%sasynchronous\n",
  1007. tp->width ? "wide " : "");
  1008. }
  1009. }
  1010. EXPORT_SYMBOL(spi_display_xfer_agreement);
  1011. int spi_populate_width_msg(unsigned char *msg, int width)
  1012. {
  1013. msg[0] = EXTENDED_MESSAGE;
  1014. msg[1] = 2;
  1015. msg[2] = EXTENDED_WDTR;
  1016. msg[3] = width;
  1017. return 4;
  1018. }
  1019. EXPORT_SYMBOL_GPL(spi_populate_width_msg);
  1020. int spi_populate_sync_msg(unsigned char *msg, int period, int offset)
  1021. {
  1022. msg[0] = EXTENDED_MESSAGE;
  1023. msg[1] = 3;
  1024. msg[2] = EXTENDED_SDTR;
  1025. msg[3] = period;
  1026. msg[4] = offset;
  1027. return 5;
  1028. }
  1029. EXPORT_SYMBOL_GPL(spi_populate_sync_msg);
  1030. int spi_populate_ppr_msg(unsigned char *msg, int period, int offset,
  1031. int width, int options)
  1032. {
  1033. msg[0] = EXTENDED_MESSAGE;
  1034. msg[1] = 6;
  1035. msg[2] = EXTENDED_PPR;
  1036. msg[3] = period;
  1037. msg[4] = 0;
  1038. msg[5] = offset;
  1039. msg[6] = width;
  1040. msg[7] = options;
  1041. return 8;
  1042. }
  1043. EXPORT_SYMBOL_GPL(spi_populate_ppr_msg);
  1044. /**
  1045. * spi_populate_tag_msg - place a tag message in a buffer
  1046. * @msg: pointer to the area to place the tag
  1047. * @cmd: pointer to the scsi command for the tag
  1048. *
  1049. * Notes:
  1050. * designed to create the correct type of tag message for the
  1051. * particular request. Returns the size of the tag message.
  1052. * May return 0 if TCQ is disabled for this device.
  1053. **/
  1054. int spi_populate_tag_msg(unsigned char *msg, struct scsi_cmnd *cmd)
  1055. {
  1056. if (cmd->flags & SCMD_TAGGED) {
  1057. *msg++ = SIMPLE_QUEUE_TAG;
  1058. *msg++ = cmd->request->tag;
  1059. return 2;
  1060. }
  1061. return 0;
  1062. }
  1063. EXPORT_SYMBOL_GPL(spi_populate_tag_msg);
  1064. #ifdef CONFIG_SCSI_CONSTANTS
  1065. static const char * const one_byte_msgs[] = {
  1066. /* 0x00 */ "Task Complete", NULL /* Extended Message */, "Save Pointers",
  1067. /* 0x03 */ "Restore Pointers", "Disconnect", "Initiator Error",
  1068. /* 0x06 */ "Abort Task Set", "Message Reject", "Nop", "Message Parity Error",
  1069. /* 0x0a */ "Linked Command Complete", "Linked Command Complete w/flag",
  1070. /* 0x0c */ "Target Reset", "Abort Task", "Clear Task Set",
  1071. /* 0x0f */ "Initiate Recovery", "Release Recovery",
  1072. /* 0x11 */ "Terminate Process", "Continue Task", "Target Transfer Disable",
  1073. /* 0x14 */ NULL, NULL, "Clear ACA", "LUN Reset"
  1074. };
  1075. static const char * const two_byte_msgs[] = {
  1076. /* 0x20 */ "Simple Queue Tag", "Head of Queue Tag", "Ordered Queue Tag",
  1077. /* 0x23 */ "Ignore Wide Residue", "ACA"
  1078. };
  1079. static const char * const extended_msgs[] = {
  1080. /* 0x00 */ "Modify Data Pointer", "Synchronous Data Transfer Request",
  1081. /* 0x02 */ "SCSI-I Extended Identify", "Wide Data Transfer Request",
  1082. /* 0x04 */ "Parallel Protocol Request", "Modify Bidirectional Data Pointer"
  1083. };
  1084. static void print_nego(const unsigned char *msg, int per, int off, int width)
  1085. {
  1086. if (per) {
  1087. char buf[20];
  1088. period_to_str(buf, msg[per]);
  1089. printk("period = %s ns ", buf);
  1090. }
  1091. if (off)
  1092. printk("offset = %d ", msg[off]);
  1093. if (width)
  1094. printk("width = %d ", 8 << msg[width]);
  1095. }
  1096. static void print_ptr(const unsigned char *msg, int msb, const char *desc)
  1097. {
  1098. int ptr = (msg[msb] << 24) | (msg[msb+1] << 16) | (msg[msb+2] << 8) |
  1099. msg[msb+3];
  1100. printk("%s = %d ", desc, ptr);
  1101. }
  1102. int spi_print_msg(const unsigned char *msg)
  1103. {
  1104. int len = 1, i;
  1105. if (msg[0] == EXTENDED_MESSAGE) {
  1106. len = 2 + msg[1];
  1107. if (len == 2)
  1108. len += 256;
  1109. if (msg[2] < ARRAY_SIZE(extended_msgs))
  1110. printk ("%s ", extended_msgs[msg[2]]);
  1111. else
  1112. printk ("Extended Message, reserved code (0x%02x) ",
  1113. (int) msg[2]);
  1114. switch (msg[2]) {
  1115. case EXTENDED_MODIFY_DATA_POINTER:
  1116. print_ptr(msg, 3, "pointer");
  1117. break;
  1118. case EXTENDED_SDTR:
  1119. print_nego(msg, 3, 4, 0);
  1120. break;
  1121. case EXTENDED_WDTR:
  1122. print_nego(msg, 0, 0, 3);
  1123. break;
  1124. case EXTENDED_PPR:
  1125. print_nego(msg, 3, 5, 6);
  1126. break;
  1127. case EXTENDED_MODIFY_BIDI_DATA_PTR:
  1128. print_ptr(msg, 3, "out");
  1129. print_ptr(msg, 7, "in");
  1130. break;
  1131. default:
  1132. for (i = 2; i < len; ++i)
  1133. printk("%02x ", msg[i]);
  1134. }
  1135. /* Identify */
  1136. } else if (msg[0] & 0x80) {
  1137. printk("Identify disconnect %sallowed %s %d ",
  1138. (msg[0] & 0x40) ? "" : "not ",
  1139. (msg[0] & 0x20) ? "target routine" : "lun",
  1140. msg[0] & 0x7);
  1141. /* Normal One byte */
  1142. } else if (msg[0] < 0x1f) {
  1143. if (msg[0] < ARRAY_SIZE(one_byte_msgs) && one_byte_msgs[msg[0]])
  1144. printk("%s ", one_byte_msgs[msg[0]]);
  1145. else
  1146. printk("reserved (%02x) ", msg[0]);
  1147. } else if (msg[0] == 0x55) {
  1148. printk("QAS Request ");
  1149. /* Two byte */
  1150. } else if (msg[0] <= 0x2f) {
  1151. if ((msg[0] - 0x20) < ARRAY_SIZE(two_byte_msgs))
  1152. printk("%s %02x ", two_byte_msgs[msg[0] - 0x20],
  1153. msg[1]);
  1154. else
  1155. printk("reserved two byte (%02x %02x) ",
  1156. msg[0], msg[1]);
  1157. len = 2;
  1158. } else
  1159. printk("reserved ");
  1160. return len;
  1161. }
  1162. EXPORT_SYMBOL(spi_print_msg);
  1163. #else /* ifndef CONFIG_SCSI_CONSTANTS */
  1164. int spi_print_msg(const unsigned char *msg)
  1165. {
  1166. int len = 1, i;
  1167. if (msg[0] == EXTENDED_MESSAGE) {
  1168. len = 2 + msg[1];
  1169. if (len == 2)
  1170. len += 256;
  1171. for (i = 0; i < len; ++i)
  1172. printk("%02x ", msg[i]);
  1173. /* Identify */
  1174. } else if (msg[0] & 0x80) {
  1175. printk("%02x ", msg[0]);
  1176. /* Normal One byte */
  1177. } else if ((msg[0] < 0x1f) || (msg[0] == 0x55)) {
  1178. printk("%02x ", msg[0]);
  1179. /* Two byte */
  1180. } else if (msg[0] <= 0x2f) {
  1181. printk("%02x %02x", msg[0], msg[1]);
  1182. len = 2;
  1183. } else
  1184. printk("%02x ", msg[0]);
  1185. return len;
  1186. }
  1187. EXPORT_SYMBOL(spi_print_msg);
  1188. #endif /* ! CONFIG_SCSI_CONSTANTS */
  1189. static int spi_device_match(struct attribute_container *cont,
  1190. struct device *dev)
  1191. {
  1192. struct scsi_device *sdev;
  1193. struct Scsi_Host *shost;
  1194. struct spi_internal *i;
  1195. if (!scsi_is_sdev_device(dev))
  1196. return 0;
  1197. sdev = to_scsi_device(dev);
  1198. shost = sdev->host;
  1199. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1200. != &spi_host_class.class)
  1201. return 0;
  1202. /* Note: this class has no device attributes, so it has
  1203. * no per-HBA allocation and thus we don't need to distinguish
  1204. * the attribute containers for the device */
  1205. i = to_spi_internal(shost->transportt);
  1206. if (i->f->deny_binding && i->f->deny_binding(sdev->sdev_target))
  1207. return 0;
  1208. return 1;
  1209. }
  1210. static int spi_target_match(struct attribute_container *cont,
  1211. struct device *dev)
  1212. {
  1213. struct Scsi_Host *shost;
  1214. struct scsi_target *starget;
  1215. struct spi_internal *i;
  1216. if (!scsi_is_target_device(dev))
  1217. return 0;
  1218. shost = dev_to_shost(dev->parent);
  1219. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1220. != &spi_host_class.class)
  1221. return 0;
  1222. i = to_spi_internal(shost->transportt);
  1223. starget = to_scsi_target(dev);
  1224. if (i->f->deny_binding && i->f->deny_binding(starget))
  1225. return 0;
  1226. return &i->t.target_attrs.ac == cont;
  1227. }
  1228. static DECLARE_TRANSPORT_CLASS(spi_transport_class,
  1229. "spi_transport",
  1230. spi_setup_transport_attrs,
  1231. NULL,
  1232. spi_target_configure);
  1233. static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class,
  1234. spi_device_match,
  1235. spi_device_configure);
  1236. static struct attribute *host_attributes[] = {
  1237. &dev_attr_signalling.attr,
  1238. &dev_attr_host_width.attr,
  1239. &dev_attr_hba_id.attr,
  1240. NULL
  1241. };
  1242. static struct attribute_group host_attribute_group = {
  1243. .attrs = host_attributes,
  1244. };
  1245. static int spi_host_configure(struct transport_container *tc,
  1246. struct device *dev,
  1247. struct device *cdev)
  1248. {
  1249. struct kobject *kobj = &cdev->kobj;
  1250. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1251. struct spi_internal *si = to_spi_internal(shost->transportt);
  1252. struct attribute *attr = &dev_attr_signalling.attr;
  1253. int rc = 0;
  1254. if (si->f->set_signalling)
  1255. rc = sysfs_chmod_file(kobj, attr, attr->mode | S_IWUSR);
  1256. return rc;
  1257. }
  1258. /* returns true if we should be showing the variable. Also
  1259. * overloads the return by setting 1<<1 if the attribute should
  1260. * be writeable */
  1261. #define TARGET_ATTRIBUTE_HELPER(name) \
  1262. (si->f->show_##name ? S_IRUGO : 0) | \
  1263. (si->f->set_##name ? S_IWUSR : 0)
  1264. static umode_t target_attribute_is_visible(struct kobject *kobj,
  1265. struct attribute *attr, int i)
  1266. {
  1267. struct device *cdev = container_of(kobj, struct device, kobj);
  1268. struct scsi_target *starget = transport_class_to_starget(cdev);
  1269. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1270. struct spi_internal *si = to_spi_internal(shost->transportt);
  1271. if (attr == &dev_attr_period.attr &&
  1272. spi_support_sync(starget))
  1273. return TARGET_ATTRIBUTE_HELPER(period);
  1274. else if (attr == &dev_attr_min_period.attr &&
  1275. spi_support_sync(starget))
  1276. return TARGET_ATTRIBUTE_HELPER(period);
  1277. else if (attr == &dev_attr_offset.attr &&
  1278. spi_support_sync(starget))
  1279. return TARGET_ATTRIBUTE_HELPER(offset);
  1280. else if (attr == &dev_attr_max_offset.attr &&
  1281. spi_support_sync(starget))
  1282. return TARGET_ATTRIBUTE_HELPER(offset);
  1283. else if (attr == &dev_attr_width.attr &&
  1284. spi_support_wide(starget))
  1285. return TARGET_ATTRIBUTE_HELPER(width);
  1286. else if (attr == &dev_attr_max_width.attr &&
  1287. spi_support_wide(starget))
  1288. return TARGET_ATTRIBUTE_HELPER(width);
  1289. else if (attr == &dev_attr_iu.attr &&
  1290. spi_support_ius(starget))
  1291. return TARGET_ATTRIBUTE_HELPER(iu);
  1292. else if (attr == &dev_attr_max_iu.attr &&
  1293. spi_support_ius(starget))
  1294. return TARGET_ATTRIBUTE_HELPER(iu);
  1295. else if (attr == &dev_attr_dt.attr &&
  1296. spi_support_dt(starget))
  1297. return TARGET_ATTRIBUTE_HELPER(dt);
  1298. else if (attr == &dev_attr_qas.attr &&
  1299. spi_support_qas(starget))
  1300. return TARGET_ATTRIBUTE_HELPER(qas);
  1301. else if (attr == &dev_attr_max_qas.attr &&
  1302. spi_support_qas(starget))
  1303. return TARGET_ATTRIBUTE_HELPER(qas);
  1304. else if (attr == &dev_attr_wr_flow.attr &&
  1305. spi_support_ius(starget))
  1306. return TARGET_ATTRIBUTE_HELPER(wr_flow);
  1307. else if (attr == &dev_attr_rd_strm.attr &&
  1308. spi_support_ius(starget))
  1309. return TARGET_ATTRIBUTE_HELPER(rd_strm);
  1310. else if (attr == &dev_attr_rti.attr &&
  1311. spi_support_ius(starget))
  1312. return TARGET_ATTRIBUTE_HELPER(rti);
  1313. else if (attr == &dev_attr_pcomp_en.attr &&
  1314. spi_support_ius(starget))
  1315. return TARGET_ATTRIBUTE_HELPER(pcomp_en);
  1316. else if (attr == &dev_attr_hold_mcs.attr &&
  1317. spi_support_ius(starget))
  1318. return TARGET_ATTRIBUTE_HELPER(hold_mcs);
  1319. else if (attr == &dev_attr_revalidate.attr)
  1320. return S_IWUSR;
  1321. return 0;
  1322. }
  1323. static struct attribute *target_attributes[] = {
  1324. &dev_attr_period.attr,
  1325. &dev_attr_min_period.attr,
  1326. &dev_attr_offset.attr,
  1327. &dev_attr_max_offset.attr,
  1328. &dev_attr_width.attr,
  1329. &dev_attr_max_width.attr,
  1330. &dev_attr_iu.attr,
  1331. &dev_attr_max_iu.attr,
  1332. &dev_attr_dt.attr,
  1333. &dev_attr_qas.attr,
  1334. &dev_attr_max_qas.attr,
  1335. &dev_attr_wr_flow.attr,
  1336. &dev_attr_rd_strm.attr,
  1337. &dev_attr_rti.attr,
  1338. &dev_attr_pcomp_en.attr,
  1339. &dev_attr_hold_mcs.attr,
  1340. &dev_attr_revalidate.attr,
  1341. NULL
  1342. };
  1343. static struct attribute_group target_attribute_group = {
  1344. .attrs = target_attributes,
  1345. .is_visible = target_attribute_is_visible,
  1346. };
  1347. static int spi_target_configure(struct transport_container *tc,
  1348. struct device *dev,
  1349. struct device *cdev)
  1350. {
  1351. struct kobject *kobj = &cdev->kobj;
  1352. /* force an update based on parameters read from the device */
  1353. sysfs_update_group(kobj, &target_attribute_group);
  1354. return 0;
  1355. }
  1356. struct scsi_transport_template *
  1357. spi_attach_transport(struct spi_function_template *ft)
  1358. {
  1359. struct spi_internal *i = kzalloc(sizeof(struct spi_internal),
  1360. GFP_KERNEL);
  1361. if (unlikely(!i))
  1362. return NULL;
  1363. i->t.target_attrs.ac.class = &spi_transport_class.class;
  1364. i->t.target_attrs.ac.grp = &target_attribute_group;
  1365. i->t.target_attrs.ac.match = spi_target_match;
  1366. transport_container_register(&i->t.target_attrs);
  1367. i->t.target_size = sizeof(struct spi_transport_attrs);
  1368. i->t.host_attrs.ac.class = &spi_host_class.class;
  1369. i->t.host_attrs.ac.grp = &host_attribute_group;
  1370. i->t.host_attrs.ac.match = spi_host_match;
  1371. transport_container_register(&i->t.host_attrs);
  1372. i->t.host_size = sizeof(struct spi_host_attrs);
  1373. i->f = ft;
  1374. return &i->t;
  1375. }
  1376. EXPORT_SYMBOL(spi_attach_transport);
  1377. void spi_release_transport(struct scsi_transport_template *t)
  1378. {
  1379. struct spi_internal *i = to_spi_internal(t);
  1380. transport_container_unregister(&i->t.target_attrs);
  1381. transport_container_unregister(&i->t.host_attrs);
  1382. kfree(i);
  1383. }
  1384. EXPORT_SYMBOL(spi_release_transport);
  1385. static __init int spi_transport_init(void)
  1386. {
  1387. int error = scsi_dev_info_add_list(SCSI_DEVINFO_SPI,
  1388. "SCSI Parallel Transport Class");
  1389. if (!error) {
  1390. int i;
  1391. for (i = 0; spi_static_device_list[i].vendor; i++)
  1392. scsi_dev_info_list_add_keyed(1, /* compatible */
  1393. spi_static_device_list[i].vendor,
  1394. spi_static_device_list[i].model,
  1395. NULL,
  1396. spi_static_device_list[i].flags,
  1397. SCSI_DEVINFO_SPI);
  1398. }
  1399. error = transport_class_register(&spi_transport_class);
  1400. if (error)
  1401. return error;
  1402. error = anon_transport_class_register(&spi_device_class);
  1403. return transport_class_register(&spi_host_class);
  1404. }
  1405. static void __exit spi_transport_exit(void)
  1406. {
  1407. transport_class_unregister(&spi_transport_class);
  1408. anon_transport_class_unregister(&spi_device_class);
  1409. transport_class_unregister(&spi_host_class);
  1410. scsi_dev_info_remove_list(SCSI_DEVINFO_SPI);
  1411. }
  1412. MODULE_AUTHOR("Martin Hicks");
  1413. MODULE_DESCRIPTION("SPI Transport Attributes");
  1414. MODULE_LICENSE("GPL");
  1415. module_init(spi_transport_init);
  1416. module_exit(spi_transport_exit);